From 203e4c2624493c0db8c69c9ad830090c5b79be67 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 29 Sep 2021 20:15:45 -0600 Subject: [PATCH] t/io_uring: store TSC rate in local file Doesn't change on a single machine, so let's just cache the value instead of requiring it to be specified every time. If we specify the rate, the local data is updated. If we don't specify it, we check the file, and use the rate in there if it exists. Signed-off-by: Jens Axboe --- .gitignore | 1 + t/io_uring.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/.gitignore b/.gitignore index 6651f96e..72494a1e 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ doc/output /TAGS /t/zbd/test-zbd-support.log.* /t/fuzz/fuzz_parseini +tsc-rate diff --git a/t/io_uring.c b/t/io_uring.c index e5568aa2..d7ae18b0 100644 --- a/t/io_uring.c +++ b/t/io_uring.c @@ -110,6 +110,8 @@ static int nthreads = 1; static int stats = 0; /* generate IO stats */ static unsigned long tsc_rate; +#define TSC_RATE_FILE "tsc-rate" + static int vectored = 1; static float plist[] = { 1.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, @@ -816,6 +818,50 @@ static void usage(char *argv, int status) exit(status); } +static void read_tsc_rate(void) +{ + char buffer[32]; + int fd, ret; + + if (tsc_rate) + return; + + fd = open(TSC_RATE_FILE, O_RDONLY); + if (fd < 0) + return; + + ret = read(fd, buffer, sizeof(buffer)); + if (ret < 0) { + close(fd); + return; + } + + tsc_rate = strtoul(buffer, NULL, 10); + printf("Using TSC rate %luHz\n", tsc_rate); + close(fd); +} + +static void write_tsc_rate(void) +{ + char buffer[32]; + struct stat sb; + int fd, ret; + + if (!stat(TSC_RATE_FILE, &sb)) + return; + + fd = open(TSC_RATE_FILE, O_WRONLY | O_CREAT, 0644); + if (fd < 0) + return; + + memset(buffer, 0, sizeof(buffer)); + sprintf(buffer, "%lu", tsc_rate); + ret = write(fd, buffer, strlen(buffer)); + if (ret < 0) + perror("write"); + close(fd); +} + int main(int argc, char *argv[]) { struct submitter *s; @@ -881,6 +927,7 @@ int main(int argc, char *argv[]) return 1; #endif tsc_rate = strtoul(optarg, NULL, 10); + write_tsc_rate(); break; case 'h': case '?': @@ -890,6 +937,9 @@ int main(int argc, char *argv[]) } } + if (stats) + read_tsc_rate(); + if (batch_complete > depth) batch_complete = depth; if (batch_submit > depth) -- 2.25.1