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> |
77b31f39 |
57 | #include <sys/stat.h> |
58 | #include <fcntl.h> |
59 | #include <stdlib.h> |
60 | #include <sys/param.h> |
47d7d576 |
61 | #include <linux/futex.h> |
62 | #include <sys/time.h> |
77b31f39 |
63 | |
64 | #include <asm/timex.h> //for get_cycles() |
b09f3215 |
65 | |
1c48e587 |
66 | #include "ltt-usertrace-fast.h" |
b09f3215 |
67 | |
b09f3215 |
68 | |
e8efa18d |
69 | /* Writer (the traced application) */ |
b09f3215 |
70 | |
e8efa18d |
71 | __thread struct ltt_trace_info *thread_trace_info = NULL; |
700d350d |
72 | |
e8efa18d |
73 | void ltt_usertrace_fast_buffer_switch(void) |
74 | { |
a85b8f41 |
75 | struct ltt_trace_info *tmp = thread_trace_info; |
76 | if(tmp) |
77 | kill(tmp->daemon_id, SIGUSR1); |
e8efa18d |
78 | } |
700d350d |
79 | |
77b31f39 |
80 | /* The cleanup should never be called from a signal handler */ |
e8efa18d |
81 | static void ltt_usertrace_fast_cleanup(void *arg) |
b09f3215 |
82 | { |
a85b8f41 |
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 | } |
700d350d |
89 | } |
b09f3215 |
90 | |
e8efa18d |
91 | /* Reader (the disk dumper daemon) */ |
700d350d |
92 | |
a85b8f41 |
93 | static pid_t traced_pid = 0; |
77b31f39 |
94 | static pthread_t traced_thread = 0; |
e8efa18d |
95 | static int parent_exited = 0; |
700d350d |
96 | |
e8efa18d |
97 | /* signal handling */ |
98 | static void handler_sigusr1(int signo) |
700d350d |
99 | { |
a35eaa9c |
100 | printf("LTT Signal %d received : parent buffer switch.\n", signo); |
e8efa18d |
101 | } |
102 | |
103 | static void handler_sigusr2(int signo) |
104 | { |
a35eaa9c |
105 | printf("LTT Signal %d received : parent exited.\n", signo); |
e8efa18d |
106 | parent_exited = 1; |
107 | } |
108 | |
109 | static void handler_sigalarm(int signo) |
110 | { |
a35eaa9c |
111 | printf("LTT Signal %d received\n", signo); |
e8efa18d |
112 | |
a85b8f41 |
113 | if(getppid() != traced_pid) { |
e8efa18d |
114 | /* Parent died */ |
a85b8f41 |
115 | printf("LTT Parent %lu died, cleaning up\n", traced_pid); |
116 | traced_pid = 0; |
e8efa18d |
117 | } |
118 | alarm(3); |
b09f3215 |
119 | } |
120 | |
47d7d576 |
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(<t_buf->consumed); |
133 | consumed_idx = SUBBUF_INDEX(consumed_old, ltt_buf); |
134 | |
135 | if(atomic_read(<t_buf->commit_count[consumed_idx]) |
136 | != atomic_read(<t_buf->reserve_count[consumed_idx])) { |
137 | return -EAGAIN; |
138 | } |
139 | if((SUBBUF_TRUNC(atomic_read(<t_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(<t_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(<t_buf->full) == 1) { |
165 | /* tell the client that buffer is now unfull */ |
166 | ret = futex(<t_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(<t_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(<t_buf->full, 1); |
185 | ret = futex(<t_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 | } |
e8efa18d |
234 | |
a85b8f41 |
235 | /* This function is called by ltt_rw_init which has signals blocked */ |
700d350d |
236 | static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info, |
77b31f39 |
237 | sigset_t oldset, pid_t l_traced_pid, pthread_t l_traced_thread) |
700d350d |
238 | { |
239 | struct sigaction act; |
240 | int ret; |
77b31f39 |
241 | int fd_fac; |
242 | int fd_cpu; |
243 | char outfile_name[PATH_MAX]; |
244 | char identifier_name[PATH_MAX]; |
245 | |
700d350d |
246 | |
a85b8f41 |
247 | traced_pid = l_traced_pid; |
77b31f39 |
248 | traced_thread = l_traced_thread; |
e8efa18d |
249 | |
a85b8f41 |
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); |
700d350d |
252 | |
e8efa18d |
253 | act.sa_handler = handler_sigusr1; |
700d350d |
254 | act.sa_flags = 0; |
255 | sigemptyset(&(act.sa_mask)); |
256 | sigaddset(&(act.sa_mask), SIGUSR1); |
257 | sigaction(SIGUSR1, &act, NULL); |
e8efa18d |
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 | |
700d350d |
271 | /* Enable signals */ |
272 | ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
273 | if(ret) { |
a35eaa9c |
274 | printf("LTT Error in pthread_sigmask\n"); |
700d350d |
275 | } |
276 | |
e8efa18d |
277 | alarm(3); |
278 | |
77b31f39 |
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); |
5efa73ea |
294 | fd_fac = creat(outfile_name, 0644); |
77b31f39 |
295 | |
296 | snprintf(outfile_name, PATH_MAX-1, "cpu-%s", identifier_name); |
5efa73ea |
297 | fd_cpu = creat(outfile_name, 0644); |
77b31f39 |
298 | |
299 | |
700d350d |
300 | while(1) { |
301 | pause(); |
a85b8f41 |
302 | if(traced_pid == 0) break; /* parent died */ |
e8efa18d |
303 | if(parent_exited) break; |
a35eaa9c |
304 | printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid()); |
47d7d576 |
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); |
700d350d |
313 | } |
314 | |
e8efa18d |
315 | /* Buffer force switch (flush) */ |
47d7d576 |
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 | |
77b31f39 |
327 | close(fd_fac); |
328 | close(fd_cpu); |
329 | |
e8efa18d |
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); |
700d350d |
334 | } |
b09f3215 |
335 | |
e8efa18d |
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) |
b09f3215 |
344 | { |
700d350d |
345 | pid_t pid; |
346 | struct ltt_trace_info *shared_trace_info; |
347 | int ret; |
348 | sigset_t set, oldset; |
a85b8f41 |
349 | pid_t l_traced_pid = getpid(); |
77b31f39 |
350 | pthread_t l_traced_thread = pthread_self(); |
700d350d |
351 | |
352 | /* parent : create the shared memory map */ |
a85b8f41 |
353 | shared_trace_info = mmap(0, sizeof(*thread_trace_info), |
700d350d |
354 | PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0); |
a85b8f41 |
355 | memset(shared_trace_info, 0, sizeof(*shared_trace_info)); |
47d7d576 |
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; |
a85b8f41 |
364 | shared_trace_info->init = 1; |
700d350d |
365 | |
366 | /* Disable signals */ |
367 | ret = sigfillset(&set); |
368 | if(ret) { |
a35eaa9c |
369 | printf("LTT Error in sigfillset\n"); |
700d350d |
370 | } |
371 | |
372 | |
373 | ret = pthread_sigmask(SIG_BLOCK, &set, &oldset); |
374 | if(ret) { |
a35eaa9c |
375 | printf("LTT Error in pthread_sigmask\n"); |
700d350d |
376 | } |
a85b8f41 |
377 | |
700d350d |
378 | pid = fork(); |
379 | if(pid > 0) { |
380 | /* Parent */ |
a85b8f41 |
381 | shared_trace_info->daemon_id = pid; |
382 | thread_trace_info = shared_trace_info; |
700d350d |
383 | |
384 | /* Enable signals */ |
385 | ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
386 | if(ret) { |
a35eaa9c |
387 | printf("LTT Error in pthread_sigmask\n"); |
700d350d |
388 | } |
389 | } else if(pid == 0) { |
390 | /* Child */ |
e8efa18d |
391 | role = LTT_ROLE_READER; |
77b31f39 |
392 | ltt_usertrace_fast_daemon(shared_trace_info, oldset, l_traced_pid, |
393 | l_traced_thread); |
700d350d |
394 | /* Should never return */ |
395 | exit(-1); |
396 | } else if(pid < 0) { |
397 | /* fork error */ |
a35eaa9c |
398 | perror("LTT Error in forking ltt-usertrace-fast"); |
700d350d |
399 | } |
b09f3215 |
400 | } |
401 | |
e8efa18d |
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 | |
04180f7f |
410 | void __attribute__((constructor)) __ltt_usertrace_fast_init(void) |
b09f3215 |
411 | { |
700d350d |
412 | printf("LTT usertrace-fast init\n"); |
b09f3215 |
413 | |
e8efa18d |
414 | ltt_rw_init(); |
700d350d |
415 | } |
416 | |
417 | void __attribute__((destructor)) __ltt_usertrace_fast_fini(void) |
418 | { |
e8efa18d |
419 | if(role == LTT_ROLE_WRITER) { |
420 | printf("LTT usertrace-fast fini\n"); |
421 | ltt_usertrace_fast_cleanup(NULL); |
422 | } |
b09f3215 |
423 | } |
424 | |