forked from OpenCloudOS/perf-prof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.c
172 lines (156 loc) · 4.5 KB
/
signal.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <monitor.h>
#include <dlfcn.h>
#include <monitor.h>
#include <tep.h>
#include <trace_helpers.h>
#include <stack_helpers.h>
struct monitor monitor_signal;
static void signal_sample_callchain(union perf_event *event, int instance);
static struct monitor_ctx {
struct callchain_ctx *cc;
struct env *env;
} ctx;
static int monitor_ctx_init(struct env *env)
{
tep__ref();
if (env->callchain) {
ctx.cc = callchain_ctx_new(CALLCHAIN_KERNEL, stdout);
monitor_signal.pages *= 2;
monitor_signal.sample = signal_sample_callchain;
}
ctx.env = env;
return 0;
}
static void monitor_ctx_exit(void)
{
if (ctx.env->callchain) {
callchain_ctx_free(ctx.cc);
}
tep__unref();
}
static int signal_init(struct perf_evlist *evlist, struct env *env)
{
struct perf_event_attr attr = {
.type = PERF_TYPE_TRACEPOINT,
.config = 0,
.size = sizeof(struct perf_event_attr),
.sample_period = 1,
.sample_type = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_RAW |
(env->callchain ? PERF_SAMPLE_CALLCHAIN : 0),
.read_format = 0,
.pinned = 1,
.disabled = 1,
.exclude_callchain_user = 1,
.wakeup_events = 1,
};
struct perf_evsel *evsel;
int id;
if (monitor_ctx_init(env) < 0)
return -1;
id = tep__event_id("signal", "signal_generate");
if (id < 0)
return -1;
attr.config = id;
evsel = perf_evsel__new(&attr);
if (!evsel) {
return -1;
}
perf_evlist__add(evlist, evsel);
return 0;
}
static int signal_filter(struct perf_evlist *evlist, struct env *env)
{
char filter[64];
struct perf_evsel *evsel;
int err;
if (env->filter) {
perf_evlist__for_each_evsel(evlist, evsel) {
snprintf(filter, sizeof(filter), "comm~\"%s\"", env->filter);
err = perf_evsel__apply_filter(evsel, filter);
if (err < 0)
return err;
}
}
return 0;
}
static void signal_exit(struct perf_evlist *evlist)
{
monitor_ctx_exit();
}
static void signal_sample(union perf_event *event, int instance)
{
// in linux/perf_event.h
// PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_RAW
struct sample_type_data {
struct {
__u32 pid;
__u32 tid;
} tid_entry;
__u64 time;
struct {
__u32 cpu;
__u32 reserved;
} cpu_entry;
struct {
__u32 size;
__u8 data[0];
} raw;
} *data = (void *)event->sample.array;
tep__update_comm(NULL, data->tid_entry.tid);
print_time(stdout);
tep__print_event(data->time/1000, data->cpu_entry.cpu, data->raw.data, data->raw.size);
}
static void signal_sample_callchain(union perf_event *event, int instance)
{
// in linux/perf_event.h
// PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_RAW
struct sample_type_data {
struct {
__u32 pid;
__u32 tid;
} tid_entry;
__u64 time;
struct {
__u32 cpu;
__u32 reserved;
} cpu_entry;
struct callchain callchain;
} *data = (void *)event->sample.array;
struct {
__u32 size;
__u8 data[0];
} *raw = (void *)data->callchain.ips + data->callchain.nr * sizeof(__u64);
tep__update_comm(NULL, data->tid_entry.tid);
print_time(stdout);
tep__print_event(data->time/1000, data->cpu_entry.cpu, raw->data, raw->size);
if (ctx.env->callchain) {
print_callchain_common(ctx.cc, &data->callchain, 0/*only kernel stack*/);
}
}
static const char *signal_desc[] = PROFILER_DESC("signal",
"[OPTION...] [--filter comm] [-g]",
"Demo", "",
"TRACEPOINT", "",
" signal:signal_generate", "",
"EXAMPLES", "",
" "PROGRAME" signal --filter python");
static const char *signal_argv[] = PROFILER_ARGV("signal",
PROFILER_ARGV_OPTION,
PROFILER_ARGV_PROFILER, "filter", "call-graph");
struct monitor monitor_signal = {
.name = "signal",
.desc = signal_desc,
.argv = signal_argv,
.pages = 2,
.init = signal_init,
.filter = signal_filter,
.deinit = signal_exit,
.comm = monitor_tep__comm,
.sample = signal_sample,
};
MONITOR_REGISTER(monitor_signal)