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