Improve display of max size display values

This commit is contained in:
Joel Rosdahl 2010-02-27 14:01:14 +01:00
parent d4d8843588
commit 2b2f5ddcd8
6 changed files with 30 additions and 12 deletions

2
NEWS
View File

@ -57,6 +57,8 @@ New features and improvements:
- Clarified cache size limit options' semantics
- Improved display of cache max size values.
Bug fixes:
- Fixed build on FreeBSD.

View File

@ -61,7 +61,8 @@ static const char USAGE_TEXT[] =
" -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
" no limit)\n"
" -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
" limit; available suffixes: G, M and K)\n"
" limit; available suffixes: G, M and K; default\n"
" suffix: G)\n"
" -s, --show-stats show statistics summary\n"
" -z, --zero-stats zero statistics counters\n"
"\n"
@ -1736,7 +1737,10 @@ static int ccache_main(int argc, char *argv[])
if (v == 0) {
printf("Unset cache size limit\n");
} else {
printf("Set cache size limit to %uk\n", (unsigned)v);
char *s = format_size(v);
printf("Set cache size limit to %s\n",
s);
free(s);
}
} else {
printf("Could not set cache size limit.\n");

View File

@ -102,7 +102,7 @@ void stats_tocache(size_t size);
void stats_read(const char *stats_file, unsigned counters[STATS_END]);
int stats_set_limits(long maxfiles, long maxsize);
size_t value_units(const char *s);
void display_size(unsigned v);
char *format_size(size_t v);
void stats_set_sizes(const char *dir, size_t num_files, size_t total_size);
int unify_hash(struct mdfour *hash, const char *fname);

View File

@ -25,7 +25,8 @@ verb(
-F, --max-files=N set maximum number of files in cache to N (use 0 for
no limit)
-M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no
limit; available suffixes: G, M and K)
limit; available suffixes: G, M and K; default
suffix: G)
-s, --show-stats show statistics summary
-z, --zero-stats zero statistics counters

11
stats.c
View File

@ -41,11 +41,13 @@ extern char *cache_dir;
#define FLAG_NOZERO 1 /* don't zero with the -z option */
#define FLAG_ALWAYS 2 /* always show, even if zero */
static void display_size(size_t v);
/* statistics fields in display order */
static struct {
enum stats stat;
char *message;
void (*fn)(unsigned );
void (*fn)(size_t );
unsigned flags;
} stats_info[] = {
{ STATS_CACHEHIT_DIR, "cache hit (direct) ", NULL, FLAG_ALWAYS },
@ -75,6 +77,13 @@ static struct {
{ STATS_NONE, NULL, NULL, 0 }
};
static void display_size(size_t v)
{
char *s = format_size(v);
printf("%15s", s);
free(s);
}
/* parse a stats file from a buffer - adding to the counters */
static void parse_stats(unsigned counters[STATS_END], char *buf)
{

16
util.c
View File

@ -547,16 +547,18 @@ int safe_open(const char *fname)
return fd;
}
/* display a kilobyte unsigned value in M, k or G */
void display_size(unsigned v)
/* Format a size as a human-readable string. Caller frees. */
char *format_size(size_t v)
{
if (v > 1024*1024) {
printf("%8.1f Gbytes", v/((double)(1024*1024)));
} else if (v > 1024) {
printf("%8.1f Mbytes", v/((double)(1024)));
char *s;
if (v >= 1024*1024) {
x_asprintf(&s, "%.1f Gbytes", v/((double)(1024*1024)));
} else if (v >= 1024) {
x_asprintf(&s, "%.1f Mbytes", v/((double)(1024)));
} else {
printf("%8u Kbytes", v);
x_asprintf(&s, "%.0f Kbytes", (double)v);
}
return s;
}
/* return a value in multiples of 1024 give a string that can end