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
29 #include <sys/types.h>
34 #include <urcu/futex.h>
35 #include <urcu/uatomic.h>
37 #include <sys/socket.h>
40 #include <../../libringbuffer/backend.h>
41 #include <../../libringbuffer/frontend.h>
45 #define MAX_NR_STREAMS 64
48 const char *evname
[] = {
49 "ust_tests_hello_tptest",
50 "ust_tests_hello_tptest_sighandler",
51 "ust_tests_hello_dontexist",
54 static int session_handle
[NR_SESSIONS
];
55 static struct lttng_ust_object_data metadata_stream_data
[NR_SESSIONS
];
56 static struct lttng_ust_object_data metadata_data
[NR_SESSIONS
];
57 static struct lttng_ust_object_data channel_data
[NR_SESSIONS
][NR_CHANNELS
];
58 static struct lttng_ust_object_data stream_data
[NR_SESSIONS
][NR_CHANNELS
][MAX_NR_STREAMS
];
59 static int event_handle
[NR_SESSIONS
][NR_CHANNELS
][NR_EVENTS
];
61 static int apps_socket
= -1;
62 static char apps_sock_path
[PATH_MAX
];
63 static char local_apps_wait_shm_path
[PATH_MAX
];
65 static volatile int quit_program
;
67 static void handle_signals(int signo
)
73 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
79 struct ustcomm_ust_msg lum
;
80 struct ustcomm_ust_reply lur
;
82 memset(&lum
, 0, sizeof(lum
));
83 lum
.handle
= channel_handle
;
84 lum
.cmd
= LTTNG_UST_STREAM
;
85 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
88 stream_datas
[k
].handle
= lur
.ret_val
;
89 printf("received stream handle %u\n",
90 stream_datas
[k
].handle
);
91 if (lur
.ret_code
== USTCOMM_OK
) {
94 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
96 len
= ustcomm_recv_fd(sock
);
99 stream_datas
[k
].shm_fd
= len
;
101 len
= ustcomm_recv_fd(sock
);
104 stream_datas
[k
].wait_fd
= len
;
117 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
121 for (k
= 0; k
< nr_check
; k
++) {
122 struct ustcomm_ust_msg lum
;
123 struct ustcomm_ust_reply lur
;
125 if (!stream_datas
[k
].handle
)
127 memset(&lum
, 0, sizeof(lum
));
128 lum
.handle
= stream_datas
[k
].handle
;
129 lum
.cmd
= LTTNG_UST_RELEASE
;
130 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
132 printf("Error closing stream\n");
135 if (stream_datas
[k
].shm_fd
>= 0) {
136 ret
= close(stream_datas
[k
].shm_fd
);
138 printf("Error closing stream shm_fd\n");
142 if (stream_datas
[k
].wait_fd
>= 0) {
143 ret
= close(stream_datas
[k
].wait_fd
);
145 printf("Error closing stream wait_fd\n");
154 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
155 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
157 struct lttng_ust_shm_handle
*handle
;
158 struct channel
*chan
;
161 /* map metadata channel */
162 handle
= channel_handle_create(chan_data
->shm_fd
,
164 chan_data
->memory_map_size
);
166 printf("create handle error\n");
169 chan_data
->shm_fd
= -1;
170 chan_data
->wait_fd
= -1;
171 chan
= shmp(handle
, handle
->chan
);
173 for (k
= 0; k
< nr_check
; k
++) {
174 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
176 if (!stream_data
->handle
)
179 ret
= channel_handle_add_stream(handle
,
181 stream_data
->wait_fd
,
182 stream_data
->memory_map_size
);
184 printf("add stream error\n");
187 stream_data
->shm_fd
= -1;
188 stream_data
->wait_fd
= -1;
193 channel_destroy(chan
, handle
, 1);
198 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
200 struct channel
*chan
;
202 chan
= shmp(handle
, handle
->chan
);
204 channel_destroy(chan
, handle
, 1);
208 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
210 struct channel
*chan
;
211 struct lttng_ust_lib_ring_buffer
*buf
;
214 uint64_t memory_map_size
;
216 chan
= shmp(handle
, handle
->chan
);
219 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
220 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
223 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
229 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
230 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
232 perror("open output");
236 printf("Waiting for buffer data for %s\n", outfile
);
238 unsigned long read_size
;
239 unsigned long copy_size
;
242 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
243 printf("get next ret %d\n", ret
);
246 if (ret
== -EAGAIN
) {
251 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
254 read_size
= lib_ring_buffer_get_read_data_size(
255 &chan
->backend
.config
, buf
, handle
);
256 read_size
= PAGE_ALIGN(read_size
);
257 ptr
= lib_ring_buffer_read_offset_address(
258 &buf
->backend
, 0, handle
);
259 printf("WRITE: copy %lu bytes\n", read_size
);
260 copy_size
= write(outfd
, ptr
, read_size
);
261 if (copy_size
< read_size
) {
262 printf("write issue: copied %zd, expected %lu\n", copy_size
, read_size
);
264 lib_ring_buffer_put_next_subbuf(buf
, handle
);
274 lib_ring_buffer_release_read(buf
, handle
, 1);
279 int consume_buffers(void)
284 for (i
= 0; i
< NR_SESSIONS
; i
++) {
285 char pathname
[PATH_MAX
];
286 struct lttng_ust_shm_handle
*handle
;
288 snprintf(pathname
, PATH_MAX
- 1, "/tmp/testtrace%u", i
);
289 old_umask
= umask(0);
290 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
291 if (ret
&& errno
!= EEXIST
) {
299 handle
= map_channel(&metadata_data
[i
],
300 &metadata_stream_data
[i
], 1);
303 snprintf(pathname
, PATH_MAX
- 1,
304 "/tmp/testtrace%u/metadata", i
);
305 ret
= consume_stream(handle
, -1, pathname
);
306 if (ret
&& ret
!= -ENOENT
) {
307 printf("Error in consume_stream\n");
310 unmap_channel(handle
);
312 /* copy binary data */
313 for (j
= 0; j
< NR_CHANNELS
; j
++) {
314 handle
= map_channel(&channel_data
[i
][j
],
315 stream_data
[i
][j
], MAX_NR_STREAMS
);
318 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
319 snprintf(pathname
, PATH_MAX
- 1,
320 "/tmp/testtrace%u/data_%u", i
, k
);
321 ret
= consume_stream(handle
, k
, pathname
);
322 if (ret
&& ret
!= -ENOENT
) {
323 printf("Error in consume_stream\n");
327 unmap_channel(handle
);
334 int send_app_msgs(int sock
)
336 struct ustcomm_ust_msg lum
;
337 struct ustcomm_ust_reply lur
;
340 for (i
= 0; i
< NR_SESSIONS
; i
++) {
342 memset(&lum
, 0, sizeof(lum
));
343 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
344 lum
.cmd
= LTTNG_UST_SESSION
;
345 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
348 session_handle
[i
] = lur
.ret_val
;
349 printf("received session handle %u\n", session_handle
[i
]);
351 /* Create metadata channel */
352 memset(&lum
, 0, sizeof(lum
));
353 lum
.handle
= session_handle
[i
];
354 lum
.cmd
= LTTNG_UST_METADATA
;
355 lum
.u
.channel
.overwrite
= 0;
356 lum
.u
.channel
.subbuf_size
= 32768;
357 lum
.u
.channel
.num_subbuf
= 4;
358 lum
.u
.channel
.switch_timer_interval
= 0;
359 lum
.u
.channel
.read_timer_interval
= 0;
360 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
361 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
364 metadata_data
[i
].handle
= lur
.ret_val
;
365 printf("received metadata handle %u\n", metadata_data
[i
].handle
);
366 if (lur
.ret_code
== USTCOMM_OK
) {
369 metadata_data
[i
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
371 len
= ustcomm_recv_fd(sock
);
374 metadata_data
[i
].shm_fd
= len
;
376 len
= ustcomm_recv_fd(sock
);
379 metadata_data
[i
].wait_fd
= len
;
382 ret
= open_streams(sock
, metadata_data
[i
].handle
,
383 &metadata_stream_data
[i
], 1);
385 printf("Error in open_streams\n");
389 /* Create channels */
390 for (j
= 0; j
< NR_CHANNELS
; j
++) {
391 memset(&lum
, 0, sizeof(lum
));
392 lum
.handle
= session_handle
[i
];
393 lum
.cmd
= LTTNG_UST_CHANNEL
;
394 //lum.u.channel.overwrite = 0;
395 lum
.u
.channel
.overwrite
= 1;
396 lum
.u
.channel
.subbuf_size
= 32768;
397 lum
.u
.channel
.num_subbuf
= 8;
398 //lum.u.channel.num_subbuf = 4;
399 //lum.u.channel.num_subbuf = 2;
400 lum
.u
.channel
.switch_timer_interval
= 0;
401 lum
.u
.channel
.read_timer_interval
= 0;
402 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
403 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
406 channel_data
[i
][j
].handle
= lur
.ret_val
;
407 printf("received channel handle %u\n", channel_data
[i
][j
].handle
);
408 if (lur
.ret_code
== USTCOMM_OK
) {
411 channel_data
[i
][j
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
413 len
= ustcomm_recv_fd(sock
);
416 channel_data
[i
][j
].shm_fd
= len
;
418 len
= ustcomm_recv_fd(sock
);
421 channel_data
[i
][j
].wait_fd
= len
;
425 for (k
= 0; k
< NR_EVENTS
; k
++) {
426 memset(&lum
, 0, sizeof(lum
));
427 lum
.handle
= channel_data
[i
][j
].handle
;
428 lum
.cmd
= LTTNG_UST_EVENT
;
429 strncpy(lum
.u
.event
.name
, evname
[k
],
430 LTTNG_UST_SYM_NAME_LEN
);
431 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
432 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
435 event_handle
[i
][j
][k
] = lur
.ret_val
;
436 printf("received event handle %u\n", event_handle
[i
][j
][k
]);
439 /* Get references to channel streams */
440 ret
= open_streams(sock
, channel_data
[i
][j
].handle
,
441 stream_data
[i
][j
], MAX_NR_STREAMS
);
443 printf("Error in open_streams\n");
448 memset(&lum
, 0, sizeof(lum
));
449 lum
.handle
= session_handle
[i
];
450 lum
.cmd
= LTTNG_UST_SESSION_START
;
451 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
454 printf("Session handle %u started.\n", session_handle
[i
]);
457 /* Tell application registration is done */
458 memset(&lum
, 0, sizeof(lum
));
459 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
460 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
461 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
464 printf("Registration done acknowledged.\n");
468 ret
= consume_buffers();
470 printf("Error in consume_buffers\n");
474 for (i
= 0; i
< NR_SESSIONS
; i
++) {
475 /* Release channels */
476 for (j
= 0; j
< NR_CHANNELS
; j
++) {
477 /* Release streams */
478 ret
= close_streams(sock
, stream_data
[i
][j
],
484 for (k
= 0; k
< NR_EVENTS
; k
++) {
485 memset(&lum
, 0, sizeof(lum
));
486 lum
.handle
= event_handle
[i
][j
][k
];
487 lum
.cmd
= LTTNG_UST_RELEASE
;
488 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
492 memset(&lum
, 0, sizeof(lum
));
493 lum
.handle
= channel_data
[i
][j
].handle
;
494 lum
.cmd
= LTTNG_UST_RELEASE
;
495 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
498 if (channel_data
[i
][j
].shm_fd
>= 0) {
499 ret
= close(channel_data
[i
][j
].shm_fd
);
503 if (channel_data
[i
][j
].wait_fd
>= 0) {
504 ret
= close(channel_data
[i
][j
].wait_fd
);
510 /* Release metadata channel */
511 ret
= close_streams(sock
, &metadata_stream_data
[i
], 1);
515 memset(&lum
, 0, sizeof(lum
));
516 lum
.handle
= metadata_data
[i
].handle
;
517 lum
.cmd
= LTTNG_UST_RELEASE
;
518 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
521 if (metadata_data
[i
].shm_fd
>= 0) {
522 ret
= close(metadata_data
[i
].shm_fd
);
526 if (metadata_data
[i
].wait_fd
>= 0) {
527 ret
= close(metadata_data
[i
].wait_fd
);
532 /* Release session */
533 memset(&lum
, 0, sizeof(lum
));
534 lum
.handle
= session_handle
[i
];
535 lum
.cmd
= LTTNG_UST_RELEASE
;
536 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
545 * Using fork to set umask in the child process (not multi-thread safe). We
546 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
547 * the shm and does not let everybody modify it, to ensure safety against
548 * shm_unlink) by simply letting the mmap fail and retrying after a few
549 * seconds. For global shm, everybody has rw access to it until the sessiond
552 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
554 int wait_shm_fd
, ret
;
557 /* Default permissions */
558 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
560 /* Change owner of the shm path */
562 ret
= chown(shm_path
, 0, 0);
564 if (errno
!= ENOENT
) {
565 perror("chown wait shm");
571 * If global session daemon, any application can register so the shm
572 * needs to be set in read-only mode for others.
576 ret
= chown(shm_path
, getuid(), getgid());
578 if (errno
!= ENOENT
) {
579 perror("chown wait shm");
586 * Set permissions to the shm even if we did not create the shm.
588 ret
= chmod(shm_path
, mode
);
590 if (errno
!= ENOENT
) {
591 perror("chmod wait shm");
597 * We're alone in a child process, so we can modify the process-wide
603 * Try creating shm (or get rw access). We don't do an exclusive open,
604 * because we allow other processes to create+ftruncate it concurrently.
606 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
607 if (wait_shm_fd
< 0) {
608 perror("shm_open wait shm");
612 ret
= ftruncate(wait_shm_fd
, mmap_size
);
614 perror("ftruncate wait shm");
618 ret
= fchmod(wait_shm_fd
, mode
);
624 printf("Got the wait shm fd %d\n", wait_shm_fd
);
629 printf("Failing to get the wait shm fd\n");
634 int update_futex(int fd
, int active
)
636 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
640 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
642 if (wait_shm_mmap
== MAP_FAILED
) {
648 uatomic_set((int32_t *) wait_shm_mmap
, 1);
649 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
650 INT_MAX
, NULL
, NULL
, 0);
652 uatomic_set((int32_t *) wait_shm_mmap
, 0);
654 ret
= munmap(wait_shm_mmap
, mmap_size
);
656 perror("Error unmapping wait shm");
665 * Set open files limit to unlimited. This daemon can open a large number of
666 * file descriptors in order to consumer multiple kernel traces.
668 static void set_ulimit(void)
674 * If not root, we cannot increase our max open files. But our
675 * scope is then limited to processes from a single user.
679 /* The kernel does not allowed an infinite limit for open files */
680 lim
.rlim_cur
= 65535;
681 lim
.rlim_max
= 65535;
683 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
685 perror("failed to set open files limit");
689 int main(int argc
, char **argv
)
691 const char *home_dir
;
692 int ret
, wait_shm_fd
;
693 struct sigaction act
;
694 mode_t old_umask
= 0;
699 memset(&act
, 0, sizeof(act
));
700 ret
= sigemptyset(&act
.sa_mask
);
702 perror("sigemptyset");
706 act
.sa_handler
= SIG_IGN
;
707 ret
= sigaction(SIGPIPE
, &act
, NULL
);
714 act
.sa_handler
= handle_signals
;
715 ret
= sigaction(SIGTERM
, &act
, NULL
);
721 ret
= sigaction(SIGINT
, &act
, NULL
);
727 if (geteuid() == 0) {
728 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
729 if (ret
&& errno
!= EEXIST
) {
733 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
734 sysconf(_SC_PAGE_SIZE
), 1);
735 if (wait_shm_fd
< 0) {
736 perror("global wait shm error");
739 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
740 old_umask
= umask(0);
742 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
743 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
744 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
745 sysconf(_SC_PAGE_SIZE
), 0);
746 if (wait_shm_fd
< 0) {
747 perror("local wait shm error");
750 home_dir
= (const char *) getenv("HOME");
752 perror("getenv error");
755 snprintf(apps_sock_path
, PATH_MAX
,
756 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
759 ret
= ustcomm_create_unix_sock(apps_sock_path
);
761 perror("create error");
767 /* File permission MUST be 666 */
768 ret
= chmod(apps_sock_path
,
769 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
771 printf("Set file permissions failed: %s\n", apps_sock_path
);
777 ret
= ustcomm_listen_unix_sock(apps_socket
);
779 perror("listen error");
783 /* wake up futexes */
784 ret
= update_futex(wait_shm_fd
, 1);
786 fprintf(stderr
, "Error wakeup futex\n");
800 char name
[16]; /* Process name */
807 printf("Accepting application registration\n");
808 sock
= ustcomm_accept_unix_sock(apps_socket
);
810 perror("accept error");
815 * Basic recv here to handle the very simple data
816 * that the libust send to register (reg_msg).
818 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
819 if (len
< 0 || len
!= sizeof(reg_msg
)) {
820 perror("ustcomm_recv_unix_sock");
823 memcpy(bufname
, reg_msg
.name
, 16);
825 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
826 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
827 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
828 ret
= send_app_msgs(sock
);
830 printf("Error in send_app_msgs.\n");
837 printf("quitting.\n");
838 /* Let applications know we are not responding anymore */
839 ret
= update_futex(wait_shm_fd
, 0);
841 fprintf(stderr
, "Error wakeup futex\n");