Add gtod_cpu option for pinning gettimeofday() to a single CPU

Similar to what real life products sometimes do, offload gettimeofday()
calls to a single CPU and have that update the current time into a shared
memory location. This option pins a specific CPU for that job, and excludes
it from participating in any of the IO jobs.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/fio.c b/fio.c
index 5a87ae4..a58effc 100644
--- a/fio.c
+++ b/fio.c
@@ -55,6 +55,7 @@
 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];
 
@@ -964,6 +965,18 @@
 		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");
@@ -1229,6 +1242,39 @@
 		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.
  */
@@ -1241,6 +1287,9 @@
 	if (fio_pin_memory())
 		return;
 
+	if (fio_gtod_offload && fio_start_gtod_thread())
+		return;
+
 	if (!terse_output) {
 		printf("Starting ");
 		if (nr_thread)