[PATCH 2/2] PowerPC - Add a faster way to read the Time Base register
Tulio Magno Quites Machado Filho
tuliom@linux.vnet.ibm.com
Wed Apr 4 20:42:00 GMT 2012
Add function __ppc_get_timebase() to directly read the Time Base register.
This is required for applications that measure time at high frequencies
with high precision that can't afford a syscall.
2012-03-26 Tulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>
* sysdeps/powerpc/sys/platform/ppc.h (__ppc_get_timebase): New
function definition.
* sysdeps/powerpc/Makefile (tests): Add test-gettimebase.
* sysdeps/powerpc/test-gettimebase.c: Test for
__ppc_get_timebase() to catch future ISA opcode/insn changes.
* manual/Makefile (appendices): Include platform.texi.
* manual/contrib.texi: Likewise.
* manual/maint.texi: Likewise.
* manual/platform.texi: New file. Document the new function.
---
manual/Makefile | 3 +-
manual/contrib.texi | 2 +-
manual/maint.texi | 2 +-
manual/platform.texi | 29 ++++++++++++++++++++++
sysdeps/powerpc/Makefile | 2 +
sysdeps/powerpc/sys/platform/ppc.h | 35 ++++++++++++++++++++++++++
sysdeps/powerpc/test-gettimebase.c | 47 ++++++++++++++++++++++++++++++++++++
7 files changed, 117 insertions(+), 3 deletions(-)
create mode 100644 manual/platform.texi
create mode 100644 sysdeps/powerpc/test-gettimebase.c
diff --git a/manual/Makefile b/manual/Makefile
index daddd29..2dd7c96 100644
--- a/manual/Makefile
+++ b/manual/Makefile
@@ -58,7 +58,8 @@ chapters = $(addsuffix .texi, \
resource setjmp signal startup process job nss \
users sysinfo conf crypt debug)
add-chapters = $(wildcard $(foreach d, $(add-ons), ../$d/$d.texi))
-appendices = lang.texi header.texi install.texi maint.texi contrib.texi
+appendices = lang.texi header.texi install.texi maint.texi platform.texi \
+ contrib.texi
licenses = freemanuals.texi lgpl-2.1.texi fdl-1.3.texi
-include texis
diff --git a/manual/contrib.texi b/manual/contrib.texi
index ed10656..8de97be 100644
--- a/manual/contrib.texi
+++ b/manual/contrib.texi
@@ -1,4 +1,4 @@
-@node Contributors, Free Manuals, Maintenance, Top
+@node Contributors, Free Manuals, Platform, Top
@c %MENU% Who wrote what parts of the GNU C Library
@appendix Contributors to @theglibc{}
diff --git a/manual/maint.texi b/manual/maint.texi
index 4a8fede..900f524 100644
--- a/manual/maint.texi
+++ b/manual/maint.texi
@@ -1,4 +1,4 @@
-@node Maintenance, Contributors, Installation, Top
+@node Maintenance, Platform, Installation, Top
@c %MENU% How to enhance and port the GNU C Library
@appendix Library Maintenance
diff --git a/manual/platform.texi b/manual/platform.texi
new file mode 100644
index 0000000..9aceb26
--- /dev/null
+++ b/manual/platform.texi
@@ -0,0 +1,29 @@
+@node Platform, Contributors, Maintenance, Top
+@c %MENU% Describe all platform specific facilities provided
+@appendix Summary of platform specific facilities
+
+@Theglibc{} can provide platform specific functionality.
+@ref{Adding Platform Specific Features} describes the process to include
+these facilities in @theglibc{} and whether they should be included in
+GCC as well.
+
+@menu
+* Power Architecture Facilities:: Summary of Power Architecture
+ specific facilities
+@end menu
+
+@node Power Architecture Facilities
+@appendixsec Summary of Power Architecture specific facilities
+
+All Power Architecture specific facilities that aren't OS specific are
+provided by the header @file{sys/platform/ppc.h}.
+
+@table @code
+
+@item typedef unsigned long long int __ppc_timebase
+Represents the value read from the timebase register.
+
+@item __ppc_timebase __ppc_get_timebase (void)
+Read the timebase register without the need of a system call.
+
+@end table
diff --git a/sysdeps/powerpc/Makefile b/sysdeps/powerpc/Makefile
index 2112f10..54cda7c 100644
--- a/sysdeps/powerpc/Makefile
+++ b/sysdeps/powerpc/Makefile
@@ -28,3 +28,5 @@ gen-as-const-headers += locale-defines.sym
endif
sysdep_headers += sys/platform/ppc.h
+
+tests += test-gettimebase
diff --git a/sysdeps/powerpc/sys/platform/ppc.h b/sysdeps/powerpc/sys/platform/ppc.h
index b4bf7c8..cdfcba5 100644
--- a/sysdeps/powerpc/sys/platform/ppc.h
+++ b/sysdeps/powerpc/sys/platform/ppc.h
@@ -19,5 +19,40 @@
#define _SYS_PLATFORM_PPC_H 1
+typedef unsigned long long int __ppc_timebase;
+
+/* Read the Time Base Register
+ The Time Base Register is a 64-bit register that stores a monotonically
+ incremented value updated at a system dependent frequency that may be
+ different of processor frequency.
+ More information in Power ISA 2.06b - Book II - Section 5.2 */
+#ifdef __powerpc64__
+static inline __ppc_timebase
+__ppc_get_timebase (void)
+{
+ __ppc_timebase __tb;
+ __asm__ volatile (
+ "mfspr %0, 268\n"
+ : "=r" (__tb)
+ : );
+ return __tb;
+}
+#else /* not __powerpc64__ */
+static inline __ppc_timebase
+__ppc_get_timebase (void)
+{
+ register unsigned long __tbu, __tbl, __tmp; \
+ __asm__ volatile (
+ "0:\n"
+ "mftbu %0\n"
+ "mftbl %1\n"
+ "mftbu %2\n"
+ "cmpw %0, %2\n"
+ "bne- 0b\n"
+ : "=r" (__tbu), "=r" (__tbl), "=r" (__tmp)
+ : );
+ return (( (__ppc_timebase) __tbu << 32) | __tbl);
+}
+#endif /* not __powerpc64__ */
#endif /* sys/platform/ppc.h */
diff --git a/sysdeps/powerpc/test-gettimebase.c b/sysdeps/powerpc/test-gettimebase.c
new file mode 100644
index 0000000..f2f76bd
--- /dev/null
+++ b/sysdeps/powerpc/test-gettimebase.c
@@ -0,0 +1,47 @@
+/* Copyright (C) 2012 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Tulio Magno Quites Machado <tuliom@linux.vnet.ibm.com>, 2012.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+/* Test if __ppc_get_timebase() is compatible with the current processor and if
+ it's changing between reads.
+ In case of a read failure it may indicate a future Power ISA or a binutils
+ change. */
+
+#include <stdio.h>
+
+#include <sys/platform/ppc.h>
+
+static int
+do_test(void)
+{
+ __ppc_timebase t1, t2, t3;
+ t1 = __ppc_get_timebase ();
+ printf ("Time Base = %llx\n", t1);
+ t2 = __ppc_get_timebase ();
+ printf ("Time Base = %llx\n", t2);
+ t3 = __ppc_get_timebase ();
+ printf ("Time Base = %llx\n", t3);
+ if ((t1 != t2) && (t1 != t3) && (t2 != t3))
+ return 0;
+
+ printf ("Fail: timebase reads should always be different.");
+ return 1;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
--
1.7.4.4
More information about the Libc-alpha
mailing list