qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: peter.maydell@linaro.org
Cc: "Kevin Wolf" <kwolf@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	qemu-devel@nongnu.org,
	"open list:Block layer core" <qemu-block@nongnu.org>,
	"Max Reitz" <mreitz@redhat.com>
Subject: [Qemu-devel] [PULL 02/52] qemu-io-cmds: use clock_gettime for benchmarking
Date: Fri,  7 Jun 2019 10:05:01 +0100	[thread overview]
Message-ID: <20190607090552.12434-3-alex.bennee@linaro.org> (raw)
In-Reply-To: <20190607090552.12434-1-alex.bennee@linaro.org>

The previous use of gettimeofday() ran into undefined behaviour when
we ended up doing a div 0 for a very short operation. This is because
gettimeofday only works at the microsecond level as well as being
prone to discontinuous jumps in system time. Using clock_gettime with
CLOCK_MONOTONIC gives greater precision and alleviates some of the
potential problems with time jumping around.

We could use CLOCK_MONOTONIC_RAW to avoid being tripped up by NTP and
adjtime but that is Linux specific so I decided it would do for now.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
index 30a7d9a13b..8904733961 100644
--- a/qemu-io-cmds.c
+++ b/qemu-io-cmds.c
@@ -248,20 +248,21 @@ static void cvtstr(double value, char *str, size_t size)
 
 
 
-static struct timeval tsub(struct timeval t1, struct timeval t2)
+static struct timespec tsub(struct timespec t1, struct timespec t2)
 {
-    t1.tv_usec -= t2.tv_usec;
-    if (t1.tv_usec < 0) {
-        t1.tv_usec += 1000000;
+    t1.tv_nsec -= t2.tv_nsec;
+    if (t1.tv_nsec < 0) {
+        t1.tv_nsec += NANOSECONDS_PER_SECOND;
         t1.tv_sec--;
     }
     t1.tv_sec -= t2.tv_sec;
     return t1;
 }
 
-static double tdiv(double value, struct timeval tv)
+static double tdiv(double value, struct timespec tv)
 {
-    return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
+    double seconds = tv.tv_sec + (tv.tv_nsec / 1e9);
+    return value / seconds;
 }
 
 #define HOURS(sec)      ((sec) / (60 * 60))
@@ -274,29 +275,27 @@ enum {
     VERBOSE_FIXED_TIME  = 0x2,
 };
 
-static void timestr(struct timeval *tv, char *ts, size_t size, int format)
+static void timestr(struct timespec *tv, char *ts, size_t size, int format)
 {
-    double usec = (double)tv->tv_usec / 1000000.0;
+    double frac_sec = tv->tv_nsec / 1e9;
 
     if (format & TERSE_FIXED_TIME) {
         if (!HOURS(tv->tv_sec)) {
-            snprintf(ts, size, "%u:%02u.%02u",
-                    (unsigned int) MINUTES(tv->tv_sec),
-                    (unsigned int) SECONDS(tv->tv_sec),
-                    (unsigned int) (usec * 100));
+            snprintf(ts, size, "%u:%05.2f",
+                     (unsigned int) MINUTES(tv->tv_sec),
+                     SECONDS(tv->tv_sec) + frac_sec);
             return;
         }
         format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
     }
 
     if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
-        snprintf(ts, size, "%u:%02u:%02u.%02u",
+        snprintf(ts, size, "%u:%02u:%05.2f",
                 (unsigned int) HOURS(tv->tv_sec),
                 (unsigned int) MINUTES(tv->tv_sec),
-                (unsigned int) SECONDS(tv->tv_sec),
-                (unsigned int) (usec * 100));
+                 SECONDS(tv->tv_sec) + frac_sec);
     } else {
-        snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
+        snprintf(ts, size, "%05.2f sec", frac_sec);
     }
 }
 
@@ -376,7 +375,7 @@ static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
     }
 }
 
-static void print_report(const char *op, struct timeval *t, int64_t offset,
+static void print_report(const char *op, struct timespec *t, int64_t offset,
                          int64_t count, int64_t total, int cnt, bool Cflag)
 {
     char s1[64], s2[64], ts[64];
@@ -649,7 +648,7 @@ static const cmdinfo_t read_cmd = {
 
 static int read_f(BlockBackend *blk, int argc, char **argv)
 {
-    struct timeval t1, t2;
+    struct timespec t1, t2;
     bool Cflag = false, qflag = false, vflag = false;
     bool Pflag = false, sflag = false, lflag = false, bflag = false;
     int c, cnt, ret;
@@ -758,13 +757,13 @@ static int read_f(BlockBackend *blk, int argc, char **argv)
 
     buf = qemu_io_alloc(blk, count, 0xab);
 
-    gettimeofday(&t1, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t1);
     if (bflag) {
         ret = do_load_vmstate(blk, buf, offset, count, &total);
     } else {
         ret = do_pread(blk, buf, offset, count, &total);
     }
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
     if (ret < 0) {
         printf("read failed: %s\n", strerror(-ret));
@@ -836,7 +835,7 @@ static const cmdinfo_t readv_cmd = {
 
 static int readv_f(BlockBackend *blk, int argc, char **argv)
 {
-    struct timeval t1, t2;
+    struct timespec t1, t2;
     bool Cflag = false, qflag = false, vflag = false;
     int c, cnt, ret;
     char *buf;
@@ -891,9 +890,9 @@ static int readv_f(BlockBackend *blk, int argc, char **argv)
         return -EINVAL;
     }
 
-    gettimeofday(&t1, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t1);
     ret = do_aio_readv(blk, &qiov, offset, &total);
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
     if (ret < 0) {
         printf("readv failed: %s\n", strerror(-ret));
@@ -972,7 +971,7 @@ static const cmdinfo_t write_cmd = {
 
 static int write_f(BlockBackend *blk, int argc, char **argv)
 {
-    struct timeval t1, t2;
+    struct timespec t1, t2;
     bool Cflag = false, qflag = false, bflag = false;
     bool Pflag = false, zflag = false, cflag = false;
     int flags = 0;
@@ -1091,7 +1090,7 @@ static int write_f(BlockBackend *blk, int argc, char **argv)
         buf = qemu_io_alloc(blk, count, pattern);
     }
 
-    gettimeofday(&t1, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t1);
     if (bflag) {
         ret = do_save_vmstate(blk, buf, offset, count, &total);
     } else if (zflag) {
@@ -1101,7 +1100,7 @@ static int write_f(BlockBackend *blk, int argc, char **argv)
     } else {
         ret = do_pwrite(blk, buf, offset, count, flags, &total);
     }
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
     if (ret < 0) {
         printf("write failed: %s\n", strerror(-ret));
@@ -1160,7 +1159,7 @@ static const cmdinfo_t writev_cmd = {
 
 static int writev_f(BlockBackend *blk, int argc, char **argv)
 {
-    struct timeval t1, t2;
+    struct timespec t1, t2;
     bool Cflag = false, qflag = false;
     int flags = 0;
     int c, cnt, ret;
@@ -1213,9 +1212,9 @@ static int writev_f(BlockBackend *blk, int argc, char **argv)
         return -EINVAL;
     }
 
-    gettimeofday(&t1, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t1);
     ret = do_aio_writev(blk, &qiov, offset, flags, &total);
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
     if (ret < 0) {
         printf("writev failed: %s\n", strerror(-ret));
@@ -1250,15 +1249,15 @@ struct aio_ctx {
     bool zflag;
     BlockAcctCookie acct;
     int pattern;
-    struct timeval t1;
+    struct timespec t1;
 };
 
 static void aio_write_done(void *opaque, int ret)
 {
     struct aio_ctx *ctx = opaque;
-    struct timeval t2;
+    struct timespec t2;
 
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
 
     if (ret < 0) {
@@ -1288,9 +1287,9 @@ out:
 static void aio_read_done(void *opaque, int ret)
 {
     struct aio_ctx *ctx = opaque;
-    struct timeval t2;
+    struct timespec t2;
 
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
     if (ret < 0) {
         printf("readv failed: %s\n", strerror(-ret));
@@ -1425,7 +1424,7 @@ static int aio_read_f(BlockBackend *blk, int argc, char **argv)
         return -EINVAL;
     }
 
-    gettimeofday(&ctx->t1, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
     block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
                      BLOCK_ACCT_READ);
     blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
@@ -1570,7 +1569,7 @@ static int aio_write_f(BlockBackend *blk, int argc, char **argv)
             return -EINVAL;
         }
 
-        gettimeofday(&ctx->t1, NULL);
+        clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
         block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
                          BLOCK_ACCT_WRITE);
 
@@ -1746,7 +1745,7 @@ static const cmdinfo_t discard_cmd = {
 
 static int discard_f(BlockBackend *blk, int argc, char **argv)
 {
-    struct timeval t1, t2;
+    struct timespec t1, t2;
     bool Cflag = false, qflag = false;
     int c, ret;
     int64_t offset, bytes;
@@ -1787,9 +1786,9 @@ static int discard_f(BlockBackend *blk, int argc, char **argv)
         return -EINVAL;
     }
 
-    gettimeofday(&t1, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t1);
     ret = blk_pdiscard(blk, offset, bytes);
-    gettimeofday(&t2, NULL);
+    clock_gettime(CLOCK_MONOTONIC, &t2);
 
     if (ret < 0) {
         printf("discard failed: %s\n", strerror(-ret));
-- 
2.20.1



  parent reply	other threads:[~2019-06-07  9:11 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-07  9:04 [Qemu-devel] [PULL 00/52] testing, gdbstub and cputlb fixes Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 01/52] editorconfig: add setting for shell scripts Alex Bennée
2019-06-07  9:05 ` Alex Bennée [this message]
2019-06-07  9:05 ` [Qemu-devel] [PULL 03/52] tests/docker: Update the Fedora image to Fedora 30 Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 04/52] tests/docker: Update the Fedora cross compile images to 30 Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 05/52] tests/docker: Update the Ubuntu image to 19.04 Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 06/52] .travis.yml: bump gcc sanitiser job to gcc-9 Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 07/52] .travis.yml: add clang ubsan job Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 08/52] tests/vm: Use python configured on build Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 09/52] tests/vm: Port basevm to Python 3 Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 10/52] tests/vm: Fix build-centos docker-based tests run Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 11/52] tests/vm: Add missing variables on help Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 12/52] scripts: use git archive in archive-source Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 13/52] tests/vm: python3 fixes Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 14/52] tests/vm: send proxy environment variables over ssh Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 15/52] tests/vm: use ssh with pty unconditionally Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 16/52] tests/vm: run test builds on snapshot Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 17/52] tests/vm: proper guest shutdown Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 18/52] tests/vm: add vm-boot-{ssh, serial}-<guest> targets Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 19/52] tests/vm: add DEBUG=1 to help text Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 20/52] tests/vm: serial console support helpers Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 21/52] tests/vm: openbsd autoinstall, using serial console Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 22/52] tests/vm: freebsd " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 23/52] tests/vm: netbsd " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 24/52] tests/vm: fedora " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 25/52] tests/vm: ubuntu.i386: apt proxy setup Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 26/52] semihosting: split console_out into string and char versions Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 27/52] cputlb: use uint64_t for interim values for unaligned load Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 28/52] tests/tcg: better detect truncated reads Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 29/52] tests/tcg: clean-up VPATH/TESTS for i386 Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 30/52] tests/tcg/x86_64: add a PVH crt.o for x86_64 system tests Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 31/52] MAINTAINERS: put myself forward for gdbstub Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 32/52] cputlb: cast size_t to target_ulong before using for address masks Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 33/52] gdbstub: Add infrastructure to parse cmd packets Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 34/52] gdbstub: Implement deatch (D pkt) with new infra Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 35/52] gdbstub: Implement thread_alive (T " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 36/52] gdbstub: Implement continue (c " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 37/52] gdbstub: Implement continue with signal (C " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 38/52] gdbstub: Implement set_thread (H " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 39/52] gdbstub: Implement breakpoint commands (Z/z " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 40/52] gdbstub: Implement set register (P " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 41/52] gdbstub: Implement get register (p " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 42/52] gdbstub: Implement write memory (M " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 43/52] gdbstub: Implement read memory (m " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 44/52] gdbstub: Implement write all registers (G " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 45/52] gdbstub: Implement read all registers (g " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 46/52] gdbstub: Implement file io (F " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 47/52] gdbstub: Implement step (s " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 48/52] gdbstub: Implement v commands " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 49/52] gdbstub: Implement generic set/query (Q/q pkt) " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 50/52] gdbstub: Implement target halted (? " Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 51/52] gdbstub: Clear unused variables in gdb_handle_packet Alex Bennée
2019-06-07  9:05 ` [Qemu-devel] [PULL 52/52] gdbstub: Implement qemu physical memory mode Alex Bennée
2019-06-07  9:50 ` [Qemu-devel] [PULL 00/52] testing, gdbstub and cputlb fixes Peter Maydell
2019-06-07 13:25   ` Alex Bennée
2019-06-11 14:18   ` Alex Bennée

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190607090552.12434-3-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).