1717#include <ctype.h>
1818
1919#ifndef WITH_TSC
20- #define rdtscll (var )
21- #else /*WITH_TSC defined*/
20+
21+ #define READ_TIMESTAMP (var )
22+
23+ #else
2224
2325typedef unsigned long long uint64 ;
2426
2527#if defined(__ppc__ ) /* <- Don't know if this is the correct symbol; this
2628 section should work for GCC on any PowerPC platform,
2729 irrespective of OS. POWER? Who knows :-) */
2830
29- #define rdtscll (var ) ppc_getcounter(&var)
31+ #define READ_TIMESTAMP (var ) ppc_getcounter(&var)
3032
3133static void
3234ppc_getcounter (uint64 * v )
@@ -45,9 +47,10 @@ ppc_getcounter(uint64 *v)
4547 ((long * )(v ))[1 ] = tb ;
4648}
4749
48- #else /* this section is for linux/x86 */
50+ #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
4951
50- #include <asm/msr.h>
52+ #define READ_TIMESTAMP (val ) \
53+ __asm__ __volatile__("rdtsc" : "=A" (val))
5154
5255#endif
5356
@@ -575,10 +578,10 @@ PyEval_EvalFrame(PyFrameObject *f)
575578 uint64 inst0 , inst1 , loop0 , loop1 , intr0 = 0 , intr1 = 0 ;
576579 int ticked = 0 ;
577580
578- rdtscll (inst0 );
579- rdtscll (inst1 );
580- rdtscll (loop0 );
581- rdtscll (loop1 );
581+ READ_TIMESTAMP (inst0 );
582+ READ_TIMESTAMP (inst1 );
583+ READ_TIMESTAMP (loop0 );
584+ READ_TIMESTAMP (loop1 );
582585
583586 /* shut up the compiler */
584587 opcode = 0 ;
@@ -748,7 +751,7 @@ PyEval_EvalFrame(PyFrameObject *f)
748751 or a continue, preventing inst1 from being set
749752 on the way out of the loop.
750753 */
751- rdtscll (inst1 );
754+ READ_TIMESTAMP (inst1 );
752755 loop1 = inst1 ;
753756 }
754757 dump_tsc (opcode , ticked , inst0 , inst1 , loop0 , loop1 ,
@@ -757,7 +760,7 @@ PyEval_EvalFrame(PyFrameObject *f)
757760 inst1 = 0 ;
758761 intr0 = 0 ;
759762 intr1 = 0 ;
760- rdtscll (loop0 );
763+ READ_TIMESTAMP (loop0 );
761764#endif
762765 assert (stack_pointer >= f -> f_valuestack ); /* else underflow */
763766 assert (STACK_LEVEL () <= f -> f_stacksize ); /* else overflow */
@@ -879,7 +882,7 @@ PyEval_EvalFrame(PyFrameObject *f)
879882#endif
880883
881884 /* Main switch on opcode */
882- rdtscll (inst0 );
885+ READ_TIMESTAMP (inst0 );
883886
884887 switch (opcode ) {
885888
@@ -1638,9 +1641,9 @@ PyEval_EvalFrame(PyFrameObject *f)
16381641 v = SECOND ();
16391642 u = THIRD ();
16401643 STACKADJ (-3 );
1641- rdtscll (intr0 );
1644+ READ_TIMESTAMP (intr0 );
16421645 err = exec_statement (f , u , v , w );
1643- rdtscll (intr1 );
1646+ READ_TIMESTAMP (intr1 );
16441647 Py_DECREF (u );
16451648 Py_DECREF (v );
16461649 Py_DECREF (w );
@@ -2016,9 +2019,9 @@ PyEval_EvalFrame(PyFrameObject *f)
20162019 x = NULL ;
20172020 break ;
20182021 }
2019- rdtscll (intr0 );
2022+ READ_TIMESTAMP (intr0 );
20202023 x = PyEval_CallObject (x , w );
2021- rdtscll (intr1 );
2024+ READ_TIMESTAMP (intr1 );
20222025 Py_DECREF (w );
20232026 SET_TOP (x );
20242027 if (x != NULL ) continue ;
@@ -2032,9 +2035,9 @@ PyEval_EvalFrame(PyFrameObject *f)
20322035 "no locals found during 'import *'" );
20332036 break ;
20342037 }
2035- rdtscll (intr0 );
2038+ READ_TIMESTAMP (intr0 );
20362039 err = import_all_from (x , v );
2037- rdtscll (intr1 );
2040+ READ_TIMESTAMP (intr1 );
20382041 PyFrame_LocalsToFast (f , 0 );
20392042 Py_DECREF (v );
20402043 if (err == 0 ) continue ;
@@ -2043,9 +2046,9 @@ PyEval_EvalFrame(PyFrameObject *f)
20432046 case IMPORT_FROM :
20442047 w = GETITEM (names , oparg );
20452048 v = TOP ();
2046- rdtscll (intr0 );
2049+ READ_TIMESTAMP (intr0 );
20472050 x = import_from (v , w );
2048- rdtscll (intr1 );
2051+ READ_TIMESTAMP (intr1 );
20492052 PUSH (x );
20502053 if (x != NULL ) continue ;
20512054 break ;
@@ -2199,9 +2202,9 @@ PyEval_EvalFrame(PyFrameObject *f)
21992202 } else
22002203 Py_INCREF (func );
22012204 sp = stack_pointer ;
2202- rdtscll (intr0 );
2205+ READ_TIMESTAMP (intr0 );
22032206 x = ext_do_call (func , & sp , flags , na , nk );
2204- rdtscll (intr1 );
2207+ READ_TIMESTAMP (intr1 );
22052208 stack_pointer = sp ;
22062209 Py_DECREF (func );
22072210
@@ -2314,7 +2317,7 @@ PyEval_EvalFrame(PyFrameObject *f)
23142317
23152318 on_error :
23162319
2317- rdtscll (inst1 );
2320+ READ_TIMESTAMP (inst1 );
23182321
23192322 /* Quickly continue if no error occurred */
23202323
@@ -2327,7 +2330,7 @@ PyEval_EvalFrame(PyFrameObject *f)
23272330 "XXX undetected error\n" );
23282331 else {
23292332#endif
2330- rdtscll (loop1 );
2333+ READ_TIMESTAMP (loop1 );
23312334 continue ; /* Normal, fast path */
23322335#ifdef CHECKEXC
23332336 }
@@ -2446,7 +2449,7 @@ PyEval_EvalFrame(PyFrameObject *f)
24462449
24472450 if (why != WHY_NOT )
24482451 break ;
2449- rdtscll (loop1 );
2452+ READ_TIMESTAMP (loop1 );
24502453
24512454 } /* main loop */
24522455
@@ -3543,9 +3546,9 @@ call_function(PyObject ***pp_stack, int oparg
35433546 else {
35443547 PyObject * callargs ;
35453548 callargs = load_args (pp_stack , na );
3546- rdtscll (* pintr0 );
3549+ READ_TIMESTAMP (* pintr0 );
35473550 C_TRACE (x = PyCFunction_Call (func ,callargs ,NULL ));
3548- rdtscll (* pintr1 );
3551+ READ_TIMESTAMP (* pintr1 );
35493552 Py_XDECREF (callargs );
35503553 }
35513554 } else {
@@ -3563,12 +3566,12 @@ call_function(PyObject ***pp_stack, int oparg
35633566 n ++ ;
35643567 } else
35653568 Py_INCREF (func );
3566- rdtscll (* pintr0 );
3569+ READ_TIMESTAMP (* pintr0 );
35673570 if (PyFunction_Check (func ))
35683571 x = fast_function (func , pp_stack , n , na , nk );
35693572 else
35703573 x = do_call (func , pp_stack , na , nk );
3571- rdtscll (* pintr1 );
3574+ READ_TIMESTAMP (* pintr1 );
35723575 Py_DECREF (func );
35733576 }
35743577
0 commit comments