Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <math.h> |
| 3 | #include <sys/time.h> |
| 4 | #include <time.h> |
| 5 | |
| 6 | #include "fio.h" |
| 7 | #include "smalloc.h" |
| 8 | |
Jens Axboe | 5be4c94 | 2013-02-11 16:33:25 +0100 | [diff] [blame] | 9 | struct timeval *fio_tv = NULL; |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 10 | int fio_gtod_offload = 0; |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 11 | static os_cpu_mask_t fio_gtod_cpumask; |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 12 | static pthread_t gtod_thread; |
| 13 | |
| 14 | void fio_gtod_init(void) |
| 15 | { |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 16 | if (fio_tv) |
| 17 | return; |
| 18 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 19 | fio_tv = smalloc(sizeof(struct timeval)); |
Jens Axboe | fba5c5f | 2013-01-29 22:29:09 +0100 | [diff] [blame] | 20 | if (!fio_tv) |
| 21 | log_err("fio: smalloc pool exhausted\n"); |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static void fio_gtod_update(void) |
| 25 | { |
Jens Axboe | 0419419 | 2014-12-16 19:43:55 -0700 | [diff] [blame] | 26 | if (fio_tv) { |
| 27 | struct timeval __tv; |
| 28 | |
| 29 | gettimeofday(&__tv, NULL); |
| 30 | fio_tv->tv_sec = __tv.tv_sec; |
| 31 | write_barrier(); |
| 32 | fio_tv->tv_usec = __tv.tv_usec; |
| 33 | write_barrier(); |
| 34 | } |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 37 | struct gtod_cpu_data { |
| 38 | struct fio_mutex *mutex; |
| 39 | unsigned int cpu; |
| 40 | }; |
| 41 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 42 | static void *gtod_thread_main(void *data) |
| 43 | { |
| 44 | struct fio_mutex *mutex = data; |
| 45 | |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 46 | fio_setaffinity(gettid(), fio_gtod_cpumask); |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 47 | fio_mutex_up(mutex); |
| 48 | |
| 49 | /* |
| 50 | * As long as we have jobs around, update the clock. It would be nice |
| 51 | * to have some way of NOT hammering that CPU with gettimeofday(), |
| 52 | * but I'm not sure what to use outside of a simple CPU nop to relax |
| 53 | * it - we don't want to lose precision. |
| 54 | */ |
| 55 | while (threads) { |
| 56 | fio_gtod_update(); |
| 57 | nop; |
| 58 | } |
| 59 | |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | int fio_start_gtod_thread(void) |
| 64 | { |
| 65 | struct fio_mutex *mutex; |
| 66 | pthread_attr_t attr; |
| 67 | int ret; |
| 68 | |
| 69 | mutex = fio_mutex_init(FIO_MUTEX_LOCKED); |
| 70 | if (!mutex) |
| 71 | return 1; |
| 72 | |
| 73 | pthread_attr_init(&attr); |
| 74 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN); |
Jens Axboe | 2496101 | 2014-12-16 19:49:54 -0700 | [diff] [blame] | 75 | ret = pthread_create(>od_thread, &attr, gtod_thread_main, mutex); |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 76 | pthread_attr_destroy(&attr); |
| 77 | if (ret) { |
| 78 | log_err("Can't create gtod thread: %s\n", strerror(ret)); |
| 79 | goto err; |
| 80 | } |
| 81 | |
| 82 | ret = pthread_detach(gtod_thread); |
| 83 | if (ret) { |
| 84 | log_err("Can't detatch gtod thread: %s\n", strerror(ret)); |
| 85 | goto err; |
| 86 | } |
| 87 | |
| 88 | dprint(FD_MUTEX, "wait on startup_mutex\n"); |
| 89 | fio_mutex_down(mutex); |
| 90 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); |
| 91 | err: |
| 92 | fio_mutex_remove(mutex); |
| 93 | return ret; |
| 94 | } |
| 95 | |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 96 | void fio_gtod_set_cpu(unsigned int cpu) |
| 97 | { |
Jens Axboe | 32bbea4 | 2014-12-19 15:03:15 -0700 | [diff] [blame^] | 98 | #ifdef FIO_HAVE_CPU_AFFINITY |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 99 | fio_cpu_set(&fio_gtod_cpumask, cpu); |
Jens Axboe | 32bbea4 | 2014-12-19 15:03:15 -0700 | [diff] [blame^] | 100 | #endif |
Jens Axboe | 80ac020 | 2014-12-16 20:38:53 -0700 | [diff] [blame] | 101 | } |