]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Improve display of max size display values
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 27 Feb 2010 13:01:14 +0000 (14:01 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 27 Feb 2010 13:01:14 +0000 (14:01 +0100)
NEWS
ccache.c
ccache.h
ccache.yo
stats.c
util.c

diff --git a/NEWS b/NEWS
index 56a755a33495b24876556da4d538934be8ab8611..12c2bf41333782377085d5f7915997f9d67fc0f4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -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.
index 2c23d342b64d3b6653733977dc6ca9e1e0405193..1a43698cb0e2cfea8a8fa499f02eaa1238e94f44 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -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");
index f4163f12a456fc4a7a81b006dabfa59f91f71ba8..82b786787211d4bc03966a547af025e72f334297 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -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);
index 4a093224efed22af940a33d0e8ae4a74eddcbd35..a829e99076551f6bf212cc17f972d89870367d14 100644 (file)
--- a/ccache.yo
+++ b/ccache.yo
@@ -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
 
diff --git a/stats.c b/stats.c
index 27d304a46fde4c07c236762bca26b03dac3d1d68..89336c44b4fe6848623b8b2adcaa745dca294d3f 100644 (file)
--- a/stats.c
+++ b/stats.c
@@ -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)
 {
diff --git a/util.c b/util.c
index 1268f3fa05775734d250219da03040a1f9bbaf4a..951ace18c6aba37cf984abee8e3e76e1fe1b92d4 100644 (file)
--- a/util.c
+++ b/util.c
@@ -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)
-{
-       if (v > 1024*1024) {
-               printf("%8.1f Gbytes", v/((double)(1024*1024)));
-       } else if (v > 1024) {
-               printf("%8.1f Mbytes", v/((double)(1024)));
+/* Format a size as a human-readable string. Caller frees. */
+char *format_size(size_t v)
+{
+       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