t/io_uring: store TSC rate in local file
authorJens Axboe <axboe@kernel.dk>
Thu, 30 Sep 2021 02:15:45 +0000 (20:15 -0600)
committerJens Axboe <axboe@kernel.dk>
Thu, 30 Sep 2021 02:16:54 +0000 (20:16 -0600)
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 <axboe@kernel.dk>
.gitignore
t/io_uring.c

index 6651f96edc72ea3295c75cc9f9628eea9e267386..72494a1e2a9edafb7b77f50af94fce7d4435454b 100644 (file)
@@ -31,3 +31,4 @@ doc/output
 /TAGS
 /t/zbd/test-zbd-support.log.*
 /t/fuzz/fuzz_parseini
+tsc-rate
index e5568aa2e5a8f8420e714bfb0752c04feb762497..d7ae18b0da7181eb0dc4b193ea61c1ee021a56b0 100644 (file)
@@ -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)