return format_bytes_full(buf, l, t, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | FORMAT_BYTES_TRAILING_B);
}
+/* Note: the lifetime of the compound literal is the immediately surrounding block,
+ * see C11 §6.5.2.5, and
+ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
+#define FORMAT_BYTES(t) format_bytes((char[FORMAT_BYTES_MAX]){}, FORMAT_BYTES_MAX, t)
+
static inline char *format_bytes_cgroup_protection(char *buf, size_t l, uint64_t t) {
if (t == CGROUP_LIMIT_MAX) {
(void) snprintf(buf, l, "%s", "infinity");
l->path,
FORMAT_TIMESPAN(l->target_usec, 1));
- LIST_FOREACH(device_limits, il, c->io_device_limits) {
- char buf[FORMAT_BYTES_MAX];
-
+ LIST_FOREACH(device_limits, il, c->io_device_limits)
for (CGroupIOLimitType type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
if (il->limits[type] != cgroup_io_limit_defaults[type])
fprintf(f,
prefix,
cgroup_io_limit_type_to_string(type),
il->path,
- format_bytes(buf, sizeof(buf), il->limits[type]));
- }
+ FORMAT_BYTES(il->limits[type]));
LIST_FOREACH(device_weights, w, c->blockio_device_weights)
fprintf(f,
w->weight);
LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
- char buf[FORMAT_BYTES_MAX];
-
if (b->rbps != CGROUP_LIMIT_MAX)
fprintf(f,
"%sBlockIOReadBandwidth: %s %s\n",
prefix,
b->path,
- format_bytes(buf, sizeof(buf), b->rbps));
+ FORMAT_BYTES(b->rbps));
if (b->wbps != CGROUP_LIMIT_MAX)
fprintf(f,
"%sBlockIOWriteBandwidth: %s %s\n",
prefix,
b->path,
- format_bytes(buf, sizeof(buf), b->wbps));
+ FORMAT_BYTES(b->wbps));
}
LIST_FOREACH(items, iaai, c->ip_address_allow) {
available = (uint64_t) svfs.f_bfree * (uint64_t) svfs.f_bsize;
- if (available < RELOAD_DISK_SPACE_MIN) {
- char fb_available[FORMAT_BYTES_MAX], fb_need[FORMAT_BYTES_MAX];
+ if (available < RELOAD_DISK_SPACE_MIN)
return sd_bus_error_setf(error,
BUS_ERROR_DISK_FULL,
"%s, not enough space available on /run/systemd. "
"Currently, %s are free, but a safety buffer of %s is enforced.",
message,
- format_bytes(fb_available, sizeof(fb_available), available),
- format_bytes(fb_need, sizeof(fb_need), RELOAD_DISK_SPACE_MIN));
- }
+ FORMAT_BYTES(available),
+ FORMAT_BYTES(RELOAD_DISK_SPACE_MIN));
return 0;
}
available = (uint64_t) svfs.f_bfree * (uint64_t) svfs.f_bsize;
- if (available < RELOAD_DISK_SPACE_MIN) {
- char fb_available[FORMAT_BYTES_MAX], fb_need[FORMAT_BYTES_MAX];
+ if (available < RELOAD_DISK_SPACE_MIN)
log_warning("Dangerously low amount of free space on /run/systemd, root switching might fail.\n"
"Currently, %s are free, but %s are suggested. Proceeding anyway.",
- format_bytes(fb_available, sizeof(fb_available), available),
- format_bytes(fb_need, sizeof(fb_need), RELOAD_DISK_SPACE_MIN));
- }
+ FORMAT_BYTES(available),
+ FORMAT_BYTES(RELOAD_DISK_SPACE_MIN));
r = mac_selinux_access_check(message, "reboot", error);
if (r < 0)
}
for (CGroupIOAccountingMetric k = 0; k < _CGROUP_IO_ACCOUNTING_METRIC_MAX; k++) {
- char buf[FORMAT_BYTES_MAX] = "";
uint64_t value = UINT64_MAX;
assert(io_fields[k]);
* for the bytes counters (and not for the operations counters) */
if (k == CGROUP_IO_READ_BYTES) {
assert(!rr);
- rr = strjoin("read ", format_bytes(buf, sizeof(buf), value), " from disk");
+ rr = strjoin("read ", strna(FORMAT_BYTES(value)), " from disk");
if (!rr) {
r = log_oom();
goto finish;
}
} else if (k == CGROUP_IO_WRITE_BYTES) {
assert(!wr);
- wr = strjoin("written ", format_bytes(buf, sizeof(buf), value), " to disk");
+ wr = strjoin("written ", strna(FORMAT_BYTES(value)), " to disk");
if (!wr) {
r = log_oom();
goto finish;
}
for (CGroupIPAccountingMetric m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) {
- char buf[FORMAT_BYTES_MAX] = "";
uint64_t value = UINT64_MAX;
assert(ip_fields[m]);
* bytes counters (and not for the packets counters) */
if (m == CGROUP_IP_INGRESS_BYTES) {
assert(!igress);
- igress = strjoin("received ", format_bytes(buf, sizeof(buf), value), " IP traffic");
+ igress = strjoin("received ", strna(FORMAT_BYTES(value)), " IP traffic");
if (!igress) {
r = log_oom();
goto finish;
}
} else if (m == CGROUP_IP_EGRESS_BYTES) {
assert(!egress);
- egress = strjoin("sent ", format_bytes(buf, sizeof(buf), value), " IP traffic");
+ egress = strjoin("sent ", strna(FORMAT_BYTES(value)), " IP traffic");
if (!egress) {
r = log_oom();
goto finish;
if (filename) {
const char *state = NULL, *color = NULL;
uint64_t size = UINT64_MAX;
- char buf[FORMAT_BYTES_MAX];
analyze_coredump_file(filename, &state, &color, &size);
ansi_normal());
if (size != UINT64_MAX)
- fprintf(file,
- " Disk Size: %s\n",
- format_bytes(buf, sizeof(buf), size));
+ fprintf(file, " Disk Size: %s\n", FORMAT_BYTES(size));
+
} else if (coredump)
fprintf(file, " Storage: journal\n");
else
if (ioctl(d->fd, BLKGETSIZE64, &size) < 0)
log_debug_errno(errno, "Failed to query size of loopback device: %m");
- else if (arg_json_format_flags & JSON_FORMAT_OFF) {
- char s[FORMAT_BYTES_MAX];
- printf(" Size: %s\n", format_bytes(s, sizeof(s), size));
- }
+ else if (arg_json_format_flags & JSON_FORMAT_OFF)
+ printf(" Size: %s\n", FORMAT_BYTES(size));
if (arg_json_format_flags & JSON_FORMAT_OFF)
putc('\n', stdout);
}
int run_fitrim(int root_fd) {
- char buf[FORMAT_BYTES_MAX];
struct fstrim_range range = {
.len = UINT64_MAX,
};
return log_warning_errno(errno, "Failed to invoke FITRIM, ignoring: %m");
}
- log_info("Discarded unused %s.",
- format_bytes(buf, sizeof(buf), range.len));
+ log_info("Discarded unused %s.", FORMAT_BYTES(range.len));
return 1;
}
}
int run_fallocate(int backing_fd, const struct stat *st) {
- char buf[FORMAT_BYTES_MAX];
struct stat stbuf;
assert(backing_fd >= 0);
}
log_info("Allocated additional %s.",
- format_bytes(buf, sizeof(buf), (DIV_ROUND_UP(st->st_size, 512) - st->st_blocks) * 512));
+ FORMAT_BYTES((DIV_ROUND_UP(st->st_size, 512) - st->st_blocks) * 512));
return 1;
}
}
static void print_size_summary(uint64_t host_size, uint64_t encrypted_size, struct statfs *sfs) {
- char buffer1[FORMAT_BYTES_MAX], buffer2[FORMAT_BYTES_MAX], buffer3[FORMAT_BYTES_MAX], buffer4[FORMAT_BYTES_MAX];
-
assert(sfs);
log_info("Image size is %s, file system size is %s, file system payload size is %s, file system free is %s.",
- format_bytes(buffer1, sizeof(buffer1), host_size),
- format_bytes(buffer2, sizeof(buffer2), encrypted_size),
- format_bytes(buffer3, sizeof(buffer3), (uint64_t) sfs->f_blocks * (uint64_t) sfs->f_frsize),
- format_bytes(buffer4, sizeof(buffer4), (uint64_t) sfs->f_bfree * (uint64_t) sfs->f_frsize));
+ FORMAT_BYTES(host_size),
+ FORMAT_BYTES(encrypted_size),
+ FORMAT_BYTES((uint64_t) sfs->f_blocks * (uint64_t) sfs->f_frsize),
+ FORMAT_BYTES((uint64_t) sfs->f_bfree * (uint64_t) sfs->f_frsize));
}
int home_activate_luks(
}
static int calculate_disk_size(UserRecord *h, const char *parent_dir, uint64_t *ret) {
- char buf[FORMAT_BYTES_MAX];
struct statfs sfs;
uint64_t m;
log_info("Sizing home to %u%% of available disk space, which is %s.",
USER_DISK_SIZE_DEFAULT_PERCENT,
- format_bytes(buf, sizeof(buf), *ret));
+ FORMAT_BYTES(*ret));
} else {
*ret = DISK_SIZE_ROUND_DOWN((uint64_t) ((double) m * (double) h->disk_size_relative / (double) UINT32_MAX));
log_info("Sizing home to %" PRIu64 ".%01" PRIu64 "%% of available disk space, which is %s.",
(h->disk_size_relative * 100) / UINT32_MAX,
((h->disk_size_relative * 1000) / UINT32_MAX) % 10,
- format_bytes(buf, sizeof(buf), *ret));
+ FORMAT_BYTES(*ret));
}
if (*ret < USER_DISK_SIZE_MIN)
HomeSetup *setup,
UserRecord **ret_home) {
- char buffer1[FORMAT_BYTES_MAX], buffer2[FORMAT_BYTES_MAX], buffer3[FORMAT_BYTES_MAX],
- buffer4[FORMAT_BYTES_MAX], buffer5[FORMAT_BYTES_MAX], buffer6[FORMAT_BYTES_MAX];
uint64_t old_image_size, new_image_size, old_fs_size, new_fs_size, crypto_offset, new_partition_size;
_cleanup_(user_record_unrefp) UserRecord *header_home = NULL, *embedded_home = NULL, *new_home = NULL;
_cleanup_(fdisk_unref_tablep) struct fdisk_table *table = NULL;
return log_error_errno(SYNTHETIC_ERRNO(ETXTBSY), "File systems of this type can only be resized offline, but is currently online.");
log_info("Ready to resize image size %s → %s, partition size %s → %s, file system size %s → %s.",
- format_bytes(buffer1, sizeof(buffer1), old_image_size),
- format_bytes(buffer2, sizeof(buffer2), new_image_size),
- format_bytes(buffer3, sizeof(buffer3), setup->partition_size),
- format_bytes(buffer4, sizeof(buffer4), new_partition_size),
- format_bytes(buffer5, sizeof(buffer5), old_fs_size),
- format_bytes(buffer6, sizeof(buffer6), new_fs_size));
+ FORMAT_BYTES(old_image_size),
+ FORMAT_BYTES(new_image_size),
+ FORMAT_BYTES(setup->partition_size),
+ FORMAT_BYTES(new_partition_size),
+ FORMAT_BYTES(old_fs_size),
+ FORMAT_BYTES(new_fs_size));
r = prepare_resize_partition(
image_fd,
if (p->size == 0)
log_info("Copying tree, currently at '%s'...", p->path);
- else {
- char buffer[FORMAT_BYTES_MAX];
-
- log_info("Copying tree, currently at '%s' (@%s)...", p->path, format_bytes(buffer, sizeof(buffer), p->size));
- }
+ else
+ log_info("Copying tree, currently at '%s' (@%s)...", p->path, FORMAT_BYTES(p->size));
}
static int progress_path(const char *path, const struct stat *st, void *userdata) {
(void) safe_atou64(length, &j->content_length);
if (j->content_length != UINT64_MAX) {
- char bytes[FORMAT_BYTES_MAX];
-
if (j->content_length > j->compressed_max) {
log_error("Content too large.");
r = -EFBIG;
goto fail;
}
- log_info("Downloading %s for %s.", format_bytes(bytes, sizeof(bytes), j->content_length), j->url);
+ log_info("Downloading %s for %s.", FORMAT_BYTES(j->content_length), j->url);
}
return sz;
dlnow < dltotal) {
if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) {
- char y[FORMAT_BYTES_MAX];
usec_t left, done;
done = n - j->start_usec;
percent,
j->url,
FORMAT_TIMESPAN(left, USEC_PER_SEC),
- format_bytes(y, sizeof(y), (uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC))));
+ FORMAT_BYTES((uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC))));
} else
log_info("Got %u%% of %s.", percent, j->url);
case ACTION_DISK_USAGE: {
uint64_t bytes = 0;
- char sbytes[FORMAT_BYTES_MAX];
r = sd_journal_get_usage(j, &bytes);
if (r < 0)
goto finish;
printf("Archived and active journals take up %s in the file system.\n",
- format_bytes(sbytes, sizeof(sbytes), bytes));
+ FORMAT_BYTES(bytes));
goto finish;
}
}
void server_space_usage_message(Server *s, JournalStorage *storage) {
- char fb1[FORMAT_BYTES_MAX], fb2[FORMAT_BYTES_MAX], fb3[FORMAT_BYTES_MAX],
- fb4[FORMAT_BYTES_MAX], fb5[FORMAT_BYTES_MAX], fb6[FORMAT_BYTES_MAX];
- JournalMetrics *metrics;
-
assert(s);
if (!storage)
if (cache_space_refresh(s, storage) < 0)
return;
- metrics = &storage->metrics;
- format_bytes(fb1, sizeof(fb1), storage->space.vfs_used);
- format_bytes(fb2, sizeof(fb2), metrics->max_use);
- format_bytes(fb3, sizeof(fb3), metrics->keep_free);
- format_bytes(fb4, sizeof(fb4), storage->space.vfs_available);
- format_bytes(fb5, sizeof(fb5), storage->space.limit);
- format_bytes(fb6, sizeof(fb6), storage->space.available);
+ const JournalMetrics *metrics = &storage->metrics;
+
+ const char
+ *vfs_used = FORMAT_BYTES(storage->space.vfs_used),
+ *limit = FORMAT_BYTES(storage->space.limit),
+ *available = FORMAT_BYTES(storage->space.available);
server_driver_message(s, 0,
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_USAGE_STR,
LOG_MESSAGE("%s (%s) is %s, max %s, %s free.",
- storage->name, storage->path, fb1, fb5, fb6),
+ storage->name, storage->path, vfs_used, limit, available),
"JOURNAL_NAME=%s", storage->name,
"JOURNAL_PATH=%s", storage->path,
"CURRENT_USE=%"PRIu64, storage->space.vfs_used,
- "CURRENT_USE_PRETTY=%s", fb1,
+ "CURRENT_USE_PRETTY=%s", vfs_used,
"MAX_USE=%"PRIu64, metrics->max_use,
- "MAX_USE_PRETTY=%s", fb2,
+ "MAX_USE_PRETTY=%s", FORMAT_BYTES(metrics->max_use),
"DISK_KEEP_FREE=%"PRIu64, metrics->keep_free,
- "DISK_KEEP_FREE_PRETTY=%s", fb3,
+ "DISK_KEEP_FREE_PRETTY=%s", FORMAT_BYTES(metrics->keep_free),
"DISK_AVAILABLE=%"PRIu64, storage->space.vfs_available,
- "DISK_AVAILABLE_PRETTY=%s", fb4,
+ "DISK_AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage->space.vfs_available),
"LIMIT=%"PRIu64, storage->space.limit,
- "LIMIT_PRETTY=%s", fb5,
+ "LIMIT_PRETTY=%s", limit,
"AVAILABLE=%"PRIu64, storage->space.available,
- "AVAILABLE_PRETTY=%s", fb6,
+ "AVAILABLE_PRETTY=%s", available,
NULL);
}
void journal_file_print_header(JournalFile *f) {
char a[SD_ID128_STRING_MAX], b[SD_ID128_STRING_MAX], c[SD_ID128_STRING_MAX], d[SD_ID128_STRING_MAX];
struct stat st;
- char bytes[FORMAT_BYTES_MAX];
assert(f);
assert(f->header);
f->header->data_hash_chain_depth);
if (fstat(f->fd, &st) >= 0)
- printf("Disk usage: %s\n", format_bytes(bytes, sizeof(bytes), (uint64_t) st.st_blocks * 512ULL));
+ printf("Disk usage: %s\n", FORMAT_BYTES((uint64_t) st.st_blocks * 512ULL));
}
static int journal_file_warn_btrfs(JournalFile *f) {
if (DEBUG_LOGGING) {
static int last_seal = -1, last_compress = -1, last_keyed_hash = -1;
static uint64_t last_bytes = UINT64_MAX;
- char bytes[FORMAT_BYTES_MAX];
if (last_seal != f->seal ||
last_keyed_hash != f->keyed_hash ||
log_debug("Journal effective settings seal=%s keyed_hash=%s compress=%s compress_threshold_bytes=%s",
yes_no(f->seal), yes_no(f->keyed_hash), yes_no(JOURNAL_FILE_COMPRESS(f)),
- format_bytes(bytes, sizeof bytes, f->compress_threshold_bytes));
+ FORMAT_BYTES(f->compress_threshold_bytes));
last_seal = f->seal;
last_keyed_hash = f->keyed_hash;
last_compress = JOURNAL_FILE_COMPRESS(f);
}
void journal_default_metrics(JournalMetrics *m, int fd) {
- char a[FORMAT_BYTES_MAX], b[FORMAT_BYTES_MAX], c[FORMAT_BYTES_MAX], d[FORMAT_BYTES_MAX], e[FORMAT_BYTES_MAX];
struct statvfs ss;
uint64_t fs_size = 0;
m->n_max_files = DEFAULT_N_MAX_FILES;
log_debug("Fixed min_use=%s max_use=%s max_size=%s min_size=%s keep_free=%s n_max_files=%" PRIu64,
- format_bytes(a, sizeof(a), m->min_use),
- format_bytes(b, sizeof(b), m->max_use),
- format_bytes(c, sizeof(c), m->max_size),
- format_bytes(d, sizeof(d), m->min_size),
- format_bytes(e, sizeof(e), m->keep_free),
+ FORMAT_BYTES(m->min_use),
+ FORMAT_BYTES(m->max_use),
+ FORMAT_BYTES(m->max_size),
+ FORMAT_BYTES(m->min_size),
+ FORMAT_BYTES(m->keep_free),
m->n_max_files);
}
_cleanup_closedir_ DIR *d = NULL;
struct vacuum_info *list = NULL;
usec_t retention_limit = 0;
- char sbytes[FORMAT_BYTES_MAX];
struct dirent *de;
int r;
return -errno;
FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) {
-
unsigned long long seqnum = 0, realtime;
_cleanup_free_ char *p = NULL;
sd_id128_t seqnum_id;
if (r >= 0) {
log_full(verbose ? LOG_INFO : LOG_DEBUG,
- "Deleted empty archived journal %s/%s (%s).", directory, p, format_bytes(sbytes, sizeof(sbytes), size));
+ "Deleted empty archived journal %s/%s (%s).", directory, p, FORMAT_BYTES(size));
freed += size;
} else if (r != -ENOENT)
r = unlinkat_deallocate(dirfd(d), list[i].filename, 0);
if (r >= 0) {
- log_full(verbose ? LOG_INFO : LOG_DEBUG, "Deleted archived journal %s/%s (%s).", directory, list[i].filename, format_bytes(sbytes, sizeof(sbytes), list[i].usage));
+ log_full(verbose ? LOG_INFO : LOG_DEBUG, "Deleted archived journal %s/%s (%s).",
+ directory, list[i].filename, FORMAT_BYTES(list[i].usage));
freed += list[i].usage;
if (list[i].usage < sum)
free(list[i].filename);
free(list);
- log_full(verbose ? LOG_INFO : LOG_DEBUG, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes, sizeof(sbytes), freed), directory);
+ log_full(verbose ? LOG_INFO : LOG_DEBUG, "Vacuuming done, freed %s of archived journals from %s.",
+ FORMAT_BYTES(freed), directory);
return r;
}
} ImageStatusInfo;
static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
- char bs[FORMAT_BYTES_MAX];
- char bs_exclusive[FORMAT_BYTES_MAX];
const char *s1, *s2, *s3, *s4;
assert(bus);
else if (s2)
printf("\tModified: %s\n", s2);
- s3 = format_bytes(bs, sizeof(bs), i->usage);
- s4 = i->usage_exclusive != i->usage ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->usage_exclusive) : NULL;
+ s3 = FORMAT_BYTES(i->usage);
+ s4 = i->usage_exclusive != i->usage ? FORMAT_BYTES(i->usage_exclusive) : NULL;
if (s3 && s4)
printf("\t Usage: %s (exclusive: %s)\n", s3, s4);
else if (s3)
printf("\t Usage: %s\n", s3);
- s3 = format_bytes(bs, sizeof(bs), i->limit);
- s4 = i->limit_exclusive != i->limit ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->limit_exclusive) : NULL;
+ s3 = FORMAT_BYTES(i->limit);
+ s4 = i->limit_exclusive != i->limit ? FORMAT_BYTES(i->limit_exclusive) : NULL;
if (s3 && s4)
printf("\t Limit: %s (exclusive: %s)\n", s3, s4);
else if (s3)
} PoolStatusInfo;
static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
- char bs[FORMAT_BYTES_MAX], *s;
+ char *s;
if (i->path)
printf("\t Path: %s\n", i->path);
- s = format_bytes(bs, sizeof(bs), i->usage);
+ s = FORMAT_BYTES(i->usage);
if (s)
printf("\t Usage: %s\n", s);
- s = format_bytes(bs, sizeof(bs), i->limit);
+ s = FORMAT_BYTES(i->limit);
if (s)
printf("\t Limit: %s\n", s);
}
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
uint64_t usage, total = 0;
- char fb[FORMAT_BYTES_MAX];
sd_bus *bus = userdata;
const char *name;
unsigned c = 0;
total = UINT64_MAX;
} else {
log_info("Removed image '%s'. Freed exclusive disk space: %s",
- name, format_bytes(fb, sizeof(fb), usage));
+ name, FORMAT_BYTES(usage));
if (total != UINT64_MAX)
total += usage;
}
log_info("Removed %u images in total.", c);
else
log_info("Removed %u images in total. Total freed exclusive disk space: %s.",
- c, format_bytes(fb, sizeof(fb), total));
+ c, FORMAT_BYTES(total));
return 0;
}
}
void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix) {
- char swap[FORMAT_BYTES_MAX];
-
assert(ctx);
assert(f);
"%sPath: %s\n"
"%s\tSwap Usage: %s\n",
strempty(prefix), ctx->path,
- strempty(prefix), format_bytes(swap, sizeof(swap), ctx->swap_usage));
+ strempty(prefix), FORMAT_BYTES(ctx->swap_usage));
else
fprintf(f,
"%sPath: %s\n"
}
void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix) {
- char mem_use[FORMAT_BYTES_MAX], mem_min[FORMAT_BYTES_MAX], mem_low[FORMAT_BYTES_MAX];
+ char mem_min[FORMAT_BYTES_MAX], mem_low[FORMAT_BYTES_MAX];
assert(ctx);
assert(f);
LOAD_INT(ctx->memory_pressure.avg60), LOAD_FRAC(ctx->memory_pressure.avg60),
LOAD_INT(ctx->memory_pressure.avg300), LOAD_FRAC(ctx->memory_pressure.avg300),
FORMAT_TIMESPAN(ctx->memory_pressure.total, USEC_PER_SEC),
- strempty(prefix), format_bytes(mem_use, sizeof(mem_use), ctx->current_memory_usage));
+ strempty(prefix), FORMAT_BYTES(ctx->current_memory_usage));
if (!empty_or_root(ctx->path))
fprintf(f,
}
void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix) {
- char mem_used[FORMAT_BYTES_MAX], mem_total[FORMAT_BYTES_MAX];
- char swap_used[FORMAT_BYTES_MAX], swap_total[FORMAT_BYTES_MAX];
-
assert(ctx);
assert(f);
"%sMemory: Used: %s Total: %s\n"
"%sSwap: Used: %s Total: %s\n",
strempty(prefix),
- format_bytes(mem_used, sizeof(mem_used), ctx->mem_used),
- format_bytes(mem_total, sizeof(mem_total), ctx->mem_total),
+ FORMAT_BYTES(ctx->mem_used),
+ FORMAT_BYTES(ctx->mem_total),
strempty(prefix),
- format_bytes(swap_used, sizeof(swap_used), ctx->swap_used),
- format_bytes(swap_total, sizeof(swap_total), ctx->swap_total));
+ FORMAT_BYTES(ctx->swap_used),
+ FORMAT_BYTES(ctx->swap_total));
}
_cleanup_close_ int mountfd = -1, devfd = -1;
_cleanup_free_ char *devpath = NULL;
uint64_t size, newsize;
- char fb[FORMAT_BYTES_MAX];
dev_t devno;
int r;
if (newsize == size)
log_info("Successfully resized \"%s\" to %s bytes.",
arg_target,
- format_bytes(fb, sizeof fb, newsize));
+ FORMAT_BYTES(newsize));
else
log_info("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).",
arg_target,
- format_bytes(fb, sizeof fb, newsize),
+ FORMAT_BYTES(newsize),
size - newsize);
return 0;
}
}
static int format_size_change(uint64_t from, uint64_t to, char **ret) {
- char format_buffer1[FORMAT_BYTES_MAX], format_buffer2[FORMAT_BYTES_MAX], *buf;
-
- if (from != UINT64_MAX)
- format_bytes(format_buffer1, sizeof(format_buffer1), from);
- if (to != UINT64_MAX)
- format_bytes(format_buffer2, sizeof(format_buffer2), to);
+ char *t;
if (from != UINT64_MAX) {
if (from == to || to == UINT64_MAX)
- buf = strdup(format_buffer1);
+ t = strdup(FORMAT_BYTES(from));
else
- buf = strjoin(format_buffer1, " ", special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2);
+ t = strjoin(FORMAT_BYTES(from), " ", special_glyph(SPECIAL_GLYPH_ARROW), " ", FORMAT_BYTES(to));
} else if (to != UINT64_MAX)
- buf = strjoin(special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2);
+ t = strjoin(special_glyph(SPECIAL_GLYPH_ARROW), " ", FORMAT_BYTES(to));
else {
*ret = NULL;
return 0;
}
- if (!buf)
+ if (!t)
return log_oom();
- *ret = TAKE_PTR(buf);
+ *ret = t;
return 1;
}
}
if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
- char s[FORMAT_BYTES_MAX];
const char *a, *b;
- a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_size));
- b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_padding));
+ a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
+ b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
r = table_add_many(
t,
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
_cleanup_free_ char *encrypted = NULL;
_cleanup_close_ int encrypted_dev_fd = -1;
- char buf[FORMAT_BYTES_MAX];
int target_fd;
if (p->copy_blocks_fd < 0)
target_fd = whole_fd;
}
- log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".", p->copy_blocks_path, format_bytes(buf, sizeof(buf), p->copy_blocks_size), p->partno);
+ log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
+ p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
r = copy_bytes_full(p->copy_blocks_fd, target_fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL);
if (r < 0)
const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
LoopDevice *loop_device) {
- char buf1[FORMAT_BYTES_MAX], buf2[FORMAT_BYTES_MAX];
_cleanup_close_ int writable_fd = -1;
uint64_t current_size;
struct stat st;
current_size = st.st_size;
}
- assert_se(format_bytes(buf1, sizeof(buf1), current_size));
- assert_se(format_bytes(buf2, sizeof(buf2), arg_size));
-
if (current_size >= arg_size) {
- log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)", node, buf1, buf2);
+ log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
+ node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
return 0;
}
if ((uint64_t) st.st_size != current_size)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
- "Size of backing file '%s' of loopback block device '%s' don't match, refusing.", node, backing_file);
+ "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
+ node, backing_file);
} else {
assert(S_ISREG(st.st_mode));
assert(!backing_file);
if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
if (!ERRNO_IS_NOT_SUPPORTED(errno))
return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
- node, buf1, buf2);
+ node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
/* Fallback to truncation, if fallocate() is not supported. */
log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
} else {
if (current_size == 0) /* Likely regular file just created by us */
- log_info("Allocated %s for '%s'.", buf2, node);
+ log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node);
else
- log_info("File '%s' grown from %s to %s by allocation.", node, buf1, buf2);
+ log_info("File '%s' grown from %s to %s by allocation.",
+ node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
goto done;
}
if (ftruncate(writable_fd, arg_size) < 0)
return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
- node, buf1, buf2);
+ node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
if (current_size == 0) /* Likely regular file just created by us */
- log_info("Sized '%s' to %s.", node, buf2);
+ log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size));
else
- log_info("File '%s' grown from %s to %s by truncation.", node, buf1, buf2);
+ log_info("File '%s' grown from %s to %s by truncation.",
+ node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
done:
r = resize_pt(writable_fd);
static int determine_auto_size(Context *c) {
uint64_t sum = round_up_size(GPT_METADATA_SIZE, 4096);
- char buf[FORMAT_BYTES_MAX];
Partition *p;
assert_se(c);
sum += m;
}
- assert_se(format_bytes(buf, sizeof(buf), sum));
- if (c->total != UINT64_MAX) { /* Image already allocated? Then show its size */
- char buf2[FORMAT_BYTES_MAX];
- assert_se(format_bytes(buf2, sizeof(buf2), c->total));
- log_info("Automatically determined minimal disk image size as %s, current image size is %s.", buf, buf2);
- } else /* If the image is being created right now, then it has no previous size, suppress any comment about it hence */
- log_info("Automatically determined minimal disk image size as %s.", buf);
+ if (c->total != UINT64_MAX)
+ /* Image already allocated? Then show its size. */
+ log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
+ FORMAT_BYTES(sum), FORMAT_BYTES(c->total));
+ else
+ /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
+ log_info("Automatically determined minimal disk image size as %s.",
+ FORMAT_BYTES(sum));
arg_size = sum;
return 0;
break; /* Success! */
if (!context_drop_one_priority(context)) {
- char buf[FORMAT_BYTES_MAX];
r = log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Can't fit requested partitions into available free space (%s), refusing.",
- format_bytes(buf, sizeof(buf), largest_free_area));
-
+ FORMAT_BYTES(largest_free_area));
determine_auto_size(context);
return r;
}
log_info("CPU time consumed: %s",
FORMAT_TIMESPAN(DIV_ROUND_UP(c.cpu_usage_nsec, NSEC_PER_USEC), USEC_PER_MSEC));
- if (c.ip_ingress_bytes != UINT64_MAX) {
- char bytes[FORMAT_BYTES_MAX];
- log_info("IP traffic received: %s", format_bytes(bytes, sizeof bytes, c.ip_ingress_bytes));
- }
- if (c.ip_egress_bytes != UINT64_MAX) {
- char bytes[FORMAT_BYTES_MAX];
- log_info("IP traffic sent: %s", format_bytes(bytes, sizeof bytes, c.ip_egress_bytes));
- }
- if (c.io_read_bytes != UINT64_MAX) {
- char bytes[FORMAT_BYTES_MAX];
- log_info("IO bytes read: %s", format_bytes(bytes, sizeof bytes, c.io_read_bytes));
- }
- if (c.io_write_bytes != UINT64_MAX) {
- char bytes[FORMAT_BYTES_MAX];
- log_info("IO bytes written: %s", format_bytes(bytes, sizeof bytes, c.io_write_bytes));
- }
+ if (c.ip_ingress_bytes != UINT64_MAX)
+ log_info("IP traffic received: %s", FORMAT_BYTES(c.ip_ingress_bytes));
+
+ if (c.ip_egress_bytes != UINT64_MAX)
+ log_info("IP traffic sent: %s", FORMAT_BYTES(c.ip_egress_bytes));
+
+ if (c.io_read_bytes != UINT64_MAX)
+ log_info("IO bytes read: %s", FORMAT_BYTES(c.io_read_bytes));
+
+ if (c.io_write_bytes != UINT64_MAX)
+ log_info("IO bytes written: %s", FORMAT_BYTES(c.io_write_bytes));
}
/* Try to propagate the service's return value. But if the service defines
static int fs_grow(const char *node_path, const char *mount_path) {
_cleanup_close_ int mount_fd = -1, node_fd = -1;
- char fb[FORMAT_BYTES_MAX];
uint64_t size, newsize;
int r;
if (newsize == size)
log_debug("Successfully resized \"%s\" to %s bytes.",
- mount_path,
- format_bytes(fb, sizeof fb, newsize));
+ mount_path, FORMAT_BYTES(newsize));
else {
assert(newsize < size);
log_debug("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).",
- mount_path,
- format_bytes(fb, sizeof fb, newsize),
- size - newsize);
+ mount_path, FORMAT_BYTES(newsize), size - newsize);
}
return 0;
}
}
- if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) {
- char bytes[FORMAT_BYTES_MAX];
- fprintf(f, "[%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
- } else {
+ if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len))
+ fprintf(f, "[%s blob data]\n", FORMAT_BYTES(message_len));
+ else {
/* URLify config_file string in message, if the message starts with it.
* Skip URLification if the highlighted pattern overlaps. */
p, valuelen,
NULL);
fputs(off, f);
- } else {
- char bytes[FORMAT_BYTES_MAX];
-
+ } else
fprintf(f, " %s%.*s=[%s blob data]%s\n",
on,
(int) (c - (const char*) data),
(const char*) data,
- format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
+ FORMAT_BYTES(length - (c - (const char *) data) - 1),
off);
- }
}
if (r < 0)
if (hr->tasks_max != UINT64_MAX)
printf(" Tasks Max: %" PRIu64 "\n", hr->tasks_max);
- if (hr->memory_high != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
- printf(" Memory High: %s\n", format_bytes(buf, sizeof(buf), hr->memory_high));
- }
+ if (hr->memory_high != UINT64_MAX)
+ printf(" Memory High: %s\n", FORMAT_BYTES(hr->memory_high));
- if (hr->memory_max != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
- printf(" Memory Max: %s\n", format_bytes(buf, sizeof(buf), hr->memory_max));
- }
+ if (hr->memory_max != UINT64_MAX)
+ printf(" Memory Max: %s\n", FORMAT_BYTES(hr->memory_max));
if (hr->cpu_weight != UINT64_MAX)
printf(" CPU Weight: %" PRIu64 "\n", hr->cpu_weight);
printf(" PBKDF Hash: %s\n", hr->luks_pbkdf_hash_algorithm);
if (hr->luks_pbkdf_time_cost_usec != UINT64_MAX)
printf(" PBKDF Time: %s\n", FORMAT_TIMESPAN(hr->luks_pbkdf_time_cost_usec, 0));
- if (hr->luks_pbkdf_memory_cost != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
- printf(" PBKDF Bytes: %s\n", format_bytes(buf, sizeof(buf), hr->luks_pbkdf_memory_cost));
- }
+ if (hr->luks_pbkdf_memory_cost != UINT64_MAX)
+ printf(" PBKDF Bytes: %s\n", FORMAT_BYTES(hr->luks_pbkdf_memory_cost));
+
if (hr->luks_pbkdf_parallel_threads != UINT64_MAX)
printf("PBKDF Thread: %" PRIu64 "\n", hr->luks_pbkdf_parallel_threads);
if (hr->skeleton_directory)
printf(" Skel. Dir.: %s\n", user_record_skeleton_directory(hr));
- if (hr->disk_size != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
- printf(" Disk Size: %s\n", format_bytes(buf, sizeof(buf), hr->disk_size));
- }
+ if (hr->disk_size != UINT64_MAX)
+ printf(" Disk Size: %s\n", FORMAT_BYTES(hr->disk_size));
if (hr->disk_usage != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
-
if (hr->disk_size != UINT64_MAX) {
unsigned permille;
permille = (unsigned) DIV_ROUND_UP(hr->disk_usage * 1000U, hr->disk_size); /* Round up! */
printf(" Disk Usage: %s (= %u.%01u%%)\n",
- format_bytes(buf, sizeof(buf), hr->disk_usage),
+ FORMAT_BYTES(hr->disk_usage),
permille / 10, permille % 10);
} else
- printf(" Disk Usage: %s\n", format_bytes(buf, sizeof(buf), hr->disk_usage));
+ printf(" Disk Usage: %s\n", FORMAT_BYTES(hr->disk_usage));
}
if (hr->disk_free != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
-
if (hr->disk_size != UINT64_MAX) {
const char *color_on, *color_off;
unsigned permille;
printf(" Disk Free: %s%s (= %u.%01u%%)%s\n",
color_on,
- format_bytes(buf, sizeof(buf), hr->disk_free),
+ FORMAT_BYTES(hr->disk_free),
permille / 10, permille % 10,
color_off);
} else
- printf(" Disk Free: %s\n", format_bytes(buf, sizeof(buf), hr->disk_free));
+ printf(" Disk Free: %s\n", FORMAT_BYTES(hr->disk_free));
}
- if (hr->disk_floor != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
- printf(" Disk Floor: %s\n", format_bytes(buf, sizeof(buf), hr->disk_floor));
- }
+ if (hr->disk_floor != UINT64_MAX)
+ printf(" Disk Floor: %s\n", FORMAT_BYTES(hr->disk_floor));
- if (hr->disk_ceiling != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
- printf("Disk Ceiling: %s\n", format_bytes(buf, sizeof(buf), hr->disk_ceiling));
- }
+ if (hr->disk_ceiling != UINT64_MAX)
+ printf("Disk Ceiling: %s\n", FORMAT_BYTES(hr->disk_ceiling));
if (hr->good_authentication_counter != UINT64_MAX)
printf(" Good Auth.: %" PRIu64 "\n", hr->good_authentication_counter);
if (i->status_errno > 0)
printf(" Error: %i (%s)\n", i->status_errno, strerror_safe(i->status_errno));
- if (i->ip_ingress_bytes != UINT64_MAX && i->ip_egress_bytes != UINT64_MAX) {
- char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX];
-
+ if (i->ip_ingress_bytes != UINT64_MAX && i->ip_egress_bytes != UINT64_MAX)
printf(" IP: %s in, %s out\n",
- format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes),
- format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes));
- }
-
- if (i->io_read_bytes != UINT64_MAX && i->io_write_bytes != UINT64_MAX) {
- char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX];
+ FORMAT_BYTES(i->ip_ingress_bytes),
+ FORMAT_BYTES(i->ip_egress_bytes));
+ if (i->io_read_bytes != UINT64_MAX && i->io_write_bytes != UINT64_MAX)
printf(" IO: %s read, %s written\n",
- format_bytes(buf_in, sizeof(buf_in), i->io_read_bytes),
- format_bytes(buf_out, sizeof(buf_out), i->io_write_bytes));
- }
+ FORMAT_BYTES(i->io_read_bytes),
+ FORMAT_BYTES(i->io_write_bytes));
if (i->tasks_current != UINT64_MAX) {
printf(" Tasks: %" PRIu64, i->tasks_current);
}
if (i->memory_current != UINT64_MAX) {
- char buf[FORMAT_BYTES_MAX];
-
- printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current));
+ printf(" Memory: %s", FORMAT_BYTES(i->memory_current));
if (i->memory_min > 0 || i->memory_low > 0 ||
i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX ||
i->memory_swap_max != CGROUP_LIMIT_MAX ||
i->memory_available != CGROUP_LIMIT_MAX ||
i->memory_limit != CGROUP_LIMIT_MAX) {
+ char buf[FORMAT_BYTES_MAX];
const char *prefix = "";
printf(" (");
prefix = " ";
}
if (i->memory_high != CGROUP_LIMIT_MAX) {
- printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high));
+ printf("%shigh: %s", prefix, FORMAT_BYTES(i->memory_high));
prefix = " ";
}
if (i->memory_max != CGROUP_LIMIT_MAX) {
- printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max));
+ printf("%smax: %s", prefix, FORMAT_BYTES(i->memory_max));
prefix = " ";
}
if (i->memory_swap_max != CGROUP_LIMIT_MAX) {
- printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max));
+ printf("%sswap max: %s", prefix, FORMAT_BYTES(i->memory_swap_max));
prefix = " ";
}
if (i->memory_limit != CGROUP_LIMIT_MAX) {
- printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit));
+ printf("%slimit: %s", prefix, FORMAT_BYTES(i->memory_limit));
prefix = " ";
}
if (i->memory_available != CGROUP_LIMIT_MAX) {
- printf("%savailable: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_available));
+ printf("%savailable: %s", prefix, FORMAT_BYTES(i->memory_available));
prefix = " ";
}
printf(")");
if (fd < 0)
log_error_errno(errno, "Failed to open root directory: %m");
else {
- char bs[FORMAT_BYTES_MAX];
BtrfsSubvolInfo info;
r = btrfs_subvol_get_info_fd(fd, 0, &info);
if (r < 0)
log_error_errno(r, "Failed to get quota info: %m");
else {
- log_info("referenced: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced)));
- log_info("exclusive: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive)));
- log_info("referenced_max: %s", strna(format_bytes(bs, sizeof(bs), quota.referenced_max)));
- log_info("exclusive_max: %s", strna(format_bytes(bs, sizeof(bs), quota.exclusive_max)));
+ log_info("referenced: %s", strna(FORMAT_BYTES(quota.referenced)));
+ log_info("exclusive: %s", strna(FORMAT_BYTES(quota.exclusive)));
+ log_info("referenced_max: %s", strna(FORMAT_BYTES(quota.referenced_max)));
+ log_info("exclusive_max: %s", strna(FORMAT_BYTES(quota.exclusive_max)));
}
r = btrfs_subvol_get_read_only_fd(fd);
#include "tests.h"
int main(int argc, char *argv[]) {
- char buf[FORMAT_BYTES_MAX];
nsec_t nsec;
uint64_t v;
int r;
log_info("Current system CPU time: %s", FORMAT_TIMESPAN(nsec/NSEC_PER_USEC, 1));
assert_se(procfs_memory_get_used(&v) >= 0);
- log_info("Current memory usage: %s", format_bytes(buf, sizeof(buf), v));
+ log_info("Current memory usage: %s", FORMAT_BYTES(v));
assert_se(procfs_tasks_get_current(&v) >= 0);
log_info("Current number of tasks: %" PRIu64, v);
static void test_physical_memory(void) {
uint64_t p;
- char buf[FORMAT_BYTES_MAX];
log_info("/* %s */", __func__);
assert_se(p < UINT64_MAX);
assert_se(p % page_size() == 0);
- log_info("Memory: %s (%" PRIu64 ")", format_bytes(buf, sizeof(buf), p), p);
+ log_info("Memory: %s (%" PRIu64 ")", FORMAT_BYTES(p), p);
}
static void test_physical_memory_scale(void) {