remove unnecessary check in signal map
[archipelago] / xseg / peers / user / peer.c
index 85e78ec..2f3e3a6 100644 (file)
@@ -29,6 +29,8 @@ uint32_t ta = 0;
 #endif
 
 #ifdef MT
+struct peerd *global_peer;
+
 struct thread {
        struct peerd *peer;
        pthread_t tid;
@@ -79,7 +81,8 @@ static inline int isTerminate()
 {
 /* ta doesn't need to be taken into account, because the main loops
  * doesn't check the terminated flag if ta is not 0.
- *
+ */
+       /*
 #ifdef ST_THREADS
        return (!ta & terminated);
 #else
@@ -93,12 +96,24 @@ void signal_handler(int signal)
 {
        XSEGLOG2(&lc, I, "Caught signal. Terminating gracefully");
        terminated = 1;
+#ifdef MT
+       wake_up_next_thread(global_peer);
+#endif
 }
 
-static int setup_signals()
+void renew_logfile(int signal)
+{
+       XSEGLOG2(&lc, I, "Caught signal. Renewing logfile");
+       renew_logctx(&lc, NULL, lc.log_level, NULL, REOPEN_FILE);
+}
+
+static int setup_signals(struct peerd *peer)
 {
        int r;
        struct sigaction sa;
+#ifdef MT
+       global_peer = peer;
+#endif
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;
        sa.sa_handler = signal_handler;
@@ -111,6 +126,12 @@ static int setup_signals()
        r = sigaction(SIGQUIT, &sa, NULL);
        if (r < 0)
                return r;
+
+       sa.sa_handler = renew_logfile;
+       r = sigaction(SIGUSR1, &sa, NULL);
+       if (r < 0)
+               return r;
+
        return r;
 }
 
@@ -372,7 +393,7 @@ static void* thread_loop(void *arg)
        pid_t pid =syscall(SYS_gettid);
        uint64_t loops;
        uint64_t threshold=1000/(1 + portno_end - portno_start);
-       
+
        XSEGLOG2(&lc, D, "thread %u\n",  (unsigned int) (t- peer->thread));
 
        XSEGLOG2(&lc, I, "Thread %u has tid %u.\n", (unsigned int) (t- peer->thread), pid);
@@ -398,6 +419,7 @@ static void* thread_loop(void *arg)
                xseg_cancel_wait(xseg, peer->portno_start);
                XSEGLOG2(&lc, I, "Thread %u woke up\n", (unsigned int) (t- peer->thread));
        }
+       wake_up_next_thread(peer);
        return NULL;
 }
 
@@ -443,7 +465,7 @@ static int peerd_loop(struct peerd *peer)
        uint64_t threshold=1000/(1 + portno_end - portno_start);
        pid_t pid =syscall(SYS_gettid);
        uint64_t loops;
-       
+
        XSEGLOG2(&lc, I, "Peer has tid %u.\n", pid);
        xseg_init_local_signal(xseg, peer->portno_start);
        for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
@@ -638,7 +660,8 @@ int main(int argc, char *argv[])
        // -dp xseg_portno to defer blocking requests
        // -l log file ?
        //TODO print messages on arg parsing error
-       
+       //TODO string checking
+
        for (i = 1; i < argc; i++) {
                if (!strcmp(argv[i], "-g") && i + 1 < argc) {
                        spec = argv[i+1];
@@ -651,7 +674,7 @@ int main(int argc, char *argv[])
                        i += 1;
                        continue;
                }
-               
+
                if (!strcmp(argv[i], "-ep") && i + 1 < argc) {
                        portno_end = strtoul(argv[i+1], NULL, 10);
                        i += 1;
@@ -700,9 +723,14 @@ int main(int argc, char *argv[])
                }
 
        }
-       init_logctx(&lc, argv[0], debug_level, logfile);
+       r = init_logctx(&lc, argv[0], debug_level, logfile,
+                       REDIRECT_STDOUT|REDIRECT_STDERR);
+       if (r < 0){
+               XSEGLOG("Cannot initialize logging to logfile");
+               return -1;
+       }
        XSEGLOG2(&lc, D, "Main thread has tid %ld.\n", syscall(SYS_gettid));
-       
+
        if (pidfile){
                pid_fd = pidfile_open(pidfile, &old_pid);
                if (pid_fd < 0) {
@@ -714,7 +742,7 @@ int main(int argc, char *argv[])
                        return -1;
                }
        }
-       
+
        if (daemonize){
                if (daemon(0, 1) < 0){
                        XSEGLOG2(&lc, E, "Cannot daemonize");
@@ -724,7 +752,7 @@ int main(int argc, char *argv[])
        }
 
        pidfile_write(pid_fd);
-       
+
        //TODO perform argument sanity checks
        verbose = debug_level;
        if (portno != -1) {
@@ -732,13 +760,13 @@ int main(int argc, char *argv[])
                portno_end = portno;
        }
 
-       setup_signals();
        //TODO err check
        peer = peerd_init(nr_ops, spec, portno_start, portno_end, nr_threads, defer_portno);
        if (!peer){
                r = -1;
                goto out;
        }
+       setup_signals(peer);
        r = custom_peer_init(peer, argc, argv);
        if (r < 0)
                goto out;