-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperf.c
164 lines (144 loc) · 3.79 KB
/
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
/*
* Copyright (C) Colin Ian King 2015-2021
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#define _GNU_SOURCE
#include "perf.h"
#if defined(PERF_ENABLED)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <linux/perf_event.h>
#define UNRESOLVED (~0UL)
static perf_tp_info_t perf_tp_info[] = {
{ PERF_TP_PAGE_FAULT_USER, "exceptions/page_fault_user" },
{ PERF_TP_PAGE_FAULT_KERNEL, "exceptions/page_fault_kernel" },
{ PERF_TP_MM_PAGE_ALLOC, "kmem/mm_page_alloc" },
{ PERF_TP_MM_PAGE_FREE, "kmem/mm_page_free" },
};
static inline unsigned long
perf_type_tracepoint_resolve_config(const char *path)
{
char perf_path[PATH_MAX];
unsigned long config;
FILE *fp;
(void)snprintf(perf_path, sizeof(perf_path),
"/sys/kernel/debug/tracing/events/%s/id", path);
if ((fp = fopen(perf_path, "r")) == NULL)
return UNRESOLVED;
if (fscanf(fp, "%lu", &config) != 1) {
fclose(fp);
return UNRESOLVED;
}
(void)fclose(fp);
return config;
}
int perf_start(perf_t *p, const pid_t pid)
{
int i;
if (pid <= 0)
return 0;
p->perf_opened = 0;
for (i = 0; i < PERF_MAX; i++) {
struct perf_event_attr attr;
(void)memset(&attr, 0, sizeof(attr));
attr.type = PERF_TYPE_TRACEPOINT;
attr.config = perf_type_tracepoint_resolve_config(perf_tp_info[i].path);
attr.disabled = 1;
attr.inherit = 1;
attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING;
attr.size = sizeof(attr);
p->perf_stat[i].fd = syscall(__NR_perf_event_open, &attr, pid, -1, -1, 0);
if (p->perf_stat[i].fd > -1)
p->perf_opened++;
else
return -1;
}
if (!p->perf_opened)
return -1;
for (i = 0; i < PERF_MAX; i++) {
int fd = p->perf_stat[i].fd;
if (fd > -1) {
if (ioctl(fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) < 0) {
(void)close(fd);
p->perf_stat[i].fd = -1;
continue;
}
if (ioctl(fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) < 0) {
(void)close(fd);
p->perf_stat[i].fd = -1;
}
}
}
return 0;
}
/*
* perf_stop()
* stop and read counters
*/
int perf_stop(perf_t *p)
{
size_t i = 0;
if (!p)
return -1;
if (!p->perf_opened)
return -1;
for (i = 0; i < PERF_MAX; i++) {
const int fd = p->perf_stat[i].fd;
/* assume invalid unless we get good data */
p->perf_stat[i].valid = false;
if (fd < 0)
continue;
if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == 0) {
perf_data_t data;
if (read(fd, &data, sizeof(data)) == sizeof(data)) {
const double scale = data.time_running ?
data.time_enabled / data.time_running :
((data.time_enabled == 0) ? 1.0 : 0.0);
p->perf_stat[i].valid = true;
p->perf_stat[i].counter += (uint64_t)
((double)data.counter * scale);
}
}
(void)close(fd);
p->perf_stat[i].fd = -1;
}
return 0;
}
/*
* perf_counter
* fetch counter and index via perf index
*/
uint64_t perf_counter(
const perf_t *p,
const int i)
{
if ((i < 0) || (i >= PERF_MAX))
return 0ULL;
if (p->perf_stat[i].valid)
return p->perf_stat[i].counter;
return 0ULL;
}
#endif