-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtlbstat.c
276 lines (243 loc) · 8.6 KB
/
tlbstat.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/compiler.h>
#include <api/fs/fs.h>
#include <monitor.h>
#include <tep.h>
#include <stack_helpers.h>
struct cache {
uint64_t counter;
uint64_t incremental;
};
struct tlbstat_ctx {
int nr_ins;
struct cpuinfo_x86 cpuinfo;
struct perf_evlist *evlist;
struct perf_evsel *leader;
struct cache *total_time_enabled;
struct cache *total_time_running;
struct cache *dTLB_load_misses;
struct cache *dTLB_loads;
struct cache *dTLB_store_misses;
struct cache *dTLB_stores;
__u64 dTLB_load_misses_config;
__u64 dTLB_loads_config;
__u64 dTLB_store_misses_config;
__u64 dTLB_stores_config;
};
#define C(x) PERF_COUNT_HW_CACHE_##x
#define CONFIG(cache_type, cache_op, cache_result) \
((C(cache_result)<<16) | (C(cache_op)<<8) | C(cache_type))
static void tlbstat_exit(struct prof_dev *dev);
static int tlbstat_init(struct prof_dev *dev)
{
struct perf_evlist *evlist = dev->evlist;
struct env *env = dev->env;
struct tlbstat_ctx *ctx;
struct perf_event_attr attr = {
.type = PERF_TYPE_HW_CACHE,
.config = 0,
.size = sizeof(struct perf_event_attr),
.sample_period = 0,
.sample_type = 0, //PERF_SAMPLE_TID | PERF_SAMPLE_CPU | PERF_SAMPLE_READ,
.read_format = 0,
.exclude_host = env->exclude_host, //only guest
.pinned = 0,
.disabled = 1,
.wakeup_events = 1,
};
struct perf_evsel *evsel;
__u64 dTLB_load_misses = 0;
__u64 dTLB_loads = 0;
__u64 dTLB_store_misses = 0;
__u64 dTLB_stores = 0;
if (!prof_dev_ins_oncpu(dev)) {
fprintf(stderr, "can only be bound to CPU\n");
return -1;
}
ctx = zalloc(sizeof(*ctx));
if (!ctx)
return -1;
dev->private = ctx;
if (get_cpuinfo(&ctx->cpuinfo) < 0)
goto failed;
if (env->interval == 0)
env->interval = 1000;
if (ctx->cpuinfo.vendor == X86_VENDOR_INTEL) {
dTLB_load_misses = CONFIG(DTLB, OP_READ, RESULT_MISS);
dTLB_loads = CONFIG(DTLB, OP_READ, RESULT_ACCESS);
dTLB_store_misses = CONFIG(DTLB, OP_WRITE, RESULT_MISS);
dTLB_stores = CONFIG(DTLB, OP_WRITE, RESULT_ACCESS);
} else
goto failed;
ctx->nr_ins = perf_cpu_map__nr(dev->cpus);
ctx->total_time_enabled = calloc(ctx->nr_ins, sizeof(struct cache));
ctx->total_time_running = calloc(ctx->nr_ins, sizeof(struct cache));
ctx->dTLB_load_misses = calloc(ctx->nr_ins, sizeof(struct cache));
ctx->dTLB_loads = calloc(ctx->nr_ins, sizeof(struct cache));
ctx->dTLB_store_misses = calloc(ctx->nr_ins, sizeof(struct cache));
ctx->dTLB_stores = calloc(ctx->nr_ins, sizeof(struct cache));
if (!ctx->total_time_enabled || !ctx->total_time_running ||
!ctx->dTLB_load_misses || !ctx->dTLB_loads ||
!ctx->dTLB_store_misses || !ctx->dTLB_stores)
goto failed;
// PERF_FORMAT_GROUP
// Use the leader event to read all counters at once.
//
// PERF_FORMAT_TOTAL_TIME_ENABLED
// PERF_FORMAT_TOTAL_TIME_RUNNING
// Use the leader event to get the running time of all events.
//
attr.read_format = PERF_FORMAT_ID | PERF_FORMAT_GROUP | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
ctx->dTLB_load_misses_config = attr.config = dTLB_load_misses;
evsel = perf_evsel__new(&attr);
if (!evsel) {
fprintf(stderr, "failed to init dTLB_load_misses counter\n");
goto failed;
}
perf_evlist__add(evlist, evsel);
ctx->leader = evsel;
ctx->evlist = evlist;
attr.read_format = PERF_FORMAT_ID;
ctx->dTLB_loads_config = attr.config = dTLB_loads;
evsel = perf_evsel__new(&attr);
if (!evsel) {
fprintf(stderr, "failed to init dTLB_loads counter\n");
goto failed;
}
perf_evlist__add(evlist, evsel);
ctx->dTLB_store_misses_config = attr.config = dTLB_store_misses;
evsel = perf_evsel__new(&attr);
if (!evsel) {
fprintf(stderr, "failed to init dTLB_store_misses counter\n");
goto failed;
}
perf_evlist__add(evlist, evsel);
ctx->dTLB_stores_config = attr.config = dTLB_stores;
evsel = perf_evsel__new(&attr);
if (!evsel) {
fprintf(stderr, "failed to init dTLB_stores counter\n");
goto failed;
}
perf_evlist__add(evlist, evsel);
perf_evlist__set_leader(evlist);
return 0;
failed:
tlbstat_exit(dev);
return -1;
}
static void tlbstat_exit(struct prof_dev *dev)
{
struct tlbstat_ctx *ctx = dev->private;
if (ctx->total_time_enabled)
free(ctx->total_time_enabled);
if (ctx->total_time_running)
free(ctx->total_time_running);
if (ctx->dTLB_load_misses)
free(ctx->dTLB_load_misses);
if (ctx->dTLB_loads)
free(ctx->dTLB_loads);
if (ctx->dTLB_store_misses)
free(ctx->dTLB_store_misses);
if (ctx->dTLB_stores)
free(ctx->dTLB_stores);
free(ctx);
}
static int tlbstat_read(struct prof_dev *dev, struct perf_evsel *evsel, struct perf_counts_values *count, int instance)
{
struct tlbstat_ctx *ctx = dev->private;
struct perf_counts {
u64 nr;
u64 total_time_enabled;
u64 total_time_running;
struct {
u64 value;
u64 id;
} ctnr[0];
} *groups = (void *)count;
struct cache *cache;
int i;
#define UPDATE_COUNTER(c) \
if (c > cache[instance].counter) { \
cache[instance].incremental = c - cache[instance].counter; \
cache[instance].counter = c; \
} else \
cache[instance].incremental = 0;
if (evsel != ctx->leader)
return 0;
cache = ctx->total_time_enabled;
UPDATE_COUNTER(groups->total_time_enabled);
cache = ctx->total_time_running;
UPDATE_COUNTER(groups->total_time_running);
for (i = 0; i < groups->nr; i++) {
__u64 config;
u64 value = groups->ctnr[i].value;
evsel = perf_evlist__id_to_evsel(ctx->evlist, groups->ctnr[i].id, NULL);
if (!evsel)
continue;
config = perf_evsel__attr(evsel)->config;
if (config == ctx->dTLB_load_misses_config)
cache = ctx->dTLB_load_misses;
else if (config == ctx->dTLB_loads_config)
cache = ctx->dTLB_loads;
else if (config == ctx->dTLB_store_misses_config)
cache = ctx->dTLB_store_misses;
else if (config == ctx->dTLB_stores_config)
cache = ctx->dTLB_stores;
else
continue;
UPDATE_COUNTER(value);
}
return 1;
}
static void tlbstat_interval(struct prof_dev *dev)
{
struct tlbstat_ctx *ctx = dev->private;
int ins;
print_time(stdout); printf("\n");
printf("[CPU] %9s %9s %7s %9s %9s %7s %7s\n", "LOADS", "MISSES", "HIT%", "STORES", "MISSES", "HIT%", "RUN%");
for (ins = 0; ins < ctx->nr_ins; ins ++) {
float load_hit = 0.0;
float store_hit = 0.0;
float run = 0.0;
if (ctx->dTLB_loads[ins].incremental > ctx->dTLB_load_misses[ins].incremental)
load_hit = (ctx->dTLB_loads[ins].incremental - ctx->dTLB_load_misses[ins].incremental) * 100.0 /
ctx->dTLB_loads[ins].incremental;
if (ctx->dTLB_stores[ins].incremental > ctx->dTLB_store_misses[ins].incremental)
store_hit = (ctx->dTLB_stores[ins].incremental - ctx->dTLB_store_misses[ins].incremental) * 100.0 /
ctx->dTLB_stores[ins].incremental;
run = ctx->total_time_running[ins].incremental * 100.0 / ctx->total_time_enabled[ins].incremental;
printf("[%03d] %9lu %9lu %6.2f%% %9lu %9lu %6.2f%% %6.2f%%\n", prof_dev_ins_cpu(dev, ins),
ctx->dTLB_loads[ins].incremental, ctx->dTLB_load_misses[ins].incremental, load_hit,
ctx->dTLB_stores[ins].incremental, ctx->dTLB_store_misses[ins].incremental, store_hit,
run);
}
}
static const char *tlbstat_desc[] = PROFILER_DESC("tlbstat",
"[OPTION...] [--exclude-host]",
"dTLB state on x86 platform.", "",
"EXAMPLES",
" "PROGRAME" tlbstat -i 1000",
" "PROGRAME" tlbstat -C 0-3 -i 1000");
static const char *tlbstat_argv[] = PROFILER_ARGV("tlbstat",
"OPTION:",
"cpus",
"interval", "output", "usage-self",
"version", "verbose", "quiet", "help",
"FILTER OPTION:",
"exclude-host");
static profiler tlbstat = {
.name = "tlbstat",
.desc = tlbstat_desc,
.argv = tlbstat_argv,
.pages = 1,
.init = tlbstat_init,
.deinit = tlbstat_exit,
.interval = tlbstat_interval,
.read = tlbstat_read,
};
PROFILER_REGISTER(tlbstat)