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>
19 #include <urcu/list.h>
21 #include <lttng/domain-internal.hpp>
22 #include <common/mi-lttng.hpp>
24 #include "../command.hpp"
26 static char *opt_channel_name
;
27 static char *opt_session_name
;
28 static int opt_kernel
;
29 static int opt_userspace
;
32 static char *opt_type
;
34 #ifdef LTTNG_EMBED_HELP
35 static const char help_msg
[] =
36 #include <lttng-add-context.1.h>
50 static struct lttng_handle
*handle
;
51 static struct mi_writer
*writer
;
54 * Taken from the LTTng ABI except for "UNKNOWN".
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 optstr, type, PERF_COUNT_HW_##name, hide \
192 #define PERF_SW(optstr, name, type, hide) \
194 optstr, type, PERF_COUNT_SW_##name, hide \
197 #define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
200 PERF_COUNT_HW_CACHE_##name, \
201 PERF_COUNT_HW_CACHE_OP_##op, \
202 PERF_COUNT_HW_CACHE_RESULT_##result, \
206 #define PERF_HW_CACHE(optstr, name, type, hide) \
207 _PERF_HW_CACHE(optstr "-loads", name, type, \
208 READ, ACCESS, hide), \
209 _PERF_HW_CACHE(optstr "-load-misses", name, type, \
211 _PERF_HW_CACHE(optstr "-stores", name, type, \
212 WRITE, ACCESS, hide), \
213 _PERF_HW_CACHE(optstr "-store-misses", name, type, \
214 WRITE, MISS, hide), \
215 _PERF_HW_CACHE(optstr "-prefetches", name, type, \
216 PREFETCH, ACCESS, hide), \
217 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, \
218 PREFETCH, MISS, hide)
221 const struct ctx_opts
{
222 /* Needed for end-of-list item. */
224 : ctx_opts(nullptr, CONTEXT_UNKNOWN
)
227 ctx_opts(const char *symbol_
, context_type ctx_type_
, bool hide_help_
= false)
228 : symbol((char *) symbol_
), ctx_type(ctx_type_
), hide_help(hide_help_
)
231 ctx_opts(const char *symbol_
, context_type ctx_type_
, perf_count_hard perf_count_hard
, bool hide_help_
)
232 : ctx_opts(symbol_
, ctx_type_
, hide_help_
)
234 u
.perf
.type
= PERF_TYPE_HARDWARE
;
235 u
.perf
.config
= perf_count_hard
;
238 ctx_opts(const char *symbol_
, context_type ctx_type_
, perf_count_soft perf_count_soft
, bool hide_help_
)
239 : ctx_opts(symbol_
, ctx_type_
, hide_help_
)
241 u
.perf
.type
= PERF_TYPE_SOFTWARE
;
242 u
.perf
.config
= perf_count_soft
;
245 ctx_opts(const char *symbol_
, context_type ctx_type_
,
246 perf_hw_cache_id perf_hw_cache_id
,
247 perf_hw_cache_op_id perf_hw_cache_op_id
,
248 perf_hw_cache_op_result_id perf_hw_cache_op_result_id
,
250 : ctx_opts(symbol_
, ctx_type_
, hide_help_
)
252 u
.perf
.type
= PERF_TYPE_HW_CACHE
;
253 u
.perf
.config
= perf_hw_cache_id
| perf_hw_cache_op_id
<< 8 | perf_hw_cache_op_result_id
<< 16;
257 enum context_type ctx_type
;
258 bool hide_help
; /* Hide from --help */
271 * These (char *) casts (as well as those in the PERF_* macros) are
272 * safe because we never free these instances of `struct ctx_opts`.
274 { (char *) "pid", CONTEXT_PID
},
275 { (char *) "procname", CONTEXT_PROCNAME
},
276 { (char *) "prio", CONTEXT_PRIO
},
277 { (char *) "nice", CONTEXT_NICE
},
278 { (char *) "vpid", CONTEXT_VPID
},
279 { (char *) "tid", CONTEXT_TID
},
280 { (char *) "pthread_id", CONTEXT_PTHREAD_ID
},
281 { (char *) "vtid", CONTEXT_VTID
},
282 { (char *) "ppid", CONTEXT_PPID
},
283 { (char *) "vppid", CONTEXT_VPPID
},
284 { (char *) "hostname", CONTEXT_HOSTNAME
},
285 { (char *) "ip", CONTEXT_IP
},
286 { (char *) "interruptible", CONTEXT_INTERRUPTIBLE
},
287 { (char *) "preemptible", CONTEXT_PREEMPTIBLE
},
288 { (char *) "need_reschedule", CONTEXT_NEED_RESCHEDULE
},
289 { (char *) "migratable", CONTEXT_MIGRATABLE
},
290 { (char *) "callstack-kernel", CONTEXT_CALLSTACK_KERNEL
},
291 #ifdef HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT
292 { (char *) "callstack-user", CONTEXT_CALLSTACK_USER
},
294 { (char *) "cgroup_ns", CONTEXT_CGROUP_NS
},
295 { (char *) "ipc_ns", CONTEXT_IPC_NS
},
296 { (char *) "mnt_ns", CONTEXT_MNT_NS
},
297 { (char *) "net_ns", CONTEXT_NET_NS
},
298 { (char *) "pid_ns", CONTEXT_PID_NS
},
299 { (char *) "time_ns", CONTEXT_TIME_NS
},
300 { (char *) "user_ns", CONTEXT_USER_NS
},
301 { (char *) "uts_ns", CONTEXT_UTS_NS
},
302 { (char *) "uid", CONTEXT_UID
},
303 { (char *) "euid", CONTEXT_EUID
},
304 { (char *) "suid", CONTEXT_SUID
},
305 { (char *) "gid", CONTEXT_GID
},
306 { (char *) "egid", CONTEXT_EGID
},
307 { (char *) "sgid", CONTEXT_SGID
},
308 { (char *) "vuid", CONTEXT_VUID
},
309 { (char *) "veuid", CONTEXT_VEUID
},
310 { (char *) "vsuid", CONTEXT_VSUID
},
311 { (char *) "vgid", CONTEXT_VGID
},
312 { (char *) "vegid", CONTEXT_VEGID
},
313 { (char *) "vsgid", CONTEXT_VSGID
},
317 /* Perf per-CPU counters */
318 PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES
,
319 CONTEXT_PERF_CPU_COUNTER
, 0),
320 PERF_HW("perf:cpu:cycles", CPU_CYCLES
,
321 CONTEXT_PERF_CPU_COUNTER
, 0),
322 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
323 CONTEXT_PERF_CPU_COUNTER
, 0),
324 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
325 CONTEXT_PERF_CPU_COUNTER
, 0),
326 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
327 CONTEXT_PERF_CPU_COUNTER
, 0),
328 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
329 CONTEXT_PERF_CPU_COUNTER
, 0),
330 PERF_HW("perf:cpu:instructions", INSTRUCTIONS
,
331 CONTEXT_PERF_CPU_COUNTER
, 0),
332 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES
,
333 CONTEXT_PERF_CPU_COUNTER
, 0),
334 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES
,
335 CONTEXT_PERF_CPU_COUNTER
, 0),
336 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS
,
337 CONTEXT_PERF_CPU_COUNTER
, 0),
338 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS
,
339 CONTEXT_PERF_CPU_COUNTER
, 0),
340 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES
,
341 CONTEXT_PERF_CPU_COUNTER
, 0),
342 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES
,
343 CONTEXT_PERF_CPU_COUNTER
, 0),
345 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D
,
346 CONTEXT_PERF_CPU_COUNTER
, 0),
347 PERF_HW_CACHE("perf:cpu:L1-icache", L1I
,
348 CONTEXT_PERF_CPU_COUNTER
, 0),
349 PERF_HW_CACHE("perf:cpu:LLC", LL
,
350 CONTEXT_PERF_CPU_COUNTER
, 0),
351 PERF_HW_CACHE("perf:cpu:dTLB", DTLB
,
352 CONTEXT_PERF_CPU_COUNTER
, 0),
353 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB
,
354 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
355 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB
,
356 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
357 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU
,
358 CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
359 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU
,
360 CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
362 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK
,
363 CONTEXT_PERF_CPU_COUNTER
, 0),
364 PERF_SW("perf:cpu:task-clock", TASK_CLOCK
,
365 CONTEXT_PERF_CPU_COUNTER
, 0),
366 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS
,
367 CONTEXT_PERF_CPU_COUNTER
, 0),
368 PERF_SW("perf:cpu:faults", PAGE_FAULTS
,
369 CONTEXT_PERF_CPU_COUNTER
, 0),
370 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ
,
371 CONTEXT_PERF_CPU_COUNTER
, 0),
372 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN
,
373 CONTEXT_PERF_CPU_COUNTER
, 0),
374 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES
,
375 CONTEXT_PERF_CPU_COUNTER
, 0),
376 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES
,
377 CONTEXT_PERF_CPU_COUNTER
, 0),
378 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS
,
379 CONTEXT_PERF_CPU_COUNTER
, 0),
380 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS
,
381 CONTEXT_PERF_CPU_COUNTER
, 0),
382 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS
,
383 CONTEXT_PERF_CPU_COUNTER
, 0),
384 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS
,
385 CONTEXT_PERF_CPU_COUNTER
, 0),
387 /* Perf per-thread counters */
388 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES
,
389 CONTEXT_PERF_THREAD_COUNTER
, 0),
390 PERF_HW("perf:thread:cycles", CPU_CYCLES
,
391 CONTEXT_PERF_THREAD_COUNTER
, 0),
392 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
393 CONTEXT_PERF_THREAD_COUNTER
, 0),
394 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
395 CONTEXT_PERF_THREAD_COUNTER
, 0),
396 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
397 CONTEXT_PERF_THREAD_COUNTER
, 0),
398 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
399 CONTEXT_PERF_THREAD_COUNTER
, 0),
400 PERF_HW("perf:thread:instructions", INSTRUCTIONS
,
401 CONTEXT_PERF_THREAD_COUNTER
, 0),
402 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES
,
403 CONTEXT_PERF_THREAD_COUNTER
, 0),
404 PERF_HW("perf:thread:cache-misses", CACHE_MISSES
,
405 CONTEXT_PERF_THREAD_COUNTER
, 0),
406 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS
,
407 CONTEXT_PERF_THREAD_COUNTER
, 0),
408 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS
,
409 CONTEXT_PERF_THREAD_COUNTER
, 0),
410 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES
,
411 CONTEXT_PERF_THREAD_COUNTER
, 0),
412 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES
,
413 CONTEXT_PERF_THREAD_COUNTER
, 0),
415 PERF_HW_CACHE("perf:thread:L1-dcache", L1D
,
416 CONTEXT_PERF_THREAD_COUNTER
, 0),
417 PERF_HW_CACHE("perf:thread:L1-icache", L1I
,
418 CONTEXT_PERF_THREAD_COUNTER
, 0),
419 PERF_HW_CACHE("perf:thread:LLC", LL
,
420 CONTEXT_PERF_THREAD_COUNTER
, 0),
421 PERF_HW_CACHE("perf:thread:dTLB", DTLB
,
422 CONTEXT_PERF_THREAD_COUNTER
, 0),
423 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB
,
424 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
425 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB
,
426 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
427 _PERF_HW_CACHE("perf:thread:branch-loads", BPU
,
428 CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
429 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU
,
430 CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
432 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK
,
433 CONTEXT_PERF_THREAD_COUNTER
, 0),
434 PERF_SW("perf:thread:task-clock", TASK_CLOCK
,
435 CONTEXT_PERF_THREAD_COUNTER
, 0),
436 PERF_SW("perf:thread:page-fault", PAGE_FAULTS
,
437 CONTEXT_PERF_THREAD_COUNTER
, 0),
438 PERF_SW("perf:thread:faults", PAGE_FAULTS
,
439 CONTEXT_PERF_THREAD_COUNTER
, 0),
440 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ
,
441 CONTEXT_PERF_THREAD_COUNTER
, 0),
442 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN
,
443 CONTEXT_PERF_THREAD_COUNTER
, 0),
444 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES
,
445 CONTEXT_PERF_THREAD_COUNTER
, 0),
446 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES
,
447 CONTEXT_PERF_THREAD_COUNTER
, 0),
448 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS
,
449 CONTEXT_PERF_THREAD_COUNTER
, 0),
450 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS
,
451 CONTEXT_PERF_THREAD_COUNTER
, 0),
452 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS
,
453 CONTEXT_PERF_THREAD_COUNTER
, 0),
454 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS
,
455 CONTEXT_PERF_THREAD_COUNTER
, 0),
458 * Perf per-CPU counters, backward compatibilty for names.
459 * Hidden from help listing.
461 PERF_HW("perf:cpu-cycles", CPU_CYCLES
,
462 CONTEXT_PERF_COUNTER
, 1),
463 PERF_HW("perf:cycles", CPU_CYCLES
,
464 CONTEXT_PERF_COUNTER
, 1),
465 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
,
466 CONTEXT_PERF_COUNTER
, 1),
467 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
,
468 CONTEXT_PERF_COUNTER
, 1),
469 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND
,
470 CONTEXT_PERF_COUNTER
, 1),
471 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND
,
472 CONTEXT_PERF_COUNTER
, 1),
473 PERF_HW("perf:instructions", INSTRUCTIONS
,
474 CONTEXT_PERF_COUNTER
, 1),
475 PERF_HW("perf:cache-references", CACHE_REFERENCES
,
476 CONTEXT_PERF_COUNTER
, 1),
477 PERF_HW("perf:cache-misses", CACHE_MISSES
,
478 CONTEXT_PERF_COUNTER
, 1),
479 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS
,
480 CONTEXT_PERF_COUNTER
, 1),
481 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS
,
482 CONTEXT_PERF_COUNTER
, 1),
483 PERF_HW("perf:branch-misses", BRANCH_MISSES
,
484 CONTEXT_PERF_COUNTER
, 1),
485 PERF_HW("perf:bus-cycles", BUS_CYCLES
,
486 CONTEXT_PERF_COUNTER
, 1),
488 PERF_HW_CACHE("perf:L1-dcache", L1D
,
489 CONTEXT_PERF_COUNTER
, 1),
490 PERF_HW_CACHE("perf:L1-icache", L1I
,
491 CONTEXT_PERF_COUNTER
, 1),
492 PERF_HW_CACHE("perf:LLC", LL
,
493 CONTEXT_PERF_COUNTER
, 1),
494 PERF_HW_CACHE("perf:dTLB", DTLB
,
495 CONTEXT_PERF_COUNTER
, 1),
496 _PERF_HW_CACHE("perf:iTLB-loads", ITLB
,
497 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
498 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB
,
499 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
500 _PERF_HW_CACHE("perf:branch-loads", BPU
,
501 CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
502 _PERF_HW_CACHE("perf:branch-load-misses", BPU
,
503 CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
505 PERF_SW("perf:cpu-clock", CPU_CLOCK
,
506 CONTEXT_PERF_COUNTER
, 1),
507 PERF_SW("perf:task-clock", TASK_CLOCK
,
508 CONTEXT_PERF_COUNTER
, 1),
509 PERF_SW("perf:page-fault", PAGE_FAULTS
,
510 CONTEXT_PERF_COUNTER
, 1),
511 PERF_SW("perf:faults", PAGE_FAULTS
,
512 CONTEXT_PERF_COUNTER
, 1),
513 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ
,
514 CONTEXT_PERF_COUNTER
, 1),
515 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN
,
516 CONTEXT_PERF_COUNTER
, 1),
517 PERF_SW("perf:context-switches", CONTEXT_SWITCHES
,
518 CONTEXT_PERF_COUNTER
, 1),
519 PERF_SW("perf:cs", CONTEXT_SWITCHES
,
520 CONTEXT_PERF_COUNTER
, 1),
521 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS
,
522 CONTEXT_PERF_COUNTER
, 1),
523 PERF_SW("perf:migrations", CPU_MIGRATIONS
,
524 CONTEXT_PERF_COUNTER
, 1),
525 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS
,
526 CONTEXT_PERF_COUNTER
, 1),
527 PERF_SW("perf:emulation-faults", EMULATION_FAULTS
,
528 CONTEXT_PERF_COUNTER
, 1),
534 #undef _PERF_HW_CACHE
539 * Context type for command line option parsing.
542 struct ctx_opts
*opt
;
543 struct cds_list_head list
;
547 * List of context type. Use to enable multiple context on a single command
550 struct ctx_type_list
{
551 struct cds_list_head head
;
553 .head
= CDS_LIST_HEAD_INIT(ctx_type_list
.head
),
558 * Find context numerical value from string.
560 * Return -1 if not found.
562 static int find_ctx_type_idx(const char *opt
)
566 while (ctx_opts
[i
].symbol
!= NULL
) {
567 if (strcmp(opt
, ctx_opts
[i
].symbol
) == 0) {
580 enum lttng_domain_type
get_domain(void)
583 return LTTNG_DOMAIN_KERNEL
;
584 } else if (opt_userspace
) {
585 return LTTNG_DOMAIN_UST
;
586 } else if (opt_jul
) {
587 return LTTNG_DOMAIN_JUL
;
588 } else if (opt_log4j
) {
589 return LTTNG_DOMAIN_LOG4J
;
606 ret
= fileno(stdout
);
608 PERROR("Unable to retrieve fileno of stdout");
613 writer
= mi_lttng_writer_create(ret
, lttng_opt_mi
);
619 /* Open command element */
620 ret
= mi_lttng_writer_command_open(writer
,
621 mi_lttng_element_command_add_context
);
627 /* Open output element */
628 ret
= mi_lttng_writer_open_element(writer
,
629 mi_lttng_element_command_output
);
639 int mi_close(enum cmd_error_code success
)
648 /* Close output element */
649 ret
= mi_lttng_writer_close_element(writer
);
656 ret
= mi_lttng_writer_write_element_bool(writer
,
657 mi_lttng_element_command_success
, !success
);
663 /* Command element close */
664 ret
= mi_lttng_writer_command_close(writer
);
674 void populate_context(struct lttng_event_context
*context
,
675 const struct ctx_opts
*opt
)
679 context
->ctx
= (enum lttng_event_context_type
) opt
->ctx_type
;
680 switch (context
->ctx
) {
681 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
682 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
683 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
684 context
->u
.perf_counter
.type
= opt
->u
.perf
.type
;
685 context
->u
.perf_counter
.config
= opt
->u
.perf
.config
;
686 strncpy(context
->u
.perf_counter
.name
, opt
->symbol
,
687 LTTNG_SYMBOL_NAME_LEN
);
688 context
->u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
689 /* Replace : and - by _ */
690 while ((ptr
= strchr(context
->u
.perf_counter
.name
, '-')) != NULL
) {
693 while ((ptr
= strchr(context
->u
.perf_counter
.name
, ':')) != NULL
) {
697 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
698 context
->u
.app_ctx
.provider_name
=
699 opt
->u
.app_ctx
.provider_name
;
700 context
->u
.app_ctx
.ctx_name
=
701 opt
->u
.app_ctx
.ctx_name
;
709 * Pretty print context type.
712 int print_ctx_type(void)
718 struct lttng_event_context context
;
720 memset(&context
, 0, sizeof(context
));
729 /* Open a contexts element */
730 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
737 while (ctx_opts
[i
].symbol
!= NULL
) {
738 if (!ctx_opts
[i
].hide_help
) {
740 populate_context(&context
, &ctx_opts
[i
]);
741 ret
= mi_lttng_context(writer
, &context
, 1);
747 ret
= mi_lttng_writer_write_element_string(
749 mi_lttng_element_context_symbol
,
756 ret
= mi_lttng_writer_close_element(writer
);
762 fprintf(ofp
, "%s\n", ctx_opts
[i
].symbol
);
769 /* Close contexts element */
770 ret
= mi_lttng_writer_close_element(writer
);
777 ret
= mi_close((cmd_error_code
) ret
);
785 * Add context to channel or event.
787 static int add_context(char *session_name
)
789 int ret
= CMD_SUCCESS
, warn
= 0, success
= 0;
790 struct lttng_event_context context
;
791 struct lttng_domain dom
;
792 struct ctx_type
*type
;
794 memset(&context
, 0, sizeof(context
));
795 memset(&dom
, 0, sizeof(dom
));
797 dom
.type
= get_domain();
798 handle
= lttng_create_handle(session_name
, &dom
);
799 if (handle
== NULL
) {
805 /* Open a contexts element */
806 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
812 /* Iterate over all the context types given */
813 cds_list_for_each_entry(type
, &ctx_type_list
.head
, list
) {
814 DBG("Adding context...");
816 populate_context(&context
, type
->opt
);
819 /* We leave context open the update the success of the command */
820 ret
= mi_lttng_context(writer
, &context
, 1);
826 ret
= mi_lttng_writer_write_element_string(writer
,
827 mi_lttng_element_context_symbol
,
835 ret
= lttng_add_context(handle
, &context
, NULL
, opt_channel_name
);
837 ERR("%s: %s", type
->opt
->symbol
, lttng_strerror(ret
));
841 if (opt_channel_name
) {
842 MSG("%s context %s added to channel %s",
843 lttng_domain_type_str(dom
.type
),
847 MSG("%s context %s added to all channels",
848 lttng_domain_type_str(dom
.type
),
855 /* Is the single operation a success ? */
856 ret
= mi_lttng_writer_write_element_bool(writer
,
857 mi_lttng_element_success
, success
);
863 /* Close the context element */
864 ret
= mi_lttng_writer_close_element(writer
);
873 /* Close contexts element */
874 ret
= mi_lttng_writer_close_element(writer
);
883 lttng_destroy_handle(handle
);
886 * This means that at least one add_context failed and tells the user to
887 * look on stderr for error(s).
896 void destroy_ctx_type(struct ctx_type
*type
)
902 free(type
->opt
->symbol
);
909 struct ctx_type
*create_ctx_type(void)
911 struct ctx_type
*type
= zmalloc
<ctx_type
>();
914 PERROR("malloc ctx_type");
918 type
->opt
= new struct ctx_opts
;
920 PERROR("malloc ctx_type options");
921 destroy_ctx_type(type
);
930 int find_ctx_type_perf_raw(const char *ctx
, struct ctx_type
*type
)
934 char *tmp_list
, *cur_list
;
936 cur_list
= tmp_list
= strdup(ctx
);
938 PERROR("strdup temp list");
943 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
947 next
= strtok(cur_list
, ":");
954 if (strncmp(next
, "perf", 4) != 0) {
960 if (strncmp(next
, "cpu", 3) == 0) {
961 type
->opt
->ctx_type
= CONTEXT_PERF_CPU_COUNTER
;
962 } else if (strncmp(next
, "thread", 4) == 0) {
963 type
->opt
->ctx_type
= CONTEXT_PERF_THREAD_COUNTER
;
970 if (strncmp(next
, "raw", 3) != 0) {
979 if (strlen(next
) < 2 || next
[0] != 'r') {
980 ERR("Wrong perf raw mask format: expected rNNN");
985 type
->opt
->u
.perf
.config
= strtoll(next
+ 1, &endptr
, 16);
986 if (errno
!= 0 || !endptr
|| *endptr
) {
987 ERR("Wrong perf raw mask format: expected rNNN");
997 ERR("Too many ':' in perf raw format");
1004 if (field_pos
< 5) {
1005 ERR("Invalid perf counter specifier, expected a specifier of "
1006 "the form perf:cpu:raw:rNNN:<name> or "
1007 "perf:thread:raw:rNNN:<name>");
1021 struct ctx_type
*get_context_type(const char *ctx
)
1024 struct ctx_type
*type
= NULL
;
1025 const char app_ctx_prefix
[] = "$app.";
1026 char *provider_name
= NULL
, *ctx_name
= NULL
;
1027 size_t i
, len
, colon_pos
= 0, provider_name_len
, ctx_name_len
;
1033 type
= create_ctx_type();
1038 /* Check if ctx matches a known static context. */
1039 opt_index
= find_ctx_type_idx(ctx
);
1040 if (opt_index
>= 0) {
1041 *type
->opt
= ctx_opts
[opt_index
];
1042 type
->opt
->symbol
= strdup(ctx_opts
[opt_index
].symbol
);
1046 /* Check if ctx is a raw perf context. */
1047 ret
= find_ctx_type_perf_raw(ctx
, type
);
1049 type
->opt
->u
.perf
.type
= PERF_TYPE_RAW
;
1050 type
->opt
->symbol
= strdup(ctx
);
1051 if (!type
->opt
->symbol
) {
1052 PERROR("Copy perf field name");
1059 * No match found against static contexts; check if it is an app
1063 if (len
<= sizeof(app_ctx_prefix
) - 1) {
1067 /* String starts with $app. */
1068 if (strncmp(ctx
, app_ctx_prefix
, sizeof(app_ctx_prefix
) - 1)) {
1072 /* Validate that the ':' separator is present. */
1073 for (i
= sizeof(app_ctx_prefix
); i
< len
; i
++) {
1074 const char c
= ctx
[i
];
1083 * No colon found or no ctx name ("$app.provider:") or no provider name
1084 * given ("$app.:..."), which is invalid.
1086 if (!colon_pos
|| colon_pos
== len
||
1087 colon_pos
== sizeof(app_ctx_prefix
)) {
1088 ERR("Invalid application context provided: no provider or context name provided.");
1092 provider_name_len
= colon_pos
- sizeof(app_ctx_prefix
) + 2;
1093 provider_name
= calloc
<char>(provider_name_len
);
1094 if (!provider_name
) {
1095 PERROR("malloc provider_name");
1098 strncpy(provider_name
, ctx
+ sizeof(app_ctx_prefix
) - 1,
1099 provider_name_len
- 1);
1100 type
->opt
->u
.app_ctx
.provider_name
= provider_name
;
1102 ctx_name_len
= len
- colon_pos
;
1103 ctx_name
= calloc
<char>(ctx_name_len
);
1105 PERROR("malloc ctx_name");
1108 strncpy(ctx_name
, ctx
+ colon_pos
+ 1, ctx_name_len
- 1);
1109 type
->opt
->u
.app_ctx
.ctx_name
= ctx_name
;
1110 type
->opt
->ctx_type
= CONTEXT_APP_CONTEXT
;
1111 type
->opt
->symbol
= strdup(ctx
);
1115 free(provider_name
);
1117 destroy_ctx_type(type
);
1122 * Add context to channel or event.
1124 int cmd_add_context(int argc
, const char **argv
)
1126 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
1127 static poptContext pc
;
1128 struct ctx_type
*type
, *tmptype
;
1129 char *session_name
= NULL
;
1130 const char *leftover
= NULL
;
1137 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1138 poptReadDefaultConfig(pc
, 0);
1140 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1146 ret
= print_ctx_type();
1150 type
= get_context_type(opt_type
);
1152 ERR("Unknown context type %s", opt_type
);
1156 cds_list_add_tail(&type
->list
, &ctx_type_list
.head
);
1168 case OPT_LIST_OPTIONS
:
1169 list_cmd_options(stdout
, long_options
);
1172 ret
= CMD_UNDEFINED
;
1177 leftover
= poptGetArg(pc
);
1179 ERR("Unknown argument: %s", leftover
);
1184 ret
= print_missing_or_multiple_domains(
1185 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
, true);
1192 ERR("Missing mandatory -t TYPE");
1197 if (!opt_session_name
) {
1198 session_name
= get_session_name();
1199 if (session_name
== NULL
) {
1204 session_name
= opt_session_name
;
1212 command_ret
= add_context(session_name
);
1213 ret
= mi_close((cmd_error_code
) command_ret
);
1219 if (!opt_session_name
) {
1224 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1225 /* Preserve original error code */
1226 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1229 /* Cleanup allocated memory */
1230 cds_list_for_each_entry_safe(type
, tmptype
, &ctx_type_list
.head
, list
) {
1231 destroy_ctx_type(type
);
1234 /* Overwrite ret if an error occurred during add_context() */
1235 ret
= command_ret
? command_ret
: ret
;
1237 poptFreeContext(pc
);