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