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