mirror of
https://github.com/reactos/ccache.git
synced 2024-11-23 11:49:41 +00:00
Use real size instead of multiples of 1024 for sizes
Exception: Size values in stats files are still in units of 1024 for backward compatibility reasons.
This commit is contained in:
parent
9fb0c7219c
commit
cadd1e1793
36
ccache.c
36
ccache.c
@ -69,10 +69,6 @@ static const char USAGE_TEXT[] =
|
|||||||
"\n"
|
"\n"
|
||||||
"See also <http://ccache.samba.org>.\n";
|
"See also <http://ccache.samba.org>.\n";
|
||||||
|
|
||||||
#ifndef DEFAULT_MAXSIZE
|
|
||||||
#define DEFAULT_MAXSIZE (1024*1024)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Global configuration data. */
|
/* Global configuration data. */
|
||||||
struct conf *conf = NULL;
|
struct conf *conf = NULL;
|
||||||
|
|
||||||
@ -685,7 +681,7 @@ to_cache(struct args *args)
|
|||||||
failed();
|
failed();
|
||||||
}
|
}
|
||||||
|
|
||||||
stats_update_size(STATS_TOCACHE, added_bytes / 1024, added_files);
|
stats_update_size(STATS_TOCACHE, added_bytes, added_files);
|
||||||
|
|
||||||
/* Make sure we have a CACHEDIR.TAG
|
/* Make sure we have a CACHEDIR.TAG
|
||||||
* This can be almost anywhere, but might as well do it near the end
|
* This can be almost anywhere, but might as well do it near the end
|
||||||
@ -1131,7 +1127,7 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
|
|||||||
} else {
|
} else {
|
||||||
cc_log("Stored in cache: %s", cached_dep);
|
cc_log("Stored in cache: %s", cached_dep);
|
||||||
stat(cached_dep, &st);
|
stat(cached_dep, &st);
|
||||||
stats_update_size(STATS_NONE, file_size(&st) / 1024, 1);
|
stats_update_size(STATS_NONE, file_size(&st), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1157,7 +1153,7 @@ from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
|
|||||||
update_mtime(manifest_path);
|
update_mtime(manifest_path);
|
||||||
stat(manifest_path, &st);
|
stat(manifest_path, &st);
|
||||||
stats_update_size(STATS_NONE,
|
stats_update_size(STATS_NONE,
|
||||||
(file_size(&st) - old_size) / 1024,
|
(file_size(&st) - old_size),
|
||||||
old_size == 0 ? 1 : 0);
|
old_size == 0 ? 1 : 0);
|
||||||
} else {
|
} else {
|
||||||
cc_log("Failed to add object file hash to %s", manifest_path);
|
cc_log("Failed to add object file hash to %s", manifest_path);
|
||||||
@ -1776,7 +1772,8 @@ out:
|
|||||||
static void
|
static void
|
||||||
create_initial_config_file(struct conf *conf, const char *path)
|
create_initial_config_file(struct conf *conf, const char *path)
|
||||||
{
|
{
|
||||||
unsigned max_files, max_size;
|
unsigned max_files;
|
||||||
|
uint64_t max_size;
|
||||||
char *stats_dir;
|
char *stats_dir;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@ -1793,7 +1790,7 @@ create_initial_config_file(struct conf *conf, const char *path)
|
|||||||
max_size *= 16;
|
max_size *= 16;
|
||||||
} else {
|
} else {
|
||||||
max_files = 0;
|
max_files = 0;
|
||||||
max_size = DEFAULT_MAXSIZE;
|
max_size = conf->max_size;
|
||||||
}
|
}
|
||||||
free(stats_dir);
|
free(stats_dir);
|
||||||
|
|
||||||
@ -2088,7 +2085,6 @@ static int
|
|||||||
ccache_main_options(int argc, char *argv[])
|
ccache_main_options(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
size_t v;
|
|
||||||
char *errmsg;
|
char *errmsg;
|
||||||
|
|
||||||
enum longopts {
|
enum longopts {
|
||||||
@ -2130,35 +2126,43 @@ ccache_main_options(int argc, char *argv[])
|
|||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
case 'F': /* --max-files */
|
case 'F': /* --max-files */
|
||||||
|
{
|
||||||
|
unsigned files;
|
||||||
initialize();
|
initialize();
|
||||||
v = atoi(optarg);
|
files = atoi(optarg);
|
||||||
if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
|
if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
|
||||||
&errmsg)) {
|
&errmsg)) {
|
||||||
if (v == 0) {
|
if (files == 0) {
|
||||||
printf("Unset cache file limit\n");
|
printf("Unset cache file limit\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Set cache file limit to %u\n", (unsigned)v);
|
printf("Set cache file limit to %u\n", files);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fatal("could not set cache file limit: %s", errmsg);
|
fatal("could not set cache file limit: %s", errmsg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'M': /* --max-size */
|
case 'M': /* --max-size */
|
||||||
|
{
|
||||||
|
uint64_t size;
|
||||||
initialize();
|
initialize();
|
||||||
parse_size_with_suffix(optarg, &v);
|
if (!parse_size_with_suffix(optarg, &size)) {
|
||||||
|
fatal("invalid size: %s", optarg);
|
||||||
|
}
|
||||||
if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
|
if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
|
||||||
&errmsg)) {
|
&errmsg)) {
|
||||||
if (v == 0) {
|
if (size == 0) {
|
||||||
printf("Unset cache size limit\n");
|
printf("Unset cache size limit\n");
|
||||||
} else {
|
} else {
|
||||||
char *s = format_human_readable_size(v);
|
char *s = format_human_readable_size(size);
|
||||||
printf("Set cache size limit to %s\n", s);
|
printf("Set cache size limit to %s\n", s);
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fatal("could not set cache size limit: %s", errmsg);
|
fatal("could not set cache size limit: %s", errmsg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 's': /* --show-stats */
|
case 's': /* --show-stats */
|
||||||
|
10
ccache.h
10
ccache.h
@ -129,9 +129,9 @@ const char *get_extension(const char *path);
|
|||||||
char *remove_extension(const char *path);
|
char *remove_extension(const char *path);
|
||||||
size_t file_size(struct stat *st);
|
size_t file_size(struct stat *st);
|
||||||
int safe_create_wronly(const char *fname);
|
int safe_create_wronly(const char *fname);
|
||||||
char *format_human_readable_size(size_t size);
|
char *format_human_readable_size(uint64_t size);
|
||||||
char *format_parsable_size_with_suffix(size_t size);
|
char *format_parsable_size_with_suffix(uint64_t size);
|
||||||
bool parse_size_with_suffix(const char *str, size_t *size);
|
bool parse_size_with_suffix(const char *str, uint64_t *size);
|
||||||
char *x_realpath(const char *path);
|
char *x_realpath(const char *path);
|
||||||
char *gnu_getcwd(void);
|
char *gnu_getcwd(void);
|
||||||
#ifndef HAVE_STRTOK_R
|
#ifndef HAVE_STRTOK_R
|
||||||
@ -164,9 +164,9 @@ void stats_flush(void);
|
|||||||
unsigned stats_get_pending(enum stats stat);
|
unsigned stats_get_pending(enum stats stat);
|
||||||
void stats_zero(void);
|
void stats_zero(void);
|
||||||
void stats_summary(struct conf *conf);
|
void stats_summary(struct conf *conf);
|
||||||
void stats_update_size(enum stats stat, size_t size, unsigned files);
|
void stats_update_size(enum stats stat, uint64_t size, unsigned files);
|
||||||
void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles,
|
void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles,
|
||||||
unsigned *maxsize);
|
uint64_t *maxsize);
|
||||||
void stats_set_sizes(const char *dir, size_t num_files, size_t total_size);
|
void stats_set_sizes(const char *dir, size_t num_files, size_t total_size);
|
||||||
void stats_read(const char *path, struct counters *counters);
|
void stats_read(const char *path, struct counters *counters);
|
||||||
void stats_write(const char *path, struct counters *counters);
|
void stats_write(const char *path, struct counters *counters);
|
||||||
|
10
cleanup.c
10
cleanup.c
@ -29,14 +29,14 @@
|
|||||||
static struct files {
|
static struct files {
|
||||||
char *fname;
|
char *fname;
|
||||||
time_t mtime;
|
time_t mtime;
|
||||||
size_t size; /* In KiB. */
|
uint64_t size;
|
||||||
} **files;
|
} **files;
|
||||||
static unsigned allocated; /* Size of the files array. */
|
static unsigned allocated; /* Size of the files array. */
|
||||||
static unsigned num_files; /* Number of used entries in the files array. */
|
static unsigned num_files; /* Number of used entries in the files array. */
|
||||||
|
|
||||||
static size_t cache_size; /* In KiB. */
|
static uint64_t cache_size;
|
||||||
static size_t files_in_cache;
|
static size_t files_in_cache;
|
||||||
static size_t cache_size_threshold;
|
static uint64_t cache_size_threshold;
|
||||||
static size_t files_in_cache_threshold;
|
static size_t files_in_cache_threshold;
|
||||||
|
|
||||||
/* File comparison function that orders files in mtime order, oldest first. */
|
/* File comparison function that orders files in mtime order, oldest first. */
|
||||||
@ -86,7 +86,7 @@ traverse_fn(const char *fname, struct stat *st)
|
|||||||
files[num_files] = (struct files *)x_malloc(sizeof(struct files));
|
files[num_files] = (struct files *)x_malloc(sizeof(struct files));
|
||||||
files[num_files]->fname = x_strdup(fname);
|
files[num_files]->fname = x_strdup(fname);
|
||||||
files[num_files]->mtime = st->st_mtime;
|
files[num_files]->mtime = st->st_mtime;
|
||||||
files[num_files]->size = file_size(st) / 1024;
|
files[num_files]->size = file_size(st);
|
||||||
cache_size += files[num_files]->size;
|
cache_size += files[num_files]->size;
|
||||||
files_in_cache++;
|
files_in_cache++;
|
||||||
num_files++;
|
num_files++;
|
||||||
@ -114,7 +114,7 @@ delete_sibling_file(const char *base, const char *extension)
|
|||||||
|
|
||||||
path = format("%s%s", base, extension);
|
path = format("%s%s", base, extension);
|
||||||
if (lstat(path, &st) == 0) {
|
if (lstat(path, &st) == 0) {
|
||||||
delete_file(path, file_size(&st) / 1024);
|
delete_file(path, file_size(&st));
|
||||||
} else if (errno != ENOENT) {
|
} else if (errno != ENOENT) {
|
||||||
cc_log("Failed to stat %s (%s)", path, strerror(errno));
|
cc_log("Failed to stat %s (%s)", path, strerror(errno));
|
||||||
}
|
}
|
||||||
|
6
conf.c
6
conf.c
@ -78,8 +78,8 @@ parse_octal(const char *str, void *result, char **errmsg)
|
|||||||
static bool
|
static bool
|
||||||
parse_size(const char *str, void *result, char **errmsg)
|
parse_size(const char *str, void *result, char **errmsg)
|
||||||
{
|
{
|
||||||
unsigned *value = (unsigned *)result;
|
uint64_t *value = (uint64_t *)result;
|
||||||
size_t size;
|
uint64_t size;
|
||||||
*errmsg = NULL;
|
*errmsg = NULL;
|
||||||
if (parse_size_with_suffix(str, &size)) {
|
if (parse_size_with_suffix(str, &size)) {
|
||||||
*value = size;
|
*value = size;
|
||||||
@ -426,7 +426,7 @@ conf_create(void)
|
|||||||
conf->hash_dir = false;
|
conf->hash_dir = false;
|
||||||
conf->log_file = x_strdup("");
|
conf->log_file = x_strdup("");
|
||||||
conf->max_files = 0;
|
conf->max_files = 0;
|
||||||
conf->max_size = 1024 * 1024; /* kilobyte */
|
conf->max_size = 1024 * 1024 * 1024;
|
||||||
conf->path = x_strdup("");
|
conf->path = x_strdup("");
|
||||||
conf->prefix_command = x_strdup("");
|
conf->prefix_command = x_strdup("");
|
||||||
conf->read_only = false;
|
conf->read_only = false;
|
||||||
|
2
conf.h
2
conf.h
@ -19,7 +19,7 @@ struct conf {
|
|||||||
bool hash_dir;
|
bool hash_dir;
|
||||||
char *log_file;
|
char *log_file;
|
||||||
unsigned max_files;
|
unsigned max_files;
|
||||||
unsigned max_size;
|
uint64_t max_size;
|
||||||
char *path;
|
char *path;
|
||||||
char *prefix_command;
|
char *prefix_command;
|
||||||
bool read_only;
|
bool read_only;
|
||||||
|
26
stats.c
26
stats.c
@ -43,7 +43,7 @@ static struct counters *counter_updates;
|
|||||||
#define FLAG_ALWAYS 2 /* always show, even if zero */
|
#define FLAG_ALWAYS 2 /* always show, even if zero */
|
||||||
#define FLAG_NEVER 4 /* never show */
|
#define FLAG_NEVER 4 /* never show */
|
||||||
|
|
||||||
static void display_size(size_t v);
|
static void display_size_times_1024(uint64_t size);
|
||||||
|
|
||||||
/* statistics fields in display order */
|
/* statistics fields in display order */
|
||||||
static struct {
|
static struct {
|
||||||
@ -77,20 +77,26 @@ static struct {
|
|||||||
{ STATS_NOINPUT, "no input file ", NULL, 0 },
|
{ STATS_NOINPUT, "no input file ", NULL, 0 },
|
||||||
{ STATS_BADEXTRAFILE, "error hashing extra file ", NULL, 0 },
|
{ STATS_BADEXTRAFILE, "error hashing extra file ", NULL, 0 },
|
||||||
{ STATS_NUMFILES, "files in cache ", NULL, FLAG_NOZERO|FLAG_ALWAYS },
|
{ STATS_NUMFILES, "files in cache ", NULL, FLAG_NOZERO|FLAG_ALWAYS },
|
||||||
{ STATS_TOTALSIZE, "cache size ", display_size , FLAG_NOZERO|FLAG_ALWAYS },
|
{ STATS_TOTALSIZE, "cache size ", display_size_times_1024 , FLAG_NOZERO|FLAG_ALWAYS },
|
||||||
{ STATS_OBSOLETE_MAXFILES, "OBSOLETE", NULL, FLAG_NOZERO|FLAG_NEVER},
|
{ STATS_OBSOLETE_MAXFILES, "OBSOLETE", NULL, FLAG_NOZERO|FLAG_NEVER},
|
||||||
{ STATS_OBSOLETE_MAXSIZE, "OBSOLETE", NULL, FLAG_NOZERO|FLAG_NEVER},
|
{ STATS_OBSOLETE_MAXSIZE, "OBSOLETE", NULL, FLAG_NOZERO|FLAG_NEVER},
|
||||||
{ STATS_NONE, NULL, NULL, 0 }
|
{ STATS_NONE, NULL, NULL, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
display_size(size_t v)
|
display_size(uint64_t size)
|
||||||
{
|
{
|
||||||
char *s = format_human_readable_size(v);
|
char *s = format_human_readable_size(size);
|
||||||
printf("%15s", s);
|
printf("%15s", s);
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
display_size_times_1024(uint64_t size)
|
||||||
|
{
|
||||||
|
display_size(size * 1024);
|
||||||
|
}
|
||||||
|
|
||||||
/* parse a stats file from a buffer - adding to the counters */
|
/* parse a stats file from a buffer - adding to the counters */
|
||||||
static void
|
static void
|
||||||
parse_stats(struct counters *counters, const char *buf)
|
parse_stats(struct counters *counters, const char *buf)
|
||||||
@ -159,14 +165,14 @@ init_counter_updates(void)
|
|||||||
* number of bytes and files have been added to the cache. Size is in KiB.
|
* number of bytes and files have been added to the cache. Size is in KiB.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
stats_update_size(enum stats stat, size_t size, unsigned files)
|
stats_update_size(enum stats stat, uint64_t size, unsigned files)
|
||||||
{
|
{
|
||||||
init_counter_updates();
|
init_counter_updates();
|
||||||
if (stat != STATS_NONE) {
|
if (stat != STATS_NONE) {
|
||||||
counter_updates->data[stat]++;
|
counter_updates->data[stat]++;
|
||||||
}
|
}
|
||||||
counter_updates->data[STATS_NUMFILES] += files;
|
counter_updates->data[STATS_NUMFILES] += files;
|
||||||
counter_updates->data[STATS_TOTALSIZE] += size;
|
counter_updates->data[STATS_TOTALSIZE] += size / 1024;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read in the stats from one directory and add to the counters. */
|
/* Read in the stats from one directory and add to the counters. */
|
||||||
@ -244,7 +250,7 @@ stats_flush(void)
|
|||||||
need_cleanup = true;
|
need_cleanup = true;
|
||||||
}
|
}
|
||||||
if (conf->max_size != 0
|
if (conf->max_size != 0
|
||||||
&& counters->data[STATS_TOTALSIZE] > conf->max_size / 16) {
|
&& counters->data[STATS_TOTALSIZE] * 1024 > conf->max_size / 16) {
|
||||||
need_cleanup = true;
|
need_cleanup = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,13 +367,13 @@ stats_zero(void)
|
|||||||
|
|
||||||
/* Get the per directory limits */
|
/* Get the per directory limits */
|
||||||
void
|
void
|
||||||
stats_get_obsolete_limits(const char *dir, unsigned *maxfiles, unsigned *maxsize)
|
stats_get_obsolete_limits(const char *dir, unsigned *maxfiles, uint64_t *maxsize)
|
||||||
{
|
{
|
||||||
struct counters *counters = counters_init(STATS_END);
|
struct counters *counters = counters_init(STATS_END);
|
||||||
char *sname = format("%s/stats", dir);
|
char *sname = format("%s/stats", dir);
|
||||||
stats_read(sname, counters);
|
stats_read(sname, counters);
|
||||||
*maxfiles = counters->data[STATS_OBSOLETE_MAXFILES];
|
*maxfiles = counters->data[STATS_OBSOLETE_MAXFILES];
|
||||||
*maxsize = counters->data[STATS_OBSOLETE_MAXSIZE];
|
*maxsize = counters->data[STATS_OBSOLETE_MAXSIZE] * 1024;
|
||||||
free(sname);
|
free(sname);
|
||||||
counters_free(counters);
|
counters_free(counters);
|
||||||
}
|
}
|
||||||
@ -384,7 +390,7 @@ stats_set_sizes(const char *dir, size_t num_files, size_t total_size)
|
|||||||
if (lockfile_acquire(statsfile, lock_staleness_limit)) {
|
if (lockfile_acquire(statsfile, lock_staleness_limit)) {
|
||||||
stats_read(statsfile, counters);
|
stats_read(statsfile, counters);
|
||||||
counters->data[STATS_NUMFILES] = num_files;
|
counters->data[STATS_NUMFILES] = num_files;
|
||||||
counters->data[STATS_TOTALSIZE] = total_size;
|
counters->data[STATS_TOTALSIZE] = total_size / 1024;
|
||||||
stats_write(statsfile, counters);
|
stats_write(statsfile, counters);
|
||||||
lockfile_release(statsfile);
|
lockfile_release(statsfile);
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ TEST(conf_create)
|
|||||||
CHECK(!conf->hash_dir);
|
CHECK(!conf->hash_dir);
|
||||||
CHECK_STR_EQ("", conf->log_file);
|
CHECK_STR_EQ("", conf->log_file);
|
||||||
CHECK_INT_EQ(0, conf->max_files);
|
CHECK_INT_EQ(0, conf->max_files);
|
||||||
CHECK_INT_EQ(1024*1024, conf->max_size);
|
CHECK_INT_EQ(1024 * 1024 * 1024, conf->max_size);
|
||||||
CHECK_STR_EQ("", conf->path);
|
CHECK_STR_EQ("", conf->path);
|
||||||
CHECK_STR_EQ("", conf->prefix_command);
|
CHECK_STR_EQ("", conf->prefix_command);
|
||||||
CHECK(!conf->read_only);
|
CHECK(!conf->read_only);
|
||||||
@ -123,7 +123,7 @@ TEST(conf_read_valid_config)
|
|||||||
CHECK(conf->hash_dir);
|
CHECK(conf->hash_dir);
|
||||||
CHECK_STR_EQ_FREE1(format("%s%s", user, user), conf->log_file);
|
CHECK_STR_EQ_FREE1(format("%s%s", user, user), conf->log_file);
|
||||||
CHECK_INT_EQ(17, conf->max_files);
|
CHECK_INT_EQ(17, conf->max_files);
|
||||||
CHECK_INT_EQ(123 * 1024, conf->max_size);
|
CHECK_INT_EQ(123 * 1024 * 1024, conf->max_size);
|
||||||
CHECK_STR_EQ_FREE1(format("%s.x", user), conf->path);
|
CHECK_STR_EQ_FREE1(format("%s.x", user), conf->path);
|
||||||
CHECK_STR_EQ_FREE1(format("x%s", user), conf->prefix_command);
|
CHECK_STR_EQ_FREE1(format("x%s", user), conf->prefix_command);
|
||||||
CHECK(conf->read_only);
|
CHECK(conf->read_only);
|
||||||
|
@ -96,47 +96,50 @@ TEST(subst_env_in_string)
|
|||||||
|
|
||||||
TEST(format_human_readable_size)
|
TEST(format_human_readable_size)
|
||||||
{
|
{
|
||||||
CHECK_STR_EQ_FREE2("0 Kbytes", format_human_readable_size(0));
|
CHECK_STR_EQ_FREE2("0 bytes", format_human_readable_size(0));
|
||||||
CHECK_STR_EQ_FREE2("42 Kbytes", format_human_readable_size(42));
|
CHECK_STR_EQ_FREE2("42.0 Kbytes", format_human_readable_size(42 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("1.0 Mbytes", format_human_readable_size(1024));
|
CHECK_STR_EQ_FREE2("1.0 Mbytes", format_human_readable_size(1024 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("1.2 Mbytes", format_human_readable_size(1234));
|
CHECK_STR_EQ_FREE2("1.2 Mbytes", format_human_readable_size(1234 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("438.5 Mbytes", format_human_readable_size(438.5 * 1024));
|
CHECK_STR_EQ_FREE2("438.5 Mbytes",
|
||||||
CHECK_STR_EQ_FREE2("1.0 Gbytes", format_human_readable_size(1024 * 1024));
|
format_human_readable_size(438.5 * 1024 * 1024));
|
||||||
|
CHECK_STR_EQ_FREE2("1.0 Gbytes",
|
||||||
|
format_human_readable_size(1024 * 1024 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("17.1 Gbytes",
|
CHECK_STR_EQ_FREE2("17.1 Gbytes",
|
||||||
format_human_readable_size(17.11 * 1024 * 1024));
|
format_human_readable_size(17.11 * 1024 * 1024 * 1024));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(format_parsable_size_with_suffix)
|
TEST(format_parsable_size_with_suffix)
|
||||||
{
|
{
|
||||||
CHECK_STR_EQ_FREE2("0", format_parsable_size_with_suffix(0));
|
CHECK_STR_EQ_FREE2("0", format_parsable_size_with_suffix(0));
|
||||||
CHECK_STR_EQ_FREE2("42K", format_parsable_size_with_suffix(42));
|
CHECK_STR_EQ_FREE2("42.0K", format_parsable_size_with_suffix(42 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("1.0M", format_parsable_size_with_suffix(1024));
|
CHECK_STR_EQ_FREE2("1.0M", format_parsable_size_with_suffix(1024 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("1.2M", format_parsable_size_with_suffix(1234));
|
CHECK_STR_EQ_FREE2("1.2M", format_parsable_size_with_suffix(1234 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("438.5M",
|
CHECK_STR_EQ_FREE2("438.5M",
|
||||||
format_parsable_size_with_suffix(438.5 * 1024));
|
format_parsable_size_with_suffix(438.5 * 1024 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("1.0G",
|
CHECK_STR_EQ_FREE2("1.0G",
|
||||||
format_parsable_size_with_suffix(1024 * 1024));
|
format_parsable_size_with_suffix(1024 * 1024 * 1024));
|
||||||
CHECK_STR_EQ_FREE2("17.1G",
|
CHECK_STR_EQ_FREE2(
|
||||||
format_parsable_size_with_suffix(17.11 * 1024 * 1024));
|
"17.1G",
|
||||||
|
format_parsable_size_with_suffix(17.11 * 1024 * 1024 * 1024));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(parse_size_with_suffix)
|
TEST(parse_size_with_suffix)
|
||||||
{
|
{
|
||||||
size_t size;
|
uint64_t size;
|
||||||
CHECK(parse_size_with_suffix("0", &size));
|
CHECK(parse_size_with_suffix("0", &size));
|
||||||
CHECK_INT_EQ(0, size);
|
CHECK_INT_EQ(0, size);
|
||||||
CHECK(parse_size_with_suffix("42K", &size));
|
CHECK(parse_size_with_suffix("42K", &size));
|
||||||
CHECK_INT_EQ(42, size);
|
CHECK_INT_EQ(42 * 1024, size);
|
||||||
CHECK(parse_size_with_suffix("1.0M", &size));
|
CHECK(parse_size_with_suffix("1.0M", &size));
|
||||||
CHECK_INT_EQ(1024, size);
|
|
||||||
CHECK(parse_size_with_suffix("1.1M", &size));
|
|
||||||
CHECK_INT_EQ(1.1 * 1024, size);
|
|
||||||
CHECK(parse_size_with_suffix("438.5M", &size));
|
|
||||||
CHECK_INT_EQ(438.5 * 1024, size);
|
|
||||||
CHECK(parse_size_with_suffix("1.0G", &size));
|
|
||||||
CHECK_INT_EQ(1024 * 1024, size);
|
CHECK_INT_EQ(1024 * 1024, size);
|
||||||
|
CHECK(parse_size_with_suffix("1.1M", &size));
|
||||||
|
CHECK_INT_EQ(1.1 * 1024 * 1024, size);
|
||||||
|
CHECK(parse_size_with_suffix("438.5M", &size));
|
||||||
|
CHECK_INT_EQ(438.5 * 1024 * 1024, size);
|
||||||
|
CHECK(parse_size_with_suffix("1.0G", &size));
|
||||||
|
CHECK_INT_EQ(1024 * 1024 * 1024, size);
|
||||||
CHECK(parse_size_with_suffix("17.1G", &size));
|
CHECK(parse_size_with_suffix("17.1G", &size));
|
||||||
CHECK_INT_EQ(17.1 * 1024 * 1024, size);
|
CHECK_INT_EQ(17.1 * 1024 * 1024 * 1024, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_SUITE_END
|
TEST_SUITE_END
|
||||||
|
35
util.c
35
util.c
@ -814,44 +814,45 @@ safe_create_wronly(const char *fname)
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Format a size (in KiB) as a human-readable string. Caller frees. */
|
/* Format a size as a human-readable string. Caller frees. */
|
||||||
char *
|
char *
|
||||||
format_human_readable_size(size_t v)
|
format_human_readable_size(uint64_t v)
|
||||||
{
|
{
|
||||||
char *s;
|
char *s;
|
||||||
if (v >= 1024*1024) {
|
if (v >= 1024*1024*1024) {
|
||||||
s = format("%.1f Gbytes", v/((double)(1024*1024)));
|
s = format("%.1f Gbytes", v/((double)(1024*1024*1024)));
|
||||||
|
} else if (v >= 1024*1024) {
|
||||||
|
s = format("%.1f Mbytes", v/((double)(1024*1024)));
|
||||||
} else if (v >= 1024) {
|
} else if (v >= 1024) {
|
||||||
s = format("%.1f Mbytes", v/((double)(1024)));
|
s = format("%.1f Kbytes", v/((double)(1024)));
|
||||||
} else {
|
} else {
|
||||||
s = format("%.0f Kbytes", (double)v);
|
s = format("%u bytes", (unsigned)v);
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Format a size (in KiB) as a human-readable string. Caller frees. */
|
/* Format a size (in KiB) as a human-readable string. Caller frees. */
|
||||||
char *
|
char *
|
||||||
format_parsable_size_with_suffix(size_t size)
|
format_parsable_size_with_suffix(uint64_t size)
|
||||||
{
|
{
|
||||||
char *s;
|
char *s;
|
||||||
if (size >= 1024*1024) {
|
if (size >= 1024*1024*1024) {
|
||||||
s = format("%.1fG", size / ((double)(1024*1024)));
|
s = format("%.1fG", size / ((double)(1024*1024*1024)));
|
||||||
|
} else if (size >= 1024*1024) {
|
||||||
|
s = format("%.1fM", size / ((double)(1024*1024)));
|
||||||
} else if (size >= 1024) {
|
} else if (size >= 1024) {
|
||||||
s = format("%.1fM", size / ((double)(1024)));
|
s = format("%.1fK", size / ((double)(1024)));
|
||||||
} else if (size > 0) {
|
|
||||||
s = format("%.0fK", (double)size);
|
|
||||||
} else {
|
} else {
|
||||||
s = x_strdup("0");
|
s = format("%u", (unsigned)size);
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse a value in multiples of 1024 given a string that can end in K, M or G.
|
* Parse a value given a string that can end in K, M or G. Default suffix: G.
|
||||||
* Default suffix: G.
|
|
||||||
*/
|
*/
|
||||||
bool
|
bool
|
||||||
parse_size_with_suffix(const char *str, size_t *size)
|
parse_size_with_suffix(const char *str, uint64_t *size)
|
||||||
{
|
{
|
||||||
char *endptr;
|
char *endptr;
|
||||||
double x;
|
double x;
|
||||||
@ -877,7 +878,7 @@ parse_size_with_suffix(const char *str, size_t *size)
|
|||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*size = x;
|
*size = x * 1024;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user