2 * ust-multi-test.c - single-proces, multi-session, multi-channel, multi-event UST tracing
4 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; only version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place - Suite 330, Boston, MA 02111-1307, USA.
21 #define _LARGEFILE64_SOURCE
30 #include <sys/types.h>
32 #include <sys/resource.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
40 #include <sys/socket.h>
43 #include <../../libringbuffer/backend.h>
44 #include <../../libringbuffer/frontend.h>
45 #include "../../liblttng-ust/compat.h" /* For ENODATA */
49 #define MAX_NR_STREAMS 64
52 const char *evname
[] = {
53 "ust_tests_hello_tptest",
54 "ust_tests_hello_tptest_sighandler",
55 "ust_tests_hello_dontexist",
58 static int session_handle
[NR_SESSIONS
];
59 static struct lttng_ust_object_data metadata_stream_data
[NR_SESSIONS
];
60 static struct lttng_ust_object_data metadata_data
[NR_SESSIONS
];
61 static struct lttng_ust_object_data channel_data
[NR_SESSIONS
][NR_CHANNELS
];
62 static struct lttng_ust_object_data stream_data
[NR_SESSIONS
][NR_CHANNELS
][MAX_NR_STREAMS
];
63 static int event_handle
[NR_SESSIONS
][NR_CHANNELS
][NR_EVENTS
];
65 static int apps_socket
= -1;
66 static char apps_sock_path
[PATH_MAX
];
67 static char local_apps_wait_shm_path
[PATH_MAX
];
69 static volatile int quit_program
;
71 static void handle_signals(int signo
)
77 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
83 struct ustcomm_ust_msg lum
;
84 struct ustcomm_ust_reply lur
;
86 memset(&lum
, 0, sizeof(lum
));
87 lum
.handle
= channel_handle
;
88 lum
.cmd
= LTTNG_UST_STREAM
;
89 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
92 stream_datas
[k
].handle
= lur
.ret_val
;
93 printf("received stream handle %u\n",
94 stream_datas
[k
].handle
);
95 if (lur
.ret_code
== USTCOMM_OK
) {
98 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
100 len
= ustcomm_recv_fd(sock
);
103 stream_datas
[k
].shm_fd
= len
;
105 len
= ustcomm_recv_fd(sock
);
108 stream_datas
[k
].wait_fd
= len
;
121 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
125 for (k
= 0; k
< nr_check
; k
++) {
126 struct ustcomm_ust_msg lum
;
127 struct ustcomm_ust_reply lur
;
129 if (!stream_datas
[k
].handle
)
131 memset(&lum
, 0, sizeof(lum
));
132 lum
.handle
= stream_datas
[k
].handle
;
133 lum
.cmd
= LTTNG_UST_RELEASE
;
134 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
136 printf("Error closing stream\n");
139 if (stream_datas
[k
].shm_fd
>= 0) {
140 ret
= close(stream_datas
[k
].shm_fd
);
142 printf("Error closing stream shm_fd\n");
146 if (stream_datas
[k
].wait_fd
>= 0) {
147 ret
= close(stream_datas
[k
].wait_fd
);
149 printf("Error closing stream wait_fd\n");
158 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
159 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
161 struct lttng_ust_shm_handle
*handle
;
162 struct channel
*chan
;
165 /* map metadata channel */
166 handle
= channel_handle_create(chan_data
->shm_fd
,
168 chan_data
->memory_map_size
);
170 printf("create handle error\n");
173 chan_data
->shm_fd
= -1;
174 chan_data
->wait_fd
= -1;
175 chan
= shmp(handle
, handle
->chan
);
177 for (k
= 0; k
< nr_check
; k
++) {
178 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
180 if (!stream_data
->handle
)
183 ret
= channel_handle_add_stream(handle
,
185 stream_data
->wait_fd
,
186 stream_data
->memory_map_size
);
188 printf("add stream error\n");
191 stream_data
->shm_fd
= -1;
192 stream_data
->wait_fd
= -1;
197 channel_destroy(chan
, handle
, 1);
202 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
204 struct channel
*chan
;
206 chan
= shmp(handle
, handle
->chan
);
208 channel_destroy(chan
, handle
, 1);
212 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
214 struct channel
*chan
;
215 struct lttng_ust_lib_ring_buffer
*buf
;
217 int *shm_fd
, *wait_fd
;
218 uint64_t *memory_map_size
;
220 chan
= shmp(handle
, handle
->chan
);
223 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
224 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
227 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
233 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
234 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
236 perror("open output");
240 printf("Waiting for buffer data for %s\n", outfile
);
242 unsigned long read_size
;
243 unsigned long copy_size
;
246 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
247 printf("get next ret %d\n", ret
);
250 if (ret
== -EAGAIN
) {
255 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
258 read_size
= lib_ring_buffer_get_read_data_size(
259 &chan
->backend
.config
, buf
, handle
);
260 read_size
= PAGE_ALIGN(read_size
);
261 ptr
= lib_ring_buffer_read_offset_address(
262 &buf
->backend
, 0, handle
);
263 printf("WRITE: copy %lu bytes\n", read_size
);
264 copy_size
= write(outfd
, ptr
, read_size
);
265 if (copy_size
< read_size
) {
266 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
268 lib_ring_buffer_put_next_subbuf(buf
, handle
);
278 lib_ring_buffer_release_read(buf
, handle
, 1);
283 int consume_buffers(void)
288 for (i
= 0; i
< NR_SESSIONS
; i
++) {
289 char pathname
[PATH_MAX
];
290 struct lttng_ust_shm_handle
*handle
;
292 snprintf(pathname
, PATH_MAX
- 1, "/tmp/testtrace%u", i
);
293 old_umask
= umask(0);
294 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
295 if (ret
&& errno
!= EEXIST
) {
303 handle
= map_channel(&metadata_data
[i
],
304 &metadata_stream_data
[i
], 1);
307 snprintf(pathname
, PATH_MAX
- 1,
308 "/tmp/testtrace%u/metadata", i
);
309 ret
= consume_stream(handle
, -1, pathname
);
310 if (ret
&& ret
!= -ENOENT
) {
311 printf("Error in consume_stream\n");
314 unmap_channel(handle
);
316 /* copy binary data */
317 for (j
= 0; j
< NR_CHANNELS
; j
++) {
318 handle
= map_channel(&channel_data
[i
][j
],
319 stream_data
[i
][j
], MAX_NR_STREAMS
);
322 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
323 snprintf(pathname
, PATH_MAX
- 1,
324 "/tmp/testtrace%u/data_%u", i
, k
);
325 ret
= consume_stream(handle
, k
, pathname
);
326 if (ret
&& ret
!= -ENOENT
) {
327 printf("Error in consume_stream\n");
331 unmap_channel(handle
);
338 int send_app_msgs(int sock
)
340 struct ustcomm_ust_msg lum
;
341 struct ustcomm_ust_reply lur
;
344 for (i
= 0; i
< NR_SESSIONS
; i
++) {
346 memset(&lum
, 0, sizeof(lum
));
347 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
348 lum
.cmd
= LTTNG_UST_SESSION
;
349 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
352 session_handle
[i
] = lur
.ret_val
;
353 printf("received session handle %u\n", session_handle
[i
]);
355 /* Create metadata channel */
356 memset(&lum
, 0, sizeof(lum
));
357 lum
.handle
= session_handle
[i
];
358 lum
.cmd
= LTTNG_UST_METADATA
;
359 lum
.u
.channel
.overwrite
= 0;
360 lum
.u
.channel
.subbuf_size
= 32768;
361 lum
.u
.channel
.num_subbuf
= 4;
362 lum
.u
.channel
.switch_timer_interval
= 0;
363 lum
.u
.channel
.read_timer_interval
= 0;
364 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
365 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
368 metadata_data
[i
].handle
= lur
.ret_val
;
369 printf("received metadata handle %u\n", metadata_data
[i
].handle
);
370 if (lur
.ret_code
== USTCOMM_OK
) {
373 metadata_data
[i
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
375 len
= ustcomm_recv_fd(sock
);
378 metadata_data
[i
].shm_fd
= len
;
380 len
= ustcomm_recv_fd(sock
);
383 metadata_data
[i
].wait_fd
= len
;
386 ret
= open_streams(sock
, metadata_data
[i
].handle
,
387 &metadata_stream_data
[i
], 1);
389 printf("Error in open_streams\n");
393 /* Create channels */
394 for (j
= 0; j
< NR_CHANNELS
; j
++) {
395 memset(&lum
, 0, sizeof(lum
));
396 lum
.handle
= session_handle
[i
];
397 lum
.cmd
= LTTNG_UST_CHANNEL
;
398 //lum.u.channel.overwrite = 0;
399 lum
.u
.channel
.overwrite
= 1;
400 lum
.u
.channel
.subbuf_size
= 32768;
401 lum
.u
.channel
.num_subbuf
= 8;
402 //lum.u.channel.num_subbuf = 4;
403 //lum.u.channel.num_subbuf = 2;
404 lum
.u
.channel
.switch_timer_interval
= 0;
405 lum
.u
.channel
.read_timer_interval
= 0;
406 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
407 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
410 channel_data
[i
][j
].handle
= lur
.ret_val
;
411 printf("received channel handle %u\n", channel_data
[i
][j
].handle
);
412 if (lur
.ret_code
== USTCOMM_OK
) {
415 channel_data
[i
][j
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
417 len
= ustcomm_recv_fd(sock
);
420 channel_data
[i
][j
].shm_fd
= len
;
422 len
= ustcomm_recv_fd(sock
);
425 channel_data
[i
][j
].wait_fd
= len
;
429 for (k
= 0; k
< NR_EVENTS
; k
++) {
430 memset(&lum
, 0, sizeof(lum
));
431 lum
.handle
= channel_data
[i
][j
].handle
;
432 lum
.cmd
= LTTNG_UST_EVENT
;
433 strncpy(lum
.u
.event
.name
, evname
[k
],
434 LTTNG_UST_SYM_NAME_LEN
);
435 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
436 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
439 event_handle
[i
][j
][k
] = lur
.ret_val
;
440 printf("received event handle %u\n", event_handle
[i
][j
][k
]);
443 /* Get references to channel streams */
444 ret
= open_streams(sock
, channel_data
[i
][j
].handle
,
445 stream_data
[i
][j
], MAX_NR_STREAMS
);
447 printf("Error in open_streams\n");
452 memset(&lum
, 0, sizeof(lum
));
453 lum
.handle
= session_handle
[i
];
454 lum
.cmd
= LTTNG_UST_SESSION_START
;
455 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
458 printf("Session handle %u started.\n", session_handle
[i
]);
461 /* Tell application registration is done */
462 memset(&lum
, 0, sizeof(lum
));
463 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
464 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
465 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
468 printf("Registration done acknowledged.\n");
472 ret
= consume_buffers();
474 printf("Error in consume_buffers\n");
478 for (i
= 0; i
< NR_SESSIONS
; i
++) {
479 /* Release channels */
480 for (j
= 0; j
< NR_CHANNELS
; j
++) {
481 /* Release streams */
482 ret
= close_streams(sock
, stream_data
[i
][j
],
488 for (k
= 0; k
< NR_EVENTS
; k
++) {
489 memset(&lum
, 0, sizeof(lum
));
490 lum
.handle
= event_handle
[i
][j
][k
];
491 lum
.cmd
= LTTNG_UST_RELEASE
;
492 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
496 memset(&lum
, 0, sizeof(lum
));
497 lum
.handle
= channel_data
[i
][j
].handle
;
498 lum
.cmd
= LTTNG_UST_RELEASE
;
499 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
502 if (channel_data
[i
][j
].shm_fd
>= 0) {
503 ret
= close(channel_data
[i
][j
].shm_fd
);
507 if (channel_data
[i
][j
].wait_fd
>= 0) {
508 ret
= close(channel_data
[i
][j
].wait_fd
);
514 /* Release metadata channel */
515 ret
= close_streams(sock
, &metadata_stream_data
[i
], 1);
519 memset(&lum
, 0, sizeof(lum
));
520 lum
.handle
= metadata_data
[i
].handle
;
521 lum
.cmd
= LTTNG_UST_RELEASE
;
522 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
525 if (metadata_data
[i
].shm_fd
>= 0) {
526 ret
= close(metadata_data
[i
].shm_fd
);
530 if (metadata_data
[i
].wait_fd
>= 0) {
531 ret
= close(metadata_data
[i
].wait_fd
);
536 /* Release session */
537 memset(&lum
, 0, sizeof(lum
));
538 lum
.handle
= session_handle
[i
];
539 lum
.cmd
= LTTNG_UST_RELEASE
;
540 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
549 * Using fork to set umask in the child process (not multi-thread safe). We
550 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
551 * the shm and does not let everybody modify it, to ensure safety against
552 * shm_unlink) by simply letting the mmap fail and retrying after a few
553 * seconds. For global shm, everybody has rw access to it until the sessiond
556 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
558 int wait_shm_fd
, ret
;
561 /* Default permissions */
562 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
564 /* Change owner of the shm path */
566 ret
= chown(shm_path
, 0, 0);
568 if (errno
!= ENOENT
) {
569 perror("chown wait shm");
575 * If global session daemon, any application can register so the shm
576 * needs to be set in read-only mode for others.
580 ret
= chown(shm_path
, getuid(), getgid());
582 if (errno
!= ENOENT
) {
583 perror("chown wait shm");
590 * Set permissions to the shm even if we did not create the shm.
592 ret
= chmod(shm_path
, mode
);
594 if (errno
!= ENOENT
) {
595 perror("chmod wait shm");
601 * We're alone in a child process, so we can modify the process-wide
607 * Try creating shm (or get rw access). We don't do an exclusive open,
608 * because we allow other processes to create+ftruncate it concurrently.
610 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
611 if (wait_shm_fd
< 0) {
612 perror("shm_open wait shm");
616 ret
= ftruncate(wait_shm_fd
, mmap_size
);
618 perror("ftruncate wait shm");
622 ret
= fchmod(wait_shm_fd
, mode
);
628 printf("Got the wait shm fd %d\n", wait_shm_fd
);
633 printf("Failing to get the wait shm fd\n");
638 int update_futex(int fd
, int active
)
640 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
644 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
646 if (wait_shm_mmap
== MAP_FAILED
) {
652 uatomic_set((int32_t *) wait_shm_mmap
, 1);
653 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
654 INT_MAX
, NULL
, NULL
, 0);
656 uatomic_set((int32_t *) wait_shm_mmap
, 0);
658 ret
= munmap(wait_shm_mmap
, mmap_size
);
660 perror("Error unmapping wait shm");
669 * Set open files limit to unlimited. This daemon can open a large number of
670 * file descriptors in order to consumer multiple kernel traces.
672 static void set_ulimit(void)
678 * If not root, we cannot increase our max open files. But our
679 * scope is then limited to processes from a single user.
683 /* The kernel does not allowed an infinite limit for open files */
684 lim
.rlim_cur
= 65535;
685 lim
.rlim_max
= 65535;
687 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
689 perror("failed to set open files limit");
693 int main(int argc
, char **argv
)
695 const char *home_dir
;
696 int ret
, wait_shm_fd
;
697 struct sigaction act
;
698 mode_t old_umask
= 0;
703 memset(&act
, 0, sizeof(act
));
704 ret
= sigemptyset(&act
.sa_mask
);
706 perror("sigemptyset");
710 act
.sa_handler
= SIG_IGN
;
711 ret
= sigaction(SIGPIPE
, &act
, NULL
);
718 act
.sa_handler
= handle_signals
;
719 ret
= sigaction(SIGTERM
, &act
, NULL
);
725 ret
= sigaction(SIGINT
, &act
, NULL
);
731 if (geteuid() == 0) {
732 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
733 if (ret
&& errno
!= EEXIST
) {
737 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
738 sysconf(_SC_PAGE_SIZE
), 1);
739 if (wait_shm_fd
< 0) {
740 perror("global wait shm error");
743 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
744 old_umask
= umask(0);
746 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
747 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
748 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
749 sysconf(_SC_PAGE_SIZE
), 0);
750 if (wait_shm_fd
< 0) {
751 perror("local wait shm error");
754 home_dir
= (const char *) getenv("HOME");
756 perror("getenv error");
759 snprintf(apps_sock_path
, PATH_MAX
,
760 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
763 ret
= ustcomm_create_unix_sock(apps_sock_path
);
765 perror("create error");
771 /* File permission MUST be 666 */
772 ret
= chmod(apps_sock_path
,
773 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
775 printf("Set file permissions failed: %s\n", apps_sock_path
);
781 ret
= ustcomm_listen_unix_sock(apps_socket
);
783 perror("listen error");
787 /* wake up futexes */
788 ret
= update_futex(wait_shm_fd
, 1);
790 fprintf(stderr
, "Error wakeup futex\n");
804 char name
[16]; /* Process name */
811 printf("Accepting application registration\n");
812 sock
= ustcomm_accept_unix_sock(apps_socket
);
814 perror("accept error");
819 * Basic recv here to handle the very simple data
820 * that the libust send to register (reg_msg).
822 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
823 if (len
< 0 || len
!= sizeof(reg_msg
)) {
824 perror("ustcomm_recv_unix_sock");
827 memcpy(bufname
, reg_msg
.name
, 16);
829 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
830 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
831 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
832 ret
= send_app_msgs(sock
);
834 printf("Error in send_app_msgs.\n");
841 printf("quitting.\n");
842 /* Let applications know we are not responding anymore */
843 ret
= update_futex(wait_shm_fd
, 0);
845 fprintf(stderr
, "Error wakeup futex\n");