usertrace fast work
authorcompudj <compudj@04897980-b3bd-0310-b5e0-8ef037075253>
Tue, 7 Mar 2006 17:36:36 +0000 (17:36 +0000)
committercompudj <compudj@04897980-b3bd-0310-b5e0-8ef037075253>
Tue, 7 Mar 2006 17:36:36 +0000 (17:36 +0000)
git-svn-id: http://ltt.polymtl.ca/svn@1607 04897980-b3bd-0310-b5e0-8ef037075253

usertrace-fast/ltt-usertrace-fast.c
usertrace-fast/ltt-usertrace-fast.h
usertrace-fast/test.c

index 42dde3e50695f53fd73448a4bfcd1856fe1b9777..cb1316951f101692dd6a71e6353a0c7859662e0e 100644 (file)
@@ -1,11 +1,39 @@
-
-/* LTTng user-space "fast" tracing code
+/* LTTng user-space "fast" library
+ *
+ * This daemon is spawned by each traced thread (to share the mmap).
+ *
+ * Its job is to dump periodically this buffer to disk (when it receives a
+ * SIGUSR1 from its parent).
+ *
+ * It uses the control information in the shared memory area (producer/consumer
+ * count).
+ *
+ * When the parent thread dies (yes, those thing may happen) ;) , this daemon
+ * will flush the last buffer and write it to disk.
+ *
+ * Supplement note for streaming : the daemon is responsible for flushing
+ * periodically the buffer if it is streaming data.
+ * 
  *
+ * Notes :
+ * shm memory is typically limited to 4096 units (system wide limit SHMMNI in
+ * /proc/sys/kernel/shmmni). As it requires computation time upon creation, we
+ * do not use it : we will use a shared mmap() instead which is passed through
+ * the fork().
+ * MAP_SHARED mmap segment. Updated when msync or munmap are called.
+ * MAP_ANONYMOUS.
+ * Memory  mapped  by  mmap()  is  preserved across fork(2), with the same
+ *   attributes.
+ * 
+ * Eventually, there will be two mode :
+ * * Slow thread spawn : a fork() is done for each new thread. If the process
+ *   dies, the data is not lost.
+ * * Fast thread spawn : a pthread_create() is done by the application for each
+ *   new thread.
  * Copyright 2006 Mathieu Desnoyers
  *
  */
 
-
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -17,6 +45,8 @@
 #include <pthread.h>
 #include <malloc.h>
 #include <string.h>
+#include <sys/mman.h>
+#include <signal.h>
 
 #include "ltt-usertrace-fast.h"
 
@@ -30,8 +60,8 @@
  *     object of thread storage duration shall not require dynamic
  *     initialization.
  */
-
-__thread struct lttng_trace_info lttng_trace_info =
+#if 0
+__thread struct ltt_trace_info ltt_trace_info =
 {
        .init = 0,
        .filter = 0,
@@ -49,29 +79,108 @@ __thread struct lttng_trace_info lttng_trace_info =
                        ATOMIC_INIT(0)
                },
 };
+#endif //0
 
+__thread struct ltt_trace_info *thread_trace_info = NULL;
 
-static void ltt_cleanup_thread(void *arg)
+
+/* signal handling */
+
+static void handler(int signo)
 {
-       /* Flush the data in the lttng_trace_info */
+       printf("Signal %d received\n", signo);
+}
 
+
+
+void ltt_usertrace_fast_buffer_switch(void)
+{
+       kill(thread_trace_info->daemon_id, SIGUSR1);
 }
 
+static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info,
+               sigset_t oldset)
+{
+       struct sigaction act;
+       int ret;
+
+       printf("ltt_usertrace_fast_daemon : init is %d, pid is %lu\n",
+                       shared_trace_info->init, getpid());
+
+       act.sa_handler = handler;
+       act.sa_flags = 0;
+       sigemptyset(&(act.sa_mask));
+       sigaddset(&(act.sa_mask), SIGUSR1);
+       sigaction(SIGUSR1, &act, NULL);
+       /* Enable signals */
+       ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+       if(ret) {
+               printf("Error in pthread_sigmask\n");
+       }
+
+       while(1) {
+               pause();
+               printf("Doing a buffer switch read. pid is : %lu\n", getpid());
+       }
+
+}
 
 void ltt_thread_init(void)
 {
-       _pthread_cleanup_push(&lttng_trace_info.cleanup,
-                       ltt_cleanup_thread, NULL);
+       pid_t pid;
+       struct ltt_trace_info *shared_trace_info;
+       int ret;
+       sigset_t set, oldset;
+
+       /* parent : create the shared memory map */
+       shared_trace_info = thread_trace_info = mmap(0, sizeof(*thread_trace_info),
+                       PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);
+       memset(shared_trace_info, 0, sizeof(*thread_trace_info));
+       thread_trace_info->init = 1;
+
+       /* Disable signals */
+  ret = sigfillset(&set);
+  if(ret) {
+    printf("Error in sigfillset\n");
+  } 
+       
+       
+  ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
+  if(ret) {
+    printf("Error in pthread_sigmask\n");
+  }
+       
+       pid = fork();
+       if(pid > 0) {
+               /* Parent */
+               thread_trace_info->daemon_id = pid;
+
+               /* Enable signals */
+               ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+               if(ret) {
+                       printf("Error in pthread_sigmask\n");
+               }
+       } else if(pid == 0) {
+               /* Child */
+               ltt_usertrace_fast_daemon(shared_trace_info, oldset);
+               /* Should never return */
+               exit(-1);
+       } else if(pid < 0) {
+               /* fork error */
+               perror("Error in forking ltt-usertrace-fast");
+       }
 }
 
-
 void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
 {
-       int err;
-
-  printf("LTTng usertrace-fast init\n");
+  printf("LTT usertrace-fast init\n");
 
        ltt_thread_init();
+}
+
+void __attribute__((destructor)) __ltt_usertrace_fast_fini(void)
+{
+  printf("LTT usertrace-fast fini\n");
 
 }
 
index ac509c24a8933bf19bbac5b57b3f3f3cd03a0506..dd3bd02591d077dbacb2d5f8e755e8c73d227d09 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <errno.h>
 #include <asm/atomic.h>
+#include <pthread.h>
 
 #ifndef        LTT_BUF_SIZE_CPU
 #define LTT_BUF_SIZE_CPU 1048576
@@ -27,21 +28,27 @@ struct ltt_buf {
        atomic_t        events_lost;
 };
 
-struct lttng_trace_info {
-       struct _pthread_cleanup_buffer cleanup;
+struct ltt_trace_info {
        int init;
        int filter;
+#ifndef LTT_USE_THREADS
+       pid_t daemon_id;
+#else
+       pthread_t daemon_id;
+#endif //LTT_USE_THREADS
        atomic_t nesting;
        struct {
                struct ltt_buf facilities;
-               char facilities_buf[LTT_BUF_SIZE_FACILITIES] __attribute__ ((aligned (8)));
                struct ltt_buf cpu;
+               char facilities_buf[LTT_BUF_SIZE_FACILITIES] __attribute__ ((aligned (8)));
                char cpu_buf[LTT_BUF_SIZE_CPU] __attribute__ ((aligned (8)));
        } channel;
 };
 
-extern __thread struct lttng_trace_info lttng_trace_info;
+extern __thread struct ltt_trace_info *thread_trace_info;
 
 void ltt_thread_init(void);
 
+void ltt_usertrace_fast_buffer_switch(void);
+
 #endif //_LTT_USERTRACE_FAST_H
index 2bd12e130adcf413e6b4c9b628301da1958d5a70..898b86af6402d0521f31da7fabdcf9b711384182 100644 (file)
 
 void *thr1(void *arg)
 {
+       int i;
        ltt_thread_init();
   printf("thread 1, thread id : %lu, pid %lu\n", pthread_self(), getpid());
 
-  while(1) {}
-
-  return ((void*)1);
+  //while(1) {}
+       for(i=0; i<5; i++) {
+               ltt_usertrace_fast_buffer_switch();
+               sleep(1);
+       }
 
+  //return ((void*)1);
+       //pthread_exit((void*)1);
 }
 
 void *thr2(void *arg)
 {
+       int i;
        ltt_thread_init();
-  while(1) {
+  //while(1) {
     printf("thread 2, thread id : %lu, pid %lu\n", pthread_self(), getpid());
     sleep(2);
-  }
-  return ((void*)2);
+  //}
+  //return ((void*)2);
+       //pthread_exit((void*)2);
+       for(i=0; i<2; i++) {
+               ltt_usertrace_fast_buffer_switch();
+               sleep(3);
+       }
+
+
 }
 
 
 int main()
 {
+       int i;
        int err;
        pthread_t tid1, tid2;
        void *tret;
@@ -43,10 +57,10 @@ int main()
   err = pthread_create(&tid2, NULL, thr2, NULL);
   if(err!=0) exit(1);
 
-  while(1)
-  {
-    
-  }
+       for(i=0; i<2; i++) {
+               ltt_usertrace_fast_buffer_switch();
+               sleep(3);
+       }
 
   err = pthread_join(tid1, &tret);
   if(err!= 0) exit(1);
This page took 0.028804 seconds and 4 git commands to generate.