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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License only.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <lttng/ust-error.h>
44 #include <../../libringbuffer/backend.h>
45 #include <../../libringbuffer/frontend.h>
46 #include "../../liblttng-ust/compat.h" /* For ENODATA */
50 #define MAX_NR_STREAMS 64
53 const char *evname
[] = {
54 "ust_tests_hello_tptest",
55 "ust_tests_hello_tptest_sighandler",
56 "ust_tests_hello_dontexist",
59 static int session_handle
[NR_SESSIONS
];
60 static struct lttng_ust_object_data metadata_stream_data
[NR_SESSIONS
];
61 static struct lttng_ust_object_data metadata_data
[NR_SESSIONS
];
62 static struct lttng_ust_object_data channel_data
[NR_SESSIONS
][NR_CHANNELS
];
63 static struct lttng_ust_object_data stream_data
[NR_SESSIONS
][NR_CHANNELS
][MAX_NR_STREAMS
];
64 static int event_handle
[NR_SESSIONS
][NR_CHANNELS
][NR_EVENTS
];
66 static int apps_socket
= -1;
67 static char apps_sock_path
[PATH_MAX
];
68 static char local_apps_wait_shm_path
[PATH_MAX
];
70 static volatile int quit_program
;
72 static void handle_signals(int signo
)
78 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
84 struct ustcomm_ust_msg lum
;
85 struct ustcomm_ust_reply lur
;
87 memset(&lum
, 0, sizeof(lum
));
88 lum
.handle
= channel_handle
;
89 lum
.cmd
= LTTNG_UST_STREAM
;
90 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
93 stream_datas
[k
].handle
= lur
.ret_val
;
94 printf("received stream handle %u\n",
95 stream_datas
[k
].handle
);
96 if (lur
.ret_code
== LTTNG_UST_OK
) {
99 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
101 len
= ustcomm_recv_fd(sock
);
104 stream_datas
[k
].shm_fd
= len
;
106 len
= ustcomm_recv_fd(sock
);
109 stream_datas
[k
].wait_fd
= len
;
113 if (ret
== -LTTNG_UST_ERR_NOENT
)
122 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
126 for (k
= 0; k
< nr_check
; k
++) {
127 struct ustcomm_ust_msg lum
;
128 struct ustcomm_ust_reply lur
;
130 if (!stream_datas
[k
].handle
)
132 memset(&lum
, 0, sizeof(lum
));
133 lum
.handle
= stream_datas
[k
].handle
;
134 lum
.cmd
= LTTNG_UST_RELEASE
;
135 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
137 printf("Error closing stream\n");
140 if (stream_datas
[k
].shm_fd
>= 0) {
141 ret
= close(stream_datas
[k
].shm_fd
);
143 printf("Error closing stream shm_fd\n");
147 if (stream_datas
[k
].wait_fd
>= 0) {
148 ret
= close(stream_datas
[k
].wait_fd
);
150 printf("Error closing stream wait_fd\n");
159 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
160 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
162 struct lttng_ust_shm_handle
*handle
;
163 struct channel
*chan
;
166 /* map metadata channel */
167 handle
= channel_handle_create(chan_data
->shm_fd
,
169 chan_data
->memory_map_size
);
171 printf("create handle error\n");
174 chan_data
->shm_fd
= -1;
175 chan_data
->wait_fd
= -1;
176 chan
= shmp(handle
, handle
->chan
);
178 for (k
= 0; k
< nr_check
; k
++) {
179 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
181 if (!stream_data
->handle
)
184 ret
= channel_handle_add_stream(handle
,
186 stream_data
->wait_fd
,
187 stream_data
->memory_map_size
);
189 printf("add stream error\n");
192 stream_data
->shm_fd
= -1;
193 stream_data
->wait_fd
= -1;
198 channel_destroy(chan
, handle
, 1);
203 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
205 struct channel
*chan
;
207 chan
= shmp(handle
, handle
->chan
);
209 channel_destroy(chan
, handle
, 1);
213 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
215 struct channel
*chan
;
216 struct lttng_ust_lib_ring_buffer
*buf
;
218 int *shm_fd
, *wait_fd
;
219 uint64_t *memory_map_size
;
221 chan
= shmp(handle
, handle
->chan
);
224 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
225 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
228 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
234 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
235 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
237 perror("open output");
241 printf("Waiting for buffer data for %s\n", outfile
);
243 unsigned long read_size
;
244 unsigned long copy_size
;
247 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
248 printf("get next ret %d\n", ret
);
251 if (ret
== -EAGAIN
) {
256 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
259 read_size
= lib_ring_buffer_get_read_data_size(
260 &chan
->backend
.config
, buf
, handle
);
261 read_size
= PAGE_ALIGN(read_size
);
262 ptr
= lib_ring_buffer_read_offset_address(
263 &buf
->backend
, 0, handle
);
264 printf("WRITE: copy %lu bytes\n", read_size
);
265 copy_size
= write(outfd
, ptr
, read_size
);
266 if (copy_size
< read_size
) {
267 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
269 lib_ring_buffer_put_next_subbuf(buf
, handle
);
279 lib_ring_buffer_release_read(buf
, handle
, 1);
284 int consume_buffers(void)
289 for (i
= 0; i
< NR_SESSIONS
; i
++) {
290 char pathname
[PATH_MAX
];
291 struct lttng_ust_shm_handle
*handle
;
293 snprintf(pathname
, PATH_MAX
- 1, "/tmp/testtrace%u", i
);
294 old_umask
= umask(0);
295 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
296 if (ret
&& errno
!= EEXIST
) {
304 handle
= map_channel(&metadata_data
[i
],
305 &metadata_stream_data
[i
], 1);
308 snprintf(pathname
, PATH_MAX
- 1,
309 "/tmp/testtrace%u/metadata", i
);
310 ret
= consume_stream(handle
, -1, pathname
);
311 if (ret
&& ret
!= -ENOENT
) {
312 printf("Error in consume_stream\n");
315 unmap_channel(handle
);
317 /* copy binary data */
318 for (j
= 0; j
< NR_CHANNELS
; j
++) {
319 handle
= map_channel(&channel_data
[i
][j
],
320 stream_data
[i
][j
], MAX_NR_STREAMS
);
323 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
324 snprintf(pathname
, PATH_MAX
- 1,
325 "/tmp/testtrace%u/data_%u", i
, k
);
326 ret
= consume_stream(handle
, k
, pathname
);
327 if (ret
&& ret
!= -ENOENT
) {
328 printf("Error in consume_stream\n");
332 unmap_channel(handle
);
339 int send_app_msgs(int sock
)
341 struct ustcomm_ust_msg lum
;
342 struct ustcomm_ust_reply lur
;
345 for (i
= 0; i
< NR_SESSIONS
; i
++) {
347 memset(&lum
, 0, sizeof(lum
));
348 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
349 lum
.cmd
= LTTNG_UST_SESSION
;
350 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
353 session_handle
[i
] = lur
.ret_val
;
354 printf("received session handle %u\n", session_handle
[i
]);
356 /* Create metadata channel */
357 memset(&lum
, 0, sizeof(lum
));
358 lum
.handle
= session_handle
[i
];
359 lum
.cmd
= LTTNG_UST_METADATA
;
360 lum
.u
.channel
.overwrite
= 0;
361 lum
.u
.channel
.subbuf_size
= 32768;
362 lum
.u
.channel
.num_subbuf
= 4;
363 lum
.u
.channel
.switch_timer_interval
= 0;
364 lum
.u
.channel
.read_timer_interval
= 0;
365 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
366 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
369 metadata_data
[i
].handle
= lur
.ret_val
;
370 printf("received metadata handle %u\n", metadata_data
[i
].handle
);
371 if (lur
.ret_code
== LTTNG_UST_OK
) {
374 metadata_data
[i
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
376 len
= ustcomm_recv_fd(sock
);
379 metadata_data
[i
].shm_fd
= len
;
381 len
= ustcomm_recv_fd(sock
);
384 metadata_data
[i
].wait_fd
= len
;
387 ret
= open_streams(sock
, metadata_data
[i
].handle
,
388 &metadata_stream_data
[i
], 1);
390 printf("Error in open_streams\n");
394 /* Create channels */
395 for (j
= 0; j
< NR_CHANNELS
; j
++) {
396 memset(&lum
, 0, sizeof(lum
));
397 lum
.handle
= session_handle
[i
];
398 lum
.cmd
= LTTNG_UST_CHANNEL
;
399 //lum.u.channel.overwrite = 0;
400 lum
.u
.channel
.overwrite
= 1;
401 lum
.u
.channel
.subbuf_size
= 32768;
402 lum
.u
.channel
.num_subbuf
= 8;
403 //lum.u.channel.num_subbuf = 4;
404 //lum.u.channel.num_subbuf = 2;
405 lum
.u
.channel
.switch_timer_interval
= 0;
406 lum
.u
.channel
.read_timer_interval
= 0;
407 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
408 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
411 channel_data
[i
][j
].handle
= lur
.ret_val
;
412 printf("received channel handle %u\n", channel_data
[i
][j
].handle
);
413 if (lur
.ret_code
== LTTNG_UST_OK
) {
416 channel_data
[i
][j
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
418 len
= ustcomm_recv_fd(sock
);
421 channel_data
[i
][j
].shm_fd
= len
;
423 len
= ustcomm_recv_fd(sock
);
426 channel_data
[i
][j
].wait_fd
= len
;
430 for (k
= 0; k
< NR_EVENTS
; k
++) {
431 memset(&lum
, 0, sizeof(lum
));
432 lum
.handle
= channel_data
[i
][j
].handle
;
433 lum
.cmd
= LTTNG_UST_EVENT
;
434 strncpy(lum
.u
.event
.name
, evname
[k
],
435 LTTNG_UST_SYM_NAME_LEN
);
436 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
437 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
440 event_handle
[i
][j
][k
] = lur
.ret_val
;
441 printf("received event handle %u\n", event_handle
[i
][j
][k
]);
444 /* Get references to channel streams */
445 ret
= open_streams(sock
, channel_data
[i
][j
].handle
,
446 stream_data
[i
][j
], MAX_NR_STREAMS
);
448 printf("Error in open_streams\n");
453 memset(&lum
, 0, sizeof(lum
));
454 lum
.handle
= session_handle
[i
];
455 lum
.cmd
= LTTNG_UST_SESSION_START
;
456 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
459 printf("Session handle %u started.\n", session_handle
[i
]);
462 /* Tell application registration is done */
463 memset(&lum
, 0, sizeof(lum
));
464 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
465 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
466 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
469 printf("Registration done acknowledged.\n");
473 ret
= consume_buffers();
475 printf("Error in consume_buffers\n");
479 for (i
= 0; i
< NR_SESSIONS
; i
++) {
480 /* Release channels */
481 for (j
= 0; j
< NR_CHANNELS
; j
++) {
482 /* Release streams */
483 ret
= close_streams(sock
, stream_data
[i
][j
],
489 for (k
= 0; k
< NR_EVENTS
; k
++) {
490 memset(&lum
, 0, sizeof(lum
));
491 lum
.handle
= event_handle
[i
][j
][k
];
492 lum
.cmd
= LTTNG_UST_RELEASE
;
493 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
497 memset(&lum
, 0, sizeof(lum
));
498 lum
.handle
= channel_data
[i
][j
].handle
;
499 lum
.cmd
= LTTNG_UST_RELEASE
;
500 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
503 if (channel_data
[i
][j
].shm_fd
>= 0) {
504 ret
= close(channel_data
[i
][j
].shm_fd
);
508 if (channel_data
[i
][j
].wait_fd
>= 0) {
509 ret
= close(channel_data
[i
][j
].wait_fd
);
515 /* Release metadata channel */
516 ret
= close_streams(sock
, &metadata_stream_data
[i
], 1);
520 memset(&lum
, 0, sizeof(lum
));
521 lum
.handle
= metadata_data
[i
].handle
;
522 lum
.cmd
= LTTNG_UST_RELEASE
;
523 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
526 if (metadata_data
[i
].shm_fd
>= 0) {
527 ret
= close(metadata_data
[i
].shm_fd
);
531 if (metadata_data
[i
].wait_fd
>= 0) {
532 ret
= close(metadata_data
[i
].wait_fd
);
537 /* Release session */
538 memset(&lum
, 0, sizeof(lum
));
539 lum
.handle
= session_handle
[i
];
540 lum
.cmd
= LTTNG_UST_RELEASE
;
541 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
550 * Using fork to set umask in the child process (not multi-thread safe). We
551 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
552 * the shm and does not let everybody modify it, to ensure safety against
553 * shm_unlink) by simply letting the mmap fail and retrying after a few
554 * seconds. For global shm, everybody has rw access to it until the sessiond
557 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
559 int wait_shm_fd
, ret
;
562 /* Default permissions */
563 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
565 /* Change owner of the shm path */
567 ret
= chown(shm_path
, 0, 0);
569 if (errno
!= ENOENT
) {
570 perror("chown wait shm");
576 * If global session daemon, any application can register so the shm
577 * needs to be set in read-only mode for others.
581 ret
= chown(shm_path
, getuid(), getgid());
583 if (errno
!= ENOENT
) {
584 perror("chown wait shm");
591 * Set permissions to the shm even if we did not create the shm.
593 ret
= chmod(shm_path
, mode
);
595 if (errno
!= ENOENT
) {
596 perror("chmod wait shm");
602 * We're alone in a child process, so we can modify the process-wide
608 * Try creating shm (or get rw access). We don't do an exclusive open,
609 * because we allow other processes to create+ftruncate it concurrently.
611 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
612 if (wait_shm_fd
< 0) {
613 perror("shm_open wait shm");
617 ret
= ftruncate(wait_shm_fd
, mmap_size
);
619 perror("ftruncate wait shm");
623 ret
= fchmod(wait_shm_fd
, mode
);
629 printf("Got the wait shm fd %d\n", wait_shm_fd
);
634 printf("Failing to get the wait shm fd\n");
639 int update_futex(int fd
, int active
)
645 page_size
= sysconf(_SC_PAGE_SIZE
);
649 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
,
651 if (wait_shm_mmap
== MAP_FAILED
) {
657 uatomic_set((int32_t *) wait_shm_mmap
, 1);
658 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
659 INT_MAX
, NULL
, NULL
, 0);
661 uatomic_set((int32_t *) wait_shm_mmap
, 0);
663 ret
= munmap(wait_shm_mmap
, page_size
);
665 perror("Error unmapping wait shm");
674 * Set open files limit to unlimited. This daemon can open a large number of
675 * file descriptors in order to consumer multiple kernel traces.
677 static void set_ulimit(void)
683 * If not root, we cannot increase our max open files. But our
684 * scope is then limited to processes from a single user.
688 /* The kernel does not allowed an infinite limit for open files */
689 lim
.rlim_cur
= 65535;
690 lim
.rlim_max
= 65535;
692 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
694 perror("failed to set open files limit");
698 int main(int argc
, char **argv
)
700 const char *home_dir
;
701 char home_rundir
[PATH_MAX
];
703 int ret
, wait_shm_fd
;
704 struct sigaction act
;
705 mode_t old_umask
= 0;
710 memset(&act
, 0, sizeof(act
));
711 ret
= sigemptyset(&act
.sa_mask
);
713 perror("sigemptyset");
717 act
.sa_handler
= SIG_IGN
;
718 ret
= sigaction(SIGPIPE
, &act
, NULL
);
725 act
.sa_handler
= handle_signals
;
726 ret
= sigaction(SIGTERM
, &act
, NULL
);
732 ret
= sigaction(SIGINT
, &act
, NULL
);
738 if (geteuid() == 0) {
739 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
740 if (ret
&& errno
!= EEXIST
) {
744 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
745 sysconf(_SC_PAGE_SIZE
), 1);
746 if (wait_shm_fd
< 0) {
747 perror("global wait shm error");
750 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
751 old_umask
= umask(0);
753 home_dir
= (const char *) getenv("HOME");
755 perror("getenv error");
759 snprintf(home_rundir
, PATH_MAX
,
760 LTTNG_HOME_RUNDIR
, home_dir
);
762 ret
= mkdir(home_rundir
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
763 if (ret
&& errno
!= EEXIST
) {
768 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
769 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
770 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
771 sysconf(_SC_PAGE_SIZE
), 0);
772 if (wait_shm_fd
< 0) {
773 perror("local wait shm error");
776 snprintf(apps_sock_path
, PATH_MAX
,
777 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
780 ret
= ustcomm_create_unix_sock(apps_sock_path
);
782 perror("create error");
788 /* File permission MUST be 666 */
789 ret
= chmod(apps_sock_path
,
790 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
792 printf("Set file permissions failed: %s\n", apps_sock_path
);
798 ret
= ustcomm_listen_unix_sock(apps_socket
);
800 perror("listen error");
804 /* wake up futexes */
805 ret
= update_futex(wait_shm_fd
, 1);
807 fprintf(stderr
, "Error wakeup futex\n");
821 uint32_t bits_per_long
;
822 char name
[16]; /* Process name */
829 printf("Accepting application registration\n");
830 sock
= ustcomm_accept_unix_sock(apps_socket
);
832 perror("accept error");
837 * Basic recv here to handle the very simple data
838 * that the libust send to register (reg_msg).
840 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
841 if (len
< 0 || len
!= sizeof(reg_msg
)) {
842 perror("ustcomm_recv_unix_sock");
845 memcpy(bufname
, reg_msg
.name
, 16);
847 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
848 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
849 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
850 ret
= send_app_msgs(sock
);
852 printf("Error in send_app_msgs.\n");
859 printf("quitting.\n");
860 /* Let applications know we are not responding anymore */
861 ret
= update_futex(wait_shm_fd
, 0);
863 fprintf(stderr
, "Error wakeup futex\n");
868 printf("Removing %s directory\n", home_rundir
);
869 ret
= asprintf(&cmd
, "rm -rf %s", home_rundir
);
871 printf("asprintf failed. Something is really wrong!\n");
875 /* Remove lttng run directory */
878 printf("Unable to clean %s\n", home_rundir
);