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 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/cachegrind/cg_main.c b/cachegrind/cg_main.c
index 9eb744a..f32a16a 100644
--- a/cachegrind/cg_main.c
+++ b/cachegrind/cg_main.c
@@ -907,22 +907,6 @@
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 @@
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 @@
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 @@
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 9e55a94..c51bd1d 100644
--- a/coregrind/m_libcprint.c
+++ b/coregrind/m_libcprint.c
@@ -151,6 +151,47 @@
}
/* ---------------------------------------------------------------------
+ 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.%0<d>lld%%" where <d> 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 0684cd4..3541605 100644
--- a/coregrind/m_transtab.c
+++ b/coregrind/m_transtab.c
@@ -685,32 +685,6 @@
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 @@
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 @@
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 b9155b3..67621c3 100644
--- a/include/pub_tool_libcprint.h
+++ b/include/pub_tool_libcprint.h
@@ -45,6 +45,9 @@
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
------------------------------------------------------------------ */