CMS 3D CMS Logo

HRRealTime.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_HRRealTime_H
2 #define FWCore_Utilities_HRRealTime_H
3 /*
4  * High Resolution Real Timer
5  * inline high-resolution real timer
6  * to be used for precise measurements of performance of
7  * "small" chunks of code.
8  *
9  * returns time in "nominal" cpu-clock unit
10  * on most recent hw-architecure it is compensated for clock-rate variations
11  * so to get seconds it shall be multiplied for a nominal cpu-clock unit
12  * Performance comparison make sense only if the clock-rate has been fixed
13  */
14 
15 namespace edm {
16  namespace details {
17 
18  //
19  // defines "rdtsc"
20  //
21 #if defined(__i386__)
22 
23  static __inline__ unsigned long long rdtsc(void)
24  {
25  unsigned long long int x;
26  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
27  return x;
28  }
29 #elif defined(__x86_64__)
30 
31 
32  static __inline__ unsigned long long rdtsc(void)
33  {
34  unsigned hi, lo;
35  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
36  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
37  }
38 
39 #elif defined(__powerpc__)
40 
41 
42  static __inline__ unsigned long long rdtsc(void)
43  {
44  unsigned long long int result=0;
45  unsigned long int upper, lower,tmp;
46  __asm__ volatile(
47  "0: \n"
48  "\tmftbu %0 \n"
49  "\tmftb %1 \n"
50  "\tmftbu %2 \n"
51  "\tcmpw %2,%0 \n"
52  "\tbne 0b \n"
53  : "=r"(upper),"=r"(lower),"=r"(tmp)
54  );
55  result = upper;
56  result = result<<32;
57  result = result|lower;
58 
59  return(result);
60  }
61 #elif defined(__arm__)
62 #warning unsigned long long rdtsc(void) is not implemented on ARMv7 architecture. Returning 0 by default.
63  static __inline__ unsigned long long rdtsc(void)
64  {
65  return 0;
66  }
67 #elif defined(__aarch64__)
68  static __inline__ unsigned long long rdtsc(void)
69  {
70  // We will be reading CNTVCT_EL0 (the virtual counter), which is prepared for us by OS.
71  // The system counter sits outside multiprocessor in SOC and runs on a different frequency.
72  // Increments at a fixed frequency, typically in the range 1-50MHz.
73  // Applications can figure out system counter configuration via CNTFRQ_EL0.
74  //
75  // Notice:
76  // Reads of CNTVCT_EL0 can occur speculatively and out of order relative to other
77  // instructions executed on the same PE.
78  // For example, if a read from memory is used to obtain a signal from another agent
79  // that indicates that CNTVCT_EL0 must be read, an ISB is used to ensure that the
80  // read of CNTVCT_EL0 occurs after the signal has been read from memory
81  //
82  // More details:
83  // Chapter D6: The Generic Timer in AArch64 state
84  // ARM DDI 0487B.a, ID033117 (file: DDI0487B_a_armv8_arm.pdf)
85  unsigned long long ret; // unsigned 64-bit value
86  __asm__ __volatile__ ("isb; mrs %0, cntvct_el0" : "=r" (ret));
87  return ret;
88  }
89 #else
90 #error The file FWCore/Utilities/interface/HRRealTime.h needs to be set up for your CPU type.
91 #endif
92  }
93 }
94 
95 namespace edm {
96 
97  typedef long long int HRTimeDiffType;
98  typedef unsigned long long int HRTimeType;
99 
100  // High Precision real time in clock-units
101  inline HRTimeType hrRealTime() {
102  return details::rdtsc();
103  }
104 
105 }
106 
107 
108 #endif // FWCore_Utilities__HRRealTime_H
HRTimeType hrRealTime()
Definition: HRRealTime.h:101
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
HLT enums.
long long int HRTimeDiffType
Definition: HRRealTime.h:97
unsigned long long int HRTimeType
Definition: HRRealTime.h:98