-
Notifications
You must be signed in to change notification settings - Fork 0
/
pperf.h
162 lines (137 loc) · 4.24 KB
/
pperf.h
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
#ifndef __INTRUSIVEPROFILER_H_
#define __INTRUSIVEPROFILER_H_
#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <spawn.h>
#include <getopt.h>
#include <sched.h>
#include <elf.h>
#include <sys/uio.h>
#include <sys/ptrace.h>
#include <sys/sysinfo.h>
#include <time.h>
#include <sys/time.h>
#include "pmu/pmu.h"
#include "vmmap.h"
#ifdef __riscv
#include <asm/ptrace.h>
#else
#include <sys/user.h>
#endif
// Some helper functions to handle time, timespec and frequencies more easily
void timespecAdd(struct timespec *result, struct timespec *x, struct timespec *y) {
result->tv_sec = x->tv_sec + y->tv_sec;
result->tv_nsec = x->tv_nsec;
if (x->tv_nsec + y->tv_nsec > 1000000000) {
result->tv_sec++;
result->tv_nsec -= 1000000000;
}
result->tv_nsec += y->tv_nsec;
}
void timespecAddStore(struct timespec *result, struct timespec *x) {
result->tv_sec = result->tv_sec + x->tv_sec;
if (result->tv_nsec + x->tv_nsec > 1000000000) {
result->tv_sec++;
result->tv_nsec -= 1000000000;
}
result->tv_nsec += x->tv_nsec;
}
void timespecSub (struct timespec *result, struct timespec *x, struct timespec *y) {
if (x->tv_nsec < y->tv_nsec) {
int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
y->tv_nsec -= 1000000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_nsec - y->tv_nsec > 1000000000) {
int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
y->tv_nsec += 1000000000 * nsec;
y->tv_sec -= nsec;
}
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_nsec = x->tv_nsec - y->tv_nsec;
}
void timespecSubStore (struct timespec *result, struct timespec *x) {
if (result->tv_nsec < x->tv_nsec) {
int nsec = (x->tv_nsec - result->tv_nsec) / 1000000000 + 1;
x->tv_nsec -= 1000000000 * nsec;
x->tv_sec += nsec;
}
if (result->tv_nsec - x->tv_nsec > 1000000000) {
int nsec = (result->tv_nsec - x->tv_nsec) / 1000000000;
x->tv_nsec += 1000000000 * nsec;
x->tv_sec -= nsec;
}
result->tv_sec -= x->tv_sec;
result->tv_nsec -= x->tv_nsec;
}
struct timespec tsSub(struct timespec x, struct timespec y) {
struct timespec result;
timespecSub(&result, &x, &y);
return result;
}
struct timespec tsAdd(struct timespec x, struct timespec y) {
struct timespec result;
timespecAdd(&result, &x, &y);
return result;
}
unsigned long long timespecToNanoseconds(struct timespec *t) {
if ((t->tv_sec < 0) || ((t->tv_sec ==0) && (t->tv_nsec < 0)))
return 0;
return (t->tv_sec * 1000000000) + t->tv_nsec;
}
unsigned long long timespecToMicroseconds(struct timespec *t) {
return timespecToNanoseconds(t) / 1000;
}
unsigned long long timepsecToMilliseconds(struct timespec *t) {
return timespecToMicroseconds(t) / 1000;
}
unsigned long long timespecToSeconds(struct timespec *t) {
return t->tv_sec;
}
struct timespec NanosecondsToTimespec(unsigned long long x) {
struct timespec result = {
.tv_nsec = x % 1000000000,
.tv_sec = x / 1000000000
};
return result;
}
struct timespec MicrosecondsToTimespec(unsigned long long x) {
struct timespec result = {
.tv_nsec = (x % 1000000) * 1000,
.tv_sec = x / 1000000
};
return result;
}
struct timespec MillisecondsToTimespec(unsigned long long x) {
struct timespec result = {
.tv_nsec = (x % 1000) * 1000000,
.tv_sec = x / 1000
};
return result;
}
struct timespec SecondsToTimespec(unsigned long long x) {
struct timespec result = {
.tv_nsec = 0,
.tv_sec = x
};
return result;
}
void frequencyToTimespec(struct timespec *t, double freq) {
if (freq == 0.0) {
t->tv_sec = 0;
t->tv_nsec = 0;
} else {
t->tv_sec = (time_t) (1.0 / freq);
t->tv_nsec = ((long) (1000000000.0 / freq)) % 1000000000L;
}
}
#endif // __INTRUSIVEPROFILER_H_