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.
a85b8f41 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 *
b09f3215 40 * Copyright 2006 Mathieu Desnoyers
41 *
42 */
43
b09f3215 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>
700d350d 55#include <sys/mman.h>
56#include <signal.h>
b09f3215 57
1c48e587 58#include "ltt-usertrace-fast.h"
b09f3215 59
b09f3215 60
e8efa18d 61/* Writer (the traced application) */
b09f3215 62
e8efa18d 63__thread struct ltt_trace_info *thread_trace_info = NULL;
700d350d 64
e8efa18d 65void ltt_usertrace_fast_buffer_switch(void)
66{
a85b8f41 67 struct ltt_trace_info *tmp = thread_trace_info;
68 if(tmp)
69 kill(tmp->daemon_id, SIGUSR1);
e8efa18d 70}
700d350d 71
e8efa18d 72static void ltt_usertrace_fast_cleanup(void *arg)
b09f3215 73{
a85b8f41 74 struct ltt_trace_info *tmp = thread_trace_info;
75 if(tmp) {
76 thread_trace_info = NULL;
77 kill(tmp->daemon_id, SIGUSR2);
78 munmap(tmp, sizeof(*tmp));
79 }
700d350d 80}
b09f3215 81
e8efa18d 82/* Reader (the disk dumper daemon) */
700d350d 83
a85b8f41 84static pid_t traced_pid = 0;
e8efa18d 85static int parent_exited = 0;
700d350d 86
e8efa18d 87/* signal handling */
88static void handler_sigusr1(int signo)
700d350d 89{
a35eaa9c 90 printf("LTT Signal %d received : parent buffer switch.\n", signo);
e8efa18d 91}
92
93static void handler_sigusr2(int signo)
94{
a35eaa9c 95 printf("LTT Signal %d received : parent exited.\n", signo);
e8efa18d 96 parent_exited = 1;
97}
98
99static void handler_sigalarm(int signo)
100{
a35eaa9c 101 printf("LTT Signal %d received\n", signo);
e8efa18d 102
a85b8f41 103 if(getppid() != traced_pid) {
e8efa18d 104 /* Parent died */
a85b8f41 105 printf("LTT Parent %lu died, cleaning up\n", traced_pid);
106 traced_pid = 0;
e8efa18d 107 }
108 alarm(3);
b09f3215 109}
110
e8efa18d 111
a85b8f41 112/* This function is called by ltt_rw_init which has signals blocked */
700d350d 113static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info,
a85b8f41 114 sigset_t oldset, pid_t l_traced_pid)
700d350d 115{
116 struct sigaction act;
117 int ret;
118
a85b8f41 119 traced_pid = l_traced_pid;
e8efa18d 120
a85b8f41 121 printf("LTT ltt_usertrace_fast_daemon : init is %d, pid is %lu, traced_pid is %lu\n",
122 shared_trace_info->init, getpid(), traced_pid);
700d350d 123
e8efa18d 124 act.sa_handler = handler_sigusr1;
700d350d 125 act.sa_flags = 0;
126 sigemptyset(&(act.sa_mask));
127 sigaddset(&(act.sa_mask), SIGUSR1);
128 sigaction(SIGUSR1, &act, NULL);
e8efa18d 129
130 act.sa_handler = handler_sigusr2;
131 act.sa_flags = 0;
132 sigemptyset(&(act.sa_mask));
133 sigaddset(&(act.sa_mask), SIGUSR2);
134 sigaction(SIGUSR2, &act, NULL);
135
136 act.sa_handler = handler_sigalarm;
137 act.sa_flags = 0;
138 sigemptyset(&(act.sa_mask));
139 sigaddset(&(act.sa_mask), SIGALRM);
140 sigaction(SIGALRM, &act, NULL);
141
700d350d 142 /* Enable signals */
143 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
144 if(ret) {
a35eaa9c 145 printf("LTT Error in pthread_sigmask\n");
700d350d 146 }
147
e8efa18d 148 alarm(3);
149
700d350d 150 while(1) {
151 pause();
a85b8f41 152 if(traced_pid == 0) break; /* parent died */
e8efa18d 153 if(parent_exited) break;
a35eaa9c 154 printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid());
e8efa18d 155 //printf("Test parent. pid is : %lu, ppid is %lu\n", getpid(), getppid());
700d350d 156 }
157
e8efa18d 158 /* Buffer force switch (flush) */
159 //TODO
160
161 /* The parent thread is dead and we have finished with the buffer */
162 munmap(shared_trace_info, sizeof(*shared_trace_info));
163
164 exit(0);
700d350d 165}
b09f3215 166
e8efa18d 167
168/* Reader-writer initialization */
169
170static enum ltt_process_role { LTT_ROLE_WRITER, LTT_ROLE_READER }
171 role = LTT_ROLE_WRITER;
172
173
174void ltt_rw_init(void)
b09f3215 175{
700d350d 176 pid_t pid;
177 struct ltt_trace_info *shared_trace_info;
178 int ret;
179 sigset_t set, oldset;
a85b8f41 180 pid_t l_traced_pid = getpid();
700d350d 181
182 /* parent : create the shared memory map */
a85b8f41 183 shared_trace_info = mmap(0, sizeof(*thread_trace_info),
700d350d 184 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
a85b8f41 185 memset(shared_trace_info, 0, sizeof(*shared_trace_info));
186 shared_trace_info->init = 1;
700d350d 187
188 /* Disable signals */
189 ret = sigfillset(&set);
190 if(ret) {
a35eaa9c 191 printf("LTT Error in sigfillset\n");
700d350d 192 }
193
194
195 ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
196 if(ret) {
a35eaa9c 197 printf("LTT Error in pthread_sigmask\n");
700d350d 198 }
a85b8f41 199
700d350d 200 pid = fork();
201 if(pid > 0) {
202 /* Parent */
a85b8f41 203 shared_trace_info->daemon_id = pid;
204 thread_trace_info = shared_trace_info;
700d350d 205
206 /* Enable signals */
207 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
208 if(ret) {
a35eaa9c 209 printf("LTT Error in pthread_sigmask\n");
700d350d 210 }
211 } else if(pid == 0) {
212 /* Child */
e8efa18d 213 role = LTT_ROLE_READER;
a85b8f41 214 ltt_usertrace_fast_daemon(shared_trace_info, oldset, l_traced_pid);
700d350d 215 /* Should never return */
216 exit(-1);
217 } else if(pid < 0) {
218 /* fork error */
a35eaa9c 219 perror("LTT Error in forking ltt-usertrace-fast");
700d350d 220 }
b09f3215 221}
222
e8efa18d 223static __thread struct _pthread_cleanup_buffer cleanup_buffer;
224
225void ltt_thread_init(void)
226{
227 _pthread_cleanup_push(&cleanup_buffer, ltt_usertrace_fast_cleanup, NULL);
228 ltt_rw_init();
229}
230
04180f7f 231void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
b09f3215 232{
700d350d 233 printf("LTT usertrace-fast init\n");
b09f3215 234
e8efa18d 235 ltt_rw_init();
700d350d 236}
237
238void __attribute__((destructor)) __ltt_usertrace_fast_fini(void)
239{
e8efa18d 240 if(role == LTT_ROLE_WRITER) {
241 printf("LTT usertrace-fast fini\n");
242 ltt_usertrace_fast_cleanup(NULL);
243 }
b09f3215 244}
245
This page took 0.033374 seconds and 4 git commands to generate.