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>
46 #define MAX_NR_STREAMS 64
49 const char *evname
[] = {
50 "ust_tests_hello_tptest",
51 "ust_tests_hello_tptest_sighandler",
52 "ust_tests_hello_dontexist",
55 static int session_handle
[NR_SESSIONS
];
56 static struct lttng_ust_object_data metadata_stream_data
[NR_SESSIONS
];
57 static struct lttng_ust_object_data metadata_data
[NR_SESSIONS
];
58 static struct lttng_ust_object_data channel_data
[NR_SESSIONS
][NR_CHANNELS
];
59 static struct lttng_ust_object_data stream_data
[NR_SESSIONS
][NR_CHANNELS
][MAX_NR_STREAMS
];
60 static int event_handle
[NR_SESSIONS
][NR_CHANNELS
][NR_EVENTS
];
62 static int apps_socket
= -1;
63 static char apps_sock_path
[PATH_MAX
];
64 static char local_apps_wait_shm_path
[PATH_MAX
];
66 static volatile int quit_program
;
68 static void handle_signals(int signo
)
74 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
80 struct ustcomm_ust_msg lum
;
81 struct ustcomm_ust_reply lur
;
83 memset(&lum
, 0, sizeof(lum
));
84 lum
.handle
= channel_handle
;
85 lum
.cmd
= LTTNG_UST_STREAM
;
86 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
89 stream_datas
[k
].handle
= lur
.ret_val
;
90 printf("received stream handle %u\n",
91 stream_datas
[k
].handle
);
92 if (lur
.ret_code
== USTCOMM_OK
) {
95 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
97 len
= ustcomm_recv_fd(sock
);
100 stream_datas
[k
].shm_fd
= len
;
102 len
= ustcomm_recv_fd(sock
);
105 stream_datas
[k
].wait_fd
= len
;
118 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
122 for (k
= 0; k
< nr_check
; k
++) {
123 struct ustcomm_ust_msg lum
;
124 struct ustcomm_ust_reply lur
;
126 if (!stream_datas
[k
].handle
)
128 memset(&lum
, 0, sizeof(lum
));
129 lum
.handle
= stream_datas
[k
].handle
;
130 lum
.cmd
= LTTNG_UST_RELEASE
;
131 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
133 printf("Error closing stream\n");
136 if (stream_datas
[k
].shm_fd
>= 0) {
137 ret
= close(stream_datas
[k
].shm_fd
);
139 printf("Error closing stream shm_fd\n");
143 if (stream_datas
[k
].wait_fd
>= 0) {
144 ret
= close(stream_datas
[k
].wait_fd
);
146 printf("Error closing stream wait_fd\n");
155 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
156 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
158 struct lttng_ust_shm_handle
*handle
;
159 struct channel
*chan
;
162 /* map metadata channel */
163 handle
= channel_handle_create(chan_data
->shm_fd
,
165 chan_data
->memory_map_size
);
167 printf("create handle error\n");
170 chan_data
->shm_fd
= -1;
171 chan_data
->wait_fd
= -1;
172 chan
= shmp(handle
, handle
->chan
);
174 for (k
= 0; k
< nr_check
; k
++) {
175 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
177 if (!stream_data
->handle
)
180 ret
= channel_handle_add_stream(handle
,
182 stream_data
->wait_fd
,
183 stream_data
->memory_map_size
);
185 printf("add stream error\n");
188 stream_data
->shm_fd
= -1;
189 stream_data
->wait_fd
= -1;
194 channel_destroy(chan
, handle
, 1);
199 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
201 struct channel
*chan
;
203 chan
= shmp(handle
, handle
->chan
);
205 channel_destroy(chan
, handle
, 1);
209 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
211 struct channel
*chan
;
212 struct lttng_ust_lib_ring_buffer
*buf
;
214 int *shm_fd
, *wait_fd
;
215 uint64_t *memory_map_size
;
217 chan
= shmp(handle
, handle
->chan
);
220 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
221 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
224 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
230 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
231 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
233 perror("open output");
237 printf("Waiting for buffer data for %s\n", outfile
);
239 unsigned long read_size
;
240 unsigned long copy_size
;
243 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
244 printf("get next ret %d\n", ret
);
247 if (ret
== -EAGAIN
) {
252 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
255 read_size
= lib_ring_buffer_get_read_data_size(
256 &chan
->backend
.config
, buf
, handle
);
257 read_size
= PAGE_ALIGN(read_size
);
258 ptr
= lib_ring_buffer_read_offset_address(
259 &buf
->backend
, 0, handle
);
260 printf("WRITE: copy %lu bytes\n", read_size
);
261 copy_size
= write(outfd
, ptr
, read_size
);
262 if (copy_size
< read_size
) {
263 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
265 lib_ring_buffer_put_next_subbuf(buf
, handle
);
275 lib_ring_buffer_release_read(buf
, handle
, 1);
280 int consume_buffers(void)
285 for (i
= 0; i
< NR_SESSIONS
; i
++) {
286 char pathname
[PATH_MAX
];
287 struct lttng_ust_shm_handle
*handle
;
289 snprintf(pathname
, PATH_MAX
- 1, "/tmp/testtrace%u", i
);
290 old_umask
= umask(0);
291 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
292 if (ret
&& errno
!= EEXIST
) {
300 handle
= map_channel(&metadata_data
[i
],
301 &metadata_stream_data
[i
], 1);
304 snprintf(pathname
, PATH_MAX
- 1,
305 "/tmp/testtrace%u/metadata", i
);
306 ret
= consume_stream(handle
, -1, pathname
);
307 if (ret
&& ret
!= -ENOENT
) {
308 printf("Error in consume_stream\n");
311 unmap_channel(handle
);
313 /* copy binary data */
314 for (j
= 0; j
< NR_CHANNELS
; j
++) {
315 handle
= map_channel(&channel_data
[i
][j
],
316 stream_data
[i
][j
], MAX_NR_STREAMS
);
319 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
320 snprintf(pathname
, PATH_MAX
- 1,
321 "/tmp/testtrace%u/data_%u", i
, k
);
322 ret
= consume_stream(handle
, k
, pathname
);
323 if (ret
&& ret
!= -ENOENT
) {
324 printf("Error in consume_stream\n");
328 unmap_channel(handle
);
335 int send_app_msgs(int sock
)
337 struct ustcomm_ust_msg lum
;
338 struct ustcomm_ust_reply lur
;
341 for (i
= 0; i
< NR_SESSIONS
; i
++) {
343 memset(&lum
, 0, sizeof(lum
));
344 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
345 lum
.cmd
= LTTNG_UST_SESSION
;
346 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
349 session_handle
[i
] = lur
.ret_val
;
350 printf("received session handle %u\n", session_handle
[i
]);
352 /* Create metadata channel */
353 memset(&lum
, 0, sizeof(lum
));
354 lum
.handle
= session_handle
[i
];
355 lum
.cmd
= LTTNG_UST_METADATA
;
356 lum
.u
.channel
.overwrite
= 0;
357 lum
.u
.channel
.subbuf_size
= 32768;
358 lum
.u
.channel
.num_subbuf
= 4;
359 lum
.u
.channel
.switch_timer_interval
= 0;
360 lum
.u
.channel
.read_timer_interval
= 0;
361 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
362 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
365 metadata_data
[i
].handle
= lur
.ret_val
;
366 printf("received metadata handle %u\n", metadata_data
[i
].handle
);
367 if (lur
.ret_code
== USTCOMM_OK
) {
370 metadata_data
[i
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
372 len
= ustcomm_recv_fd(sock
);
375 metadata_data
[i
].shm_fd
= len
;
377 len
= ustcomm_recv_fd(sock
);
380 metadata_data
[i
].wait_fd
= len
;
383 ret
= open_streams(sock
, metadata_data
[i
].handle
,
384 &metadata_stream_data
[i
], 1);
386 printf("Error in open_streams\n");
390 /* Create channels */
391 for (j
= 0; j
< NR_CHANNELS
; j
++) {
392 memset(&lum
, 0, sizeof(lum
));
393 lum
.handle
= session_handle
[i
];
394 lum
.cmd
= LTTNG_UST_CHANNEL
;
395 //lum.u.channel.overwrite = 0;
396 lum
.u
.channel
.overwrite
= 1;
397 lum
.u
.channel
.subbuf_size
= 32768;
398 lum
.u
.channel
.num_subbuf
= 8;
399 //lum.u.channel.num_subbuf = 4;
400 //lum.u.channel.num_subbuf = 2;
401 lum
.u
.channel
.switch_timer_interval
= 0;
402 lum
.u
.channel
.read_timer_interval
= 0;
403 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
404 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
407 channel_data
[i
][j
].handle
= lur
.ret_val
;
408 printf("received channel handle %u\n", channel_data
[i
][j
].handle
);
409 if (lur
.ret_code
== USTCOMM_OK
) {
412 channel_data
[i
][j
].memory_map_size
= lur
.u
.channel
.memory_map_size
;
414 len
= ustcomm_recv_fd(sock
);
417 channel_data
[i
][j
].shm_fd
= len
;
419 len
= ustcomm_recv_fd(sock
);
422 channel_data
[i
][j
].wait_fd
= len
;
426 for (k
= 0; k
< NR_EVENTS
; k
++) {
427 memset(&lum
, 0, sizeof(lum
));
428 lum
.handle
= channel_data
[i
][j
].handle
;
429 lum
.cmd
= LTTNG_UST_EVENT
;
430 strncpy(lum
.u
.event
.name
, evname
[k
],
431 LTTNG_UST_SYM_NAME_LEN
);
432 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
433 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
436 event_handle
[i
][j
][k
] = lur
.ret_val
;
437 printf("received event handle %u\n", event_handle
[i
][j
][k
]);
440 /* Get references to channel streams */
441 ret
= open_streams(sock
, channel_data
[i
][j
].handle
,
442 stream_data
[i
][j
], MAX_NR_STREAMS
);
444 printf("Error in open_streams\n");
449 memset(&lum
, 0, sizeof(lum
));
450 lum
.handle
= session_handle
[i
];
451 lum
.cmd
= LTTNG_UST_SESSION_START
;
452 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
455 printf("Session handle %u started.\n", session_handle
[i
]);
458 /* Tell application registration is done */
459 memset(&lum
, 0, sizeof(lum
));
460 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
461 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
462 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
465 printf("Registration done acknowledged.\n");
469 ret
= consume_buffers();
471 printf("Error in consume_buffers\n");
475 for (i
= 0; i
< NR_SESSIONS
; i
++) {
476 /* Release channels */
477 for (j
= 0; j
< NR_CHANNELS
; j
++) {
478 /* Release streams */
479 ret
= close_streams(sock
, stream_data
[i
][j
],
485 for (k
= 0; k
< NR_EVENTS
; k
++) {
486 memset(&lum
, 0, sizeof(lum
));
487 lum
.handle
= event_handle
[i
][j
][k
];
488 lum
.cmd
= LTTNG_UST_RELEASE
;
489 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
493 memset(&lum
, 0, sizeof(lum
));
494 lum
.handle
= channel_data
[i
][j
].handle
;
495 lum
.cmd
= LTTNG_UST_RELEASE
;
496 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
499 if (channel_data
[i
][j
].shm_fd
>= 0) {
500 ret
= close(channel_data
[i
][j
].shm_fd
);
504 if (channel_data
[i
][j
].wait_fd
>= 0) {
505 ret
= close(channel_data
[i
][j
].wait_fd
);
511 /* Release metadata channel */
512 ret
= close_streams(sock
, &metadata_stream_data
[i
], 1);
516 memset(&lum
, 0, sizeof(lum
));
517 lum
.handle
= metadata_data
[i
].handle
;
518 lum
.cmd
= LTTNG_UST_RELEASE
;
519 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
522 if (metadata_data
[i
].shm_fd
>= 0) {
523 ret
= close(metadata_data
[i
].shm_fd
);
527 if (metadata_data
[i
].wait_fd
>= 0) {
528 ret
= close(metadata_data
[i
].wait_fd
);
533 /* Release session */
534 memset(&lum
, 0, sizeof(lum
));
535 lum
.handle
= session_handle
[i
];
536 lum
.cmd
= LTTNG_UST_RELEASE
;
537 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
546 * Using fork to set umask in the child process (not multi-thread safe). We
547 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
548 * the shm and does not let everybody modify it, to ensure safety against
549 * shm_unlink) by simply letting the mmap fail and retrying after a few
550 * seconds. For global shm, everybody has rw access to it until the sessiond
553 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
555 int wait_shm_fd
, ret
;
558 /* Default permissions */
559 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
561 /* Change owner of the shm path */
563 ret
= chown(shm_path
, 0, 0);
565 if (errno
!= ENOENT
) {
566 perror("chown wait shm");
572 * If global session daemon, any application can register so the shm
573 * needs to be set in read-only mode for others.
577 ret
= chown(shm_path
, getuid(), getgid());
579 if (errno
!= ENOENT
) {
580 perror("chown wait shm");
587 * Set permissions to the shm even if we did not create the shm.
589 ret
= chmod(shm_path
, mode
);
591 if (errno
!= ENOENT
) {
592 perror("chmod wait shm");
598 * We're alone in a child process, so we can modify the process-wide
604 * Try creating shm (or get rw access). We don't do an exclusive open,
605 * because we allow other processes to create+ftruncate it concurrently.
607 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
608 if (wait_shm_fd
< 0) {
609 perror("shm_open wait shm");
613 ret
= ftruncate(wait_shm_fd
, mmap_size
);
615 perror("ftruncate wait shm");
619 ret
= fchmod(wait_shm_fd
, mode
);
625 printf("Got the wait shm fd %d\n", wait_shm_fd
);
630 printf("Failing to get the wait shm fd\n");
635 int update_futex(int fd
, int active
)
637 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
641 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
643 if (wait_shm_mmap
== MAP_FAILED
) {
649 uatomic_set((int32_t *) wait_shm_mmap
, 1);
650 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
651 INT_MAX
, NULL
, NULL
, 0);
653 uatomic_set((int32_t *) wait_shm_mmap
, 0);
655 ret
= munmap(wait_shm_mmap
, mmap_size
);
657 perror("Error unmapping wait shm");
666 * Set open files limit to unlimited. This daemon can open a large number of
667 * file descriptors in order to consumer multiple kernel traces.
669 static void set_ulimit(void)
675 * If not root, we cannot increase our max open files. But our
676 * scope is then limited to processes from a single user.
680 /* The kernel does not allowed an infinite limit for open files */
681 lim
.rlim_cur
= 65535;
682 lim
.rlim_max
= 65535;
684 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
686 perror("failed to set open files limit");
690 int main(int argc
, char **argv
)
692 const char *home_dir
;
693 int ret
, wait_shm_fd
;
694 struct sigaction act
;
695 mode_t old_umask
= 0;
700 memset(&act
, 0, sizeof(act
));
701 ret
= sigemptyset(&act
.sa_mask
);
703 perror("sigemptyset");
707 act
.sa_handler
= SIG_IGN
;
708 ret
= sigaction(SIGPIPE
, &act
, NULL
);
715 act
.sa_handler
= handle_signals
;
716 ret
= sigaction(SIGTERM
, &act
, NULL
);
722 ret
= sigaction(SIGINT
, &act
, NULL
);
728 if (geteuid() == 0) {
729 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
730 if (ret
&& errno
!= EEXIST
) {
734 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
735 sysconf(_SC_PAGE_SIZE
), 1);
736 if (wait_shm_fd
< 0) {
737 perror("global wait shm error");
740 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
741 old_umask
= umask(0);
743 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
744 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
745 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
746 sysconf(_SC_PAGE_SIZE
), 0);
747 if (wait_shm_fd
< 0) {
748 perror("local wait shm error");
751 home_dir
= (const char *) getenv("HOME");
753 perror("getenv error");
756 snprintf(apps_sock_path
, PATH_MAX
,
757 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
760 ret
= ustcomm_create_unix_sock(apps_sock_path
);
762 perror("create error");
768 /* File permission MUST be 666 */
769 ret
= chmod(apps_sock_path
,
770 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
772 printf("Set file permissions failed: %s\n", apps_sock_path
);
778 ret
= ustcomm_listen_unix_sock(apps_socket
);
780 perror("listen error");
784 /* wake up futexes */
785 ret
= update_futex(wait_shm_fd
, 1);
787 fprintf(stderr
, "Error wakeup futex\n");
801 char name
[16]; /* Process name */
808 printf("Accepting application registration\n");
809 sock
= ustcomm_accept_unix_sock(apps_socket
);
811 perror("accept error");
816 * Basic recv here to handle the very simple data
817 * that the libust send to register (reg_msg).
819 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
820 if (len
< 0 || len
!= sizeof(reg_msg
)) {
821 perror("ustcomm_recv_unix_sock");
824 memcpy(bufname
, reg_msg
.name
, 16);
826 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
827 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
828 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
829 ret
= send_app_msgs(sock
);
831 printf("Error in send_app_msgs.\n");
838 printf("quitting.\n");
839 /* Let applications know we are not responding anymore */
840 ret
= update_futex(wait_shm_fd
, 0);
842 fprintf(stderr
, "Error wakeup futex\n");