2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
31 #include <common/mi-lttng.h>
33 #include "../command.h"
35 static int opt_event_type
;
36 static int opt_kernel
;
37 static int opt_userspace
;
52 static struct lttng_handle
*handle
;
53 static struct mi_writer
*writer
;
55 static struct poptOption long_options
[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
58 {"kernel", 'k', POPT_ARG_NONE
, 0, OPT_KERNEL
, 0, 0},
59 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
60 {"function", 0, POPT_ARG_NONE
, 0, OPT_FUNCTION
, 0, 0},
61 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
68 static void usage(FILE *ofp
)
70 fprintf(ofp
, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
72 fprintf(ofp
, "Options:\n");
73 fprintf(ofp
, " -h, --help Show this help\n");
74 fprintf(ofp
, " --list-options Simple listing of options\n");
75 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
76 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
78 fprintf(ofp
, "Calibrate options:\n");
79 fprintf(ofp
, " --function Dynamic function entry/return probe (default)\n");
86 * Returns a CMD_* error.
88 static int calibrate_lttng(void)
90 int ret
= CMD_SUCCESS
;
91 struct lttng_domain dom
;
92 struct lttng_calibrate calibrate
;
94 memset(&dom
, 0, sizeof(dom
));
95 memset(&calibrate
, 0, sizeof(calibrate
));
97 /* Create lttng domain */
99 dom
.type
= LTTNG_DOMAIN_KERNEL
;
100 } else if (opt_userspace
) {
101 dom
.type
= LTTNG_DOMAIN_UST
;
103 /* Checked by the caller. */
107 handle
= lttng_create_handle(NULL
, &dom
);
108 if (handle
== NULL
) {
113 switch (opt_event_type
) {
114 case LTTNG_EVENT_TRACEPOINT
:
115 DBG("Calibrating kernel tracepoints");
117 case LTTNG_EVENT_PROBE
:
118 DBG("Calibrating kernel probes");
120 case LTTNG_EVENT_FUNCTION
:
121 DBG("Calibrating kernel functions");
122 calibrate
.type
= LTTNG_CALIBRATE_FUNCTION
;
123 ret
= lttng_calibrate(handle
, &calibrate
);
125 ERR("%s", lttng_strerror(ret
));
128 MSG("%s calibration done", opt_kernel
? "Kernel" : "UST");
130 case LTTNG_EVENT_FUNCTION_ENTRY
:
131 DBG("Calibrating kernel function entry");
133 case LTTNG_EVENT_SYSCALL
:
134 DBG("Calibrating kernel syscall");
143 ret
= mi_lttng_calibrate(writer
, &calibrate
);
151 lttng_destroy_handle(handle
);
157 * Calibrate LTTng tracer.
159 * Returns a CMD_* error.
161 int cmd_calibrate(int argc
, const char **argv
)
163 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
164 static poptContext pc
;
166 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
167 poptReadDefaultConfig(pc
, 0);
169 /* Default event type */
170 opt_event_type
= LTTNG_EVENT_FUNCTION
;
172 while ((opt
= poptGetNextOpt(pc
)) != -1) {
187 opt_event_type
= LTTNG_EVENT_FUNCTION
;
189 case OPT_FUNCTION_ENTRY
:
201 case OPT_LIST_OPTIONS
:
202 list_cmd_options(stdout
, long_options
);
211 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
219 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
221 ret
= -LTTNG_ERR_NOMEM
;
225 /* Open command element */
226 ret
= mi_lttng_writer_command_open(writer
,
227 mi_lttng_element_command_calibrate
);
233 /* Open output element */
234 ret
= mi_lttng_writer_open_element(writer
,
235 mi_lttng_element_command_output
);
242 command_ret
= calibrate_lttng();
249 /* Close output element */
250 ret
= mi_lttng_writer_close_element(writer
);
257 ret
= mi_lttng_writer_write_element_bool(writer
,
258 mi_lttng_element_command_success
, success
);
264 /* Command element close */
265 ret
= mi_lttng_writer_command_close(writer
);
274 if (writer
&& mi_lttng_writer_destroy(writer
)) {
275 /* Preserve original error code */
276 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
279 /* Overwrite ret if an error occurred during calibrate_lttng() */
280 ret
= command_ret
? command_ret
: ret
;