b7a482bf7963e6b13024eff7b4a13dc389499750
[lttng-tools.git] / lttng / commands / add_context.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <ctype.h>
21 #include <popt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <urcu/list.h>
30
31 #include "../cmd.h"
32 #include "../conf.h"
33 #include "../utils.h"
34
35 #define PRINT_LINE_LEN 80
36
37 static char *opt_event_name;
38 static char *opt_channel_name;
39 static char *opt_session_name;
40 static int *opt_kernel;
41 static int opt_pid_all;
42 static int opt_userspace;
43 static pid_t opt_pid;
44
45 enum {
46 OPT_HELP = 1,
47 OPT_TYPE,
48 };
49
50 /*
51 * Taken from the LTTng ABI
52 */
53 enum context_type {
54 CONTEXT_PID = 0,
55 CONTEXT_PERF_COUNTER = 1,
56 CONTEXT_COMM = 2,
57 CONTEXT_PRIO = 3,
58 CONTEXT_NICE = 4,
59 CONTEXT_VPID = 5,
60 CONTEXT_TID = 6,
61 CONTEXT_VTID = 7,
62 CONTEXT_PPID = 8,
63 CONTEXT_VPPID = 9,
64 };
65
66 /*
67 * Taken from the Perf ABI (all enum perf_*)
68 */
69 enum perf_type {
70 PERF_TYPE_HARDWARE = 0,
71 PERF_TYPE_SOFTWARE = 1,
72 PERF_TYPE_HW_CACHE = 3,
73 };
74
75 enum perf_count_hard {
76 PERF_COUNT_HW_CPU_CYCLES = 0,
77 PERF_COUNT_HW_INSTRUCTIONS = 1,
78 PERF_COUNT_HW_CACHE_REFERENCES = 2,
79 PERF_COUNT_HW_CACHE_MISSES = 3,
80 PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,
81 PERF_COUNT_HW_BRANCH_MISSES = 5,
82 PERF_COUNT_HW_BUS_CYCLES = 6,
83 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7,
84 PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8,
85 };
86
87 enum perf_count_soft {
88 PERF_COUNT_SW_CPU_CLOCK = 0,
89 PERF_COUNT_SW_TASK_CLOCK = 1,
90 PERF_COUNT_SW_PAGE_FAULTS = 2,
91 PERF_COUNT_SW_CONTEXT_SWITCHES = 3,
92 PERF_COUNT_SW_CPU_MIGRATIONS = 4,
93 PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,
94 PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,
95 PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,
96 PERF_COUNT_SW_EMULATION_FAULTS = 8,
97 };
98
99 /*
100 * Generalized hardware cache events:
101 *
102 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
103 * { read, write, prefetch } x
104 * { accesses, misses }
105 */
106 enum perf_hw_cache_id {
107 PERF_COUNT_HW_CACHE_L1D = 0,
108 PERF_COUNT_HW_CACHE_L1I = 1,
109 PERF_COUNT_HW_CACHE_LL = 2,
110 PERF_COUNT_HW_CACHE_DTLB = 3,
111 PERF_COUNT_HW_CACHE_ITLB = 4,
112 PERF_COUNT_HW_CACHE_BPU = 5,
113
114 PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
115 };
116
117 enum perf_hw_cache_op_id {
118 PERF_COUNT_HW_CACHE_OP_READ = 0,
119 PERF_COUNT_HW_CACHE_OP_WRITE = 1,
120 PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
121
122 PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
123 };
124
125 enum perf_hw_cache_op_result_id {
126 PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
127 PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
128
129 PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
130 };
131
132 static struct poptOption long_options[] = {
133 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
134 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
135 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
136 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
137 {"event", 'e', POPT_ARG_STRING, &opt_event_name, 0, 0, 0},
138 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
139 {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0},
140 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
141 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
142 {"type", 't', POPT_ARG_STRING, 0, OPT_TYPE, 0, 0},
143 {0, 0, 0, 0, 0, 0, 0}
144 };
145
146 /*
147 * Context options
148 */
149 #define PERF_HW(opt, name) \
150 { \
151 "perf:" #opt, CONTEXT_PERF_COUNTER, \
152 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
153 }
154
155 #define PERF_SW(opt, name) \
156 { \
157 "perf:" #opt, CONTEXT_PERF_COUNTER, \
158 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
159 }
160
161 #define _PERF_HW_CACHE(optstr, name, op, result) \
162 { \
163 "perf:" optstr, CONTEXT_PERF_COUNTER, \
164 .u.perf = { \
165 PERF_TYPE_HW_CACHE, \
166 (uint64_t) PERF_COUNT_HW_CACHE_##name \
167 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
168 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
169 }, \
170 }
171
172 #define PERF_HW_CACHE(opt, name) \
173 _PERF_HW_CACHE(#opt "-loads", name, READ, ACCESS), \
174 _PERF_HW_CACHE(#opt "-load-misses", name, READ, MISS), \
175 _PERF_HW_CACHE(#opt "-stores", name, WRITE, ACCESS), \
176 _PERF_HW_CACHE(#opt "-store-misses", name, WRITE, MISS), \
177 _PERF_HW_CACHE(#opt "-prefetches", name, PREFETCH, ACCESS), \
178 _PERF_HW_CACHE(#opt "-prefetch-misses", name, PREFETCH, MISS) \
179
180 static
181 const struct ctx_opts {
182 char *symbol;
183 enum context_type ctx_type;
184 union {
185 struct {
186 uint32_t type;
187 uint64_t config;
188 } perf;
189 } u;
190 } ctx_opts[] = {
191 { "pid", CONTEXT_PID },
192 { "comm", CONTEXT_COMM },
193 { "prio", CONTEXT_PRIO },
194 { "nice", CONTEXT_NICE },
195 { "vpid", CONTEXT_VPID },
196 { "tid", CONTEXT_TID },
197 { "vtid", CONTEXT_VTID },
198 { "ppid", CONTEXT_PPID },
199 { "vppid", CONTEXT_VPPID },
200 /* Perf options */
201 PERF_HW(cpu-cycles, CPU_CYCLES),
202 PERF_HW(cycles, CPU_CYCLES),
203 PERF_HW(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND),
204 PERF_HW(idle-cycles-frontend, STALLED_CYCLES_FRONTEND),
205 PERF_HW(stalled-cycles-backend, STALLED_CYCLES_BACKEND),
206 PERF_HW(idle-cycles-backend, STALLED_CYCLES_BACKEND),
207 PERF_HW(instructions, INSTRUCTIONS),
208 PERF_HW(cache-references, CACHE_REFERENCES),
209 PERF_HW(cache-misses, CACHE_MISSES),
210 PERF_HW(branch-instructions, BRANCH_INSTRUCTIONS),
211 PERF_HW(branches, BRANCH_INSTRUCTIONS),
212 PERF_HW(branch-misses, BRANCH_MISSES),
213 PERF_HW(bus-cycles, BUS_CYCLES),
214
215 PERF_HW_CACHE(L1-dcache, L1D),
216 PERF_HW_CACHE(L1-icache, L1I),
217 PERF_HW_CACHE(LLC, LL),
218 PERF_HW_CACHE(dTLB, DTLB),
219 _PERF_HW_CACHE("iTLB-loads", ITLB, READ, ACCESS),
220 _PERF_HW_CACHE("iTLB-load-misses", ITLB, READ, MISS),
221 _PERF_HW_CACHE("branch-loads", BPU, READ, ACCESS),
222 _PERF_HW_CACHE("branch-load-misses", BPU, READ, MISS),
223
224
225 PERF_SW(cpu-clock, CPU_CLOCK),
226 PERF_SW(task-clock, TASK_CLOCK),
227 PERF_SW(page-fault, PAGE_FAULTS),
228 PERF_SW(faults, PAGE_FAULTS),
229 PERF_SW(major-faults, PAGE_FAULTS_MAJ),
230 PERF_SW(minor-faults, PAGE_FAULTS_MIN),
231 PERF_SW(context-switches, CONTEXT_SWITCHES),
232 PERF_SW(cs, CONTEXT_SWITCHES),
233 PERF_SW(cpu-migrations, CPU_MIGRATIONS),
234 PERF_SW(migrations, CPU_MIGRATIONS),
235 PERF_SW(alignment-faults, ALIGNMENT_FAULTS),
236 PERF_SW(emulation-faults, EMULATION_FAULTS),
237 { NULL, -1 }, /* Closure */
238 };
239
240 #undef PERF_SW
241 #undef PERF_HW
242
243 /*
244 * Context type for command line option parsing.
245 */
246 struct ctx_type {
247 const struct ctx_opts *opt;
248 struct cds_list_head list;
249 };
250
251 /*
252 * List of context type. Use to enable multiple context on a single command
253 * line entry.
254 */
255 struct ctx_type_list {
256 struct cds_list_head head;
257 } ctx_type_list = {
258 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
259 };
260
261 /*
262 * Pretty print context type.
263 */
264 static void print_ctx_type(FILE *ofp)
265 {
266 const char *indent = " ";
267 int indent_len = strlen(indent);
268 int len, i = 0;
269
270 fprintf(ofp, "%s", indent);
271 len = indent_len;
272 while (ctx_opts[i].symbol != NULL) {
273 if (len > indent_len) {
274 if (len + strlen(ctx_opts[i].symbol) + 2
275 >= PRINT_LINE_LEN) {
276 fprintf(ofp, ",\n");
277 fprintf(ofp, "%s", indent);
278 len = indent_len;
279 } else {
280 len += fprintf(ofp, ", ");
281 }
282 }
283 len += fprintf(ofp, "%s", ctx_opts[i].symbol);
284 i++;
285 }
286 }
287
288 /*
289 * usage
290 */
291 static void usage(FILE *ofp)
292 {
293 fprintf(ofp, "usage: lttng add-context -t TYPE\n");
294 fprintf(ofp, "\n");
295 fprintf(ofp, "If no event name is given (-e), the context will be added to\n");
296 fprintf(ofp, "all events in the channel.\n");
297 fprintf(ofp, "If no channel and no event is given (-c/-e), the context\n");
298 fprintf(ofp, "will be added to all events in all channels.\n");
299 fprintf(ofp, "\n");
300 fprintf(ofp, "Options:\n");
301 fprintf(ofp, " -h, --help Show this help\n");
302 fprintf(ofp, " -s, --session Apply on session name\n");
303 fprintf(ofp, " -c, --channel NAME Apply on channel\n");
304 fprintf(ofp, " -e, --event NAME Apply on event\n");
305 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
306 fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n");
307 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
308 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
309 fprintf(ofp, " -t, --type TYPE Context type. You can repeat that option on\n");
310 fprintf(ofp, " the command line.\n");
311 fprintf(ofp, " TYPE can be one of the strings below:\n");
312 print_ctx_type(ofp);
313 fprintf(ofp, "\n");
314 fprintf(ofp, "Example:\n");
315 fprintf(ofp, "This command will add the context information 'prio' and two perf\n"
316 "counters: hardware branch misses and cache misses, to all events\n"
317 "in the trace data output:\n");
318 fprintf(ofp, "# lttng add-context -k -t prio -t perf:branch-misses -t perf:cache-misses\n");
319 fprintf(ofp, "\n");
320 }
321
322 /*
323 * Find context numerical value from string.
324 */
325 static int find_ctx_type_idx(const char *opt)
326 {
327 int ret = -1, i = 0;
328
329 while (ctx_opts[i].symbol != NULL) {
330 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
331 ret = i;
332 goto end;
333 }
334 i++;
335 }
336
337 end:
338 return ret;
339 }
340
341 /*
342 * Add context to channel or event.
343 */
344 static int add_context(void)
345 {
346 int ret = CMD_SUCCESS;
347 struct lttng_event_context context;
348 struct lttng_domain dom;
349 struct ctx_type *type;
350 char *ptr;
351
352 if (set_session_name(opt_session_name) < 0) {
353 ret = CMD_ERROR;
354 goto error;
355 }
356
357 /* Iterate over all context type given */
358 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
359 context.ctx = type->opt->ctx_type;
360 if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) {
361 context.u.perf_counter.type = type->opt->u.perf.type;
362 context.u.perf_counter.config = type->opt->u.perf.config;
363 strcpy(context.u.perf_counter.name,
364 type->opt->symbol);
365 /* Replace : and - by _ */
366 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
367 *ptr = '_';
368 }
369 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
370 *ptr = '_';
371 }
372 }
373 if (opt_kernel) {
374 /* Create kernel domain */
375 dom.type = LTTNG_DOMAIN_KERNEL;
376
377 DBG("Adding kernel context");
378 ret = lttng_add_context(&dom, &context, opt_event_name,
379 opt_channel_name);
380 if (ret < 0) {
381 goto error;
382 } else {
383 MSG("Kernel context %s added", type->opt->symbol);
384 }
385 } else if (opt_userspace) { /* User-space tracer action */
386 /*
387 * TODO: Waiting on lttng UST 2.0
388 */
389 if (opt_pid_all) {
390 } else if (opt_pid != 0) {
391 }
392 ret = CMD_NOT_IMPLEMENTED;
393 goto error;
394 } else {
395 ERR("Please specify a tracer (kernel or user-space)");
396 goto error;
397 }
398 }
399
400 error:
401 return ret;
402 }
403
404 /*
405 * Add context on channel or event.
406 */
407 int cmd_add_context(int argc, const char **argv)
408 {
409 int index, opt, ret = CMD_SUCCESS;
410 char *tmp;
411 static poptContext pc;
412 struct ctx_type *type, *tmptype;
413
414 if (argc < 2) {
415 usage(stderr);
416 goto end;
417 }
418
419 pc = poptGetContext(NULL, argc, argv, long_options, 0);
420 poptReadDefaultConfig(pc, 0);
421
422 while ((opt = poptGetNextOpt(pc)) != -1) {
423 switch (opt) {
424 case OPT_HELP:
425 usage(stderr);
426 ret = CMD_SUCCESS;
427 goto end;
428 case OPT_TYPE:
429 /* Mandatory field */
430 tmp = poptGetOptArg(pc);
431 if (tmp == NULL) {
432 usage(stderr);
433 ret = CMD_ERROR;
434 free(tmp);
435 goto end;
436 }
437 type = malloc(sizeof(struct ctx_type));
438 if (type == NULL) {
439 perror("malloc ctx_type");
440 ret = -1;
441 goto end;
442 }
443 index = find_ctx_type_idx(tmp);
444 if (index < 0) {
445 ERR("Unknown context type %s", tmp);
446 goto end;
447 }
448 type->opt = &ctx_opts[index];
449 if (type->opt->ctx_type == -1) {
450 ERR("Unknown context type %s", tmp);
451 } else {
452 cds_list_add(&type->list, &ctx_type_list.head);
453 }
454 free(tmp);
455 break;
456 default:
457 usage(stderr);
458 ret = CMD_UNDEFINED;
459 goto end;
460 }
461 }
462
463 ret = add_context();
464
465 /* Cleanup allocated memory */
466 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
467 free(type);
468 }
469
470 end:
471 return ret;
472 }
This page took 0.058358 seconds and 3 git commands to generate.