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
10 #include "../command.hpp"
12 #include <common/mi-lttng.hpp>
14 #include <lttng/domain-internal.hpp>
22 #include <sys/types.h>
24 #include <urcu/list.h>
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
, nullptr, OPT_HELP
, nullptr, nullptr },
172 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
173 { "channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, nullptr, nullptr },
174 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
175 { "userspace", 'u', POPT_ARG_NONE
, nullptr, OPT_USERSPACE
, nullptr, nullptr },
176 { "jul", 'j', POPT_ARG_NONE
, nullptr, OPT_JUL
, nullptr, nullptr },
177 { "log4j", 'l', POPT_ARG_NONE
, nullptr, OPT_LOG4J
, nullptr, nullptr },
178 { "type", 't', POPT_ARG_STRING
, &opt_type
, OPT_TYPE
, nullptr, nullptr },
179 { "list", 0, POPT_ARG_NONE
, nullptr, OPT_LIST
, nullptr, nullptr },
180 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
181 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
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) \
199 optstr, type, PERF_COUNT_HW_CACHE_##name, PERF_COUNT_HW_CACHE_OP_##op, \
200 PERF_COUNT_HW_CACHE_RESULT_##result, hide, \
203 #define PERF_HW_CACHE(optstr, name, type, hide) \
204 _PERF_HW_CACHE(optstr "-loads", name, type, READ, ACCESS, hide), \
205 _PERF_HW_CACHE(optstr "-load-misses", name, type, READ, MISS, hide), \
206 _PERF_HW_CACHE(optstr "-stores", name, type, WRITE, ACCESS, hide), \
207 _PERF_HW_CACHE(optstr "-store-misses", name, type, WRITE, MISS, hide), \
208 _PERF_HW_CACHE(optstr "-prefetches", name, type, PREFETCH, ACCESS, hide), \
209 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, PREFETCH, MISS, hide)
212 const struct ctx_opts
{
213 /* Needed for end-of-list item. */
214 ctx_opts() : ctx_opts(nullptr, CONTEXT_UNKNOWN
)
218 ctx_opts(const char *symbol_
, context_type ctx_type_
, bool hide_help_
= false) :
219 symbol((char *) symbol_
), ctx_type(ctx_type_
), hide_help(hide_help_
)
223 ctx_opts(const char *symbol_
,
224 context_type ctx_type_
,
225 perf_count_hard perf_count_hard
,
227 ctx_opts(symbol_
, ctx_type_
, hide_help_
)
229 u
.perf
.type
= PERF_TYPE_HARDWARE
;
230 u
.perf
.config
= perf_count_hard
;
233 ctx_opts(const char *symbol_
,
234 context_type ctx_type_
,
235 perf_count_soft perf_count_soft
,
237 ctx_opts(symbol_
, ctx_type_
, hide_help_
)
239 u
.perf
.type
= PERF_TYPE_SOFTWARE
;
240 u
.perf
.config
= perf_count_soft
;
243 ctx_opts(const char *symbol_
,
244 context_type ctx_type_
,
245 perf_hw_cache_id perf_hw_cache_id
,
246 perf_hw_cache_op_id perf_hw_cache_op_id
,
247 perf_hw_cache_op_result_id perf_hw_cache_op_result_id
,
249 ctx_opts(symbol_
, ctx_type_
, hide_help_
)
251 u
.perf
.type
= PERF_TYPE_HW_CACHE
;
252 u
.perf
.config
= perf_hw_cache_id
| perf_hw_cache_op_id
<< 8 |
253 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
, CONTEXT_PERF_CPU_COUNTER
, 0),
319 PERF_HW("perf:cpu:cycles", CPU_CYCLES
, CONTEXT_PERF_CPU_COUNTER
, 0),
320 PERF_HW("perf:cpu:stalled-cycles-frontend",
321 STALLED_CYCLES_FRONTEND
,
322 CONTEXT_PERF_CPU_COUNTER
,
324 PERF_HW("perf:cpu:idle-cycles-frontend",
325 STALLED_CYCLES_FRONTEND
,
326 CONTEXT_PERF_CPU_COUNTER
,
328 PERF_HW("perf:cpu:stalled-cycles-backend",
329 STALLED_CYCLES_BACKEND
,
330 CONTEXT_PERF_CPU_COUNTER
,
332 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND
, CONTEXT_PERF_CPU_COUNTER
, 0),
333 PERF_HW("perf:cpu:instructions", INSTRUCTIONS
, CONTEXT_PERF_CPU_COUNTER
, 0),
334 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES
, CONTEXT_PERF_CPU_COUNTER
, 0),
335 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES
, CONTEXT_PERF_CPU_COUNTER
, 0),
336 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS
, CONTEXT_PERF_CPU_COUNTER
, 0),
337 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS
, CONTEXT_PERF_CPU_COUNTER
, 0),
338 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES
, CONTEXT_PERF_CPU_COUNTER
, 0),
339 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES
, CONTEXT_PERF_CPU_COUNTER
, 0),
341 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D
, CONTEXT_PERF_CPU_COUNTER
, 0),
342 PERF_HW_CACHE("perf:cpu:L1-icache", L1I
, CONTEXT_PERF_CPU_COUNTER
, 0),
343 PERF_HW_CACHE("perf:cpu:LLC", LL
, CONTEXT_PERF_CPU_COUNTER
, 0),
344 PERF_HW_CACHE("perf:cpu:dTLB", DTLB
, CONTEXT_PERF_CPU_COUNTER
, 0),
345 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB
, CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
346 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB
, CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
347 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU
, CONTEXT_PERF_CPU_COUNTER
, READ
, ACCESS
, 0),
348 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU
, CONTEXT_PERF_CPU_COUNTER
, READ
, MISS
, 0),
350 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK
, CONTEXT_PERF_CPU_COUNTER
, 0),
351 PERF_SW("perf:cpu:task-clock", TASK_CLOCK
, CONTEXT_PERF_CPU_COUNTER
, 0),
352 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS
, CONTEXT_PERF_CPU_COUNTER
, 0),
353 PERF_SW("perf:cpu:faults", PAGE_FAULTS
, CONTEXT_PERF_CPU_COUNTER
, 0),
354 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ
, CONTEXT_PERF_CPU_COUNTER
, 0),
355 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN
, CONTEXT_PERF_CPU_COUNTER
, 0),
356 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES
, CONTEXT_PERF_CPU_COUNTER
, 0),
357 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES
, CONTEXT_PERF_CPU_COUNTER
, 0),
358 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS
, CONTEXT_PERF_CPU_COUNTER
, 0),
359 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS
, CONTEXT_PERF_CPU_COUNTER
, 0),
360 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS
, CONTEXT_PERF_CPU_COUNTER
, 0),
361 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS
, CONTEXT_PERF_CPU_COUNTER
, 0),
363 /* Perf per-thread counters */
364 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
365 PERF_HW("perf:thread:cycles", CPU_CYCLES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
366 PERF_HW("perf:thread:stalled-cycles-frontend",
367 STALLED_CYCLES_FRONTEND
,
368 CONTEXT_PERF_THREAD_COUNTER
,
370 PERF_HW("perf:thread:idle-cycles-frontend",
371 STALLED_CYCLES_FRONTEND
,
372 CONTEXT_PERF_THREAD_COUNTER
,
374 PERF_HW("perf:thread:stalled-cycles-backend",
375 STALLED_CYCLES_BACKEND
,
376 CONTEXT_PERF_THREAD_COUNTER
,
378 PERF_HW("perf:thread:idle-cycles-backend",
379 STALLED_CYCLES_BACKEND
,
380 CONTEXT_PERF_THREAD_COUNTER
,
382 PERF_HW("perf:thread:instructions", INSTRUCTIONS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
383 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
384 PERF_HW("perf:thread:cache-misses", CACHE_MISSES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
385 PERF_HW("perf:thread:branch-instructions",
387 CONTEXT_PERF_THREAD_COUNTER
,
389 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
390 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
391 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
393 PERF_HW_CACHE("perf:thread:L1-dcache", L1D
, CONTEXT_PERF_THREAD_COUNTER
, 0),
394 PERF_HW_CACHE("perf:thread:L1-icache", L1I
, CONTEXT_PERF_THREAD_COUNTER
, 0),
395 PERF_HW_CACHE("perf:thread:LLC", LL
, CONTEXT_PERF_THREAD_COUNTER
, 0),
396 PERF_HW_CACHE("perf:thread:dTLB", DTLB
, CONTEXT_PERF_THREAD_COUNTER
, 0),
397 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB
, CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
399 "perf:thread:iTLB-load-misses", ITLB
, CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
401 "perf:thread:branch-loads", BPU
, CONTEXT_PERF_THREAD_COUNTER
, READ
, ACCESS
, 0),
403 "perf:thread:branch-load-misses", BPU
, CONTEXT_PERF_THREAD_COUNTER
, READ
, MISS
, 0),
405 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK
, CONTEXT_PERF_THREAD_COUNTER
, 0),
406 PERF_SW("perf:thread:task-clock", TASK_CLOCK
, CONTEXT_PERF_THREAD_COUNTER
, 0),
407 PERF_SW("perf:thread:page-fault", PAGE_FAULTS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
408 PERF_SW("perf:thread:faults", PAGE_FAULTS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
409 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ
, CONTEXT_PERF_THREAD_COUNTER
, 0),
410 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN
, CONTEXT_PERF_THREAD_COUNTER
, 0),
411 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
412 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES
, CONTEXT_PERF_THREAD_COUNTER
, 0),
413 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
414 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
415 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
416 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS
, CONTEXT_PERF_THREAD_COUNTER
, 0),
419 * Perf per-CPU counters, backward compatibilty for names.
420 * Hidden from help listing.
422 PERF_HW("perf:cpu-cycles", CPU_CYCLES
, CONTEXT_PERF_COUNTER
, 1),
423 PERF_HW("perf:cycles", CPU_CYCLES
, CONTEXT_PERF_COUNTER
, 1),
424 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND
, CONTEXT_PERF_COUNTER
, 1),
425 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND
, CONTEXT_PERF_COUNTER
, 1),
426 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND
, CONTEXT_PERF_COUNTER
, 1),
427 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND
, CONTEXT_PERF_COUNTER
, 1),
428 PERF_HW("perf:instructions", INSTRUCTIONS
, CONTEXT_PERF_COUNTER
, 1),
429 PERF_HW("perf:cache-references", CACHE_REFERENCES
, CONTEXT_PERF_COUNTER
, 1),
430 PERF_HW("perf:cache-misses", CACHE_MISSES
, CONTEXT_PERF_COUNTER
, 1),
431 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS
, CONTEXT_PERF_COUNTER
, 1),
432 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS
, CONTEXT_PERF_COUNTER
, 1),
433 PERF_HW("perf:branch-misses", BRANCH_MISSES
, CONTEXT_PERF_COUNTER
, 1),
434 PERF_HW("perf:bus-cycles", BUS_CYCLES
, CONTEXT_PERF_COUNTER
, 1),
436 PERF_HW_CACHE("perf:L1-dcache", L1D
, CONTEXT_PERF_COUNTER
, 1),
437 PERF_HW_CACHE("perf:L1-icache", L1I
, CONTEXT_PERF_COUNTER
, 1),
438 PERF_HW_CACHE("perf:LLC", LL
, CONTEXT_PERF_COUNTER
, 1),
439 PERF_HW_CACHE("perf:dTLB", DTLB
, CONTEXT_PERF_COUNTER
, 1),
440 _PERF_HW_CACHE("perf:iTLB-loads", ITLB
, CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
441 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB
, CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
442 _PERF_HW_CACHE("perf:branch-loads", BPU
, CONTEXT_PERF_COUNTER
, READ
, ACCESS
, 1),
443 _PERF_HW_CACHE("perf:branch-load-misses", BPU
, CONTEXT_PERF_COUNTER
, READ
, MISS
, 1),
445 PERF_SW("perf:cpu-clock", CPU_CLOCK
, CONTEXT_PERF_COUNTER
, 1),
446 PERF_SW("perf:task-clock", TASK_CLOCK
, CONTEXT_PERF_COUNTER
, 1),
447 PERF_SW("perf:page-fault", PAGE_FAULTS
, CONTEXT_PERF_COUNTER
, 1),
448 PERF_SW("perf:faults", PAGE_FAULTS
, CONTEXT_PERF_COUNTER
, 1),
449 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ
, CONTEXT_PERF_COUNTER
, 1),
450 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN
, CONTEXT_PERF_COUNTER
, 1),
451 PERF_SW("perf:context-switches", CONTEXT_SWITCHES
, CONTEXT_PERF_COUNTER
, 1),
452 PERF_SW("perf:cs", CONTEXT_SWITCHES
, CONTEXT_PERF_COUNTER
, 1),
453 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS
, CONTEXT_PERF_COUNTER
, 1),
454 PERF_SW("perf:migrations", CPU_MIGRATIONS
, CONTEXT_PERF_COUNTER
, 1),
455 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS
, CONTEXT_PERF_COUNTER
, 1),
456 PERF_SW("perf:emulation-faults", EMULATION_FAULTS
, CONTEXT_PERF_COUNTER
, 1),
462 #undef _PERF_HW_CACHE
467 * Context type for command line option parsing.
470 struct ctx_opts
*opt
;
471 struct cds_list_head list
;
475 * List of context type. Use to enable multiple context on a single command
478 struct ctx_type_list
{
479 struct cds_list_head head
;
481 .head
= CDS_LIST_HEAD_INIT(ctx_type_list
.head
),
486 * Find context numerical value from string.
488 * Return -1 if not found.
490 static int find_ctx_type_idx(const char *opt
)
494 while (ctx_opts
[i
].symbol
!= nullptr) {
495 if (strcmp(opt
, ctx_opts
[i
].symbol
) == 0) {
507 static enum lttng_domain_type
get_domain()
510 return LTTNG_DOMAIN_KERNEL
;
511 } else if (opt_userspace
) {
512 return LTTNG_DOMAIN_UST
;
513 } else if (opt_jul
) {
514 return LTTNG_DOMAIN_JUL
;
515 } else if (opt_log4j
) {
516 return LTTNG_DOMAIN_LOG4J
;
532 ret
= fileno(stdout
);
534 PERROR("Unable to retrieve fileno of stdout");
539 writer
= mi_lttng_writer_create(ret
, lttng_opt_mi
);
545 /* Open command element */
546 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_add_context
);
552 /* Open output element */
553 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
562 static int mi_close(enum cmd_error_code success
)
571 /* Close output element */
572 ret
= mi_lttng_writer_close_element(writer
);
579 ret
= mi_lttng_writer_write_element_bool(
580 writer
, mi_lttng_element_command_success
, !success
);
586 /* Command element close */
587 ret
= mi_lttng_writer_command_close(writer
);
596 static void populate_context(struct lttng_event_context
*context
, const struct ctx_opts
*opt
)
600 context
->ctx
= (enum lttng_event_context_type
) opt
->ctx_type
;
601 switch (context
->ctx
) {
602 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
603 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
604 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
:
605 context
->u
.perf_counter
.type
= opt
->u
.perf
.type
;
606 context
->u
.perf_counter
.config
= opt
->u
.perf
.config
;
607 strncpy(context
->u
.perf_counter
.name
, opt
->symbol
, LTTNG_SYMBOL_NAME_LEN
);
608 context
->u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
609 /* Replace : and - by _ */
610 while ((ptr
= strchr(context
->u
.perf_counter
.name
, '-')) != nullptr) {
613 while ((ptr
= strchr(context
->u
.perf_counter
.name
, ':')) != nullptr) {
617 case LTTNG_EVENT_CONTEXT_APP_CONTEXT
:
618 context
->u
.app_ctx
.provider_name
= opt
->u
.app_ctx
.provider_name
;
619 context
->u
.app_ctx
.ctx_name
= opt
->u
.app_ctx
.ctx_name
;
627 * Pretty print context type.
629 static int print_ctx_type()
634 struct lttng_event_context context
;
636 memset(&context
, 0, sizeof(context
));
645 /* Open a contexts element */
646 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
653 while (ctx_opts
[i
].symbol
!= nullptr) {
654 if (!ctx_opts
[i
].hide_help
) {
656 populate_context(&context
, &ctx_opts
[i
]);
657 ret
= mi_lttng_context(writer
, &context
, 1);
663 ret
= mi_lttng_writer_write_element_string(
665 mi_lttng_element_context_symbol
,
672 ret
= mi_lttng_writer_close_element(writer
);
678 fprintf(ofp
, "%s\n", ctx_opts
[i
].symbol
);
685 /* Close contexts element */
686 ret
= mi_lttng_writer_close_element(writer
);
693 ret
= mi_close((cmd_error_code
) ret
);
701 * Add context to channel or event.
703 static int add_context(char *session_name
)
705 int ret
= CMD_SUCCESS
, warn
= 0, success
= 0;
706 struct lttng_event_context context
;
707 struct lttng_domain dom
;
708 struct ctx_type
*type
;
710 memset(&context
, 0, sizeof(context
));
711 memset(&dom
, 0, sizeof(dom
));
713 dom
.type
= get_domain();
714 handle
= lttng_create_handle(session_name
, &dom
);
715 if (handle
== nullptr) {
721 /* Open a contexts element */
722 ret
= mi_lttng_writer_open_element(writer
, config_element_contexts
);
728 /* Iterate over all the context types given */
729 cds_list_for_each_entry (type
, &ctx_type_list
.head
, list
) {
730 DBG("Adding context...");
732 populate_context(&context
, type
->opt
);
735 /* We leave context open the update the success of the command */
736 ret
= mi_lttng_context(writer
, &context
, 1);
742 ret
= mi_lttng_writer_write_element_string(
743 writer
, mi_lttng_element_context_symbol
, type
->opt
->symbol
);
750 ret
= lttng_add_context(handle
, &context
, nullptr, opt_channel_name
);
752 ERR("%s: %s", type
->opt
->symbol
, lttng_strerror(ret
));
756 if (opt_channel_name
) {
757 MSG("%s context %s added to channel %s",
758 lttng_domain_type_str(dom
.type
),
762 MSG("%s context %s added to all channels",
763 lttng_domain_type_str(dom
.type
),
770 /* Is the single operation a success ? */
771 ret
= mi_lttng_writer_write_element_bool(
772 writer
, mi_lttng_element_success
, success
);
778 /* Close the context element */
779 ret
= mi_lttng_writer_close_element(writer
);
788 /* Close contexts element */
789 ret
= mi_lttng_writer_close_element(writer
);
798 lttng_destroy_handle(handle
);
801 * This means that at least one add_context failed and tells the user to
802 * look on stderr for error(s).
810 static void destroy_ctx_type(struct ctx_type
*type
)
817 free(type
->opt
->symbol
);
819 if (type
->opt
->ctx_type
== CONTEXT_APP_CONTEXT
) {
820 free(type
->opt
->u
.app_ctx
.ctx_name
);
821 free(type
->opt
->u
.app_ctx
.provider_name
);
829 static struct ctx_type
*create_ctx_type()
831 struct ctx_type
*type
= zmalloc
<ctx_type
>();
834 PERROR("malloc ctx_type");
838 type
->opt
= new struct ctx_opts
;
840 PERROR("malloc ctx_type options");
841 destroy_ctx_type(type
);
849 static int find_ctx_type_perf_raw(const char *ctx
, struct ctx_type
*type
)
853 char *tmp_list
, *cur_list
;
855 cur_list
= tmp_list
= strdup(ctx
);
857 PERROR("strdup temp list");
862 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
866 next
= strtok(cur_list
, ":");
873 if (strncmp(next
, "perf", 4) != 0) {
879 if (strncmp(next
, "cpu", 3) == 0) {
880 type
->opt
->ctx_type
= CONTEXT_PERF_CPU_COUNTER
;
881 } else if (strncmp(next
, "thread", 4) == 0) {
882 type
->opt
->ctx_type
= CONTEXT_PERF_THREAD_COUNTER
;
889 if (strncmp(next
, "raw", 3) != 0) {
898 if (strlen(next
) < 2 || next
[0] != 'r') {
899 ERR("Wrong perf raw mask format: expected rNNN");
904 type
->opt
->u
.perf
.config
= strtoll(next
+ 1, &endptr
, 16);
905 if (errno
!= 0 || !endptr
|| *endptr
) {
906 ERR("Wrong perf raw mask format: expected rNNN");
916 ERR("Too many ':' in perf raw format");
924 ERR("Invalid perf counter specifier, expected a specifier of "
925 "the form perf:cpu:raw:rNNN:<name> or "
926 "perf:thread:raw:rNNN:<name>");
939 static struct ctx_type
*get_context_type(const char *ctx
)
942 struct ctx_type
*type
= nullptr;
943 const char app_ctx_prefix
[] = "$app.";
944 char *provider_name
= nullptr, *ctx_name
= nullptr;
945 size_t i
, len
, colon_pos
= 0, provider_name_len
, ctx_name_len
;
951 type
= create_ctx_type();
956 /* Check if ctx matches a known static context. */
957 opt_index
= find_ctx_type_idx(ctx
);
958 if (opt_index
>= 0) {
959 *type
->opt
= ctx_opts
[opt_index
];
960 type
->opt
->symbol
= strdup(ctx_opts
[opt_index
].symbol
);
964 /* Check if ctx is a raw perf context. */
965 ret
= find_ctx_type_perf_raw(ctx
, type
);
967 type
->opt
->u
.perf
.type
= PERF_TYPE_RAW
;
968 type
->opt
->symbol
= strdup(ctx
);
969 if (!type
->opt
->symbol
) {
970 PERROR("Copy perf field name");
977 * No match found against static contexts; check if it is an app
981 if (len
<= sizeof(app_ctx_prefix
) - 1) {
985 /* String starts with $app. */
986 if (strncmp(ctx
, app_ctx_prefix
, sizeof(app_ctx_prefix
) - 1) != 0) {
990 /* Validate that the ':' separator is present. */
991 for (i
= sizeof(app_ctx_prefix
); i
< len
; i
++) {
992 const char c
= ctx
[i
];
1001 * No colon found or no ctx name ("$app.provider:") or no provider name
1002 * given ("$app.:..."), which is invalid.
1004 if (!colon_pos
|| colon_pos
== len
|| colon_pos
== sizeof(app_ctx_prefix
)) {
1005 ERR("Invalid application context provided: no provider or context name provided.");
1009 provider_name_len
= colon_pos
- sizeof(app_ctx_prefix
) + 2;
1010 provider_name
= calloc
<char>(provider_name_len
);
1011 if (!provider_name
) {
1012 PERROR("malloc provider_name");
1015 strncpy(provider_name
, ctx
+ sizeof(app_ctx_prefix
) - 1, provider_name_len
- 1);
1016 type
->opt
->u
.app_ctx
.provider_name
= provider_name
;
1018 ctx_name_len
= len
- colon_pos
;
1019 ctx_name
= calloc
<char>(ctx_name_len
);
1021 PERROR("malloc ctx_name");
1024 strncpy(ctx_name
, ctx
+ colon_pos
+ 1, ctx_name_len
- 1);
1025 type
->opt
->u
.app_ctx
.ctx_name
= ctx_name
;
1026 type
->opt
->ctx_type
= CONTEXT_APP_CONTEXT
;
1027 type
->opt
->symbol
= strdup(ctx
);
1031 free(provider_name
);
1033 destroy_ctx_type(type
);
1038 * Add context to channel or event.
1040 int cmd_add_context(int argc
, const char **argv
)
1042 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
1043 static poptContext pc
;
1044 struct ctx_type
*type
, *tmptype
;
1045 char *session_name
= nullptr;
1046 const char *leftover
= nullptr;
1053 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
1054 poptReadDefaultConfig(pc
, 0);
1056 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1062 ret
= print_ctx_type();
1066 type
= get_context_type(opt_type
);
1068 ERR("Unknown context type %s", opt_type
);
1072 cds_list_add_tail(&type
->list
, &ctx_type_list
.head
);
1084 case OPT_LIST_OPTIONS
:
1085 list_cmd_options(stdout
, long_options
);
1088 ret
= CMD_UNDEFINED
;
1093 leftover
= poptGetArg(pc
);
1095 ERR("Unknown argument: %s", leftover
);
1100 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
,
1108 ERR("Missing mandatory -t TYPE");
1113 if (!opt_session_name
) {
1114 session_name
= get_session_name();
1115 if (session_name
== nullptr) {
1120 session_name
= opt_session_name
;
1128 command_ret
= add_context(session_name
);
1129 ret
= mi_close((cmd_error_code
) command_ret
);
1135 if (!opt_session_name
) {
1140 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1141 /* Preserve original error code */
1142 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1145 /* Cleanup allocated memory */
1146 cds_list_for_each_entry_safe (type
, tmptype
, &ctx_type_list
.head
, list
) {
1147 destroy_ctx_type(type
);
1150 /* Overwrite ret if an error occurred during add_context() */
1151 ret
= command_ret
? command_ret
: ret
;
1153 poptFreeContext(pc
);