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>
35 #include <urcu/futex.h>
36 #include <urcu/uatomic.h>
38 #include <sys/socket.h>
41 #include <../../libringbuffer/backend.h>
42 #include <../../libringbuffer/frontend.h>
43 #include "../../liblttng-ust/compat.h" /* For ENODATA */
47 #define MAX_NR_STREAMS 64
50 const char *evname
[] = {
51 "ust_tests_hello_tptest",
52 "ust_tests_hello_tptest_sighandler",
53 "ust_tests_hello_dontexist",
56 static int session_handle
[NR_SESSIONS
];
57 static struct lttng_ust_object_data metadata_stream_data
[NR_SESSIONS
];
58 static struct lttng_ust_object_data metadata_data
[NR_SESSIONS
];
59 static struct lttng_ust_object_data channel_data
[NR_SESSIONS
][NR_CHANNELS
];
60 static struct lttng_ust_object_data stream_data
[NR_SESSIONS
][NR_CHANNELS
][MAX_NR_STREAMS
];
61 static int event_handle
[NR_SESSIONS
][NR_CHANNELS
][NR_EVENTS
];
63 static int apps_socket
= -1;
64 static char apps_sock_path
[PATH_MAX
];
65 static char local_apps_wait_shm_path
[PATH_MAX
];
67 static volatile int quit_program
;
69 static void handle_signals(int signo
)
75 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
81 struct ustcomm_ust_msg lum
;
82 struct ustcomm_ust_reply lur
;
84 memset(&lum
, 0, sizeof(lum
));
85 lum
.handle
= channel_handle
;
86 lum
.cmd
= LTTNG_UST_STREAM
;
87 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
90 stream_datas
[k
].handle
= lur
.ret_val
;
91 printf("received stream handle %u\n",
92 stream_datas
[k
].handle
);
93 if (lur
.ret_code
== USTCOMM_OK
) {
96 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
98 len
= ustcomm_recv_fd(sock
);
101 stream_datas
[k
].shm_fd
= len
;
103 len
= ustcomm_recv_fd(sock
);
106 stream_datas
[k
].wait_fd
= len
;
119 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
123 for (k
= 0; k
< nr_check
; k
++) {
124 struct ustcomm_ust_msg lum
;
125 struct ustcomm_ust_reply lur
;
127 if (!stream_datas
[k
].handle
)
129 memset(&lum
, 0, sizeof(lum
));
130 lum
.handle
= stream_datas
[k
].handle
;
131 lum
.cmd
= LTTNG_UST_RELEASE
;
132 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
134 printf("Error closing stream\n");
137 if (stream_datas
[k
].shm_fd
>= 0) {
138 ret
= close(stream_datas
[k
].shm_fd
);
140 printf("Error closing stream shm_fd\n");
144 if (stream_datas
[k
].wait_fd
>= 0) {
145 ret
= close(stream_datas
[k
].wait_fd
);
147 printf("Error closing stream wait_fd\n");
156 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
157 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
159 struct lttng_ust_shm_handle
*handle
;
160 struct channel
*chan
;
163 /* map metadata channel */
164 handle
= channel_handle_create(chan_data
->shm_fd
,
166 chan_data
->memory_map_size
);
168 printf("create handle error\n");
171 chan_data
->shm_fd
= -1;
172 chan_data
->wait_fd
= -1;
173 chan
= shmp(handle
, handle
->chan
);
175 for (k
= 0; k
< nr_check
; k
++) {
176 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
178 if (!stream_data
->handle
)
181 ret
= channel_handle_add_stream(handle
,
183 stream_data
->wait_fd
,
184 stream_data
->memory_map_size
);
186 printf("add stream error\n");
189 stream_data
->shm_fd
= -1;
190 stream_data
->wait_fd
= -1;
195 channel_destroy(chan
, handle
, 1);
200 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
202 struct channel
*chan
;
204 chan
= shmp(handle
, handle
->chan
);
206 channel_destroy(chan
, handle
, 1);
210 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
212 struct channel
*chan
;
213 struct lttng_ust_lib_ring_buffer
*buf
;
215 int *shm_fd
, *wait_fd
;
216 uint64_t *memory_map_size
;
218 chan
= shmp(handle
, handle
->chan
);
221 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
222 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
225 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
231 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
232 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
234 perror("open output");
238 printf("Waiting for buffer data for %s\n", outfile
);
240 unsigned long read_size
;
241 unsigned long copy_size
;
244 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
245 printf("get next ret %d\n", ret
);
248 if (ret
== -EAGAIN
) {
253 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
256 read_size
= lib_ring_buffer_get_read_data_size(
257 &chan
->backend
.config
, buf
, handle
);
258 read_size
= PAGE_ALIGN(read_size
);
259 ptr
= lib_ring_buffer_read_offset_address(
260 &buf
->backend
, 0, handle
);
261 printf("WRITE: copy %lu bytes\n", read_size
);
262 copy_size
= write(outfd
, ptr
, read_size
);
263 if (copy_size
< read_size
) {
264 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
266 lib_ring_buffer_put_next_subbuf(buf
, handle
);
276 lib_ring_buffer_release_read(buf
, handle
, 1);
281 int consume_buffers(void)
286 for (i
= 0; i
< NR_SESSIONS
; i
++) {
287 char pathname
[PATH_MAX
];
288 struct lttng_ust_shm_handle
*handle
;
290 snprintf(pathname
, PATH_MAX
- 1, "/tmp/testtrace%u", i
);
291 old_umask
= umask(0);
292 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
293 if (ret
&& errno
!= EEXIST
) {
301 handle
= map_channel(&metadata_data
[i
],
302 &metadata_stream_data
[i
], 1);
305 snprintf(pathname
, PATH_MAX
- 1,
306 "/tmp/testtrace%u/metadata", i
);
307 ret
= consume_stream(handle
, -1, pathname
);
308 if (ret
&& ret
!= -ENOENT
) {
309 printf("Error in consume_stream\n");
312 unmap_channel(handle
);
314 /* copy binary data */
315 for (j
= 0; j
< NR_CHANNELS
; j
++) {
316 handle
= map_channel(&channel_data
[i
][j
],
317 stream_data
[i
][j
], MAX_NR_STREAMS
);
320 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
321 snprintf(pathname
, PATH_MAX
- 1,
322 "/tmp/testtrace%u/data_%u", i
, k
);
323 ret
= consume_stream(handle
, k
, pathname
);
324 if (ret
&& ret
!= -ENOENT
) {
325 printf("Error in consume_stream\n");
329 unmap_channel(handle
);
336 int send_app_msgs(int sock
)
338 struct ustcomm_ust_msg lum
;
339 struct ustcomm_ust_reply lur
;
342 for (i
= 0; i
< NR_SESSIONS
; i
++) {
344 memset(&lum
, 0, sizeof(lum
));
345 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
346 lum
.cmd
= LTTNG_UST_SESSION
;
347 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
350 session_handle
[i
] = lur
.ret_val
;
351 printf("received session handle %u\n", session_handle
[i
]);
353 /* Create metadata channel */
354 memset(&lum
, 0, sizeof(lum
));
355 lum
.handle
= session_handle
[i
];
356 lum
.cmd
= LTTNG_UST_METADATA
;
357 lum
.u
.channel
.overwrite
= 0;
358 lum
.u
.channel
.subbuf_size
= 32768;
359 lum
.u
.channel
.num_subbuf
= 4;
360 lum
.u
.channel
.switch_timer_interval
= 0;
361 lum
.u
.channel
.read_timer_interval
= 0;
362 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
363 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
366 metadata_data
[i
].handle
= lur
.ret_val
;
367 printf("received metadata handle %u\n", metadata_data
[i
].handle
);
368 if (lur
.ret_code
== USTCOMM_OK
) {
371 metadata_data
[i
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
373 len
= ustcomm_recv_fd(sock
);
376 metadata_data
[i
].shm_fd
= len
;
378 len
= ustcomm_recv_fd(sock
);
381 metadata_data
[i
].wait_fd
= len
;
384 ret
= open_streams(sock
, metadata_data
[i
].handle
,
385 &metadata_stream_data
[i
], 1);
387 printf("Error in open_streams\n");
391 /* Create channels */
392 for (j
= 0; j
< NR_CHANNELS
; j
++) {
393 memset(&lum
, 0, sizeof(lum
));
394 lum
.handle
= session_handle
[i
];
395 lum
.cmd
= LTTNG_UST_CHANNEL
;
396 //lum.u.channel.overwrite = 0;
397 lum
.u
.channel
.overwrite
= 1;
398 lum
.u
.channel
.subbuf_size
= 32768;
399 lum
.u
.channel
.num_subbuf
= 8;
400 //lum.u.channel.num_subbuf = 4;
401 //lum.u.channel.num_subbuf = 2;
402 lum
.u
.channel
.switch_timer_interval
= 0;
403 lum
.u
.channel
.read_timer_interval
= 0;
404 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
405 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
408 channel_data
[i
][j
].handle
= lur
.ret_val
;
409 printf("received channel handle %u\n", channel_data
[i
][j
].handle
);
410 if (lur
.ret_code
== USTCOMM_OK
) {
413 channel_data
[i
][j
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
415 len
= ustcomm_recv_fd(sock
);
418 channel_data
[i
][j
].shm_fd
= len
;
420 len
= ustcomm_recv_fd(sock
);
423 channel_data
[i
][j
].wait_fd
= len
;
427 for (k
= 0; k
< NR_EVENTS
; k
++) {
428 memset(&lum
, 0, sizeof(lum
));
429 lum
.handle
= channel_data
[i
][j
].handle
;
430 lum
.cmd
= LTTNG_UST_EVENT
;
431 strncpy(lum
.u
.event
.name
, evname
[k
],
432 LTTNG_UST_SYM_NAME_LEN
);
433 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
434 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
437 event_handle
[i
][j
][k
] = lur
.ret_val
;
438 printf("received event handle %u\n", event_handle
[i
][j
][k
]);
441 /* Get references to channel streams */
442 ret
= open_streams(sock
, channel_data
[i
][j
].handle
,
443 stream_data
[i
][j
], MAX_NR_STREAMS
);
445 printf("Error in open_streams\n");
450 memset(&lum
, 0, sizeof(lum
));
451 lum
.handle
= session_handle
[i
];
452 lum
.cmd
= LTTNG_UST_SESSION_START
;
453 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
456 printf("Session handle %u started.\n", session_handle
[i
]);
459 /* Tell application registration is done */
460 memset(&lum
, 0, sizeof(lum
));
461 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
462 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
463 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
466 printf("Registration done acknowledged.\n");
470 ret
= consume_buffers();
472 printf("Error in consume_buffers\n");
476 for (i
= 0; i
< NR_SESSIONS
; i
++) {
477 /* Release channels */
478 for (j
= 0; j
< NR_CHANNELS
; j
++) {
479 /* Release streams */
480 ret
= close_streams(sock
, stream_data
[i
][j
],
486 for (k
= 0; k
< NR_EVENTS
; k
++) {
487 memset(&lum
, 0, sizeof(lum
));
488 lum
.handle
= event_handle
[i
][j
][k
];
489 lum
.cmd
= LTTNG_UST_RELEASE
;
490 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
494 memset(&lum
, 0, sizeof(lum
));
495 lum
.handle
= channel_data
[i
][j
].handle
;
496 lum
.cmd
= LTTNG_UST_RELEASE
;
497 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
500 if (channel_data
[i
][j
].shm_fd
>= 0) {
501 ret
= close(channel_data
[i
][j
].shm_fd
);
505 if (channel_data
[i
][j
].wait_fd
>= 0) {
506 ret
= close(channel_data
[i
][j
].wait_fd
);
512 /* Release metadata channel */
513 ret
= close_streams(sock
, &metadata_stream_data
[i
], 1);
517 memset(&lum
, 0, sizeof(lum
));
518 lum
.handle
= metadata_data
[i
].handle
;
519 lum
.cmd
= LTTNG_UST_RELEASE
;
520 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
523 if (metadata_data
[i
].shm_fd
>= 0) {
524 ret
= close(metadata_data
[i
].shm_fd
);
528 if (metadata_data
[i
].wait_fd
>= 0) {
529 ret
= close(metadata_data
[i
].wait_fd
);
534 /* Release session */
535 memset(&lum
, 0, sizeof(lum
));
536 lum
.handle
= session_handle
[i
];
537 lum
.cmd
= LTTNG_UST_RELEASE
;
538 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
547 * Using fork to set umask in the child process (not multi-thread safe). We
548 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
549 * the shm and does not let everybody modify it, to ensure safety against
550 * shm_unlink) by simply letting the mmap fail and retrying after a few
551 * seconds. For global shm, everybody has rw access to it until the sessiond
554 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
556 int wait_shm_fd
, ret
;
559 /* Default permissions */
560 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
562 /* Change owner of the shm path */
564 ret
= chown(shm_path
, 0, 0);
566 if (errno
!= ENOENT
) {
567 perror("chown wait shm");
573 * If global session daemon, any application can register so the shm
574 * needs to be set in read-only mode for others.
578 ret
= chown(shm_path
, getuid(), getgid());
580 if (errno
!= ENOENT
) {
581 perror("chown wait shm");
588 * Set permissions to the shm even if we did not create the shm.
590 ret
= chmod(shm_path
, mode
);
592 if (errno
!= ENOENT
) {
593 perror("chmod wait shm");
599 * We're alone in a child process, so we can modify the process-wide
605 * Try creating shm (or get rw access). We don't do an exclusive open,
606 * because we allow other processes to create+ftruncate it concurrently.
608 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
609 if (wait_shm_fd
< 0) {
610 perror("shm_open wait shm");
614 ret
= ftruncate(wait_shm_fd
, mmap_size
);
616 perror("ftruncate wait shm");
620 ret
= fchmod(wait_shm_fd
, mode
);
626 printf("Got the wait shm fd %d\n", wait_shm_fd
);
631 printf("Failing to get the wait shm fd\n");
636 int update_futex(int fd
, int active
)
638 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
642 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
644 if (wait_shm_mmap
== MAP_FAILED
) {
650 uatomic_set((int32_t *) wait_shm_mmap
, 1);
651 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
652 INT_MAX
, NULL
, NULL
, 0);
654 uatomic_set((int32_t *) wait_shm_mmap
, 0);
656 ret
= munmap(wait_shm_mmap
, mmap_size
);
658 perror("Error unmapping wait shm");
667 * Set open files limit to unlimited. This daemon can open a large number of
668 * file descriptors in order to consumer multiple kernel traces.
670 static void set_ulimit(void)
676 * If not root, we cannot increase our max open files. But our
677 * scope is then limited to processes from a single user.
681 /* The kernel does not allowed an infinite limit for open files */
682 lim
.rlim_cur
= 65535;
683 lim
.rlim_max
= 65535;
685 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
687 perror("failed to set open files limit");
691 int main(int argc
, char **argv
)
693 const char *home_dir
;
694 int ret
, wait_shm_fd
;
695 struct sigaction act
;
696 mode_t old_umask
= 0;
701 memset(&act
, 0, sizeof(act
));
702 ret
= sigemptyset(&act
.sa_mask
);
704 perror("sigemptyset");
708 act
.sa_handler
= SIG_IGN
;
709 ret
= sigaction(SIGPIPE
, &act
, NULL
);
716 act
.sa_handler
= handle_signals
;
717 ret
= sigaction(SIGTERM
, &act
, NULL
);
723 ret
= sigaction(SIGINT
, &act
, NULL
);
729 if (geteuid() == 0) {
730 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
731 if (ret
&& errno
!= EEXIST
) {
735 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
736 sysconf(_SC_PAGE_SIZE
), 1);
737 if (wait_shm_fd
< 0) {
738 perror("global wait shm error");
741 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
742 old_umask
= umask(0);
744 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
745 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
746 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
747 sysconf(_SC_PAGE_SIZE
), 0);
748 if (wait_shm_fd
< 0) {
749 perror("local wait shm error");
752 home_dir
= (const char *) getenv("HOME");
754 perror("getenv error");
757 snprintf(apps_sock_path
, PATH_MAX
,
758 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
761 ret
= ustcomm_create_unix_sock(apps_sock_path
);
763 perror("create error");
769 /* File permission MUST be 666 */
770 ret
= chmod(apps_sock_path
,
771 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
773 printf("Set file permissions failed: %s\n", apps_sock_path
);
779 ret
= ustcomm_listen_unix_sock(apps_socket
);
781 perror("listen error");
785 /* wake up futexes */
786 ret
= update_futex(wait_shm_fd
, 1);
788 fprintf(stderr
, "Error wakeup futex\n");
802 char name
[16]; /* Process name */
809 printf("Accepting application registration\n");
810 sock
= ustcomm_accept_unix_sock(apps_socket
);
812 perror("accept error");
817 * Basic recv here to handle the very simple data
818 * that the libust send to register (reg_msg).
820 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
821 if (len
< 0 || len
!= sizeof(reg_msg
)) {
822 perror("ustcomm_recv_unix_sock");
825 memcpy(bufname
, reg_msg
.name
, 16);
827 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
828 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
829 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
830 ret
= send_app_msgs(sock
);
832 printf("Error in send_app_msgs.\n");
839 printf("quitting.\n");
840 /* Let applications know we are not responding anymore */
841 ret
= update_futex(wait_shm_fd
, 0);
843 fprintf(stderr
, "Error wakeup futex\n");