Add gtod_cpu option for pinning gettimeofday() to a single CPU
[fio.git] / fio.c
diff --git a/fio.c b/fio.c
index 08011c94b82480bf7a875b4a3d193733e8e954cd..a58effcf3394816b20d04937de81f77f1a0c179f 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -55,6 +55,7 @@ static struct fio_mutex *startup_mutex;
 static volatile int fio_abort;
 static int exit_value;
 static struct itimerval itimer;
+static pthread_t gtod_thread;
 
 struct io_log *agg_io_log[2];
 
@@ -116,6 +117,13 @@ static void sig_alrm(int fio_unused sig)
        }
 }
 
+/*
+ * Happens on thread runs with ctrl-c, ignore our own SIGQUIT
+ */
+static void sig_quit(int sig)
+{
+}
+
 static void sig_int(int sig)
 {
        if (threads) {
@@ -154,6 +162,11 @@ static void set_sig_handlers(void)
        act.sa_handler = sig_ill;
        act.sa_flags = SA_RESTART;
        sigaction(SIGILL, &act, NULL);
+
+       memset(&act, 0, sizeof(act));
+       act.sa_handler = sig_quit;
+       act.sa_flags = SA_RESTART;
+       sigaction(SIGQUIT, &act, NULL);
 }
 
 static inline int should_check_rate(struct thread_data *td)
@@ -952,6 +965,18 @@ static void *thread_main(void *data)
                goto err;
        }
 
+       if (td->o.gtod_cpu) {
+               if (fio_getaffinity(td->pid, &td->o.cpumask) == -1) {
+                       td_verror(td, errno, "cpu_get_affinity");
+                       goto err;
+               }
+               fio_cpu_clear(&td->o.cpumask, td->o.gtod_cpu);
+               if (fio_setaffinity(td) == -1) {
+                       td_verror(td, errno, "cpu_set_affinity");
+                       goto err;
+               }
+       }
+
        if (td->ioprio_set) {
                if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
                        td_verror(td, errno, "ioprio_set");
@@ -1054,12 +1079,24 @@ static void *thread_main(void *data)
        td->ts.io_bytes[0] = td->io_bytes[0];
        td->ts.io_bytes[1] = td->io_bytes[1];
 
-       if (td->ts.bw_log)
-               finish_log(td, td->ts.bw_log, "bw");
-       if (td->ts.slat_log)
-               finish_log(td, td->ts.slat_log, "slat");
-       if (td->ts.clat_log)
-               finish_log(td, td->ts.clat_log, "clat");
+       if (td->ts.bw_log) {
+               if (td->o.bw_log_file)
+                       finish_log_named(td, td->ts.bw_log, td->o.bw_log_file, "bw");
+               else
+                       finish_log(td, td->ts.bw_log, "bw");
+       }
+       if (td->ts.slat_log) {
+               if (td->o.lat_log_file)
+                       finish_log_named(td, td->ts.slat_log, td->o.lat_log_file, "clat");
+               else
+                       finish_log(td, td->ts.slat_log, "slat");
+       }
+       if (td->ts.clat_log) {
+               if (td->o.lat_log_file)
+                       finish_log_named(td, td->ts.clat_log, td->o.lat_log_file, "clat");
+               else
+                       finish_log(td, td->ts.clat_log, "clat");
+       }
        if (td->o.exec_postrun) {
                if (system(td->o.exec_postrun) < 0)
                        log_err("fio: postrun %s failed\n", td->o.exec_postrun);
@@ -1205,6 +1242,39 @@ reaped:
                terminate_threads(TERMINATE_ALL);
 }
 
+static void *gtod_thread_main(void *data)
+{
+       fio_mutex_up(startup_mutex);
+
+       /*
+        * As long as we have jobs around, update the clock. It would be nice
+        * to have some way of NOT hammering that CPU with gettimeofday(),
+        * but I'm not sure what to use outside of a simple CPU nop to relax
+        * it - we don't want to lose precision.
+        */
+       while (threads) {
+               fio_gtod_update();
+               nop;
+       }
+
+       return NULL;
+}
+
+static int fio_start_gtod_thread(void)
+{
+       if (pthread_create(&gtod_thread, NULL, gtod_thread_main, NULL)) {
+               perror("Can't create gtod thread");
+               return 1;
+       }
+       if (pthread_detach(gtod_thread) < 0) {
+               perror("Can't detatch gtod thread");
+               return 1;
+       }
+
+       fio_mutex_down(startup_mutex);
+       return 0;
+}
+
 /*
  * Main function for kicking off and reaping jobs, as needed.
  */
@@ -1217,6 +1287,9 @@ static void run_threads(void)
        if (fio_pin_memory())
                return;
 
+       if (fio_gtod_offload && fio_start_gtod_thread())
+               return;
+
        if (!terse_output) {
                printf("Starting ");
                if (nr_thread)