]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Combine the percentify() functions from Cachegrind and m_transtab into
authorNicholas Nethercote <njn@valgrind.org>
Sun, 26 Jun 2005 18:43:40 +0000 (18:43 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Sun, 26 Jun 2005 18:43:40 +0000 (18:43 +0000)
a single more general one exported from m_libcprint.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4034

cachegrind/cg_main.c
coregrind/m_libcprint.c
coregrind/m_transtab.c
include/pub_tool_libcprint.h

index 9eb744a426bf29c5c3b1de9b71663718d7f5e4ad..f32a16aa3473da692639c80b59d85eb6f8ff51a9 100644 (file)
@@ -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);
             
 
index 9e55a94360ca843802a75fdf19141dd5a24061e3..c51bd1d6be14cb323434f3a18404d6a44a963e69 100644 (file)
@@ -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.%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()
    ------------------------------------------------------------------ */
index 0684cd4e5be8087f0836836747d1b19d168d2910..3541605b26c3f40dbfdc792520eef1784d7e6474 100644 (file)
@@ -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);
index b9155b37b5351a45a8590e3939e501a75ec2c77d..67621c36aeec5fdf823487aaa98cfdf4527822dd 100644 (file)
@@ -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
    ------------------------------------------------------------------ */