Prettify channel listing
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <signal.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30
31 #include <common/error.h>
32 #include <common/utils.h>
33 #include <common/defaults.h>
34
35 #include "conf.h"
36 #include "utils.h"
37 #include "command.h"
38
39 static const char *str_kernel = "Kernel";
40 static const char *str_ust = "UST";
41 static const char *str_jul = "JUL";
42 static const char *str_log4j = "LOG4J";
43 static const char *str_python = "Python";
44
45 static
46 char *_get_session_name(int quiet)
47 {
48 char *path, *session_name = NULL;
49
50 /* Get path to config file */
51 path = utils_get_home_dir();
52 if (path == NULL) {
53 goto error;
54 }
55
56 /* Get session name from config */
57 session_name = quiet ? config_read_session_name_quiet(path) :
58 config_read_session_name(path);
59 if (session_name == NULL) {
60 goto error;
61 }
62
63 DBG2("Config file path found: %s", path);
64 DBG("Session name found: %s", session_name);
65 return session_name;
66
67 error:
68 return NULL;
69 }
70
71 /*
72 * get_session_name
73 *
74 * Return allocated string with the session name found in the config
75 * directory.
76 */
77 char *get_session_name(void)
78 {
79 return _get_session_name(0);
80 }
81
82 /*
83 * get_session_name_quiet (no warnings/errors emitted)
84 *
85 * Return allocated string with the session name found in the config
86 * directory.
87 */
88 char *get_session_name_quiet(void)
89 {
90 return _get_session_name(1);
91 }
92
93 /*
94 * list_commands
95 *
96 * List commands line by line. This is mostly for bash auto completion and to
97 * avoid difficult parsing.
98 */
99 void list_commands(struct cmd_struct *commands, FILE *ofp)
100 {
101 int i = 0;
102 struct cmd_struct *cmd = NULL;
103
104 cmd = &commands[i];
105 while (cmd->name != NULL) {
106 fprintf(ofp, "%s\n", cmd->name);
107 i++;
108 cmd = &commands[i];
109 }
110 }
111
112 /*
113 * list_cmd_options
114 *
115 * Prints a simple list of the options available to a command. This is intended
116 * to be easily parsed for bash completion.
117 */
118 void list_cmd_options(FILE *ofp, struct poptOption *options)
119 {
120 int i;
121 struct poptOption *option = NULL;
122
123 for (i = 0; options[i].longName != NULL; i++) {
124 option = &options[i];
125
126 fprintf(ofp, "--%s\n", option->longName);
127
128 if (isprint(option->shortName)) {
129 fprintf(ofp, "-%c\n", option->shortName);
130 }
131 }
132 }
133
134 /*
135 * fls: returns the position of the most significant bit.
136 * Returns 0 if no bit is set, else returns the position of the most
137 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
138 */
139 #if defined(__i386) || defined(__x86_64)
140 static inline
141 unsigned int fls_u32(uint32_t x)
142 {
143 int r;
144
145 asm("bsrl %1,%0\n\t"
146 "jnz 1f\n\t"
147 "movl $-1,%0\n\t"
148 "1:\n\t"
149 : "=r" (r) : "rm" (x));
150 return r + 1;
151 }
152 #define HAS_FLS_U32
153 #endif
154
155 #if defined(__x86_64)
156 static inline
157 unsigned int fls_u64(uint64_t x)
158 {
159 long r;
160
161 asm("bsrq %1,%0\n\t"
162 "jnz 1f\n\t"
163 "movq $-1,%0\n\t"
164 "1:\n\t"
165 : "=r" (r) : "rm" (x));
166 return r + 1;
167 }
168 #define HAS_FLS_U64
169 #endif
170
171 #ifndef HAS_FLS_U64
172 static __attribute__((unused))
173 unsigned int fls_u64(uint64_t x)
174 {
175 unsigned int r = 64;
176
177 if (!x)
178 return 0;
179
180 if (!(x & 0xFFFFFFFF00000000ULL)) {
181 x <<= 32;
182 r -= 32;
183 }
184 if (!(x & 0xFFFF000000000000ULL)) {
185 x <<= 16;
186 r -= 16;
187 }
188 if (!(x & 0xFF00000000000000ULL)) {
189 x <<= 8;
190 r -= 8;
191 }
192 if (!(x & 0xF000000000000000ULL)) {
193 x <<= 4;
194 r -= 4;
195 }
196 if (!(x & 0xC000000000000000ULL)) {
197 x <<= 2;
198 r -= 2;
199 }
200 if (!(x & 0x8000000000000000ULL)) {
201 x <<= 1;
202 r -= 1;
203 }
204 return r;
205 }
206 #endif
207
208 #ifndef HAS_FLS_U32
209 static __attribute__((unused))
210 unsigned int fls_u32(uint32_t x)
211 {
212 unsigned int r = 32;
213
214 if (!x)
215 return 0;
216 if (!(x & 0xFFFF0000U)) {
217 x <<= 16;
218 r -= 16;
219 }
220 if (!(x & 0xFF000000U)) {
221 x <<= 8;
222 r -= 8;
223 }
224 if (!(x & 0xF0000000U)) {
225 x <<= 4;
226 r -= 4;
227 }
228 if (!(x & 0xC0000000U)) {
229 x <<= 2;
230 r -= 2;
231 }
232 if (!(x & 0x80000000U)) {
233 x <<= 1;
234 r -= 1;
235 }
236 return r;
237 }
238 #endif
239
240 static
241 unsigned int fls_ulong(unsigned long x)
242 {
243 #if (CAA_BITS_PER_LONG == 32)
244 return fls_u32(x);
245 #else
246 return fls_u64(x);
247 #endif
248 }
249
250 /*
251 * Return the minimum order for which x <= (1UL << order).
252 * Return -1 if x is 0.
253 */
254 int get_count_order_u32(uint32_t x)
255 {
256 if (!x)
257 return -1;
258
259 return fls_u32(x - 1);
260 }
261
262 /*
263 * Return the minimum order for which x <= (1UL << order).
264 * Return -1 if x is 0.
265 */
266 int get_count_order_u64(uint64_t x)
267 {
268 if (!x)
269 return -1;
270
271 return fls_u64(x - 1);
272 }
273
274 /*
275 * Return the minimum order for which x <= (1UL << order).
276 * Return -1 if x is 0.
277 */
278 int get_count_order_ulong(unsigned long x)
279 {
280 if (!x)
281 return -1;
282
283 return fls_ulong(x - 1);
284 }
285
286 const char *get_domain_str(enum lttng_domain_type domain)
287 {
288 const char *str_dom;
289
290 switch (domain) {
291 case LTTNG_DOMAIN_KERNEL:
292 str_dom = str_kernel;
293 break;
294 case LTTNG_DOMAIN_UST:
295 str_dom = str_ust;
296 break;
297 case LTTNG_DOMAIN_JUL:
298 str_dom = str_jul;
299 break;
300 case LTTNG_DOMAIN_LOG4J:
301 str_dom = str_log4j;
302 break;
303 case LTTNG_DOMAIN_PYTHON:
304 str_dom = str_python;
305 break;
306 default:
307 /* Should not have an unknown domain or else define it. */
308 assert(0);
309 }
310
311 return str_dom;
312 }
313
314 /*
315 * Spawn a lttng relayd daemon by forking and execv.
316 */
317 int spawn_relayd(const char *pathname, int port)
318 {
319 int ret = 0;
320 pid_t pid;
321 char url[255];
322
323 if (!port) {
324 port = DEFAULT_NETWORK_VIEWER_PORT;
325 }
326
327 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
328 if (ret < 0) {
329 goto end;
330 }
331
332 MSG("Spawning a relayd daemon");
333 pid = fork();
334 if (pid == 0) {
335 /*
336 * Spawn session daemon and tell
337 * it to signal us when ready.
338 */
339 execlp(pathname, "lttng-relayd", "-L", url, NULL);
340 /* execlp only returns if error happened */
341 if (errno == ENOENT) {
342 ERR("No relayd found. Use --relayd-path.");
343 } else {
344 PERROR("execlp");
345 }
346 kill(getppid(), SIGTERM); /* wake parent */
347 exit(EXIT_FAILURE);
348 } else if (pid > 0) {
349 goto end;
350 } else {
351 PERROR("fork");
352 ret = -1;
353 goto end;
354 }
355
356 end:
357 return ret;
358 }
359
360 /*
361 * Check if relayd is alive.
362 *
363 * Return 1 if found else 0 if NOT found. Negative value on error.
364 */
365 int check_relayd(void)
366 {
367 int ret, fd;
368 struct sockaddr_in sin;
369
370 fd = socket(AF_INET, SOCK_STREAM, 0);
371 if (fd < 0) {
372 PERROR("socket check relayd");
373 ret = -1;
374 goto error_socket;
375 }
376
377 sin.sin_family = AF_INET;
378 sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT);
379 ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
380 if (ret < 1) {
381 PERROR("inet_pton check relayd");
382 ret = -1;
383 goto error;
384 }
385
386 /*
387 * A successful connect means the relayd exists thus returning 0 else a
388 * negative value means it does NOT exists.
389 */
390 ret = connect(fd, &sin, sizeof(sin));
391 if (ret < 0) {
392 /* Not found. */
393 ret = 0;
394 } else {
395 /* Already spawned. */
396 ret = 1;
397 }
398
399 error:
400 if (close(fd) < 0) {
401 PERROR("close relayd fd");
402 }
403 error_socket:
404 return ret;
405 }
406
407 int print_missing_or_multiple_domains(unsigned int sum)
408 {
409 int ret = 0;
410
411 if (sum == 0) {
412 ERR("Please specify a domain (-k/-u/-j).");
413 ret = -1;
414 } else if (sum > 1) {
415 ERR("Multiple domains specified.");
416 ret = -1;
417 }
418
419 return ret;
420 }
421
422 /*
423 * Get the discarded events and lost packet counts.
424 */
425 void print_session_stats(const char *session_name)
426 {
427 int count, nb_domains, domain_idx, channel_idx, session_idx;
428 struct lttng_domain *domains;
429 struct lttng_channel *channels;
430 uint64_t discarded_events_total = 0, lost_packets_total = 0;
431 struct lttng_session *sessions = NULL;
432 const struct lttng_session *selected_session = NULL;
433
434 count = lttng_list_sessions(&sessions);
435 if (count < 1) {
436 ERR("Failed to retrieve session descriptions while printing session statistics.");
437 goto end;
438 }
439
440 /* Identify the currently-selected sessions. */
441 for (session_idx = 0; session_idx < count; session_idx++) {
442 if (!strcmp(session_name, sessions[session_idx].name)) {
443 selected_session = &sessions[session_idx];
444 break;
445 }
446 }
447 if (!selected_session) {
448 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
449 goto end;
450 }
451
452 nb_domains = lttng_list_domains(session_name, &domains);
453 if (nb_domains < 0) {
454 goto end;
455 }
456 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
457 struct lttng_handle *handle = lttng_create_handle(session_name,
458 &domains[domain_idx]);
459
460 if (!handle) {
461 ERR("Failed to create session handle while printing session statistics.");
462 goto end;
463 }
464
465 count = lttng_list_channels(handle, &channels);
466 for (channel_idx = 0; channel_idx < count; channel_idx++) {
467 int ret;
468 uint64_t discarded_events = 0, lost_packets = 0;
469 struct lttng_channel *channel = &channels[channel_idx];
470
471 ret = lttng_channel_get_discarded_event_count(channel,
472 &discarded_events);
473 if (ret) {
474 ERR("Failed to retrieve discarded event count from channel %s",
475 channel->name);
476 }
477
478 ret = lttng_channel_get_lost_packet_count(channel,
479 &lost_packets);
480 if (ret) {
481 ERR("Failed to retrieve lost packet count from channel %s",
482 channel->name);
483 }
484
485 discarded_events_total += discarded_events;
486 lost_packets_total += lost_packets;
487 }
488 lttng_destroy_handle(handle);
489 }
490 if (discarded_events_total > 0 && !selected_session->snapshot_mode) {
491 MSG("[warning] %" PRIu64 " events discarded, please refer to "
492 "the documentation on channel configuration.",
493 discarded_events_total);
494 }
495 if (lost_packets_total > 0 && !selected_session->snapshot_mode) {
496 MSG("[warning] %" PRIu64 " packets lost, please refer to "
497 "the documentation on channel configuration.",
498 lost_packets_total);
499 }
500
501 end:
502 free(sessions);
503 }
504
505 int show_cmd_help(const char *cmd_name, const char *help_msg)
506 {
507 int ret;
508 char page_name[32];
509
510 ret = sprintf(page_name, "lttng-%s", cmd_name);
511 assert(ret > 0 && ret < 32);
512 ret = utils_show_help(1, page_name, help_msg);
513 if (ret && !help_msg) {
514 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
515 perror("exec");
516 }
517
518 return ret;
519 }
This page took 0.040447 seconds and 4 git commands to generate.