HPCToolkit
timer.h
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // Time utilities.
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 // Author:
59 // Nathan Tallent, Rice University.
60 //
61 //***************************************************************************
62 
63 #ifndef prof_lean_timer_h
64 #define prof_lean_timer_h
65 
66 //************************* System Include Files ****************************
67 
68 #ifndef _POSIX_SOURCE
69 # define _POSIX_SOURCE /* enable clockid_t */
70 #endif
71 
72 #include <stdlib.h>
73 #include <stdint.h>
74 
75 #include <time.h> /* clock_gettime() */
76 
77 //*************************** User Include Files ****************************
78 
79 #include <include/uint.h>
80 
81 //*************************** Forward Declarations **************************
82 
83 #if defined(__cplusplus)
84 extern "C" {
85 #endif
86 
87 //***************************************************************************
88 //
89 //***************************************************************************
90 
91 inline static uint64_t
93 {
94  static const uint64_t microsecPerSec = 1000000;
95  return (x * microsecPerSec);
96 }
97 
98 
99 inline static uint64_t
101 {
102  static const uint64_t nanosecPerMicrosec = 1000;
103  return (x / nanosecPerMicrosec);
104 }
105 
106 
107 // get_timestamp_us: compute time in microseconds. On an error,
108 // 'time' is guaranteed not to be changed.
109 inline static int
110 time_getTime_us(clockid_t clockid, uint64_t* time)
111 {
112  struct timespec ts;
113  long ret = clock_gettime(clockid, &ts);
114  if (ret != 0) {
115  return 1;
116  }
117  *time = (time_cvtNanosecToMicrosecs(ts.tv_nsec)
118  + time_cvtSecToMicrosecs(ts.tv_sec));
119  return 0;
120 }
121 
122 
123 inline static int
124 time_getTimeCPU(uint64_t* time)
125 {
126  return time_getTime_us(CLOCK_THREAD_CPUTIME_ID, time);
127 }
128 
129 
130 inline static int
131 time_getTimeReal(uint64_t* time)
132 {
133  // In contrast to CLOCK_MONOTONIC{_HR}, CLOCK_REALTIME{_HR} is
134  // affected by NTP (network time protocol) and can move forward/backward
135 
136 #ifdef CLOCK_REALTIME_HR
137  return time_getTime_us(CLOCK_REALTIME_HR, time);
138 #else
139  return time_getTime_us(CLOCK_REALTIME, time);
140 #endif
141 }
142 
143 
144 //***************************************************************************
145 //
146 //***************************************************************************
147 
148 // time_getTSC: returns value of "time stamp counter" (in cycles).
149 //
150 // Benefit over clock_gettime(): In my experiments, over 10x faster
151 // than calling clock_gettime(): 30 cycles vs. 400 cycles.
152 
153 // N.B.: Precise interpretation may be processor dependent.
154 inline static uint64_t
156 {
157  uint64_t tsc = 0;
158 
159 #if defined(__x86_64__)
160 
161  uint32_t hi, lo;
162  asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
163  tsc = (((uint64_t)hi) << 32) | ((uint64_t)lo);
164 
165 #elif defined(__i386__)
166 
167  asm volatile (".byte 0x0f, 0x31" : "=A" (tsc));
168 
169 #elif defined(__powerpc64__)
170 
171  asm volatile ("mftb %0" : "=r" (tsc) : );
172 
173 #elif defined(__powerpc__)
174 
175  uint32_t hi, lo, tmp;
176  asm volatile("0: \n"
177  "\tmftbu %0 \n"
178  "\tmftb %1 \n"
179  "\tmftbu %2 \n"
180  "\tcmpw %2,%0 \n"
181  "\tbne 0b \n"
182  : "=r" (hi), "=r" (lo), "=r" (tmp));
183  tsc = (((uint64_t)hi) << 32) | ((uint64_t)lo);
184 
185 #else
186 # warning "lib/support-lean/timer.h: time_getTSC()"
187 #endif
188 
189  return tsc;
190 }
191 
192 
193 // **************************************************************************
194 
195 #if defined(__cplusplus)
196 } /* extern "C" */
197 #endif
198 
199 #endif /* prof_lean_timer_h */
static char * tmp
Definition: tokenize.c:63
static uint64_t time_cvtNanosecToMicrosecs(uint64_t x)
Definition: timer.h:100
static uint64_t time_getTSC()
Definition: timer.h:155
static uint64_t time_cvtSecToMicrosecs(uint64_t x)
Definition: timer.h:92
static int time_getTimeCPU(uint64_t *time)
Definition: timer.h:124
static int time_getTime_us(clockid_t clockid, uint64_t *time)
Definition: timer.h:110
static int time_getTimeReal(uint64_t *time)
Definition: timer.h:131