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 * Returns a CMD_* error.
70 static int calibrate_lttng(void)
72 int ret
= CMD_SUCCESS
;
73 struct lttng_domain dom
;
74 struct lttng_calibrate calibrate
;
76 memset(&dom
, 0, sizeof(dom
));
77 memset(&calibrate
, 0, sizeof(calibrate
));
79 /* Create lttng domain */
81 dom
.type
= LTTNG_DOMAIN_KERNEL
;
82 } else if (opt_userspace
) {
83 dom
.type
= LTTNG_DOMAIN_UST
;
85 /* Checked by the caller. */
89 handle
= lttng_create_handle(NULL
, &dom
);
95 switch (opt_event_type
) {
96 case LTTNG_EVENT_TRACEPOINT
:
97 DBG("Calibrating kernel tracepoints");
99 case LTTNG_EVENT_PROBE
:
100 DBG("Calibrating kernel probes");
102 case LTTNG_EVENT_FUNCTION
:
103 DBG("Calibrating kernel functions");
104 calibrate
.type
= LTTNG_CALIBRATE_FUNCTION
;
105 ret
= lttng_calibrate(handle
, &calibrate
);
107 ERR("%s", lttng_strerror(ret
));
110 MSG("%s calibration done", opt_kernel
? "Kernel" : "UST");
112 case LTTNG_EVENT_FUNCTION_ENTRY
:
113 DBG("Calibrating kernel function entry");
115 case LTTNG_EVENT_SYSCALL
:
116 DBG("Calibrating kernel syscall");
125 ret
= mi_lttng_calibrate(writer
, &calibrate
);
133 lttng_destroy_handle(handle
);
139 * Calibrate LTTng tracer.
141 * Returns a CMD_* error.
143 int cmd_calibrate(int argc
, const char **argv
)
145 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
146 static poptContext pc
;
148 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
149 poptReadDefaultConfig(pc
, 0);
151 /* Default event type */
152 opt_event_type
= LTTNG_EVENT_FUNCTION
;
154 while ((opt
= poptGetNextOpt(pc
)) != -1) {
169 opt_event_type
= LTTNG_EVENT_FUNCTION
;
171 case OPT_FUNCTION_ENTRY
:
183 case OPT_LIST_OPTIONS
:
184 list_cmd_options(stdout
, long_options
);
192 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
);
200 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
202 ret
= -LTTNG_ERR_NOMEM
;
206 /* Open command element */
207 ret
= mi_lttng_writer_command_open(writer
,
208 mi_lttng_element_command_calibrate
);
214 /* Open output element */
215 ret
= mi_lttng_writer_open_element(writer
,
216 mi_lttng_element_command_output
);
223 command_ret
= calibrate_lttng();
230 /* Close output element */
231 ret
= mi_lttng_writer_close_element(writer
);
238 ret
= mi_lttng_writer_write_element_bool(writer
,
239 mi_lttng_element_command_success
, success
);
245 /* Command element close */
246 ret
= mi_lttng_writer_command_close(writer
);
255 if (writer
&& mi_lttng_writer_destroy(writer
)) {
256 /* Preserve original error code */
257 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
260 /* Overwrite ret if an error occurred during calibrate_lttng() */
261 ret
= command_ret
? command_ret
: ret
;