c809aff096f0fb359bdaf141c2b45d3477b103af
[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
62 #include <asm/timex.h> //for get_cycles()
63
64 #include "ltt-usertrace-fast.h"
65
66
67 /* Writer (the traced application) */
68
69 __thread struct ltt_trace_info *thread_trace_info = NULL;
70
71 void ltt_usertrace_fast_buffer_switch(void)
72 {
73 struct ltt_trace_info *tmp = thread_trace_info;
74 if(tmp)
75 kill(tmp->daemon_id, SIGUSR1);
76 }
77
78 /* The cleanup should never be called from a signal handler */
79 static void ltt_usertrace_fast_cleanup(void *arg)
80 {
81 struct ltt_trace_info *tmp = thread_trace_info;
82 if(tmp) {
83 thread_trace_info = NULL;
84 kill(tmp->daemon_id, SIGUSR2);
85 munmap(tmp, sizeof(*tmp));
86 }
87 }
88
89 /* Reader (the disk dumper daemon) */
90
91 static pid_t traced_pid = 0;
92 static pthread_t traced_thread = 0;
93 static int parent_exited = 0;
94
95 /* signal handling */
96 static void handler_sigusr1(int signo)
97 {
98 printf("LTT Signal %d received : parent buffer switch.\n", signo);
99 }
100
101 static void handler_sigusr2(int signo)
102 {
103 printf("LTT Signal %d received : parent exited.\n", signo);
104 parent_exited = 1;
105 }
106
107 static void handler_sigalarm(int signo)
108 {
109 printf("LTT Signal %d received\n", signo);
110
111 if(getppid() != traced_pid) {
112 /* Parent died */
113 printf("LTT Parent %lu died, cleaning up\n", traced_pid);
114 traced_pid = 0;
115 }
116 alarm(3);
117 }
118
119
120 /* This function is called by ltt_rw_init which has signals blocked */
121 static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info,
122 sigset_t oldset, pid_t l_traced_pid, pthread_t l_traced_thread)
123 {
124 struct sigaction act;
125 int ret;
126 int fd_fac;
127 int fd_cpu;
128 char outfile_name[PATH_MAX];
129 char identifier_name[PATH_MAX];
130
131
132 traced_pid = l_traced_pid;
133 traced_thread = l_traced_thread;
134
135 printf("LTT ltt_usertrace_fast_daemon : init is %d, pid is %lu, traced_pid is %lu\n",
136 shared_trace_info->init, getpid(), traced_pid);
137
138 act.sa_handler = handler_sigusr1;
139 act.sa_flags = 0;
140 sigemptyset(&(act.sa_mask));
141 sigaddset(&(act.sa_mask), SIGUSR1);
142 sigaction(SIGUSR1, &act, NULL);
143
144 act.sa_handler = handler_sigusr2;
145 act.sa_flags = 0;
146 sigemptyset(&(act.sa_mask));
147 sigaddset(&(act.sa_mask), SIGUSR2);
148 sigaction(SIGUSR2, &act, NULL);
149
150 act.sa_handler = handler_sigalarm;
151 act.sa_flags = 0;
152 sigemptyset(&(act.sa_mask));
153 sigaddset(&(act.sa_mask), SIGALRM);
154 sigaction(SIGALRM, &act, NULL);
155
156 /* Enable signals */
157 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
158 if(ret) {
159 printf("LTT Error in pthread_sigmask\n");
160 }
161
162 alarm(3);
163
164 /* Open output files */
165 umask(00000);
166 ret = mkdir(LTT_USERTRACE_ROOT, 0777);
167 if(ret < 0 && errno != EEXIST) {
168 perror("LTT Error in creating output (mkdir)");
169 exit(-1);
170 }
171 ret = chdir(LTT_USERTRACE_ROOT);
172 if(ret < 0) {
173 perror("LTT Error in creating output (chdir)");
174 exit(-1);
175 }
176 snprintf(identifier_name, PATH_MAX-1, "%lu.%lu.%llu",
177 traced_pid, traced_thread, get_cycles());
178 snprintf(outfile_name, PATH_MAX-1, "facilities-%s", identifier_name);
179 fd_fac = creat(outfile_name, 0644);
180
181 snprintf(outfile_name, PATH_MAX-1, "cpu-%s", identifier_name);
182 fd_cpu = creat(outfile_name, 0644);
183
184
185 while(1) {
186 pause();
187 if(traced_pid == 0) break; /* parent died */
188 if(parent_exited) break;
189 printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid());
190 //printf("Test parent. pid is : %lu, ppid is %lu\n", getpid(), getppid());
191 }
192
193 /* Buffer force switch (flush) */
194 //TODO
195
196 close(fd_fac);
197 close(fd_cpu);
198
199 /* The parent thread is dead and we have finished with the buffer */
200 munmap(shared_trace_info, sizeof(*shared_trace_info));
201
202 exit(0);
203 }
204
205
206 /* Reader-writer initialization */
207
208 static enum ltt_process_role { LTT_ROLE_WRITER, LTT_ROLE_READER }
209 role = LTT_ROLE_WRITER;
210
211
212 void ltt_rw_init(void)
213 {
214 pid_t pid;
215 struct ltt_trace_info *shared_trace_info;
216 int ret;
217 sigset_t set, oldset;
218 pid_t l_traced_pid = getpid();
219 pthread_t l_traced_thread = pthread_self();
220
221 /* parent : create the shared memory map */
222 shared_trace_info = mmap(0, sizeof(*thread_trace_info),
223 PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
224 memset(shared_trace_info, 0, sizeof(*shared_trace_info));
225 shared_trace_info->init = 1;
226
227 /* Disable signals */
228 ret = sigfillset(&set);
229 if(ret) {
230 printf("LTT Error in sigfillset\n");
231 }
232
233
234 ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
235 if(ret) {
236 printf("LTT Error in pthread_sigmask\n");
237 }
238
239 pid = fork();
240 if(pid > 0) {
241 /* Parent */
242 shared_trace_info->daemon_id = pid;
243 thread_trace_info = shared_trace_info;
244
245 /* Enable signals */
246 ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
247 if(ret) {
248 printf("LTT Error in pthread_sigmask\n");
249 }
250 } else if(pid == 0) {
251 /* Child */
252 role = LTT_ROLE_READER;
253 ltt_usertrace_fast_daemon(shared_trace_info, oldset, l_traced_pid,
254 l_traced_thread);
255 /* Should never return */
256 exit(-1);
257 } else if(pid < 0) {
258 /* fork error */
259 perror("LTT Error in forking ltt-usertrace-fast");
260 }
261 }
262
263 static __thread struct _pthread_cleanup_buffer cleanup_buffer;
264
265 void ltt_thread_init(void)
266 {
267 _pthread_cleanup_push(&cleanup_buffer, ltt_usertrace_fast_cleanup, NULL);
268 ltt_rw_init();
269 }
270
271 void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
272 {
273 printf("LTT usertrace-fast init\n");
274
275 ltt_rw_init();
276 }
277
278 void __attribute__((destructor)) __ltt_usertrace_fast_fini(void)
279 {
280 if(role == LTT_ROLE_WRITER) {
281 printf("LTT usertrace-fast fini\n");
282 ltt_usertrace_fast_cleanup(NULL);
283 }
284 }
285
This page took 0.045174 seconds and 3 git commands to generate.