-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiler.hpp
More file actions
1366 lines (1111 loc) · 45.2 KB
/
Copy pathProfiler.hpp
File metadata and controls
1366 lines (1111 loc) · 45.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#if PROFILER_ENABLED > 0
#define SNDEBUG NDEBUG
#undef NDEBUG
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include <cassert>
#include <chrono>
#include <atomic>
#include <mutex>
#include <thread>
#include <filesystem>
#include <sstream>
#include <iomanip>
#include <map>
#include <shared_mutex>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#include <processthreadsapi.h>
#include <winbase.h>
namespace {
inline int getNumberOfCores() {
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}
//! Return the cpuID starting by 1
inline unsigned int getCPUId()
{
return GetCurrentProcessorNumber();
// PROCESSOR_NUMBER procNumber;
// GetCurrentProcessorNumberEx(&procNumber);
// return procNumber.Group * 64 + procNumber.Number + 1;
}
inline std::string getHostName()
{
TCHAR infoBuf[32767];
DWORD bufCharCount = 32767;
// Get and display the name of the computer.
if (!GetComputerName( infoBuf, &bufCharCount ) )
{
perror("GetComputerName failed");
abort();
}
return infoBuf;
}
inline int getPid()
{
return GetCurrentProcessId();
}
// In MSWindows this seems possible, but unneeded.
inline void killPool()
{
}
#define FUNC_NAME __FUNCSIG__
}
#else // defined(WIN32)
#include <unistd.h>
#include <algorithm>
#include <array>
#include <numeric>
#include <linux/perf_event.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#ifdef _PSTL_PAR_BACKEND_TBB // This macro is defined in gcc libraries
#include <oneapi/tbb/global_control.h>
#endif
namespace {
inline int getNumberOfCores() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
//! Return the cpuID starting by 1
inline unsigned int getCPUId()
{
const int cpu = sched_getcpu();
assert(cpu >= 0);
return cpu + 1;
}
inline std::string getHostName()
{
constexpr size_t len = 128;
char infoBuf[len];
if (gethostname(infoBuf, len) != 0)
perror("Error getting hostname");
return infoBuf;
}
inline int getPid()
{
return getpid();
}
// function to kill tbb thread-pool on linux.
inline void killPool()
{
// In principle this works with clang and gcc... still need to check intel compiler
#ifdef _PSTL_PAR_BACKEND_TBB
oneapi::tbb::task_scheduler_handle handle
= oneapi::tbb::task_scheduler_handle{oneapi::tbb::attach{}};
oneapi::tbb::finalize(handle);
#endif
}
#define FUNC_NAME __PRETTY_FUNCTION__
}
#endif // defined(WIN32)
// Expand the empty namespace to add the common functions
namespace {
//! Get nanoseconds since the trace begins for a given timePoint
inline uint64_t getNanoseconds()
{
// Store the very first time we enter this function and then return the
// number of nanoseconds AFTER this first call.
static const std::chrono::steady_clock::time_point begin
= std::chrono::steady_clock::now();
thread_local std::chrono::steady_clock::time_point last
= std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point current
= std::chrono::steady_clock::now();
if (current < last)
throw std::runtime_error("Consecutive time error");
last = current;
return std::chrono::duration_cast<std::chrono::nanoseconds>(current - begin).count();
}
}
namespace profiler {
constexpr size_t bSize = (1 << 20); //! Default buffer size in bytes (1Mb)
/** Type used for event IDs and sub-value indices throughout the profiler. */
using EventId = uint16_t;
/** Type used for event measurement values stored in each EventEntry. */
using EventValue = uint32_t;
/** Custom error class to handle them if needed. */
class profilerError : public std::exception {
const std::string message;
public:
explicit profilerError(const std::string &msg)
: message("Profiler error: " + msg) {}
const char *what () const throw ()
{
return message.c_str();
}
};
/** Buffer class to store the events.
This class will always have a file associated to it to flush all the
data when needed. There will be 1 Buffer/Core in order to make the
tracing events registration completely lock free. */
class Buffer {
/** Header struct
This is the struct that will be written in the header of the
file. This is update don every flush and keeps information needed in
the head of the file to read it latter. */
struct TraceHeader {
const uint32_t _id; // Can be ThreadID
uint32_t _totalFlushed {0};
const uint64_t _tid;
uint64_t _startGTime; /**< Start global time >*/
TraceHeader(uint32_t id, uint64_t tid, uint64_t startGTime)
: _id(id), _tid(tid), _startGTime(startGTime)
{}
};
//! Event struct that will be reported (and saved into the traces files in binary format)
/** I reserve all the negative values for internal events. And positive
values for user/application events.
Every starting event needs and end event. But I cannot ensure that they
will be in the same cpu (due to thread migration), but they will in the
same thread. So, I also save the threadId with every event in order to
make a right reconstruction latter. */
struct EventEntry {
const uint64_t _time;
const EventId _id;
const uint16_t _core;
const EventValue _value;
EventEntry(EventId id, EventValue value, uint64_t time, uint16_t core)
: _time(time)
, _id(id)
, _core(core)
, _value(value)
{
}
explicit EventEntry(EventId id, EventValue value)
: EventEntry(id, value, getNanoseconds(), getCPUId())
{
}
};
/** Maximum size for the buffers = 1Mb */
static constexpr size_t _maxEntries = ( bSize + sizeof(EventEntry) - 1 ) / sizeof(EventEntry);
const std::string _fileName; //< Name of the binary file with trace information
std::ofstream _file; //< fstream with file open; every write will be appended and binary. >/
EventEntry *_entries {nullptr}; //< Buffer with entries; it will be flushed when the number of entries reach _maxEntries;
size_t _nEntries {0};
void flushBuffer()
{
if (_nEntries == 0)
return;
// We open the file the first time we need t flush the data.
if (!_file.is_open()) {
_file.open(_fileName, std::ios::out | std::ios::binary);
// Reserve space for the header
_file.write(reinterpret_cast<char *>(&_header), sizeof(TraceHeader));
}
for (size_t i = 1; i < _nEntries; ++i) {
if (_entries[i - 1]._time > _entries[i]._time)
std::cerr << "Two events are not time consecutive: " << std::to_string(i) << std::endl;
}
std::cout << "# Flushed: " << _fileName << " << " << _header._totalFlushed << " + " << _nEntries << std::endl;
_header._totalFlushed += _nEntries;
// Go to beginning and write the header
_file.seekp(0, std::ios_base::beg);
_file.write(reinterpret_cast<char *>(&_header), sizeof(TraceHeader));
// Go to end and write the data
_file.seekp(0, std::ios_base::end);
_file.write(reinterpret_cast<char *>(_entries), _nEntries * sizeof(EventEntry));
// Clear the buffer.
_nEntries = 0;
}
public:
// No copy
Buffer(Buffer &&) = delete;
Buffer(const Buffer &) = delete;
Buffer &operator=(const Buffer &) = delete;
Buffer(
uint32_t id, uint64_t tid, const std::string &fileName, uint64_t startGTime
) : _fileName(fileName)
, _header(id, tid, startGTime)
{
// Reserve memory for the buffer.
_entries = (EventEntry *) malloc(_maxEntries * sizeof(EventEntry));
assert(_entries);
}
~Buffer()
{
flushBuffer(); // Flush all remaining events
free(_entries);
_file.close(); // close the file only at the end.
}
void emplaceEvent(EventId id, EventValue value)
{
new (&_entries[_nEntries++]) EventEntry(id, value);
if (_nEntries > 1 && _entries[_nEntries - 2]._time > _entries[_nEntries - 1]._time)
throw profilerError("Registered event is not time consecutive: " + std::to_string(_nEntries - 1));
assert(_nEntries <= _maxEntries);
if (_nEntries == _maxEntries)
flushBuffer();
}
void emplaceMultipleEvents(const std::pair<EventId, EventValue> *events, size_t count)
{
const uint64_t t = getNanoseconds();
const uint16_t core = static_cast<uint16_t>(getCPUId());
for (size_t i = 0; i < count; ++i)
{
new (&_entries[_nEntries++]) EventEntry(events[i].first, events[i].second, t, core);
if (_nEntries > 1 && _entries[_nEntries - 2]._time > _entries[_nEntries - 1]._time)
throw profilerError("Registered event is not time consecutive: " + std::to_string(_nEntries - 1));
assert(_nEntries <= _maxEntries);
if (_nEntries == _maxEntries)
flushBuffer();
}
}
TraceHeader _header;
}; // Buffer
/** Name set thread safe map container.
This is a thread safe container to register the relation between event
name and id. This container is intended to be accessed only once/event to
register the name of the event. */
class NameSet {
struct nameInfo
{
std::string name;
std::string fileName;
size_t line;
// Needed to compare entries
bool operator==(const nameInfo &other) const
{
return name == other.name
&& fileName == other.fileName
&& line == other.line;
}
bool operator!=(const nameInfo &other) const
{
return !(*this == other);
}
operator std::string() const
{
return name + " ("+ fileName + ":" + std::to_string(line) + ")";
}
};
struct nameEntry : public nameInfo
{
std::map<EventId, nameInfo> _namesValuesMap {};
};
public:
#undef max
static constexpr EventId maxUserEvent = std::numeric_limits<EventId>::max() / 2;
static constexpr EventId maxEvent = std::numeric_limits<EventId>::max();
EventId registerEventName(
std::string eventName,
const std::string &fileName = "profiler",
size_t line = 0,
EventId event = EventId()
) {
if (eventName.empty())
{
std::filesystem::path p(fileName);
eventName = p.filename().string()+":"+std::to_string(line);
}
// Fast path: name already registered — shared lock suffices.
{
std::shared_lock sharedLock(_namesMutex);
const auto it = _nameToEventId.find(eventName);
if (it != _nameToEventId.end())
return it->second;
}
// Slow path: first time seeing this name — exclusive access to insert.
std::unique_lock exclusiveLock(_namesMutex);
// Double-check: another thread may have inserted between the two locks.
{
const auto it = _nameToEventId.find(eventName);
if (it != _nameToEventId.end())
return it->second;
}
nameEntry entry = {eventName, fileName, line};
if (event == EventId())
event = ++_counter;
auto it_pair = _namesEventMap.emplace(event, entry);
if (it_pair.second)
{
_nameToEventId[eventName] = event;
return event;
}
const std::string message
= "Cannot register event: '" + eventName
+ "' with id: " + std::to_string(event)
+ " the id is already taken by: '" + std::string(it_pair.first->second) + "'";
throw profilerError(message);
}
EventId registerValueName(
std::string valueName,
const std::string &fileName, size_t line, EventId event, EventId value
) {
if (valueName.empty())
{
std::filesystem::path p(fileName);
valueName = p.filename().string()+":"+std::to_string(line);
}
std::lock_guard<std::shared_mutex> lk(_namesMutex);
auto itEvent = _namesEventMap.find(event);
if (itEvent == _namesEventMap.end())
{
const std::string message
= "Cannot register event value: '" + valueName
+ "' with id: " + std::to_string(event) + ":" + std::to_string(value)
+ " the event ID does not exist.";
throw profilerError(message);
}
nameEntry entry = {valueName, fileName, line};
auto it_pair = itEvent->second._namesValuesMap.emplace(value, entry);
// Insertion succeeded, we can return
if (it_pair.second)
return value;
const std::string message
= "Cannot cannot register event value: '" + valueName
+ "' with id: " + std::to_string(event) + ":" + std::to_string(value)
+ " it is already taken by '" + it_pair.first->second.name + "'";
throw profilerError(message);
}
EventId lookupEventName(std::string_view name) const
{
std::shared_lock sharedLock(_namesMutex);
const auto it = _nameToEventId.find(name);
if (it == _nameToEventId.end())
throw profilerError("Event '" + std::string(name) + "' not registered");
return it->second;
}
void createPCF(const std::string &traceDirectory) const
{
// PCF File
std::ofstream pcffile(traceDirectory + "/Trace.pcf", std::ios::out);
// Register all Events types names.
for (auto it : _namesEventMap)
{
const nameEntry &eventEntry = it.second;
pcffile << "# " << eventEntry.fileName << ":" << eventEntry.line << std::endl;
pcffile << "EVENT_TYPE" << std::endl;
pcffile << "0 " << it.first << " " << eventEntry.name << std::endl;
// Create a "VALUES" sections if some value is registered for this event
if (!eventEntry._namesValuesMap.empty())
{
pcffile << "VALUES" << std::endl;
for (auto itValues : eventEntry._namesValuesMap)
pcffile << itValues.first << " "
<< eventEntry.name << ":" << itValues.second.name << std::endl;
}
pcffile << std::endl;
}
pcffile.close();
}
private:
mutable std::shared_mutex _namesMutex; /**< mutex needed to write in the global file */
EventId _counter = maxUserEvent; /**< counter for automatic function registration */
std::map<EventId, nameEntry> _namesEventMap; /**< map with the events names */
std::map<std::string, EventId, std::less<>> _nameToEventId; /**< reverse map: name → event ID; std::less<> enables heterogeneous lookup with std::string_view */
}; // NameSet
/** BufferSet container
This is container stores the buffer for every thread. in a map <tid,
Buffer> This is intended to remember the tid to reuse the Buffer because
the tid is usually recycled after a thread is deleted.
This class is allocated inside a shared_ptr to enforce that it will be
deleted only after the destruction of all the threads.
The Global container holds a reference to it; but every ThreadInfo will
also keep one reference.
This is because it seems like on GNU/Linux the global variables are
destructed after the main thread; but in MSWindows the Global variables
seems to be removed before the main thread completes. */
class BufferSet {
std::shared_mutex _mapMutex; /**< mutex needed to access the _eventsMap */
std::map<size_t, Buffer> _eventsMap; /**< This map contains the relation tid->id */
uint32_t _tcounter = 1; /**< tid counter always > 0 */
friend EventId registerName(const std::string &name, EventId value);
public:
void createROW(const std::string &traceDirectory) const
{
const std::string hostname = getHostName();
const int ncores = getNumberOfCores();
const size_t nthreads = _tcounter - 1;
// ROW File
std::ofstream rowfile(traceDirectory + "/Trace.row", std::ios::out);
rowfile << "LEVEL CPU SIZE " << ncores << std::endl;
for (int i = 1; i <= ncores; ++i)
rowfile << i << "." << hostname << std::endl;
rowfile << "\nLEVEL NODE SIZE 1" << std::endl;
rowfile << hostname << std::endl;
rowfile << "\nLEVEL THREAD SIZE " << nthreads << std::endl;
for (size_t i = 1; i <= nthreads; ++i)
rowfile << "THREAD 1.1." << i << std::endl;
rowfile.close();
}
/** Get the Buffer_t associated with a thread id hash
The threadIds are usually reused after a thread is destroyed.
Opening/closing files on every thread creation/deletion may be too
expensive; especially if the threads are created destroyed very
frequently.
We keep the associative map <tid, Buffer> in order to reuse Buffer
and only execute IO operations when the buffer is full or at the end
of the execution.
The extra cost for this is that we need to take a lock once (on
thread construction or when emitting the first event from a thread)
in order to get it's associated buffer. This function is responsible
to take the lock and return the associated buffer. When a threadId
is seen for a first time this function creates the new entry in the
map, construct the Buffer and assign an ordinal id for it. Any
optimization here will be very welcome. */
Buffer &getThreadBuffer(size_t tid);
}; // BufferSet
#ifdef __linux__
/** Descriptor for a supported perf event: its perf list name, type, and config.
Entries must be kept sorted by name so the constructor can use
std::lower_bound for a single O(log n) lookup. */
struct PerfCounterSpec
{
std::string_view name;
uint32_t type;
uint64_t config;
constexpr bool operator<(std::string_view other) const { return name < other; }
};
/** All supported perf counters (hardware and software), sorted by name.
Opened with pid=0, cpu=-1: the kernel attaches each event to the calling
thread and transparently saves/restores the counter on context switches
and CPU migrations, so deltas always reflect only this thread's activity
regardless of which core it runs on. */
constexpr PerfCounterSpec allCounters[] = {
{"branch-instructions", PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS},
{"branch-misses", PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES},
{"bus-cycles", PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES},
{"cache-misses", PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES},
{"cache-references", PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES},
{"context-switches", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES},
{"cpu-clock", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK},
{"cpu-cycles", PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES},
{"cpu-migrations", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS},
{"instructions", PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS},
{"major-faults", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ},
{"minor-faults", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN},
{"page-faults", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS},
{"stalled-cycles-backend", PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND},
{"stalled-cycles-frontend", PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND},
{"task-clock", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK},
};
class PerfCounter; // full definition after Global is complete
#endif
/** Class for thread local singleton.
This class will be allocated in a copy/thead in order to perform the
events emission completely thread free. The constructor of this object
takes place the first time an event is emitted from some thread; so it
is not very accurate to use it to measure total thread
duration. However, it is the best we have until we can enforce some
thread hooks. */
class InfoThread {
const uint64_t _tid;
public:
Buffer &eventsBuffer;
const uint32_t _id;
/** Thread local Info initialization.
The class is actually constructed the first time the thread local
variables is accesses. But the buffer is not destroyed on thread
finalization because the same threadID may be reused in the future.
This emits an event of type 2 and value tid (which is always bigger
than zero). */
InfoThread();
~InfoThread();
#ifdef __linux__
/** Returns the shared PerfCounter for the given counter names on this thread,
creating and resetting it once if it does not yet exist.
All INSTRUMENT_PERF call sites with the same names share one FD and one
reset point, so emitted values are thread-absolute. */
PerfCounter &getOrCreatePerfCounter(std::initializer_list<const char *> names);
private:
std::map<std::vector<std::string>, std::unique_ptr<PerfCounter>> _perfCounters;
#endif
}; // InfoThread
/** Info container with global variables.
This gives access to the thread and global static variables. And only
holds one pointer to the BufferSet object to avoid its premature
deletion. */
class Global {
/** Static utility function to build the trace directory */
static std::string getTraceDirectory(uint64_t systemTimePoint)
{
const time_t localTime = static_cast<time_t>(systemTimePoint);
std::stringstream ss;
ss << "TRACEDIR_" << std::put_time(std::localtime(&localTime), "%Y-%m-%d_%H_%M_%S") << "_" << getPid();
return ss.str();
}
public:
static InfoThread &getInfoThread()
{
thread_local static InfoThread threadInfo;
return threadInfo;
}
Global()
: startSystemTimePoint(std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now()).time_since_epoch().count())
, traceDirectory(getTraceDirectory(startSystemTimePoint))
, _buffersSet()
, _namesSet()
, threadEventID(_namesSet.registerEventName("ThreadRunning"))
, mutexID(_namesSet.registerEventName("mutex"))
{
// Create the directory
if (!std::filesystem::create_directory(traceDirectory))
throw profilerError("Cannot create traces directory: " + traceDirectory);
#ifdef __linux__
// Pre-register all known perf counter names so their IDs are assigned
// deterministically at startup, regardless of which counters are actually
// opened at runtime. Syscall tracepoints are dynamic and registered on
// first use by PerfCounter.
for (const PerfCounterSpec &spec : allCounters)
_namesSet.registerEventName(std::string(spec.name));
#endif
// getInfoThread() is NOT called here: InfoThread's constructor calls
// getInfoGlobal(), which would re-enter this constructor and trigger
// a recursive_init_error. InfoThread initializes lazily on the first
// instrumented call, at which point Global is already fully constructed.
}
~Global()
{
killPool(); // kills the thread pool when needed.
getInfoThread().eventsBuffer.emplaceEvent(threadEventID, 0);
_buffersSet.createROW(traceDirectory);
_namesSet.createPCF(traceDirectory);
std::cout << "# Profiler TraceDir: " << traceDirectory << std::endl;
}
static Global & getInfoGlobal()
{
static Global instance;
return instance;
}
const uint64_t startSystemTimePoint;
const std::string traceDirectory;
BufferSet _buffersSet; // Buffers register
NameSet _namesSet; // Events names register
const EventId threadEventID;
const EventId mutexID;
};
/** Reference to the global profiler instance.
Declaring it here (after Global is fully defined) ensures every
translation unit that includes this header initializes the profiler
before main() starts, while avoiding the static-initialization-order
fiasco. */
inline Global & infoGlobal = Global::getInfoGlobal();
/** Public function to create new events.
This registers a new pair eventName -> value wrapping Object oriented
calls. */
inline EventId registerName(
const std::string &name,
const std::string &fileName, size_t line,
EventId event, EventId value
) {
if (value == 0)
return infoGlobal._namesSet.registerEventName(name, fileName, line, event);
else
return infoGlobal._namesSet.registerValueName(name, fileName, line, event, value);
}
/** Guard class (more info in the constructor docstring).
This is a tricky variable to rely event pairs emission (start-end) with
RAII. This simplifies instrumentation on the user side and may rely on
the instrumentation macro. The constructor emits an event that will be
paired with the equivalent one emitted in the destructor. */
class ProfilerGuard {
const EventId _id; //< Event id for this guard. remembered to emit on the destructor
public:
// Profile guard should be unique and not transferable.
ProfilerGuard(ProfilerGuard &&) = delete;
ProfilerGuard(const ProfilerGuard &) = delete;
ProfilerGuard& operator=(const ProfilerGuard &) = delete;
//! Guard constructor
ProfilerGuard(EventId id, EventId value)
: _id(id)
{
assert(value != 0);
Global::getInfoThread().eventsBuffer.emplaceEvent(_id, value);
}
//! Guard destructor
~ProfilerGuard()
{
Global::getInfoThread().eventsBuffer.emplaceEvent(_id, 0);
}
}; // ProfilerGuard
#define INSTRUMENT_EVENT(ID, VALUE) \
profiler::Global::getInfoThread().eventsBuffer.emplaceEvent(ID, VALUE);
/** Intrumented mutex
This is an intrumented mutex wrapper intended to register information
about lock contentions.
*/
class mutex {
public:
mutex() noexcept
{
unsigned char expected = 0;
// Globals: 0 = no initialized, 1 = initialization in progress, 2 = already initialized
if (registered.compare_exchange_strong(expected, 1)) {
[[maybe_unused]] EventId wait_value
= registerName("Waiting", "", 0, infoGlobal.mutexID, waiting_value);
assert(waiting_value == wait_value);
registered.store(2); // Perform this always at the end if the initialization.
}
// block until the init members are set. This is very unlikely needed, but may happen.
while (registered.load() != 2);
_id = _counter.fetch_add(1, std::memory_order_relaxed);
registerName("mutex_" + std::to_string(_id), "", 0, infoGlobal.mutexID, _id);
}
mutex(const mutex &) = delete;
void lock()
{
INSTRUMENT_EVENT(infoGlobal.mutexID, waiting_value)
_lock.lock();
INSTRUMENT_EVENT(infoGlobal.mutexID, _id)
}
void unlock()
{
_lock.unlock();
INSTRUMENT_EVENT(infoGlobal.mutexID, 0)
}
bool try_lock()
{
const bool locked = _lock.try_lock();
if (locked) {
INSTRUMENT_EVENT(infoGlobal.mutexID, _id)
}
return locked;
}
private:
static inline std::atomic<unsigned char> registered = 0;
static constexpr EventId waiting_value = 1; /// Reserve the event 1 to blocked by a mutex
static inline std::atomic<EventId> _counter = waiting_value + 1; /// The lock counter starts after it
EventId _id;
std::mutex _lock;
};
#ifdef __linux__
/** Wrapper around a Linux perf event group.
Opens N counters as a kernel group: the first as leader
(read_format=PERF_FORMAT_GROUP), the rest as members. A single
::read(leader_fd) returns all N values atomically.
The group is reset to zero at construction via
PERF_EVENT_IOC_RESET, so readAll() returns absolute values since
that reset — no baseline tracking is needed.
Opened with pid=0, cpu=-1: each event follows the calling thread
across CPU migrations.
If any perf_event_open call fails the whole group is marked invalid
(all FDs closed) and a diagnostic is printed to stderr. No
fallback to standalone counters.
At most maxGroupSize counters per instance; enforced at the call
site by a static_assert in INSTRUMENT_PERF. */
class PerfCounter
{
public:
static constexpr size_t maxGroupSize = 4;
private:
int _fd = -1;
std::array<int, maxGroupSize> _memberFds {};
std::array<EventId, maxGroupSize> _ids {};
size_t _n = 0;
mutable std::array<uint64_t, 1 + maxGroupSize> _readBuf {};
/** Low-level wrapper around perf_event_open.
groupFd == -1: open as group leader and set PERF_FORMAT_GROUP.
groupFd >= 0: open as group member. */
static int openPerfEvent(uint32_t type, uint64_t config, bool excludeKernel, int groupFd) noexcept
{
perf_event_attr attr{
.type = type,
.size = sizeof(perf_event_attr),
.config = config,
.read_format = (groupFd < 0) ? static_cast<uint64_t>(PERF_FORMAT_GROUP) : 0ULL,
.exclude_kernel = excludeKernel ? 1u : 0u,
.exclude_hv = excludeKernel ? 1u : 0u,
};
return static_cast<int>(syscall(SYS_perf_event_open, &attr, 0, -1, groupFd, 0));
}
/** Open a named hardware or software counter from the static table. */
static std::pair<int, EventId> openNamedCounter(std::string_view name, int groupFd)
{
const auto it = std::lower_bound(std::begin(allCounters), std::end(allCounters), name);
if (it == std::end(allCounters) || it->name != name)
throw profilerError("INSTRUMENT_PERF: unsupported counter '" + std::string(name) + "'");
return {openPerfEvent(it->type, it->config, true, groupFd),
infoGlobal._namesSet.lookupEventName(name)};
}
/** Reads /proc/sys/kernel/perf_event_paranoid. Returns -1 (most permissive)
if the file cannot be read. */
static int readParanoid() noexcept
{
int val = -1;
std::ifstream("/proc/sys/kernel/perf_event_paranoid") >> val;
return val;
}
/** Returns a hint string for common group-open errno values. */
static std::string groupOpenHint(int err)
{
if (err == EINVAL)
return " (counters may be incompatible types;"
" hardware and software counters cannot be grouped on most kernels)";
if (err == ENOENT)
return " (hardware PMU not available;"
" this counter may not be supported by the CPU or hypervisor)";
return {};
}
/** Search the kernel tracing filesystem for the tracepoint ID of a syscall.
Tracepoint IDs are assigned dynamically by the kernel at boot so they
cannot live in a static table; they must be read from tracefs at runtime.
Returns the ID (always > 0) if found and readable.
Throws profilerError if the id file is not readable (e.g. paranoid > 0)
or if no tracing filesystem exposes this syscall name. */
static uint64_t findSyscallTracepointId(std::string_view syscallName)
{
const std::string entryName = "sys_enter_" + std::string(syscallName);
for (const char *base : {"/sys/kernel/tracing", "/sys/kernel/debug/tracing"}) {
const std::string syscallsDir = std::string(base) + "/events/syscalls";
// Use error_code to avoid exceptions when the tracing filesystem
// root is inaccessible (e.g. debugfs not mounted).
std::error_code ec;
const std::filesystem::directory_iterator it(syscallsDir, ec);
if (ec)
continue; // directory not accessible; try next base path
if (!std::any_of(it, std::filesystem::directory_iterator{},
[&entryName](const auto &entry) {
return entry.path().filename() == entryName;
}))
continue;
// Tracepoint exists; try to read its id file.
// This may still fail with EACCES on locked-down systems.
if (uint64_t traceId = 0; std::ifstream(syscallsDir + "/" + entryName + "/id") >> traceId)
return traceId;
throw profilerError(
"INSTRUMENT_PERF: syscall tracepoint 'syscall:" + std::string(syscallName)
+ "' exists in tracefs but its id file is not readable"
" (requires paranoid <= 0 or CAP_PERFMON)");
}
throw profilerError("INSTRUMENT_PERF: unknown syscall 'syscall:" + std::string(syscallName) + "'");
}
/** Open a syscall tracepoint counter by syscall name (e.g. "read", "write").
Unlike hardware/software counters, tracepoints fire in kernel space so
exclude_kernel must be 0. The pid=0/cpu=-1 binding keeps the counter
thread-local: only syscalls from the calling thread are counted.
Returns -1 (with errno set) if the tracepoint exists but cannot be opened
due to permissions. Throws profilerError if the syscall name is unknown. */
static std::pair<int, EventId> openSyscallTracepoint(std::string_view syscallName, int groupFd)
{
const uint64_t traceId = findSyscallTracepointId(syscallName);
const EventId id = registerName("syscall:" + std::string(syscallName), "", 0, 0, 0);
return {openPerfEvent(PERF_TYPE_TRACEPOINT, traceId, false, groupFd), id};
}
/** Open one counter (named or syscall tracepoint) into the given group.
Returns the file descriptor and the profiler event ID for the counter. */
std::pair<int, EventId> openOne(std::string_view name, int groupFd)
{
static constexpr std::string_view syscallPrefix = "syscall:";
if (name.substr(0, syscallPrefix.size()) == syscallPrefix)
return openSyscallTracepoint(name.substr(syscallPrefix.size()), groupFd);
return openNamedCounter(name, groupFd);
}
/** Close all open FDs and reset _fd to -1. */
void closeAll() noexcept
{