13
13
#include " llvm/Support/Timer.h"
14
14
#include " llvm/ADT/Statistic.h"
15
15
#include " llvm/ADT/StringMap.h"
16
+ #include " llvm/Config/config.h"
16
17
#include " llvm/Support/CommandLine.h"
17
18
#include " llvm/Support/FileSystem.h"
18
19
#include " llvm/Support/Format.h"
24
25
#include " llvm/Support/raw_ostream.h"
25
26
#include < limits>
26
27
28
+ #if HAVE_UNISTD_H
29
+ #include < unistd.h>
30
+ #endif
31
+
32
+ #ifdef HAVE_PROC_PID_RUSAGE
33
+ #include < libproc.h>
34
+ #endif
35
+
27
36
using namespace llvm ;
28
37
29
38
// This ugly hack is brought to you courtesy of constructor/destructor ordering
@@ -120,6 +129,17 @@ static inline size_t getMemUsage() {
120
129
return sys::Process::GetMallocUsage ();
121
130
}
122
131
132
+ static uint64_t getCurInstructionsExecuted () {
133
+ #if defined(HAVE_UNISTD_H) && defined(HAVE_PROC_PID_RUSAGE) && \
134
+ defined (RUSAGE_INFO_V4)
135
+ struct rusage_info_v4 ru;
136
+ if (proc_pid_rusage (getpid (), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0 ) {
137
+ return ru.ri_instructions ;
138
+ }
139
+ #endif
140
+ return 0 ;
141
+ }
142
+
123
143
TimeRecord TimeRecord::getCurrentTime (bool Start) {
124
144
using Seconds = std::chrono::duration<double , std::ratio<1 >>;
125
145
TimeRecord Result;
@@ -128,9 +148,11 @@ TimeRecord TimeRecord::getCurrentTime(bool Start) {
128
148
129
149
if (Start) {
130
150
Result.MemUsed = getMemUsage ();
151
+ Result.InstructionsExecuted = getCurInstructionsExecuted ();
131
152
sys::Process::GetTimeUsage (now, user, sys);
132
153
} else {
133
154
sys::Process::GetTimeUsage (now, user, sys);
155
+ Result.InstructionsExecuted = getCurInstructionsExecuted ();
134
156
Result.MemUsed = getMemUsage ();
135
157
}
136
158
@@ -180,6 +202,8 @@ void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
180
202
181
203
if (Total.getMemUsed ())
182
204
OS << format (" %9" PRId64 " " , (int64_t )getMemUsed ());
205
+ if (Total.getInstructionsExecuted ())
206
+ OS << format (" %9" PRId64 " " , (int64_t )getInstructionsExecuted ());
183
207
}
184
208
185
209
@@ -339,6 +363,8 @@ void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
339
363
OS << " ---Wall Time---" ;
340
364
if (Total.getMemUsed ())
341
365
OS << " ---Mem---" ;
366
+ if (Total.getInstructionsExecuted ())
367
+ OS << " ---Instr---" ;
342
368
OS << " --- Name ---\n " ;
343
369
344
370
// Loop through all of the timing data, printing it out.
@@ -433,6 +459,10 @@ const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
433
459
OS << delim;
434
460
printJSONValue (OS, R, " .mem" , T.getMemUsed ());
435
461
}
462
+ if (T.getInstructionsExecuted ()) {
463
+ OS << delim;
464
+ printJSONValue (OS, R, " .instr" , T.getInstructionsExecuted ());
465
+ }
436
466
}
437
467
TimersToPrint.clear ();
438
468
return delim;
0 commit comments