Cleanup mmap consuming
[lttng-tools.git] / lttng / commands / add_context.c
CommitLineData
d65106b1
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
d65106b1
DG
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
90b9a268 20#include <ctype.h>
d65106b1
DG
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
3301e740
DG
29#include <urcu/list.h>
30
6e2d116c
DG
31#include "../cmd.h"
32#include "../conf.h"
33#include "../utils.h"
d65106b1 34
b13d56d7
MD
35#define PRINT_LINE_LEN 80
36
d65106b1
DG
37static char *opt_event_name;
38static char *opt_channel_name;
5440dc42 39static char *opt_session_name;
d65106b1
DG
40static int *opt_kernel;
41static int opt_pid_all;
42static int opt_userspace;
d65106b1
DG
43static pid_t opt_pid;
44
45enum {
46 OPT_HELP = 1,
47 OPT_TYPE,
48};
49
90b9a268
DG
50/*
51 * Taken from the LTTng ABI
52 */
53enum 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,
3301e740
DG
64};
65
90b9a268
DG
66/*
67 * Taken from the Perf ABI (all enum perf_*)
68 */
69enum perf_type {
70 PERF_TYPE_HARDWARE = 0,
71 PERF_TYPE_SOFTWARE = 1,
b13d56d7 72 PERF_TYPE_HW_CACHE = 3,
3301e740
DG
73};
74
90b9a268 75enum perf_count_hard {
b13d56d7
MD
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,
90b9a268
DG
85};
86
87enum 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,
3301e740
DG
97};
98
b13d56d7
MD
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 */
106enum 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
117enum 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
125enum 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
d65106b1
DG
132static struct poptOption long_options[] = {
133 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
134 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 135 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
d65106b1
DG
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},
90b9a268 142 {"type", 't', POPT_ARG_STRING, 0, OPT_TYPE, 0, 0},
d65106b1
DG
143 {0, 0, 0, 0, 0, 0, 0}
144};
145
90b9a268 146/*
b13d56d7 147 * Context options
90b9a268 148 */
b13d56d7
MD
149#define PERF_HW(opt, name) \
150 { \
151 "perf:" #opt, CONTEXT_PERF_COUNTER, \
152 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
153 }
90b9a268 154
b13d56d7
MD
155#define PERF_SW(opt, name) \
156 { \
157 "perf:" #opt, CONTEXT_PERF_COUNTER, \
158 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
159 }
90b9a268 160
b13d56d7
MD
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 \
18829107
MD
167 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
168 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
b13d56d7
MD
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
180static
181const struct ctx_opts {
90b9a268 182 char *symbol;
b13d56d7
MD
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 */
90b9a268
DG
238};
239
b13d56d7
MD
240#undef PERF_SW
241#undef PERF_HW
242
90b9a268 243/*
b13d56d7 244 * Context type for command line option parsing.
90b9a268 245 */
b13d56d7
MD
246struct ctx_type {
247 const struct ctx_opts *opt;
248 struct cds_list_head list;
90b9a268
DG
249};
250
251/*
252 * List of context type. Use to enable multiple context on a single command
253 * line entry.
254 */
255struct 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
90b9a268
DG
261/*
262 * Pretty print context type.
263 */
264static void print_ctx_type(FILE *ofp)
265{
b13d56d7
MD
266 const char *indent = " ";
267 int indent_len = strlen(indent);
268 int len, i = 0;
90b9a268 269
76e3c5dd 270 fprintf(ofp, "%s", indent);
b13d56d7 271 len = indent_len;
90b9a268 272 while (ctx_opts[i].symbol != NULL) {
b13d56d7
MD
273 if (len > indent_len) {
274 if (len + strlen(ctx_opts[i].symbol) + 2
275 >= PRINT_LINE_LEN) {
276 fprintf(ofp, ",\n");
76e3c5dd 277 fprintf(ofp, "%s", indent);
b13d56d7
MD
278 len = indent_len;
279 } else {
280 len += fprintf(ofp, ", ");
90b9a268
DG
281 }
282 }
b13d56d7 283 len += fprintf(ofp, "%s", ctx_opts[i].symbol);
90b9a268
DG
284 i++;
285 }
90b9a268
DG
286}
287
d65106b1
DG
288/*
289 * usage
290 */
291static void usage(FILE *ofp)
292{
b13d56d7 293 fprintf(ofp, "usage: lttng add-context -t TYPE\n");
d65106b1 294 fprintf(ofp, "\n");
b13d56d7
MD
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");
3301e740 299 fprintf(ofp, "\n");
d65106b1
DG
300 fprintf(ofp, "Options:\n");
301 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 302 fprintf(ofp, " -s, --session Apply on session name\n");
d65106b1
DG
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");
b13d56d7
MD
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");
90b9a268 312 print_ctx_type(ofp);
d65106b1 313 fprintf(ofp, "\n");
90b9a268 314 fprintf(ofp, "Example:\n");
b13d56d7 315 fprintf(ofp, "This command will add the context information 'prio' and two perf\n"
e0899ffa 316 "counters: hardware branch misses and cache misses, to all events\n"
b13d56d7
MD
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");
d65106b1
DG
319 fprintf(ofp, "\n");
320}
321
90b9a268
DG
322/*
323 * Find context numerical value from string.
324 */
325static 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
337end:
338 return ret;
339}
340
90b9a268
DG
341/*
342 * Add context to channel or event.
d65106b1 343 */
3301e740 344static int add_context(void)
d65106b1 345{
b13d56d7 346 int ret = CMD_SUCCESS;
7d29a247
DG
347 struct lttng_event_context context;
348 struct lttng_domain dom;
3301e740 349 struct ctx_type *type;
b13d56d7 350 char *ptr;
d65106b1 351
3301e740
DG
352 /* Iterate over all context type given */
353 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
d7b91b3c
DG
354 /* Set session name for the current command */
355 if (set_session_name(opt_session_name) < 0) {
356 ret = CMD_ERROR;
357 goto error;
358 }
359
b13d56d7
MD
360 context.ctx = type->opt->ctx_type;
361 if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) {
362 context.u.perf_counter.type = type->opt->u.perf.type;
363 context.u.perf_counter.config = type->opt->u.perf.config;
d7b91b3c 364 strcpy(context.u.perf_counter.name, type->opt->symbol);
b13d56d7
MD
365 /* Replace : and - by _ */
366 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
367 *ptr = '_';
3301e740 368 }
b13d56d7
MD
369 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
370 *ptr = '_';
3301e740 371 }
3301e740 372 }
3301e740
DG
373 if (opt_kernel) {
374 /* Create kernel domain */
375 dom.type = LTTNG_DOMAIN_KERNEL;
7d29a247 376
3301e740
DG
377 DBG("Adding kernel context");
378 ret = lttng_add_context(&dom, &context, opt_event_name,
379 opt_channel_name);
380 if (ret < 0) {
d7b91b3c 381 fprintf(stderr, "%s: ", type->opt->symbol);
3301e740
DG
382 goto error;
383 } else {
b13d56d7 384 MSG("Kernel context %s added", type->opt->symbol);
3301e740
DG
385 }
386 } else if (opt_userspace) { /* User-space tracer action */
387 /*
388 * TODO: Waiting on lttng UST 2.0
389 */
390 if (opt_pid_all) {
391 } else if (opt_pid != 0) {
392 }
393 ret = CMD_NOT_IMPLEMENTED;
d65106b1
DG
394 goto error;
395 } else {
048e2159 396 ERR("Please specify a tracer (--kernel or --userspace)");
3301e740 397 goto error;
d65106b1 398 }
d65106b1
DG
399 }
400
401error:
402 return ret;
403}
404
405/*
90b9a268 406 * Add context on channel or event.
d65106b1
DG
407 */
408int cmd_add_context(int argc, const char **argv)
409{
90b9a268 410 int index, opt, ret = CMD_SUCCESS;
d65106b1
DG
411 char *tmp;
412 static poptContext pc;
b13d56d7 413 struct ctx_type *type, *tmptype;
d65106b1 414
636167b6
DG
415 if (argc < 2) {
416 usage(stderr);
417 goto end;
418 }
419
d65106b1
DG
420 pc = poptGetContext(NULL, argc, argv, long_options, 0);
421 poptReadDefaultConfig(pc, 0);
422
423 while ((opt = poptGetNextOpt(pc)) != -1) {
424 switch (opt) {
425 case OPT_HELP:
426 usage(stderr);
427 ret = CMD_SUCCESS;
428 goto end;
429 case OPT_TYPE:
430 /* Mandatory field */
431 tmp = poptGetOptArg(pc);
432 if (tmp == NULL) {
433 usage(stderr);
434 ret = CMD_ERROR;
636167b6 435 free(tmp);
d65106b1
DG
436 goto end;
437 }
3301e740
DG
438 type = malloc(sizeof(struct ctx_type));
439 if (type == NULL) {
440 perror("malloc ctx_type");
441 ret = -1;
636167b6
DG
442 goto end;
443 }
b13d56d7
MD
444 index = find_ctx_type_idx(tmp);
445 if (index < 0) {
446 ERR("Unknown context type %s", tmp);
447 goto end;
90b9a268 448 }
b13d56d7
MD
449 type->opt = &ctx_opts[index];
450 if (type->opt->ctx_type == -1) {
90b9a268
DG
451 ERR("Unknown context type %s", tmp);
452 } else {
453 cds_list_add(&type->list, &ctx_type_list.head);
454 }
636167b6 455 free(tmp);
d65106b1
DG
456 break;
457 default:
458 usage(stderr);
459 ret = CMD_UNDEFINED;
460 goto end;
461 }
462 }
463
3301e740
DG
464 ret = add_context();
465
466 /* Cleanup allocated memory */
b13d56d7 467 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
3301e740
DG
468 free(type);
469 }
470
d65106b1
DG
471end:
472 return ret;
473}
This page took 0.045522 seconds and 5 git commands to generate.