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