evilsong / gperftools

Automatically exported from code.google.com/p/gperftools
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

[PATCH] Add support to timebase value for PPC64 #453

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This patch add support to get the timebase register value using just one 
instruction and also adjusts the PPC32 code to the recently GLIBC one that 
implements the same functionality (for more information check 
http://sourceware.org/git/?p=glibc.git;a=commit;h=d9dc34cd569bcfe714fe8c708e58c0
28106e8b2e ).

This is a straightforward patch and I believe it might be improved by checking 
the system GLIBC version installed on the system to use "__ppc_get_timebase" 
where possible (it is a static inline function and don't add ABI issues).

Index: src/base/cycleclock.h
===================================================================
--- src/base/cycleclock.h   (revision 151)
+++ src/base/cycleclock.h   (working copy)
@@ -97,15 +97,24 @@
     uint64 low, high;
     __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
     return (high << 32) | low;
+#elif defined(__powerpc64__) || defined(__ppc64__)
+    uint64 tb;
+    __asm__ volatile (\
+      "mfspr %0, 268"
+      : "=r" (tb));
+    return tb;
 #elif defined(__powerpc__) || defined(__ppc__)
     // This returns a time-base, which is not always precisely a cycle-count.
-    int64 tbl, tbu0, tbu1;
-    asm("mftbu %0" : "=r" (tbu0));
-    asm("mftb  %0" : "=r" (tbl));
-    asm("mftbu %0" : "=r" (tbu1));
-    tbl &= -static_cast<int64>(tbu0 == tbu1);
-    // high 32 bits in tbu1; low 32 bits in tbl  (tbu0 is garbage)
-    return (tbu1 << 32) | tbl;
+    uint32 tbu, tbl, tmp;
+    __asm__ volatile (\
+      "0:\n"
+      "mftbu %0\n"
+      "mftbl %1\n"
+      "mftbu %2\n"
+      "cmpw %0, %2\n"
+      "bne- 0b"
+      : "=r" (tbu), "=r" (tbl), "=r" (tmp));
+    return (((uint64) tbu << 32) | tbl);
 #elif defined(__sparc__)
     int64 tick;
     asm(".byte 0x83, 0x41, 0x00, 0x00");

Original issue reported on code.google.com by zatr...@gmail.com on 25 Jul 2012 at 4:19

GoogleCodeExporter commented 9 years ago

Original comment by chapp...@gmail.com on 18 Sep 2012 at 12:16

GoogleCodeExporter commented 9 years ago
Patch has been applied and checked in to the main trunk. I have no way of 
testing this patch myself so if you could grab the development snapshot from 
the main trunk and verify that would be great.

------------------------------------------------------------------------
r153 | chappedm@gmail.com | 2012-09-17 20:42:23 -0400 (Mon, 17 Sep 2012) | 2 
lines

issue-453 Added support to get the timebase register value using just one 
instruction and also adjusts the PPC32 code to the recent GLIBC one that 
implements the same functionality
------------------------------------------------------------------------

Original comment by chapp...@gmail.com on 18 Sep 2012 at 12:48