Repository navigation
Expand file tree
/
Copy pathTimerWrapper.cc
More file actions
173 lines (151 loc) · 7.87 KB
/
Copy pathTimerWrapper.cc
File metadata and controls
173 lines (151 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <sys/time.h>
#include <unistd.h>
#include "TimerWrapper.h"
////////////////////////////////////////////////////////////////////////////////
// private data members
////////////////////////////////////////////////////////////////////////////////
static unsigned long long startTime = 0; // program start time (actual)
static unsigned long long stopTime = 0; // breakpoint stop time (actual)
static unsigned long long breakpointTime = 0; // accumulated breakpoint time
////////////////////////////////////////////////////////////////////////////////
// private function prototypes
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// returns the actual, raw, unadjusted epoch time in usec
////////////////////////////////////////////////////////////////////////////////
static unsigned long long getActualTimeUsec(void);
////////////////////////////////////////////////////////////////////////////////
// public functions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// this function has the exact same prototype and usage as the normal system
// gettimeofday function but will adjust the returned values based on any time
// spent at a GDB breakpoint
////////////////////////////////////////////////////////////////////////////////
int getTimeOfDay(struct timeval *tv, struct timezone *tz)
{
// get current epoch time
int retCode = gettimeofday(tv, tz);
// convert to usec and adjust time for the amount we've spent at a breakpoint
unsigned long long adjustedTime = (tv->tv_sec*USEC_PER_SEC+tv->tv_usec)-breakpointTime;
// reset adjusted values back into the tv structure
tv->tv_sec = adjustedTime/USEC_PER_SEC;
tv->tv_usec = adjustedTime%USEC_PER_SEC;
return (retCode);
}
////////////////////////////////////////////////////////////////////////////////
// this function will return the number of usec since the epoch adjusted for
// any time spent at a GDB breakpoint
////////////////////////////////////////////////////////////////////////////////
unsigned long long getEpochTimeUsec(void)
{
// adjust our run time to factor out the time we've spent at breakpoints
return (getActualTimeUsec()-breakpointTime);
}
////////////////////////////////////////////////////////////////////////////////
// this function will return the number of msec since the epoch adjusted for
// any time spent at a GDB breakpoint
////////////////////////////////////////////////////////////////////////////////
unsigned long long getEpochTimeMsec(void)
{
// adjust our run time to factor out the time we've spent at breakpoints
return (getEpochTimeUsec()/USEC_PER_MSEC);
}
////////////////////////////////////////////////////////////////////////////////
// this function will return the number of sec since the epoch adjusted for
// any time spent at a GDB breakpoint
////////////////////////////////////////////////////////////////////////////////
unsigned long long getEpochTimeSec(void)
{
// adjust our run time to factor out the time we've spent at breakpoints
return (getEpochTimeUsec()/USEC_PER_SEC);
}
////////////////////////////////////////////////////////////////////////////////
// set the programs start time, this will establish a baseline for all future
// calls to all the below getElapsedTimeXXX functions, this function should be
// called per-the example in the gdbHooksDemo.cc program. It should be called
// before the 'main' as part of a class based instantiation.
////////////////////////////////////////////////////////////////////////////////
void setStartTime(void)
{
startTime = getActualTimeUsec();
}
////////////////////////////////////////////////////////////////////////////////
// returns the program's start time baseline
////////////////////////////////////////////////////////////////////////////////
unsigned long long getStartTime(void)
{
return (startTime);
}
////////////////////////////////////////////////////////////////////////////////
// this function will return the elapsed program run time in usec since the
// start of the program execution, adjusted for any time spent at a GDB
// breakpoint, in order for this to work correctly, the 'setStartTime' function
// must be called as per the example in the gdbDemoHooks.cc program
////////////////////////////////////////////////////////////////////////////////
unsigned long long getElapsedTimeUsec(void)
{
// get our epoch time and factor out our program start time for our program elapsed time
return (getEpochTimeUsec()-startTime);
}
////////////////////////////////////////////////////////////////////////////////
// this function will return the elapsed program run time in msec since the
// start of the program execution, adjusted for any time spent at a GDB
// breakpoint, in order for this to work correctly, the 'setStartTime' function
// must be called as per the example in the gdbDemoHooks.cc program
////////////////////////////////////////////////////////////////////////////////
unsigned long long getElapsedTimeMsec(void)
{
// get our epoch time and factor out our program start time for our program elapsed time
return (getElapsedTimeUsec()/USEC_PER_MSEC);
}
////////////////////////////////////////////////////////////////////////////////
// this function will return the elapsed program run time in sec since the
// start of the program execution, adjusted for any time spent at a GDB
// breakpoint, in order for this to work correctly, the 'setStartTime' function
// must be called as per the example in the gdbDemoHooks.cc program
////////////////////////////////////////////////////////////////////////////////
unsigned long long getElapsedTimeSec(void)
{
// get our epoch time and factor out our program start time for our program elapsed time
return (getElapsedTimeUsec()/USEC_PER_SEC);
}
////////////////////////////////////////////////////////////////////////////////
// GDB timer callback hooks
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// the next two functions are the GDB callback hooks which get installed
// via the file 'timerHooks' which is loaded at the GDB command line startup
// via the '-x' command line GDB option
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// this is called upon program stop to lay down our actual stop time, i.e.
// when a breakpoint is hit or after a step/next command etc.
////////////////////////////////////////////////////////////////////////////////
void stopProgram(void)
{
// lay down the actual time at program stop
stopTime = getActualTimeUsec();
}
////////////////////////////////////////////////////////////////////////////////
// this is called when the program is continued to accumulate the total time
// spent at a breakpoint, i.e. after a continue, next, step command etc.
////////////////////////////////////////////////////////////////////////////////
void continueProgram(void)
{
// accumulate the total time we've spent at breakpoints
breakpointTime += getActualTimeUsec() - stopTime;
}
////////////////////////////////////////////////////////////////////////////////
// private functions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// get the raw, unadjusted time since the epoch in usec
////////////////////////////////////////////////////////////////////////////////
static unsigned long long getActualTimeUsec(void)
{
struct timeval timeOfDay;
gettimeofday(&timeOfDay, NULL);
// convert to microseconds and return
return ((unsigned long long)(timeOfDay.tv_sec*USEC_PER_SEC + timeOfDay.tv_usec));
}