-
Notifications
You must be signed in to change notification settings - Fork 244
/
sc_perf.c
217 lines (181 loc) · 5.54 KB
/
sc_perf.c
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
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include "sc_perf.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#define ITEMS_SIZE (sizeof(sc_perf_hw) / sizeof(struct sc_perf_event))
static int init = 0;
static int running = 0;
static uint64_t total = 0;
static uint64_t start = 0;
struct sc_perf_item {
struct sc_perf_event event;
double value;
double active;
int fd;
};
static struct sc_perf_item sc_perf_items[ITEMS_SIZE];
#define sc_perf_assert(val) \
do { \
if (!(val)) { \
fprintf(stderr, "%s:%d: error", __FILE__, __LINE__); \
if (errno) { \
fprintf(stderr, " (%s)", strerror(errno)); \
} \
abort(); \
} \
} while (0)
static void sc_perf_set(struct sc_perf_item *items, size_t size)
{
const uint64_t flags = PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING;
int fd;
for (size_t i = 0; i < size; i++) {
struct perf_event_attr p = {
.size = sizeof(struct perf_event_attr),
.read_format = flags,
.type = items[i].event.type,
.config = items[i].event.config,
.disabled = 1,
.inherit = 1,
.inherit_stat = 0,
.exclude_kernel = false,
.exclude_hv = false};
fd = (int) syscall(__NR_perf_event_open, &p, 0, -1, -1,
PERF_FLAG_FD_CLOEXEC);
if (fd == -1) {
fprintf(stderr,
"Failed to set counter : %s , probably your "
"system does "
"not support it! \n",
items[i].event.name);
abort();
}
items[i].fd = fd;
}
}
static void sc_read(struct sc_perf_item *items, size_t size)
{
struct read_format {
uint64_t value;
uint64_t time_enabled;
uint64_t time_running;
} fmt;
for (size_t i = 0; i < size; i++) {
double n = 1.0;
sc_perf_assert(read(items[i].fd, &fmt, sizeof(fmt)) ==
sizeof(fmt));
if (fmt.time_enabled > 0 && fmt.time_running > 0) {
n = (double) fmt.time_running /
(double) fmt.time_enabled;
items[i].active = n;
}
items[i].value += (double) fmt.value * n;
}
}
static void sc_perf_clear(void)
{
total = 0;
start = 0;
running = 0;
init = 0;
for (size_t i = 0; i < ITEMS_SIZE; i++) {
sc_perf_items[i].event = sc_perf_hw[i];
sc_perf_items[i].value = 0;
sc_perf_items[i].active = 0;
sc_perf_items[i].fd = -1;
}
}
static uint64_t sy_time_nano(void)
{
int rc;
struct timespec ts;
rc = clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
if (rc == -1) {
abort();
}
return ((uint64_t) (ts.tv_nsec + (ts.tv_sec * 1000 * 1000 * 1000)));
}
void sc_perf_start(void)
{
if (!init) {
sc_perf_clear();
sc_perf_set(sc_perf_items, ITEMS_SIZE);
init = 1;
}
sc_perf_assert(prctl(PR_TASK_PERF_EVENTS_ENABLE) == 0);
start = sy_time_nano();
running = 1;
}
void sc_perf_pause(void)
{
sc_perf_assert(init);
if (!running) {
return;
}
sc_perf_assert(prctl(PR_TASK_PERF_EVENTS_DISABLE) == 0);
total += sy_time_nano() - start;
running = 0;
}
void sc_perf_end(void)
{
sc_perf_assert(init);
sc_perf_pause();
sc_read(sc_perf_items, ITEMS_SIZE);
for (size_t i = 0; i < ITEMS_SIZE; i++) {
close(sc_perf_items[i].fd);
}
printf("\n| %-25s | %-18s | %s \n", "Event", "Value",
"Measurement time");
printf("---------------------------------------------------------------"
"\n");
printf("| %-25s | %-18.2f | %s \n", "time (seconds)",
((double) total / 1e9), "(100,00%)");
for (size_t i = 0; i < ITEMS_SIZE; i++) {
printf("| %-25s | %-18.2f | (%.2f%%) \n",
sc_perf_items[i].event.name, sc_perf_items[i].value,
sc_perf_items[i].active * 100);
}
sc_perf_clear();
}