From 449b6a65695233ea809c0f3c72dfb4fcc1f4308c Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 10 Feb 2007 12:35:33 +0100 Subject: [PATCH] Moved formatting of sizes to libucw. --- lib/sorter/govern.c | 9 ++++----- lib/sorter/sbuck.c | 22 ++-------------------- lib/stkstring.c | 22 ++++++++++++++++++++++ lib/stkstring.h | 4 +++- lib/stkstring.t | 1 + 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/sorter/govern.c b/lib/sorter/govern.c index b6dea908..b15b5eab 100644 --- a/lib/sorter/govern.c +++ b/lib/sorter/govern.c @@ -10,15 +10,14 @@ #include "lib/lib.h" #include "lib/fastbuf.h" #include "lib/mempool.h" +#include "lib/stkstring.h" #include "lib/sorter/common.h" #include #include #include -#include -#define F_SIZE(x) ({ byte *buf = alloca(16); format_size(buf, x); buf; }) -#define F_BSIZE(b) F_SIZE(sbuck_size(b)) +#define F_BSIZE(b) stk_fsize(sbuck_size(b)) static u64 sorter_clock(void) @@ -147,7 +146,7 @@ sorter_twoway(struct sort_context *ctx, struct sort_bucket *b) ASSERT(join->runs == 2); join->runs--; join_size = sbuck_size(join) - join_size; - SORT_TRACE("Mergesort pass %d (final run, %s, %dMB/s)", pass, F_SIZE(join_size), sorter_speed(ctx, join_size)); + SORT_TRACE("Mergesort pass %d (final run, %s, %dMB/s)", pass, stk_fsize(join_size), sorter_speed(ctx, join_size)); sbuck_drop(ins[0]); sbuck_drop(ins[1]); return; @@ -205,7 +204,7 @@ sorter_radix(struct sort_context *ctx, struct sort_bucket *b) } SORT_TRACE("Radix split (%d buckets, %s min, %s max, %s avg, %dMB/s)", nbuck, - F_SIZE(min), F_SIZE(max), F_SIZE(sum / nbuck), sorter_speed(ctx, sum)); + stk_fsize(min), stk_fsize(max), stk_fsize(sum / nbuck), sorter_speed(ctx, sum)); sbuck_drop(b); } diff --git a/lib/sorter/sbuck.c b/lib/sorter/sbuck.c index 287827ee..b1297c15 100644 --- a/lib/sorter/sbuck.c +++ b/lib/sorter/sbuck.c @@ -10,6 +10,7 @@ #include "lib/lib.h" #include "lib/fastbuf.h" #include "lib/mempool.h" +#include "lib/stkstring.h" #include "lib/sorter/common.h" #include @@ -144,7 +145,7 @@ sorter_alloc_buf(struct sort_context *ctx) ctx->big_buf_size = 2*bs; ctx->big_buf_half = ((byte*) ctx->big_buf) + bs; ctx->big_buf_half_size = bs; - SORT_XTRACE(2, "Allocated sorting buffer (2*%jd bytes)", (uintmax_t) bs); + SORT_XTRACE(2, "Allocated sorting buffer (2*%s)", stk_fsize(bs)); } void @@ -156,22 +157,3 @@ sorter_free_buf(struct sort_context *ctx) ctx->big_buf = NULL; SORT_XTRACE(2, "Freed sorting buffer"); } - -void -format_size(byte *buf, u64 x) -{ - if (x < 10<<10) - sprintf(buf, "%.1fK", (double)x/(1<<10)); - else if (x < 1<<20) - sprintf(buf, "%dK", (int)(x/(1<<10))); - else if (x < 10<<20) - sprintf(buf, "%.1fM", (double)x/(1<<20)); - else if (x < 1<<30) - sprintf(buf, "%dM", (int)(x/(1<<20))); - else if (x < (u64)10<<30) - sprintf(buf, "%.1fG", (double)x/(1<<30)); - else if (x != ~(u64)0) - sprintf(buf, "%dG", (int)(x/(1<<30))); - else - strcpy(buf, "unknown"); -} diff --git a/lib/stkstring.c b/lib/stkstring.c index c3b77301..c84d6279 100644 --- a/lib/stkstring.c +++ b/lib/stkstring.c @@ -62,6 +62,27 @@ stk_hexdump_internal(char *dst, byte *src, uns n) *dst = 0; } +void +stk_fsize_internal(char *buf, u64 x) +{ + if (x < 1<<10) + sprintf(buf, "%dB", (int)x); + if (x < 10<<10) + sprintf(buf, "%.1fK", (double)x/(1<<10)); + else if (x < 1<<20) + sprintf(buf, "%dK", (int)(x/(1<<10))); + else if (x < 10<<20) + sprintf(buf, "%.1fM", (double)x/(1<<20)); + else if (x < 1<<30) + sprintf(buf, "%dM", (int)(x/(1<<20))); + else if (x < (u64)10<<30) + sprintf(buf, "%.1fG", (double)x/(1<<30)); + else if (x != ~(u64)0) + sprintf(buf, "%dG", (int)(x/(1<<30))); + else + strcpy(buf, "unknown"); +} + #ifdef TEST int main(void) @@ -76,6 +97,7 @@ int main(void) puts(stk_hexdump(a, 3)); char *ary[] = { "The", "jaws", "that", "bite" }; puts(stk_strjoin(ary, 4, ' ')); + puts(stk_fsize(1234567)); return 0; } diff --git a/lib/stkstring.h b/lib/stkstring.h index f72d3c59..ac2999b5 100644 --- a/lib/stkstring.h +++ b/lib/stkstring.h @@ -1,7 +1,7 @@ /* * UCW Library -- Strings Allocated on the Stack * - * (c) 2005--2006 Martin Mares + * (c) 2005--2007 Martin Mares * (c) 2005 Tomas Valla * * This software may be freely distributed and used according to the terms @@ -20,8 +20,10 @@ #define stk_printf(f...) ({ uns _l=stk_printf_internal(f); char *_x=alloca(_l); sprintf(_x, f); _x; }) #define stk_hexdump(s,n) ({ uns _n=(n); char *_x=alloca(3*_n+1); stk_hexdump_internal(_x,(byte*)(s),_n); _x; }) #define stk_str_unesc(s) ({ byte *_s=(s); byte *_d=alloca(strlen(_s)+1); str_unesc(_d, _s); _d; }) +#define stk_fsize(n) ({ char *_s=alloca(16); stk_fsize_internal(_s, n); _s; }) uns stk_array_len(char **s, uns cnt); void stk_array_join(char *x, char **s, uns cnt, uns sep); uns stk_printf_internal(char *x, ...) FORMAT_CHECK(printf,1,2); void stk_hexdump_internal(char *dst, byte *src, uns n); +void stk_fsize_internal(char *dst, u64 size); diff --git a/lib/stkstring.t b/lib/stkstring.t index 2b0c191c..f4d1319c 100644 --- a/lib/stkstring.t +++ b/lib/stkstring.t @@ -4,3 +4,4 @@ Run: obj/lib/stkstring-t Out: Beware the Jabberwock, my son! 42 65 77 The jaws that bite + 1.2M -- 2.39.5