From: Nicholas Nethercote Date: Sun, 26 Jun 2005 18:43:40 +0000 (+0000) Subject: Combine the percentify() functions from Cachegrind and m_transtab into X-Git-Tag: svn/VALGRIND_3_0_0~284 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1273f0df14c2b24cb7862f9d451650c0b2fea9be;p=thirdparty%2Fvalgrind.git Combine the percentify() functions from Cachegrind and m_transtab into a single more general one exported from m_libcprint. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4034 --- diff --git a/cachegrind/cg_main.c b/cachegrind/cg_main.c index 9eb744a426..f32a16aa34 100644 --- a/cachegrind/cg_main.c +++ b/cachegrind/cg_main.c @@ -907,22 +907,6 @@ static UInt ULong_width(ULong n) return w + (w-1)/3; // add space for commas } -static -void percentify(Int n, Int ex, Int field_width, char buf[]) -{ - int i, len, space; - - VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex); - len = VG_(strlen)(buf); - space = field_width - len; - if (space < 0) space = 0; /* Allow for v. small field_width */ - i = len; - - /* Right justify in field */ - for ( ; i >= 0; i--) buf[i + space] = buf[i]; - for (i = 0; i < space; i++) buf[i] = ' '; -} - static void cg_fini(Int exitcode) { static char buf1[128], buf2[128], buf3[128], fmt [128]; @@ -954,10 +938,10 @@ static void cg_fini(Int exitcode) p = 100; if (0 == Ir_total.a) Ir_total.a = 1; - percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1); + VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1); VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1); - percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1); + VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1); VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1); VG_(message)(Vg_UserMsg, ""); @@ -982,14 +966,14 @@ static void cg_fini(Int exitcode) if (0 == D_total.a) D_total.a = 1; if (0 == Dr_total.a) Dr_total.a = 1; if (0 == Dw_total.a) Dw_total.a = 1; - percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1); - percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2); - percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3); + VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1); + VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2); + VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3); VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3); - percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1); - percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2); - percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3); + VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1); + VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2); + VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3); VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3); VG_(message)(Vg_UserMsg, ""); @@ -1007,9 +991,9 @@ static void cg_fini(Int exitcode) VG_(message)(Vg_UserMsg, fmt, "L2 misses: ", L2_total_m, L2_total_mr, L2_total_mw); - percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1); - percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2); - percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3); + VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1); + VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2); + VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3); VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3); diff --git a/coregrind/m_libcprint.c b/coregrind/m_libcprint.c index 9e55a94360..c51bd1d6be 100644 --- a/coregrind/m_libcprint.c +++ b/coregrind/m_libcprint.c @@ -150,6 +150,47 @@ UInt VG_(sprintf) ( Char* buf, const HChar *format, ... ) return ret; } +/* --------------------------------------------------------------------- + percentify() + ------------------------------------------------------------------ */ + +// Percentify n/m with p decimal places. Includes the '%' symbol at the end. +void VG_(percentify)(UInt n, UInt m, UInt d, Int n_buf, char buf[]) +{ + Int i, len, space; + + ULong p1 = (100*n) / m; + + if (d == 0) { + VG_(sprintf)(buf, "%lld%%", p1); + } else { + ULong p2; + UInt ex; + Char fmt[32]; + switch (d) { + case 1: ex = 10; break; + case 2: ex = 100; break; + case 3: ex = 1000; break; + default: VG_(tool_panic)("Currently can only handle 3 decimal places"); + } + p2 = ((100*n*ex) / m) % ex; + // Have to generate the format string in order to be flexible about + // the width of the post-decimal-point part. + VG_(sprintf)(fmt, "%%lld.%%0%dlld%%%%", d); + // fmt is now "%lld.%0lld%%" where is 1,2,3... + VG_(sprintf)(buf, fmt, p1, p2); + } + + len = VG_(strlen)(buf); + space = n_buf - len; + if (space < 0) space = 0; /* Allow for v. small field_width */ + i = len; + + /* Right justify in field */ + for ( ; i >= 0; i--) buf[i + space] = buf[i]; + for (i = 0; i < space; i++) buf[i] = ' '; +} + /* --------------------------------------------------------------------- message() ------------------------------------------------------------------ */ diff --git a/coregrind/m_transtab.c b/coregrind/m_transtab.c index 0684cd4e5b..3541605b26 100644 --- a/coregrind/m_transtab.c +++ b/coregrind/m_transtab.c @@ -685,32 +685,6 @@ static Bool heavier ( TTEntry* t1, TTEntry* t2 ) return score(t1) > score(t2); } -/* Print n/m in form xx.yy% */ -static -void percentify ( ULong n, ULong m, Int field_width, Char* buf) -{ - Int i, len, space; - ULong lo, hi; - if (m == 0) m = 1; /* stay sane */ - hi = (n * 100) / m; - lo = (((n * 100) - hi * m) * 100) / m; - vg_assert(lo < 100); - if (lo < 10) - VG_(sprintf)(buf, "%lld.0%lld%%", hi, lo); - else - VG_(sprintf)(buf, "%lld.%lld%%", hi, lo); - - len = VG_(strlen)(buf); - space = field_width - len; - if (space < 0) space = 0; /* Allow for v. small field_width */ - i = len; - - /* Right justify in field */ - for ( ; i >= 0; i--) buf[i + space] = buf[i]; - for (i = 0; i < space; i++) buf[i] = ' '; -} - - void VG_(show_BB_profile) ( void ) { Char name[64]; @@ -778,8 +752,8 @@ void VG_(show_BB_profile) ( void ) name[63] = 0; score_here = score(tops[r]); score_cumul += score_here; - percentify(score_cumul, score_total, 6, buf_cumul); - percentify(score_here, score_total, 6, buf_here); + VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul); + VG_(percentify)(score_here, score_total, 2, 6, buf_here); VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n", r, score_cumul, buf_cumul, @@ -801,8 +775,8 @@ void VG_(show_BB_profile) ( void ) name[63] = 0; score_here = score(tops[r]); score_cumul += score_here; - percentify(score_cumul, score_total, 6, buf_cumul); - percentify(score_here, score_total, 6, buf_here); + VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul); + VG_(percentify)(score_here, score_total, 2, 6, buf_here); VG_(printf)("\n"); VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin BB rank %d " "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r); diff --git a/include/pub_tool_libcprint.h b/include/pub_tool_libcprint.h index b9155b37b5..67621c36ae 100644 --- a/include/pub_tool_libcprint.h +++ b/include/pub_tool_libcprint.h @@ -45,6 +45,9 @@ extern UInt VG_(vprintf) ( const HChar *format, va_list vargs ); extern UInt VG_(sprintf) ( Char* buf, const HChar* format, ... ); extern UInt VG_(vsprintf)( Char* buf, const HChar* format, va_list vargs ); +// Percentify n/m with p decimal places. Includes the '%' symbol at the end. +extern void VG_(percentify)(UInt n, UInt m, UInt d, Int n_buf, char buf[]); + /* --------------------------------------------------------------------- Messages for the user ------------------------------------------------------------------ */