2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
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.
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.
26 #include <sys/types.h>
29 #include <urcu/list.h>
35 #define PRINT_LINE_LEN 80
37 static char *opt_event_name
;
38 static char *opt_channel_name
;
39 static char *opt_session_name
;
40 static int *opt_kernel
;
41 static int opt_pid_all
;
42 static int opt_userspace
;
51 * Taken from the LTTng ABI
55 CONTEXT_PERF_COUNTER
= 1,
67 * Taken from the Perf ABI (all enum perf_*)
70 PERF_TYPE_HARDWARE
= 0,
71 PERF_TYPE_SOFTWARE
= 1,
72 PERF_TYPE_HW_CACHE
= 3,
75 enum perf_count_hard
{
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,
87 enum 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,
100 * Generalized hardware cache events:
102 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
103 * { read, write, prefetch } x
104 * { accesses, misses }
106 enum 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,
114 PERF_COUNT_HW_CACHE_MAX
, /* non-ABI */
117 enum 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,
122 PERF_COUNT_HW_CACHE_OP_MAX
, /* non-ABI */
125 enum perf_hw_cache_op_result_id
{
126 PERF_COUNT_HW_CACHE_RESULT_ACCESS
= 0,
127 PERF_COUNT_HW_CACHE_RESULT_MISS
= 1,
129 PERF_COUNT_HW_CACHE_RESULT_MAX
, /* non-ABI */
132 static struct poptOption long_options
[] = {
133 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
134 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
135 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
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},
142 {"type", 't', POPT_ARG_STRING
, 0, OPT_TYPE
, 0, 0},
143 {0, 0, 0, 0, 0, 0, 0}
149 #define PERF_HW(opt, name) \
151 "perf:" #opt, CONTEXT_PERF_COUNTER, \
152 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
155 #define PERF_SW(opt, name) \
157 "perf:" #opt, CONTEXT_PERF_COUNTER, \
158 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
161 #define _PERF_HW_CACHE(optstr, name, op, result) \
163 "perf:" optstr, CONTEXT_PERF_COUNTER, \
165 PERF_TYPE_HW_CACHE, \
166 (uint64_t) PERF_COUNT_HW_CACHE_##name \
167 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
168 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
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) \
181 const struct ctx_opts
{
183 enum context_type ctx_type
;
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
},
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
),
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
),
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 */
244 * Context type for command line option parsing.
247 const struct ctx_opts
*opt
;
248 struct cds_list_head list
;
252 * List of context type. Use to enable multiple context on a single command
255 struct ctx_type_list
{
256 struct cds_list_head head
;
258 .head
= CDS_LIST_HEAD_INIT(ctx_type_list
.head
),
262 * Pretty print context type.
264 static void print_ctx_type(FILE *ofp
)
266 const char *indent
= " ";
267 int indent_len
= strlen(indent
);
270 fprintf(ofp
, "%s", indent
);
272 while (ctx_opts
[i
].symbol
!= NULL
) {
273 if (len
> indent_len
) {
274 if (len
+ strlen(ctx_opts
[i
].symbol
) + 2
277 fprintf(ofp
, "%s", indent
);
280 len
+= fprintf(ofp
, ", ");
283 len
+= fprintf(ofp
, "%s", ctx_opts
[i
].symbol
);
291 static void usage(FILE *ofp
)
293 fprintf(ofp
, "usage: lttng add-context -t TYPE\n");
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");
300 fprintf(ofp
, "Options:\n");
301 fprintf(ofp
, " -h, --help Show this help\n");
302 fprintf(ofp
, " -s, --session Apply on session name\n");
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");
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");
314 fprintf(ofp
, "Example:\n");
315 fprintf(ofp
, "This command will add the context information 'prio' and two perf\n"
316 "counters: hardware branch misses and cache misses, to all events\n"
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");
323 * Find context numerical value from string.
325 static int find_ctx_type_idx(const char *opt
)
329 while (ctx_opts
[i
].symbol
!= NULL
) {
330 if (strcmp(opt
, ctx_opts
[i
].symbol
) == 0) {
342 * Add context to channel or event.
344 static int add_context(void)
346 int ret
= CMD_SUCCESS
;
347 struct lttng_event_context context
;
348 struct lttng_domain dom
;
349 struct ctx_type
*type
;
352 /* Iterate over all context type given */
353 cds_list_for_each_entry(type
, &ctx_type_list
.head
, list
) {
354 /* Set session name for the current command */
355 if (set_session_name(opt_session_name
) < 0) {
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
;
364 strcpy(context
.u
.perf_counter
.name
, type
->opt
->symbol
);
365 /* Replace : and - by _ */
366 while ((ptr
= strchr(context
.u
.perf_counter
.name
, '-')) != NULL
) {
369 while ((ptr
= strchr(context
.u
.perf_counter
.name
, ':')) != NULL
) {
374 /* Create kernel domain */
375 dom
.type
= LTTNG_DOMAIN_KERNEL
;
377 DBG("Adding kernel context");
378 ret
= lttng_add_context(&dom
, &context
, opt_event_name
,
381 fprintf(stderr
, "%s: ", type
->opt
->symbol
);
384 MSG("Kernel context %s added", type
->opt
->symbol
);
386 } else if (opt_userspace
) { /* User-space tracer action */
388 * TODO: Waiting on lttng UST 2.0
391 } else if (opt_pid
!= 0) {
393 ret
= CMD_NOT_IMPLEMENTED
;
396 ERR("Please specify a tracer (--kernel or --userspace)");
406 * Add context on channel or event.
408 int cmd_add_context(int argc
, const char **argv
)
410 int index
, opt
, ret
= CMD_SUCCESS
;
412 static poptContext pc
;
413 struct ctx_type
*type
, *tmptype
;
420 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
421 poptReadDefaultConfig(pc
, 0);
423 while ((opt
= poptGetNextOpt(pc
)) != -1) {
430 /* Mandatory field */
431 tmp
= poptGetOptArg(pc
);
438 type
= malloc(sizeof(struct ctx_type
));
440 perror("malloc ctx_type");
444 index
= find_ctx_type_idx(tmp
);
446 ERR("Unknown context type %s", tmp
);
449 type
->opt
= &ctx_opts
[index
];
450 if (type
->opt
->ctx_type
== -1) {
451 ERR("Unknown context type %s", tmp
);
453 cds_list_add(&type
->list
, &ctx_type_list
.head
);
466 /* Cleanup allocated memory */
467 cds_list_for_each_entry_safe(type
, tmptype
, &ctx_type_list
.head
, list
) {