-
Notifications
You must be signed in to change notification settings - Fork 12
/
profile.c
342 lines (304 loc) · 10.5 KB
/
profile.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "monitor.h"
#include "trace_helpers.h"
#include "tep.h"
#include "stack_helpers.h"
struct profile_ctx {
int nr_ins;
uint64_t *counter;
uint64_t *cycles;
struct {
uint64_t start_time;
uint64_t num;
}*stat;
struct callchain_ctx *cc;
struct flame_graph *flame;
struct bpf_filter filter;
struct perf_evsel *evsel;
time_t time;
char time_str[32];
int in_guest;
int tsc_khz;
int vendor;
};
static void monitor_ctx_exit(struct prof_dev *dev);
static void profile_interval(struct prof_dev *dev);
static int monitor_ctx_init(struct prof_dev *dev)
{
struct env *env = dev->env;
struct profile_ctx *ctx = zalloc(sizeof(*ctx));
if (!ctx)
return -1;
dev->private = ctx;
tep__ref();
ctx->nr_ins = prof_dev_nr_ins(dev);
ctx->counter = calloc(ctx->nr_ins, sizeof(uint64_t));
if (!ctx->counter)
goto failed;
ctx->cycles = calloc(ctx->nr_ins, sizeof(uint64_t));
if (!ctx->cycles)
goto failed;
ctx->stat = calloc(ctx->nr_ins, sizeof(*ctx->stat));
if (!ctx->stat)
goto failed;
ctx->time = 0;
ctx->time_str[0] = '\0';
if (env->callchain) {
if (!env->flame_graph)
ctx->cc = callchain_ctx_new(callchain_flags(dev, CALLCHAIN_KERNEL | CALLCHAIN_USER), stdout);
else {
ctx->flame = flame_graph_open(callchain_flags(dev, CALLCHAIN_KERNEL | CALLCHAIN_USER), env->flame_graph);
if (env->interval) {
profile_interval(dev);
}
}
}
if (bpf_filter_init(&ctx->filter, env)) {
if (bpf_filter_open(&ctx->filter) < 0)
goto failed;
}
ctx->in_guest = in_guest();
ctx->tsc_khz = ctx->in_guest ? 0 : get_tsc_khz();
ctx->vendor = get_cpu_vendor();
return 0;
failed:
monitor_ctx_exit(dev);
return -1;
}
static void monitor_ctx_exit(struct prof_dev *dev)
{
struct profile_ctx *ctx = dev->private;
if (ctx->counter) free(ctx->counter);
if (ctx->cycles) free(ctx->cycles);
if (ctx->stat) free(ctx->stat);
bpf_filter_close(&ctx->filter);
if (dev->env->callchain) {
if (!dev->env->flame_graph)
callchain_ctx_free(ctx->cc);
else {
flame_graph_output(ctx->flame);
flame_graph_close(ctx->flame);
}
}
tep__unref();
free(ctx);
}
static int profile_init(struct prof_dev *dev)
{
struct perf_evlist *evlist = dev->evlist;
struct env *env = dev->env;
struct profile_ctx *ctx;
struct perf_event_attr attr = {
.type = PERF_TYPE_HARDWARE,
.config = PERF_COUNT_HW_CPU_CYCLES,
.size = sizeof(struct perf_event_attr),
.sample_period = env->freq,
.freq = env->freq ? 1 : 0,
.sample_type = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_READ |
(env->callchain ? PERF_SAMPLE_CALLCHAIN : 0),
.read_format = 0,
.pinned = 0,
.disabled = 1,
.exclude_user = env->exclude_user,
.exclude_kernel = env->exclude_kernel,
.exclude_guest = env->exclude_guest,
.exclude_host = env->exclude_host,
.exclude_callchain_user = exclude_callchain_user(dev, CALLCHAIN_KERNEL | CALLCHAIN_USER),
.exclude_callchain_kernel = exclude_callchain_kernel(dev, CALLCHAIN_KERNEL | CALLCHAIN_USER),
.wakeup_events = 1,
};
struct perf_evsel *evsel;
if (env->exclude_guest && env->exclude_host)
return -1;
if (env->exclude_user && env->exclude_kernel)
return -1;
if (monitor_ctx_init(dev) < 0)
return -1;
ctx = dev->private;
if (ctx->tsc_khz > 0 && env->freq > 0) {
attr.freq = 0;
attr.sample_period = ctx->tsc_khz * 1000ULL / env->freq;
}
if (ctx->in_guest) {
attr.type = PERF_TYPE_SOFTWARE;
attr.config = PERF_COUNT_SW_CPU_CLOCK;
attr.exclude_idle = 1;
} else if (ctx->vendor == X86_VENDOR_INTEL)
attr.config = PERF_COUNT_HW_REF_CPU_CYCLES;
if (env->callchain)
dev->pages *= 2;
if (env->verbose) {
printf("tsc_khz = %d\n", ctx->tsc_khz);
}
reduce_wakeup_times(dev, &attr);
evsel = perf_evsel__new(&attr);
if (!evsel)
goto failed;
perf_evlist__add(evlist, evsel);
ctx->evsel = evsel;
return 0;
failed:
monitor_ctx_exit(dev);
return -1;
}
static int profile_filter(struct prof_dev *dev)
{
struct profile_ctx *ctx = dev->private;
int err;
if (ctx->filter.bpf_fd >= 0) {
err = perf_evsel__set_bpf(ctx->evsel, ctx->filter.bpf_fd);
if (err < 0)
return err;
}
return 0;
}
static void profile_exit(struct prof_dev *dev)
{
monitor_ctx_exit(dev);
}
static int profile_read(struct prof_dev *dev, struct perf_evsel *evsel, struct perf_counts_values *count, int instance)
{
struct profile_ctx *ctx = dev->private;
uint64_t cycles = 0;
const char *str_in[] = {"host,guest", "host", "guest", "error"};
const char *str_mode[] = {"all", "usr", "sys", "error"};
int in, mode, oncpu;
if (count->val > ctx->cycles[instance]) {
cycles = count->val - ctx->cycles[instance];
ctx->cycles[instance] = count->val;
}
if (cycles) {
in = (dev->env->exclude_host << 1) | dev->env->exclude_guest;
mode = (dev->env->exclude_user << 1) | dev->env->exclude_kernel;
print_time(stdout);
oncpu = prof_dev_ins_oncpu(dev);
if (ctx->tsc_khz > 0 && ctx->vendor == X86_VENDOR_INTEL)
printf("%s %d [%s] %.2f%% [%s] %lu cycles\n", oncpu ? "cpu" : "thread",
oncpu ? prof_dev_ins_cpu(dev, instance) : prof_dev_ins_thread(dev, instance),
str_in[in],
(float)cycles * 100 / (ctx->tsc_khz * (__u64)dev->env->interval),
str_mode[mode], cycles);
else
printf("%s %d [%s] [%s] %lu cycles\n", oncpu ? "cpu" : "thread",
oncpu ? prof_dev_ins_cpu(dev, instance) : prof_dev_ins_thread(dev, instance),
str_in[in], str_mode[mode], cycles);
}
return 0;
}
static void profile_sample(struct prof_dev *dev, union perf_event *event, int instance)
{
struct profile_ctx *ctx = dev->private;
// in linux/perf_event.h
// PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_READ | PERF_SAMPLE_CALLCHAIN
struct sample_type_data {
struct {
__u32 pid;
__u32 tid;
} tid_entry;
__u64 time;
struct {
__u32 cpu;
__u32 reserved;
} cpu_entry;
__u64 counter;
struct callchain callchain;
} *data = (void *)event->sample.array;
uint64_t counter = 0;
int print = 1;
if (data->counter > ctx->counter[instance])
counter = data->counter - ctx->counter[instance];
ctx->counter[instance] = data->counter;
if (dev->env->greater_than) {
uint64_t time = ctx->stat[instance].start_time;
ctx->stat[instance].num ++;
if (data->time - time >= NSEC_PER_SEC) {
print = 0;
ctx->stat[instance].start_time = data->time;
ctx->stat[instance].num = 1;
} else {
int x = (dev->env->freq * dev->env->greater_than + 99) / 100;
if (ctx->stat[instance].num < x)
print = 0;
}
}
if (print) {
if (dev->print_title) prof_dev_print_time(dev, data->time, stdout);
tep__update_comm(NULL, data->tid_entry.tid);
printf("%16s %6u [%03d] %llu.%06llu: profile: %lu cpu-cycles\n", tep__pid_to_comm(data->tid_entry.tid), data->tid_entry.tid,
data->cpu_entry.cpu, data->time / NSEC_PER_SEC, (data->time % NSEC_PER_SEC)/1000, counter);
if (dev->env->callchain) {
if (!dev->env->flame_graph)
print_callchain_common(ctx->cc, &data->callchain, data->tid_entry.pid);
else {
const char *comm = tep__pid_to_comm((int)data->tid_entry.pid);
flame_graph_add_callchain_at_time(ctx->flame, &data->callchain, data->tid_entry.pid,
!strcmp(comm, "<...>") ? NULL : comm,
ctx->time, ctx->time_str);
}
}
}
}
static void profile_interval(struct prof_dev *dev)
{
struct profile_ctx *ctx = dev->private;
if (ctx->flame) {
ctx->time = time(NULL);
strftime(ctx->time_str, sizeof(ctx->time_str), "%Y-%m-%d;%H:%M:%S", localtime(&ctx->time));
flame_graph_output(ctx->flame);
flame_graph_reset(ctx->flame);
}
}
static const char *profile_desc[] = PROFILER_DESC("profile",
"[OPTION...] -F freq [-g [--flame-graph file [-i INT]]] [--than percent]",
"Sampling at the specified frequency to profile high CPU utilization.", "",
"EXAMPLES",
" "PROGRAME" profile -F 997 -p 2347 -g --flame-graph cpu",
" "PROGRAME" profile -F 997 -C 0-3 --than 30 -g --flame-graph cpu");
static const char *profile_argv[] = PROFILER_ARGV("profile",
PROFILER_ARGV_OPTION,
PROFILER_ARGV_FILTER,
PROFILER_ARGV_PROFILER, "freq", "call-graph", "flame-graph", "than");
struct monitor profile = {
.name = "profile",
.desc = profile_desc,
.argv = profile_argv,
.pages = 2,
.init = profile_init,
.filter = profile_filter,
.deinit = profile_exit,
.interval = profile_interval,
.sample = profile_sample,
};
PROFILER_REGISTER(profile);
static int cpu_util_init(struct prof_dev *dev)
{
if (in_guest()) {
fprintf(stderr, "cpu-util not support in guest\n");
return -1;
}
return profile_init(dev);
}
static const char *cpu_util_desc[] = PROFILER_DESC("cpu-util",
"[OPTION...] [--exclude-*] [-G]",
"Report CPU utilization for guest or host.", "",
"SYNOPSIS",
" Based on profile. See '"PROGRAME" profile -h' for more information.", "",
"EXAMPLES",
" "PROGRAME" cpu-util -C 1-4 -i 1000",
" "PROGRAME" cpu-util -C 1-4 -G -i 1000");
static const char *cpu_util_argv[] = PROFILER_ARGV("cpu-util",
PROFILER_ARGV_OPTION,
"FILTER OPTION:",
"exclude-host", "exclude-guest", "exclude-user", "exclude-kernel");
struct monitor cpu_util = {
.name = "cpu-util",
.desc = cpu_util_desc,
.argv = cpu_util_argv,
.pages = 0,
.init = cpu_util_init,
.deinit = profile_exit,
.read = profile_read,
};
PROFILER_REGISTER(cpu_util);