usertrace fast work
[lttv.git] / usertrace-fast / ltt-usertrace-fast.c
CommitLineData
700d350d 1/* LTTng user-space "fast" library
2 *
3 * This daemon is spawned by each traced thread (to share the mmap).
4 *
5 * Its job is to dump periodically this buffer to disk (when it receives a
6 * SIGUSR1 from its parent).
7 *
8 * It uses the control information in the shared memory area (producer/consumer
9 * count).
10 *
11 * When the parent thread dies (yes, those thing may happen) ;) , this daemon
12 * will flush the last buffer and write it to disk.
13 *
14 * Supplement note for streaming : the daemon is responsible for flushing
15 * periodically the buffer if it is streaming data.
16 *
b09f3215 17 *
700d350d 18 * Notes :
19 * shm memory is typically limited to 4096 units (system wide limit SHMMNI in
20 * /proc/sys/kernel/shmmni). As it requires computation time upon creation, we
21 * do not use it : we will use a shared mmap() instead which is passed through
22 * the fork().
23 * MAP_SHARED mmap segment. Updated when msync or munmap are called.
24 * MAP_ANONYMOUS.
25 * Memory mapped by mmap() is preserved across fork(2), with the same
26 * attributes.
27 *
28 * Eventually, there will be two mode :
29 * * Slow thread spawn : a fork() is done for each new thread. If the process
30 * dies, the data is not lost.
31 * * Fast thread spawn : a pthread_create() is done by the application for each
32 * new thread.
b09f3215 33 * Copyright 2006 Mathieu Desnoyers
34 *
35 */
36
b09f3215 37#include <sys/types.h>
38#include <sys/wait.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <signal.h>
43#include <syscall.h>
44#include <features.h>
45#include <pthread.h>
46#include <malloc.h>
47#include <string.h>
700d350d 48#include <sys/mman.h>
49#include <signal.h>
b09f3215 50
1c48e587 51#include "ltt-usertrace-fast.h"
b09f3215 52
51bf1553 53/* TLS for the trace buffer
b09f3215 54 * http://www.dis.com/gnu/gcc/C--98-Thread-Local-Edits.html
55 *
56 * Add after paragraph 4
57 *
58 * The storage for an object of thread storage duration shall be statically
59 * initialized before the first statement of the thread startup function. An
60 * object of thread storage duration shall not require dynamic
61 * initialization.
b09f3215 62 */
700d350d 63#if 0
64__thread struct ltt_trace_info ltt_trace_info =
51bf1553 65{
66 .init = 0,
67 .filter = 0,
68 .nesting = ATOMIC_INIT(0),
69 .channel.facilities =
70 { ATOMIC_INIT(0),
b09f3215 71 ATOMIC_INIT(0),
72 ATOMIC_INIT(0),
51bf1553 73 ATOMIC_INIT(0)
74 },
75 .channel.cpu =
76 { ATOMIC_INIT(0),
b09f3215 77 ATOMIC_INIT(0),
78 ATOMIC_INIT(0),
79 ATOMIC_INIT(0)
51bf1553 80 },
b09f3215 81};
700d350d 82#endif //0
b09f3215 83
700d350d 84__thread struct ltt_trace_info *thread_trace_info = NULL;
b09f3215 85
700d350d 86
87/* signal handling */
88
89static void handler(int signo)
b09f3215 90{
700d350d 91 printf("Signal %d received\n", signo);
92}
b09f3215 93
700d350d 94
95
96void ltt_usertrace_fast_buffer_switch(void)
97{
98 kill(thread_trace_info->daemon_id, SIGUSR1);
b09f3215 99}
100
700d350d 101static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info,
102 sigset_t oldset)
103{
104 struct sigaction act;
105 int ret;
106
107 printf("ltt_usertrace_fast_daemon : init is %d, pid is %lu\n",
108 shared_trace_info->init, getpid());
109
110 act.sa_handler = handler;
111 act.sa_flags = 0;
112 sigemptyset(&(act.sa_mask));
113 sigaddset(&(act.sa_mask), SIGUSR1);
114 sigaction(SIGUSR1, &act, NULL);
115 /* Enable signals */
116 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
117 if(ret) {
118 printf("Error in pthread_sigmask\n");
119 }
120
121 while(1) {
122 pause();
123 printf("Doing a buffer switch read. pid is : %lu\n", getpid());
124 }
125
126}
b09f3215 127
51bf1553 128void ltt_thread_init(void)
b09f3215 129{
700d350d 130 pid_t pid;
131 struct ltt_trace_info *shared_trace_info;
132 int ret;
133 sigset_t set, oldset;
134
135 /* parent : create the shared memory map */
136 shared_trace_info = thread_trace_info = mmap(0, sizeof(*thread_trace_info),
137 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
138 memset(shared_trace_info, 0, sizeof(*thread_trace_info));
139 thread_trace_info->init = 1;
140
141 /* Disable signals */
142 ret = sigfillset(&set);
143 if(ret) {
144 printf("Error in sigfillset\n");
145 }
146
147
148 ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
149 if(ret) {
150 printf("Error in pthread_sigmask\n");
151 }
152
153 pid = fork();
154 if(pid > 0) {
155 /* Parent */
156 thread_trace_info->daemon_id = pid;
157
158 /* Enable signals */
159 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
160 if(ret) {
161 printf("Error in pthread_sigmask\n");
162 }
163 } else if(pid == 0) {
164 /* Child */
165 ltt_usertrace_fast_daemon(shared_trace_info, oldset);
166 /* Should never return */
167 exit(-1);
168 } else if(pid < 0) {
169 /* fork error */
170 perror("Error in forking ltt-usertrace-fast");
171 }
b09f3215 172}
173
04180f7f 174void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
b09f3215 175{
700d350d 176 printf("LTT usertrace-fast init\n");
b09f3215 177
1c48e587 178 ltt_thread_init();
700d350d 179}
180
181void __attribute__((destructor)) __ltt_usertrace_fast_fini(void)
182{
183 printf("LTT usertrace-fast fini\n");
b09f3215 184
b09f3215 185}
186
This page took 0.030247 seconds and 4 git commands to generate.