AllianceDB  0.0.1
AllianceDB is an open-source suite, including benchmarks and libs for evaluating and improving stream operation algorithms on modern hardwares.
ThreadPerf.h
Go to the documentation of this file.
1 
2 //Copyright (C) 2022 by the IntelliStream team (https://github.com/intellistream)
3 #ifndef UTILS_ThreadPerf_H
4 #define UTILS_ThreadPerf_H
5 //#include <Utils/Logger.hpp>
6 #define PERF_ERROR(n) printf(n)
7 
8 //#include <Utils/UtilityFunctions.hpp
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <inttypes.h>
12 #include <math.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/syscall.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <linux/perf_event.h>
25 #include <signal.h>
26 #include <string>
27 #include <memory>
28 #include <vector>
29 using namespace std;
30 namespace INTELLI {
31 #define __LIBPERF_MAX_COUNTERS 32
32 #define __LIBPERF_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
33 
34 /*class:PerfEntry
35 description:the entry record in PerfTool
36 warnning: confict with the c version libperf
37 
38 date:202001013
39 */
40 class PerfEntry {
41  public:
42  //struct perf_event_attr attr;
43  int fds;
44  bool addressable;
45  uint64_t prevVale;
46  PerfEntry() { addressable = false; }
47  ~PerfEntry() {}
48 };
54 class PerfTool {
55  private:
56  /* data */
57  std::vector<PerfEntry> entries;
58  pid_t myPid;
59  int myCpu;
60  uint64_t prevValue;
61  public:
62  PerfTool() {
63 
64  }
65  PerfTool(pid_t pid, int cpu);
66  // reading result from a perf trace on [ch], will return 0 if the channel is invaild
67  uint64_t readPerf(size_t ch);
68  // start the perf trace on [ch]
69  int startPerf(size_t ch);
70  // st the perf trace on [ch]
71  int stopPerf(size_t ch);
72  //check the addressability of [ch]
73  bool isValidChannel(size_t ch);
74  ~PerfTool();
75 };
76 typedef std::shared_ptr<PerfTool> PerfToolPtr;
77 
82 class PerfPair {
83  public:
85  int ref;
87  std::string name;
89  uint64_t record;
95  PerfPair(int _ref, string _name) {
96  ref = _ref;
97  name = _name;
98  record = 0;
99  }
100  ~PerfPair() {}
101 };
102 
132 class ThreadPerf {
133  protected:
134  string getChValueAsString(size_t idx);
135  uint64_t getRunTime(void) {
136  // gettimeofday(&te, NULL);
137  int64_t s0, e0, s1, e1;
138  s0 = ts.tv_sec;
139  s1 = ts.tv_usec;
140  e0 = te.tv_sec;
141  e1 = te.tv_usec;
142  return 1000000 * (e0 - s0) + (e1 - s1);
143  }
144  PerfToolPtr myTool;
145  vector<PerfPair> pairs;
146  struct timeval ts, te;
147  uint64_t latency;
148  public:
149 
150  ThreadPerf() {}
161  ThreadPerf(int cpu);
168  ThreadPerf(int cpu, vector<PerfPair> customPair);
173  void start();
188  uint64_t getResultById(int idx);
194  uint64_t getResultByName(string name);
195 
196  ~ThreadPerf() {
197 
198  }
199 
204  void end();
205  //the headString for printf
210  string headStringPrintf(void);
215  string headStringCsv(void);
220  string resultStringPrintf(void);
225  string resultStringCsv(void);
229 };
234 typedef
235 enum perfTrace {
236  /* sw tracepoints */
237  TRACE_RUNNING_TIME = -1,
238  COUNT_SW_CPU_CLOCK = 0,
239  COUNT_SW_TASK_CLOCK = 1,
240  COUNT_SW_CONTEXT_SWITCHES = 2,
241  COUNT_SW_CPU_MIGRATIONS = 3,
242  COUNT_SW_PAGE_FAULTS = 4,
243  COUNT_SW_PAGE_FAULTS_MIN = 5,
244  COUNT_SW_PAGE_FAULTS_MAJ = 6,
245 
246  /* hw counters */
247  COUNT_HW_CPU_CYCLES = 7,
248  COUNT_HW_INSTRUCTIONS = 8,
249  COUNT_HW_CACHE_REFERENCES = 9,
250  COUNT_HW_CACHE_MISSES = 10,
251  COUNT_HW_BRANCH_INSTRUCTIONS = 11,
252  COUNT_HW_BRANCH_MISSES = 12,
253  COUNT_HW_BUS_CYCLES = 13,
254 
255  /* cache counters */
256 
257  /* L1D - data cache */
258  COUNT_HW_CACHE_L1D_LOADS = 14,
259  COUNT_HW_CACHE_L1D_LOADS_MISSES = 15,
260  COUNT_HW_CACHE_L1D_STORES = 16,
261  COUNT_HW_CACHE_L1D_STORES_MISSES = 17,
262  COUNT_HW_CACHE_L1D_PREFETCHES = 18,
263 
264  /* L1I - instruction cache */
265  COUNT_HW_CACHE_L1I_LOADS = 19,
266  COUNT_HW_CACHE_L1I_LOADS_MISSES = 20,
267 
268  /* LL - last level cache */
269  COUNT_HW_CACHE_LL_LOADS = 21,
270  COUNT_HW_CACHE_LL_LOADS_MISSES = 22,
271  COUNT_HW_CACHE_LL_STORES = 23,
272  COUNT_HW_CACHE_LL_STORES_MISSES = 24,
273 
274  /* DTLB - data translation lookaside buffer */
275  COUNT_HW_CACHE_DTLB_LOADS = 25,
276  COUNT_HW_CACHE_DTLB_LOADS_MISSES = 26,
277  COUNT_HW_CACHE_DTLB_STORES = 27,
278  COUNT_HW_CACHE_DTLB_STORES_MISSES = 28,
279 
280  /* ITLB - instructiont translation lookaside buffer */
281  COUNT_HW_CACHE_ITLB_LOADS = 29,
282  COUNT_HW_CACHE_ITLB_LOADS_MISSES = 30,
283 
284  /* BPU - branch prediction unit */
285  COUNT_HW_CACHE_BPU_LOADS = 31,
286  COUNT_HW_CACHE_BPU_LOADS_MISSES = 32,
287 
288  /* Special internally defined "counter" */
289  /* this is the _only_ floating point value */
290  //LIB_SW_WALL_TIME = 33
291 } perfIdx;
292 }
297 #endif
Definition: ThreadPerf.h:40
Combine PerfTool together with tags.
Definition: ThreadPerf.h:82
pack the perf event in cpp style, remain safe if some std linux perf failed
Definition: ThreadPerf.h:54
The all-in-on perf class for thread.
Definition: ThreadPerf.h:132
void start()
start all registered perf
ThreadPerf(int cpu, vector< PerfPair > customPair)
To start perf this thread at specific cpu, with customized perf events.
ThreadPerf(int cpu)
To start perf this thread at specific cpu, with default perf events.
uint64_t record
Definition: ThreadPerf.h:89
int ref
Definition: ThreadPerf.h:85
std::string name
Definition: ThreadPerf.h:87
perfTrace
The valid idx for perf.
Definition: ThreadPerf.h:235
PerfPair(int _ref, string _name)
The construction function.
Definition: ThreadPerf.h:95
uint64_t getResultById(int idx)
Get the perf result by event id.
void end()
end all registered perf
string resultStringCsv(void)
get the result string for csv file a result sheet
string resultStringPrintf(void)
get the result string for print a result sheet
uint64_t getResultByName(string name)
Get the perf result by event tag string.
string headStringCsv(void)
get the head string for csv file of a result sheet
string headStringPrintf(void)
get the head string for print a result sheet
Definition: DatasetTool.h:10