futex
[lttv.git] / usertrace-fast / ltt-usertrace-fast.c
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 *
17 *
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.
33 *
34 * We use a timer to check periodically if the parent died. I think it is less
35 * intrusive than a ptrace() on the parent, which would get every signal. The
36 * side effect of this is that we won't be notified if the parent does an
37 * exec(). In this case, we will just sit there until the parent exits.
38 *
39 *
40 * Copyright 2006 Mathieu Desnoyers
41 *
42 */
43
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <signal.h>
50 #include <syscall.h>
51 #include <features.h>
52 #include <pthread.h>
53 #include <malloc.h>
54 #include <string.h>
55 #include <sys/mman.h>
56 #include <signal.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <stdlib.h>
60 #include <sys/param.h>
61 #include <linux/futex.h>
62 #include <sys/time.h>
63
64 #include <asm/timex.h> //for get_cycles()
65
66 #include "ltt-usertrace-fast.h"
67
68
69 /* Writer (the traced application) */
70
71 __thread struct ltt_trace_info *thread_trace_info = NULL;
72
73 void ltt_usertrace_fast_buffer_switch(void)
74 {
75 struct ltt_trace_info *tmp = thread_trace_info;
76 if(tmp)
77 kill(tmp->daemon_id, SIGUSR1);
78 }
79
80 /* The cleanup should never be called from a signal handler */
81 static void ltt_usertrace_fast_cleanup(void *arg)
82 {
83 struct ltt_trace_info *tmp = thread_trace_info;
84 if(tmp) {
85 thread_trace_info = NULL;
86 kill(tmp->daemon_id, SIGUSR2);
87 munmap(tmp, sizeof(*tmp));
88 }
89 }
90
91 /* Reader (the disk dumper daemon) */
92
93 static pid_t traced_pid = 0;
94 static pthread_t traced_thread = 0;
95 static int parent_exited = 0;
96
97 /* signal handling */
98 static void handler_sigusr1(int signo)
99 {
100 printf("LTT Signal %d received : parent buffer switch.\n", signo);
101 }
102
103 static void handler_sigusr2(int signo)
104 {
105 printf("LTT Signal %d received : parent exited.\n", signo);
106 parent_exited = 1;
107 }
108
109 static void handler_sigalarm(int signo)
110 {
111 printf("LTT Signal %d received\n", signo);
112
113 if(getppid() != traced_pid) {
114 /* Parent died */
115 printf("LTT Parent %lu died, cleaning up\n", traced_pid);
116 traced_pid = 0;
117 }
118 alarm(3);
119 }
120
121 /* Do a buffer switch. Don't switch if buffer is completely empty */
122 static void flush_buffer(struct ltt_buf *ltt_buf)
123 {
124
125
126 }
127
128 static inline int ltt_buffer_get(struct ltt_buf *ltt_buf,
129 unsigned int *offset)
130 {
131 unsigned int consumed_old, consumed_idx;
132 consumed_old = atomic_read(&ltt_buf->consumed);
133 consumed_idx = SUBBUF_INDEX(consumed_old, ltt_buf);
134
135 if(atomic_read(&ltt_buf->commit_count[consumed_idx])
136 != atomic_read(&ltt_buf->reserve_count[consumed_idx])) {
137 return -EAGAIN;
138 }
139 if((SUBBUF_TRUNC(atomic_read(&ltt_buf->offset), ltt_buf)
140 -SUBBUF_TRUNC(consumed_old, ltt_buf)) == 0) {
141 return -EAGAIN;
142 }
143
144 *offset = consumed_old;
145
146 return 0;
147 }
148
149 static inline int ltt_buffer_put(struct ltt_buf *ltt_buf,
150 unsigned int offset)
151 {
152 unsigned int consumed_old, consumed_new;
153 int ret;
154
155 consumed_old = offset;
156 consumed_new = SUBBUF_ALIGN(consumed_old, ltt_buf);
157 if(atomic_cmpxchg(&ltt_buf->consumed, consumed_old, consumed_new)
158 != consumed_old) {
159 /* We have been pushed by the writer : the last buffer read _is_
160 * corrupted!
161 * It can also happen if this is a buffer we never got. */
162 return -EIO;
163 } else {
164 if(atomic_read(&ltt_buf->full) == 1) {
165 /* tell the client that buffer is now unfull */
166 ret = futex(&ltt_buf->full, FUTEX_WAKE, 1, NULL, NULL, 0);
167 if(ret != 1) {
168 printf("LTT warning : race condition : writer not waiting or too many writers\n");
169 }
170 atomic_set(&ltt_buf->full, 0);
171 }
172 }
173 }
174
175 /* In the writer :
176 *
177 * if(buffer full condition) {
178 * put myself in the wait queue
179 * ltt_buf->full = 1;
180 * schedule
181 * }
182 *{
183 if(buffer_is_full) {
184 atomic_set(&ltt_buf->full, 1);
185 ret = futex(&ltt_buf->full, 1, NULL, NULL, 0);
186 }
187 }
188
189 */
190
191 static int read_subbuffer(struct ltt_buf *ltt_buf, int fd)
192 {
193 int err;
194 printf("LTT read buffer\n");
195
196
197 err = ltt_buffer_get(&shared_trace_info->channel.cpu, &consumed_old);
198 if(err != -EAGAIN && err != 0) {
199 printf("LTT Reserving sub buffer failed\n");
200 goto get_error;
201 }
202
203 err = TEMP_FAILURE_RETRY(write(fd,
204 ltt_buf->start
205 + (consumed_old & ((ltt_buf->alloc_size)-1)),
206 ltt_buf->subbuf_size));
207
208 if(err < 0) {
209 perror("Error in writing to file");
210 goto write_error;
211 }
212 #if 0
213 err = fsync(pair->trace);
214 if(err < 0) {
215 ret = errno;
216 perror("Error in writing to file");
217 goto write_error;
218 }
219 #endif //0
220 write_error:
221 err = ltt_buffer_put(&shared_trace_info->channel.cpu, consumed_old);
222
223 if(err != 0) {
224 if(err == -EIO) {
225 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
226 /* FIXME : we may delete the last written buffer if we wish. */
227 }
228 goto get_error;
229 }
230
231 get_error:
232 return err;
233 }
234
235 /* This function is called by ltt_rw_init which has signals blocked */
236 static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info,
237 sigset_t oldset, pid_t l_traced_pid, pthread_t l_traced_thread)
238 {
239 struct sigaction act;
240 int ret;
241 int fd_fac;
242 int fd_cpu;
243 char outfile_name[PATH_MAX];
244 char identifier_name[PATH_MAX];
245
246
247 traced_pid = l_traced_pid;
248 traced_thread = l_traced_thread;
249
250 printf("LTT ltt_usertrace_fast_daemon : init is %d, pid is %lu, traced_pid is %lu\n",
251 shared_trace_info->init, getpid(), traced_pid);
252
253 act.sa_handler = handler_sigusr1;
254 act.sa_flags = 0;
255 sigemptyset(&(act.sa_mask));
256 sigaddset(&(act.sa_mask), SIGUSR1);
257 sigaction(SIGUSR1, &act, NULL);
258
259 act.sa_handler = handler_sigusr2;
260 act.sa_flags = 0;
261 sigemptyset(&(act.sa_mask));
262 sigaddset(&(act.sa_mask), SIGUSR2);
263 sigaction(SIGUSR2, &act, NULL);
264
265 act.sa_handler = handler_sigalarm;
266 act.sa_flags = 0;
267 sigemptyset(&(act.sa_mask));
268 sigaddset(&(act.sa_mask), SIGALRM);
269 sigaction(SIGALRM, &act, NULL);
270
271 /* Enable signals */
272 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
273 if(ret) {
274 printf("LTT Error in pthread_sigmask\n");
275 }
276
277 alarm(3);
278
279 /* Open output files */
280 umask(00000);
281 ret = mkdir(LTT_USERTRACE_ROOT, 0777);
282 if(ret < 0 && errno != EEXIST) {
283 perror("LTT Error in creating output (mkdir)");
284 exit(-1);
285 }
286 ret = chdir(LTT_USERTRACE_ROOT);
287 if(ret < 0) {
288 perror("LTT Error in creating output (chdir)");
289 exit(-1);
290 }
291 snprintf(identifier_name, PATH_MAX-1, "%lu.%lu.%llu",
292 traced_pid, traced_thread, get_cycles());
293 snprintf(outfile_name, PATH_MAX-1, "facilities-%s", identifier_name);
294 fd_fac = creat(outfile_name, 0644);
295
296 snprintf(outfile_name, PATH_MAX-1, "cpu-%s", identifier_name);
297 fd_cpu = creat(outfile_name, 0644);
298
299
300 while(1) {
301 pause();
302 if(traced_pid == 0) break; /* parent died */
303 if(parent_exited) break;
304 printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid());
305
306 do {
307 ret = read_buffer(&shared_trace_info->channel.cpu, fd_cpu);
308 } while(ret == 0);
309
310 do {
311 ret = read_buffer(&shared_trace_info->channel.facilities, fd_fac);
312 } while(ret == 0);
313 }
314
315 /* Buffer force switch (flush) */
316 flush_buffer(&shared_trace_info->channel.cpu);
317 do {
318 ret = read_buffer(&shared_trace_info->channel.cpu, fd_cpu);
319 } while(ret == 0);
320
321
322 flush_buffer(&shared_trace_info->channel.facilities);
323 do {
324 ret = read_buffer(&shared_trace_info->channel.facilities, fd_fac);
325 } while(ret == 0);
326
327 close(fd_fac);
328 close(fd_cpu);
329
330 /* The parent thread is dead and we have finished with the buffer */
331 munmap(shared_trace_info, sizeof(*shared_trace_info));
332
333 exit(0);
334 }
335
336
337 /* Reader-writer initialization */
338
339 static enum ltt_process_role { LTT_ROLE_WRITER, LTT_ROLE_READER }
340 role = LTT_ROLE_WRITER;
341
342
343 void ltt_rw_init(void)
344 {
345 pid_t pid;
346 struct ltt_trace_info *shared_trace_info;
347 int ret;
348 sigset_t set, oldset;
349 pid_t l_traced_pid = getpid();
350 pthread_t l_traced_thread = pthread_self();
351
352 /* parent : create the shared memory map */
353 shared_trace_info = mmap(0, sizeof(*thread_trace_info),
354 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
355 memset(shared_trace_info, 0, sizeof(*shared_trace_info));
356 /* Tricky semaphore : is in a shared memory space, so it's ok for a fast
357 * mutex (futex). */
358 atomic_set(&shared_trace_info->channel.facilities.full, 0);
359 shared_trace_info->channel.facilities.alloc_size = LTT_BUF_SIZE_FACILITIES;
360 shared_trace_info->channel.facilities.subbuf_size = LTT_SUBBUF_SIZE_FACILITIES;
361 atomic_set(&shared_trace_info->channel.cpu.full, 0);
362 shared_trace_info->channel.cpu.alloc_size = LTT_BUF_SIZE_CPU;
363 shared_trace_info->channel.cpu.subbuf_size = LTT_SUBBUF_SIZE_CPU;
364 shared_trace_info->init = 1;
365
366 /* Disable signals */
367 ret = sigfillset(&set);
368 if(ret) {
369 printf("LTT Error in sigfillset\n");
370 }
371
372
373 ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
374 if(ret) {
375 printf("LTT Error in pthread_sigmask\n");
376 }
377
378 pid = fork();
379 if(pid > 0) {
380 /* Parent */
381 shared_trace_info->daemon_id = pid;
382 thread_trace_info = shared_trace_info;
383
384 /* Enable signals */
385 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
386 if(ret) {
387 printf("LTT Error in pthread_sigmask\n");
388 }
389 } else if(pid == 0) {
390 /* Child */
391 role = LTT_ROLE_READER;
392 ltt_usertrace_fast_daemon(shared_trace_info, oldset, l_traced_pid,
393 l_traced_thread);
394 /* Should never return */
395 exit(-1);
396 } else if(pid < 0) {
397 /* fork error */
398 perror("LTT Error in forking ltt-usertrace-fast");
399 }
400 }
401
402 static __thread struct _pthread_cleanup_buffer cleanup_buffer;
403
404 void ltt_thread_init(void)
405 {
406 _pthread_cleanup_push(&cleanup_buffer, ltt_usertrace_fast_cleanup, NULL);
407 ltt_rw_init();
408 }
409
410 void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
411 {
412 printf("LTT usertrace-fast init\n");
413
414 ltt_rw_init();
415 }
416
417 void __attribute__((destructor)) __ltt_usertrace_fast_fini(void)
418 {
419 if(role == LTT_ROLE_WRITER) {
420 printf("LTT usertrace-fast fini\n");
421 ltt_usertrace_fast_cleanup(NULL);
422 }
423 }
424
This page took 0.038289 seconds and 5 git commands to generate.