2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
20 #include <urcu/list.h>
22 #include <lttng/domain-internal.h>
23 #include <common/mi-lttng.h>
25 #include "../command.h"
27 static char *opt_channel_name
;
28 static char *opt_session_name
;
29 static int opt_kernel
;
30 static int opt_userspace
;
33 static char *opt_type
;
35 #ifdef LTTNG_EMBED_HELP
36 static const char help_msg
[] =
37 #include <lttng-add-context.1.h>
51 static struct lttng_handle
*handle
;
52 static struct mi_writer
*writer
;
55 * Taken from the LTTng ABI
59 CONTEXT_PERF_COUNTER
= 1, /* Backward compat. */
68 CONTEXT_PTHREAD_ID
= 10,
69 CONTEXT_HOSTNAME
= 11,
71 CONTEXT_PERF_CPU_COUNTER
= 13,
72 CONTEXT_PERF_THREAD_COUNTER
= 14,
73 CONTEXT_APP_CONTEXT
= 15,
74 CONTEXT_INTERRUPTIBLE
= 16,
75 CONTEXT_PREEMPTIBLE
= 17,
76 CONTEXT_NEED_RESCHEDULE
= 18,
77 CONTEXT_MIGRATABLE
= 19,
78 CONTEXT_CALLSTACK_KERNEL
= 20,
79 CONTEXT_CALLSTACK_USER
= 21,
80 CONTEXT_CGROUP_NS
= 22,
103 * Taken from the Perf ABI (all enum perf_*)
106 PERF_TYPE_HARDWARE
= 0,
107 PERF_TYPE_SOFTWARE
= 1,
108 PERF_TYPE_HW_CACHE
= 3,
112 enum perf_count_hard
{
113 PERF_COUNT_HW_CPU_CYCLES
= 0,
114 PERF_COUNT_HW_INSTRUCTIONS
= 1,
115 PERF_COUNT_HW_CACHE_REFERENCES
= 2,
116 PERF_COUNT_HW_CACHE_MISSES
= 3,
117 PERF_COUNT_HW_BRANCH_INSTRUCTIONS
= 4,
118 PERF_COUNT_HW_BRANCH_MISSES
= 5,
119 PERF_COUNT_HW_BUS_CYCLES
= 6,
120 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
= 7,
121 PERF_COUNT_HW_STALLED_CYCLES_BACKEND
= 8,
124 enum perf_count_soft
{
125 PERF_COUNT_SW_CPU_CLOCK
= 0,
126 PERF_COUNT_SW_TASK_CLOCK
= 1,
127 PERF_COUNT_SW_PAGE_FAULTS
= 2,
128 PERF_COUNT_SW_CONTEXT_SWITCHES
= 3,
129 PERF_COUNT_SW_CPU_MIGRATIONS
= 4,
130 PERF_COUNT_SW_PAGE_FAULTS_MIN
= 5,
131 PERF_COUNT_SW_PAGE_FAULTS_MAJ
= 6,
132 PERF_COUNT_SW_ALIGNMENT_FAULTS
= 7,
133 PERF_COUNT_SW_EMULATION_FAULTS
= 8,
137 * Generalized hardware cache events:
139 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
140 * { read, write, prefetch } x
141 * { accesses, misses }
143 enum perf_hw_cache_id
{
144 PERF_COUNT_HW_CACHE_L1D
= 0,
145 PERF_COUNT_HW_CACHE_L1I
= 1,
146 PERF_COUNT_HW_CACHE_LL
= 2,
147 PERF_COUNT_HW_CACHE_DTLB
= 3,
148 PERF_COUNT_HW_CACHE_ITLB
= 4,
149 PERF_COUNT_HW_CACHE_BPU
= 5,
151 PERF_COUNT_HW_CACHE_MAX
, /* non-ABI */
154 enum perf_hw_cache_op_id
{
155 PERF_COUNT_HW_CACHE_OP_READ
= 0,
156 PERF_COUNT_HW_CACHE_OP_WRITE
= 1,
157 PERF_COUNT_HW_CACHE_OP_PREFETCH
= 2,
159 PERF_COUNT_HW_CACHE_OP_MAX
, /* non-ABI */
162 enum perf_hw_cache_op_result_id
{
163 PERF_COUNT_HW_CACHE_RESULT_ACCESS
= 0,
164 PERF_COUNT_HW_CACHE_RESULT_MISS
= 1,
166 PERF_COUNT_HW_CACHE_RESULT_MAX
, /* non-ABI */
169 static struct poptOption long_options
[] = {
170 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
171 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
172 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
173 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
174 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
175 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
176 {"jul", 'j', POPT_ARG_NONE
, 0, OPT_JUL
, 0, 0},
177 {"log4j", 'l', POPT_ARG_NONE
, 0, OPT_LOG4J
, 0, 0},
178 {"type", 't', POPT_ARG_STRING
, &opt_type
, OPT_TYPE
, 0, 0},
179 {"list", 0, POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
180 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
181 {0, 0, 0, 0, 0, 0, 0}
187 #define PERF_HW(optstr, name, type, hide) \
189 (char *) optstr, type, hide, \
190 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
193 #define PERF_SW(optstr, name, type, hide) \
195 (char *) optstr, type, hide, \
196 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
199 #define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
201 (char *) optstr, type, hide, \
203 PERF_TYPE_HW_CACHE, \
204 (uint64_t) PERF_COUNT_HW_CACHE_##name \
205 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
206 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
210 #define PERF_HW_CACHE(optstr, name, type, hide) \
211 _PERF_HW_CACHE(optstr "-loads", name, type, \
212 READ, ACCESS, hide), \
213 _PERF_HW_CACHE(optstr "-load-misses", name, type, \
215 _PERF_HW_CACHE(optstr "-stores", name, type, \
216 WRITE, ACCESS, hide), \
217 _PERF_HW_CACHE(optstr "-store-misses", name, type, \
218 WRITE, MISS, hide), \
219 _PERF_HW_CACHE(optstr "-prefetches", name, type, \
220 PREFETCH, ACCESS, hide), \
221 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, \
222 PREFETCH, MISS, hide)
225 const struct ctx_opts
{
227 enum context_type ctx_type
;
228 int hide_help
; /* Hide from --help */
241 * These (char *) casts (as well as those in the PERF_* macros) are
242 * safe because we never free these instances of `struct ctx_opts`.
244 { (char *) "pid", CONTEXT_PID
},
245 { (char *) "procname", CONTEXT_PROCNAME
},
246 { (char *) "prio", CONTEXT_PRIO
},
247 { (char *) "nice", CONTEXT_NICE
},
248 { (char *) "vpid", CONTEXT_VPID
},
249 { (char *) "tid", CONTEXT_TID
},
250 { (char *) "pthread_id", CONTEXT_PTHREAD_ID
},
251 { (char *) "vtid", CONTEXT_VTID
},
252 { (char *) "ppid", CONTEXT_PPID
},
253 { (char *) "vppid", CONTEXT_VPPID
},
254 { (char *) "hostname", CONTEXT_HOSTNAME
},
255 { (char *) "ip", CONTEXT_IP
},
256 { (char *) "interruptible", CONTEXT_INTERRUPTIBLE
},
257 { (char *) "preemptible", CONTEXT_PREEMPTIBLE
},
258 { (char *) "need_reschedule", CONTEXT_NEED_RESCHEDULE
},
259 { (char *) "migratable", CONTEXT_MIGRATABLE
},
260 { (char *) "callstack-kernel", CONTEXT_CALLSTACK_KERNEL
},
261 #if HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT
262 { (char *) "callstack-user", CONTEXT_CALLSTACK_USER
},
264 { (char *) "cgroup_ns", CONTEXT_CGROUP_NS
},
265 { (char *) "ipc_ns", CONTEXT_IPC_NS
},
266 { (char *) "mnt_ns", CONTEXT_MNT_NS
},
267 { (char *) "net_ns", CONTEXT_NET_NS
},
268 { (char *) "pid_ns", CONTEXT_PID_NS
},
269 { (char *) "time_ns", CONTEXT_TIME_NS
},
270 { (char *) "user_ns", CONTEXT_USER_NS
},
271 { (char *) "uts_ns", CONTEXT_UTS_NS
},
272 { (char *) "uid", CONTEXT_UID
},
273 { (char *) "euid", CONTEXT_EUID
},
274 { (char *) "suid", CONTEXT_SUID
},
275 { (char *) "gid", CONTEXT_GID
},
276 { (char *) "egid", CONTEXT_EGID
},
277 { (char *) "sgid", CONTEXT_SGID
},
278 { (char *) "vuid", CONTEXT_VUID
},
279 { (char *) "veuid", CONTEXT_VEUID
},
280 { (char *) "vsuid", CONTEXT_VSUID
},
281 { (char *) "vgid", CONTEXT_VGID
},
282 { (char *) "vegid", CONTEXT_VEGID
},
283 { (char *) "vsgid", CONTEXT_VSGID
},
287 /* Perf per-CPU counters */
288 PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES
,
289 CONTEXT_PERF_CPU_COUNTER
, 0),
290 PERF_HW("perf:cpu:cycles", CPU_CYCLES
,
291 CONTEXT_PERF_CPU_COUNTER
, 0),
292 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
293 CONTEXT_PERF_CPU_COUNTER
, 0),
294 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
295 CONTEXT_PERF_CPU_COUNTER
, 0),
296 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
297 CONTEXT_PERF_CPU_COUNTER
, 0),
298 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
299 CONTEXT_PERF_CPU_COUNTER
, 0),
300 PERF_HW("perf:cpu:instructions", INSTRUCTIONS
,
301 CONTEXT_PERF_CPU_COUNTER
, 0),
302 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES
,
303 CONTEXT_PERF_CPU_COUNTER
, 0),
304 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES
,
305 CONTEXT_PERF_CPU_COUNTER
, 0),
306 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS
,
307 CONTEXT_PERF_CPU_COUNTER
, 0),
308 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS
,
309 CONTEXT_PERF_CPU_COUNTER
, 0),
310 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES
,
311 CONTEXT_PERF_CPU_COUNTER
, 0),
312 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES
,
313 CONTEXT_PERF_CPU_COUNTER
, 0),
315 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D
,
316 CONTEXT_PERF_CPU_COUNTER
, 0),
317 PERF_HW_CACHE("perf:cpu:L1-icache", L1I
,
318 CONTEXT_PERF_CPU_COUNTER
, 0),
319 PERF_HW_CACHE("perf:cpu:LLC", LL
,
320 CONTEXT_PERF_CPU_COUNTER
, 0),
321 PERF_HW_CACHE("perf:cpu:dTLB", DTLB
,
322 CONTEXT_PERF_CPU_COUNTER
, 0),
323 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB
,
324 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
325 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB
,
326 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
327 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU
,
328 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
329 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU
,
330 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
332 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK
,
333 CONTEXT_PERF_CPU_COUNTER
, 0),
334 PERF_SW("perf:cpu:task-clock", TASK_CLOCK
,
335 CONTEXT_PERF_CPU_COUNTER
, 0),
336 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS
,
337 CONTEXT_PERF_CPU_COUNTER
, 0),
338 PERF_SW("perf:cpu:faults", PAGE_FAULTS
,
339 CONTEXT_PERF_CPU_COUNTER
, 0),
340 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ
,
341 CONTEXT_PERF_CPU_COUNTER
, 0),
342 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN
,
343 CONTEXT_PERF_CPU_COUNTER
, 0),
344 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES
,
345 CONTEXT_PERF_CPU_COUNTER
, 0),
346 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES
,
347 CONTEXT_PERF_CPU_COUNTER
, 0),
348 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS
,
349 CONTEXT_PERF_CPU_COUNTER
, 0),
350 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS
,
351 CONTEXT_PERF_CPU_COUNTER
, 0),
352 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS
,
353 CONTEXT_PERF_CPU_COUNTER
, 0),
354 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS
,
355 CONTEXT_PERF_CPU_COUNTER
, 0),
357 /* Perf per-thread counters */
358 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES
,
359 CONTEXT_PERF_THREAD_COUNTER
, 0),
360 PERF_HW("perf:thread:cycles", CPU_CYCLES
,
361 CONTEXT_PERF_THREAD_COUNTER
, 0),
362 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
363 CONTEXT_PERF_THREAD_COUNTER
, 0),
364 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
365 CONTEXT_PERF_THREAD_COUNTER
, 0),
366 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
367 CONTEXT_PERF_THREAD_COUNTER
, 0),
368 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
369 CONTEXT_PERF_THREAD_COUNTER
, 0),
370 PERF_HW("perf:thread:instructions", INSTRUCTIONS
,
371 CONTEXT_PERF_THREAD_COUNTER
, 0),
372 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES
,
373 CONTEXT_PERF_THREAD_COUNTER
, 0),
374 PERF_HW("perf:thread:cache-misses", CACHE_MISSES
,
375 CONTEXT_PERF_THREAD_COUNTER
, 0),
376 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS
,
377 CONTEXT_PERF_THREAD_COUNTER
, 0),
378 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS
,
379 CONTEXT_PERF_THREAD_COUNTER
, 0),
380 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES
,
381 CONTEXT_PERF_THREAD_COUNTER
, 0),
382 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES
,
383 CONTEXT_PERF_THREAD_COUNTER
, 0),
385 PERF_HW_CACHE("perf:thread:L1-dcache", L1D
,
386 CONTEXT_PERF_THREAD_COUNTER
, 0),
387 PERF_HW_CACHE("perf:thread:L1-icache", L1I
,
388 CONTEXT_PERF_THREAD_COUNTER
, 0),
389 PERF_HW_CACHE("perf:thread:LLC", LL
,
390 CONTEXT_PERF_THREAD_COUNTER
, 0),
391 PERF_HW_CACHE("perf:thread:dTLB", DTLB
,
392 CONTEXT_PERF_THREAD_COUNTER
, 0),
393 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB
,
394 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
395 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB
,
396 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
397 _PERF_HW_CACHE("perf:thread:branch-loads", BPU
,
398 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
399 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU
,
400 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
402 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK
,
403 CONTEXT_PERF_THREAD_COUNTER
, 0),
404 PERF_SW("perf:thread:task-clock", TASK_CLOCK
,
405 CONTEXT_PERF_THREAD_COUNTER
, 0),
406 PERF_SW("perf:thread:page-fault", PAGE_FAULTS
,
407 CONTEXT_PERF_THREAD_COUNTER
, 0),
408 PERF_SW("perf:thread:faults", PAGE_FAULTS
,
409 CONTEXT_PERF_THREAD_COUNTER
, 0),
410 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ
,
411 CONTEXT_PERF_THREAD_COUNTER
, 0),
412 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN
,
413 CONTEXT_PERF_THREAD_COUNTER
, 0),
414 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES
,
415 CONTEXT_PERF_THREAD_COUNTER
, 0),
416 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES
,
417 CONTEXT_PERF_THREAD_COUNTER
, 0),
418 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS
,
419 CONTEXT_PERF_THREAD_COUNTER
, 0),
420 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS
,
421 CONTEXT_PERF_THREAD_COUNTER
, 0),
422 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS
,
423 CONTEXT_PERF_THREAD_COUNTER
, 0),
424 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS
,
425 CONTEXT_PERF_THREAD_COUNTER
, 0),
428 * Perf per-CPU counters, backward compatibilty for names.
429 * Hidden from help listing.
431 PERF_HW("perf:cpu-cycles", CPU_CYCLES
,
432 CONTEXT_PERF_COUNTER
, 1),
433 PERF_HW("perf:cycles", CPU_CYCLES
,
434 CONTEXT_PERF_COUNTER
, 1),
435 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
436 CONTEXT_PERF_COUNTER
, 1),
437 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
438 CONTEXT_PERF_COUNTER
, 1),
439 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
440 CONTEXT_PERF_COUNTER
, 1),
441 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
442 CONTEXT_PERF_COUNTER
, 1),
443 PERF_HW("perf:instructions", INSTRUCTIONS
,
444 CONTEXT_PERF_COUNTER
, 1),
445 PERF_HW("perf:cache-references", CACHE_REFERENCES
,
446 CONTEXT_PERF_COUNTER
, 1),
447 PERF_HW("perf:cache-misses", CACHE_MISSES
,
448 CONTEXT_PERF_COUNTER
, 1),
449 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS
,
450 CONTEXT_PERF_COUNTER
, 1),
451 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS
,
452 CONTEXT_PERF_COUNTER
, 1),
453 PERF_HW("perf:branch-misses", BRANCH_MISSES
,
454 CONTEXT_PERF_COUNTER
, 1),
455 PERF_HW("perf:bus-cycles", BUS_CYCLES
,
456 CONTEXT_PERF_COUNTER
, 1),
458 PERF_HW_CACHE("perf:L1-dcache", L1D
,
459 CONTEXT_PERF_COUNTER
, 1),
460 PERF_HW_CACHE("perf:L1-icache", L1I
,
461 CONTEXT_PERF_COUNTER
, 1),
462 PERF_HW_CACHE("perf:LLC", LL
,
463 CONTEXT_PERF_COUNTER
, 1),
464 PERF_HW_CACHE("perf:dTLB", DTLB
,
465 CONTEXT_PERF_COUNTER
, 1),
466 _PERF_HW_CACHE("perf:iTLB-loads", ITLB
,
467 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
468 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB
,
469 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
470 _PERF_HW_CACHE("perf:branch-loads", BPU
,
471 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
472 _PERF_HW_CACHE("perf:branch-load-misses", BPU
,
473 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
475 PERF_SW("perf:cpu-clock", CPU_CLOCK
,
476 CONTEXT_PERF_COUNTER
, 1),
477 PERF_SW("perf:task-clock", TASK_CLOCK
,
478 CONTEXT_PERF_COUNTER
, 1),
479 PERF_SW("perf:page-fault", PAGE_FAULTS
,
480 CONTEXT_PERF_COUNTER
, 1),
481 PERF_SW("perf:faults", PAGE_FAULTS
,
482 CONTEXT_PERF_COUNTER
, 1),
483 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ
,
484 CONTEXT_PERF_COUNTER
, 1),
485 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN
,
486 CONTEXT_PERF_COUNTER
, 1),
487 PERF_SW("perf:context-switches", CONTEXT_SWITCHES
,
488 CONTEXT_PERF_COUNTER
, 1),
489 PERF_SW("perf:cs", CONTEXT_SWITCHES
,
490 CONTEXT_PERF_COUNTER
, 1),
491 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS
,
492 CONTEXT_PERF_COUNTER
, 1),
493 PERF_SW("perf:migrations", CPU_MIGRATIONS
,
494 CONTEXT_PERF_COUNTER
, 1),
495 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS
,
496 CONTEXT_PERF_COUNTER
, 1),
497 PERF_SW("perf:emulation-faults", EMULATION_FAULTS
,
498 CONTEXT_PERF_COUNTER
, 1),
500 { NULL
, -1 }, /* Closure */
504 #undef _PERF_HW_CACHE
509 * Context type for command line option parsing.
512 struct ctx_opts
*opt
;
513 struct cds_list_head list
;
517 * List of context type. Use to enable multiple context on a single command
520 struct ctx_type_list
{
521 struct cds_list_head head
;
523 .head
= CDS_LIST_HEAD_INIT(ctx_type_list
.head
),
529 * Find context numerical value from string.
531 * Return -1 if not found.
533 static int find_ctx_type_idx(const char *opt
)
537 while (ctx_opts
[i
].symbol
!= NULL
) {
538 if (strcmp(opt
, ctx_opts
[i
].symbol
) == 0) {
551 enum lttng_domain_type
get_domain(void)
554 return LTTNG_DOMAIN_KERNEL
;
555 } else if (opt_userspace
) {
556 return LTTNG_DOMAIN_UST
;
557 } else if (opt_jul
) {
558 return LTTNG_DOMAIN_JUL
;
559 } else if (opt_log4j
) {
560 return LTTNG_DOMAIN_LOG4J
;
577 ret
= fileno(stdout
);
579 PERROR("Unable to retrieve fileno of stdout");
584 writer
= mi_lttng_writer_create(ret
, lttng_opt_mi
);
590 /* Open command element */
591 ret
= mi_lttng_writer_command_open(writer
,
592 mi_lttng_element_command_add_context
);
598 /* Open output element */
599 ret
= mi_lttng_writer_open_element(writer
,
600 mi_lttng_element_command_output
);
610 int mi_close(enum cmd_error_code success
)
619 /* Close output element */
620 ret
= mi_lttng_writer_close_element(writer
);
627 ret
= mi_lttng_writer_write_element_bool(writer
,
628 mi_lttng_element_command_success
, !success
);
634 /* Command element close */
635 ret
= mi_lttng_writer_command_close(writer
);
645 void populate_context(struct lttng_event_context
*context
,
646 const struct ctx_opts
*opt
)
650 context
->ctx
= (enum lttng_event_context_type
) opt
->ctx_type
;
651 switch (context
->ctx
) {
652 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
653 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
654 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
655 context
->u
.perf_counter
.type
= opt
->u
.perf
.type
;
656 context
->u
.perf_counter
.config
= opt
->u
.perf
.config
;
657 strncpy(context
->u
.perf_counter
.name
, opt
->symbol
,
658 LTTNG_SYMBOL_NAME_LEN
);
659 context
->u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
660 /* Replace : and - by _ */
661 while ((ptr
= strchr(context
->u
.perf_counter
.name
, '-')) != NULL
) {
664 while ((ptr
= strchr(context
->u
.perf_counter
.name
, ':')) != NULL
) {
668 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
669 context
->u
.app_ctx
.provider_name
=
670 opt
->u
.app_ctx
.provider_name
;
671 context
->u
.app_ctx
.ctx_name
=
672 opt
->u
.app_ctx
.ctx_name
;
680 * Pretty print context type.
683 int print_ctx_type(void)
689 struct lttng_event_context context
;
691 memset(&context
, 0, sizeof(context
));
700 /* Open a contexts element */
701 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
708 while (ctx_opts
[i
].symbol
!= NULL
) {
709 if (!ctx_opts
[i
].hide_help
) {
711 populate_context(&context
, &ctx_opts
[i
]);
712 ret
= mi_lttng_context(writer
, &context
, 1);
718 ret
= mi_lttng_writer_write_element_string(
720 mi_lttng_element_context_symbol
,
727 ret
= mi_lttng_writer_close_element(writer
);
733 fprintf(ofp
, "%s\n", ctx_opts
[i
].symbol
);
740 /* Close contexts element */
741 ret
= mi_lttng_writer_close_element(writer
);
756 * Add context to channel or event.
758 static int add_context(char *session_name
)
760 int ret
= CMD_SUCCESS
, warn
= 0, success
= 0;
761 struct lttng_event_context context
;
762 struct lttng_domain dom
;
763 struct ctx_type
*type
;
765 memset(&context
, 0, sizeof(context
));
766 memset(&dom
, 0, sizeof(dom
));
768 dom
.type
= get_domain();
769 handle
= lttng_create_handle(session_name
, &dom
);
770 if (handle
== NULL
) {
776 /* Open a contexts element */
777 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
783 /* Iterate over all the context types given */
784 cds_list_for_each_entry(type
, &ctx_type_list
.head
, list
) {
785 DBG("Adding context...");
787 populate_context(&context
, type
->opt
);
790 /* We leave context open the update the success of the command */
791 ret
= mi_lttng_context(writer
, &context
, 1);
797 ret
= mi_lttng_writer_write_element_string(writer
,
798 mi_lttng_element_context_symbol
,
806 ret
= lttng_add_context(handle
, &context
, NULL
, opt_channel_name
);
808 ERR("%s: %s", type
->opt
->symbol
, lttng_strerror(ret
));
812 if (opt_channel_name
) {
813 MSG("%s context %s added to channel %s",
814 lttng_domain_type_str(dom
.type
),
818 MSG("%s context %s added to all channels",
819 lttng_domain_type_str(dom
.type
),
826 /* Is the single operation a success ? */
827 ret
= mi_lttng_writer_write_element_bool(writer
,
828 mi_lttng_element_success
, success
);
834 /* Close the context element */
835 ret
= mi_lttng_writer_close_element(writer
);
844 /* Close contexts element */
845 ret
= mi_lttng_writer_close_element(writer
);
854 lttng_destroy_handle(handle
);
857 * This means that at least one add_context failed and tells the user to
858 * look on stderr for error(s).
867 void destroy_ctx_type(struct ctx_type
*type
)
873 free(type
->opt
->symbol
);
880 struct ctx_type
*create_ctx_type(void)
882 struct ctx_type
*type
= zmalloc(sizeof(*type
));
885 PERROR("malloc ctx_type");
889 type
->opt
= zmalloc(sizeof(*type
->opt
));
891 PERROR("malloc ctx_type options");
892 destroy_ctx_type(type
);
901 int find_ctx_type_perf_raw(const char *ctx
, struct ctx_type
*type
)
905 char *tmp_list
, *cur_list
;
907 cur_list
= tmp_list
= strdup(ctx
);
909 PERROR("strdup temp list");
914 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
918 next
= strtok(cur_list
, ":");
925 if (strncmp(next
, "perf", 4) != 0) {
931 if (strncmp(next
, "cpu", 3) == 0) {
932 type
->opt
->ctx_type
= CONTEXT_PERF_CPU_COUNTER
;
933 } else if (strncmp(next
, "thread", 4) == 0) {
934 type
->opt
->ctx_type
= CONTEXT_PERF_THREAD_COUNTER
;
941 if (strncmp(next
, "raw", 3) != 0) {
950 if (strlen(next
) < 2 || next
[0] != 'r') {
951 ERR("Wrong perf raw mask format: expected rNNN");
956 type
->opt
->u
.perf
.config
= strtoll(next
+ 1, &endptr
, 16);
957 if (errno
!= 0 || !endptr
|| *endptr
) {
958 ERR("Wrong perf raw mask format: expected rNNN");
968 ERR("Too many ':' in perf raw format");
976 ERR("Invalid perf counter specifier, expected a specifier of "
977 "the form perf:cpu:raw:rNNN:<name> or "
978 "perf:thread:raw:rNNN:<name>");
992 struct ctx_type
*get_context_type(const char *ctx
)
995 struct ctx_type
*type
= NULL
;
996 const char app_ctx_prefix
[] = "$app.";
997 char *provider_name
= NULL
, *ctx_name
= NULL
;
998 size_t i
, len
, colon_pos
= 0, provider_name_len
, ctx_name_len
;
1004 type
= create_ctx_type();
1009 /* Check if ctx matches a known static context. */
1010 opt_index
= find_ctx_type_idx(ctx
);
1011 if (opt_index
>= 0) {
1012 *type
->opt
= ctx_opts
[opt_index
];
1013 type
->opt
->symbol
= strdup(ctx_opts
[opt_index
].symbol
);
1017 /* Check if ctx is a raw perf context. */
1018 ret
= find_ctx_type_perf_raw(ctx
, type
);
1020 type
->opt
->u
.perf
.type
= PERF_TYPE_RAW
;
1021 type
->opt
->symbol
= strdup(ctx
);
1022 if (!type
->opt
->symbol
) {
1023 PERROR("Copy perf field name");
1030 * No match found against static contexts; check if it is an app
1034 if (len
<= sizeof(app_ctx_prefix
) - 1) {
1038 /* String starts with $app. */
1039 if (strncmp(ctx
, app_ctx_prefix
, sizeof(app_ctx_prefix
) - 1)) {
1043 /* Validate that the ':' separator is present. */
1044 for (i
= sizeof(app_ctx_prefix
); i
< len
; i
++) {
1045 const char c
= ctx
[i
];
1054 * No colon found or no ctx name ("$app.provider:") or no provider name
1055 * given ("$app.:..."), which is invalid.
1057 if (!colon_pos
|| colon_pos
== len
||
1058 colon_pos
== sizeof(app_ctx_prefix
)) {
1059 ERR("Invalid application context provided: no provider or context name provided.");
1063 provider_name_len
= colon_pos
- sizeof(app_ctx_prefix
) + 2;
1064 provider_name
= zmalloc(provider_name_len
);
1065 if (!provider_name
) {
1066 PERROR("malloc provider_name");
1069 strncpy(provider_name
, ctx
+ sizeof(app_ctx_prefix
) - 1,
1070 provider_name_len
- 1);
1071 type
->opt
->u
.app_ctx
.provider_name
= provider_name
;
1073 ctx_name_len
= len
- colon_pos
;
1074 ctx_name
= zmalloc(ctx_name_len
);
1076 PERROR("malloc ctx_name");
1079 strncpy(ctx_name
, ctx
+ colon_pos
+ 1, ctx_name_len
- 1);
1080 type
->opt
->u
.app_ctx
.ctx_name
= ctx_name
;
1081 type
->opt
->ctx_type
= CONTEXT_APP_CONTEXT
;
1082 type
->opt
->symbol
= strdup(ctx
);
1086 free(provider_name
);
1088 destroy_ctx_type(type
);
1093 * Add context to channel or event.
1095 int cmd_add_context(int argc
, const char **argv
)
1097 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
1098 static poptContext pc
;
1099 struct ctx_type
*type
, *tmptype
;
1100 char *session_name
= NULL
;
1101 const char *leftover
= NULL
;
1108 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1109 poptReadDefaultConfig(pc
, 0);
1111 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1117 ret
= print_ctx_type();
1121 type
= get_context_type(opt_type
);
1123 ERR("Unknown context type %s", opt_type
);
1127 cds_list_add_tail(&type
->list
, &ctx_type_list
.head
);
1139 case OPT_LIST_OPTIONS
:
1140 list_cmd_options(stdout
, long_options
);
1143 ret
= CMD_UNDEFINED
;
1148 leftover
= poptGetArg(pc
);
1150 ERR("Unknown argument: %s", leftover
);
1155 ret
= print_missing_or_multiple_domains(
1156 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
, true);
1163 ERR("Missing mandatory -t TYPE");
1168 if (!opt_session_name
) {
1169 session_name
= get_session_name();
1170 if (session_name
== NULL
) {
1175 session_name
= opt_session_name
;
1183 command_ret
= add_context(session_name
);
1184 ret
= mi_close(command_ret
);
1190 if (!opt_session_name
) {
1195 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1196 /* Preserve original error code */
1197 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1200 /* Cleanup allocated memory */
1201 cds_list_for_each_entry_safe(type
, tmptype
, &ctx_type_list
.head
, list
) {
1202 destroy_ctx_type(type
);
1205 /* Overwrite ret if an error occurred during add_context() */
1206 ret
= command_ret
? command_ret
: ret
;
1208 poptFreeContext(pc
);