Skip to contentSkip to conversation

timed average infrastructure for use in block statistics

PATCH In review Benoît Canet 3 participants 12 messages Sep 24, 2014 – Oct 13, 2014

lore
Share & tools
lore
#0 lore raw

v3 cover timed average infrastructure for use in block statistics

Hi,

Here is the new iteration of the timed average module.

A better algorithm avoiding unwanted noise has been implemented and the test have been rewritten as suggested.

Best regards

in V3:
Add Eric rev by of patch 2
drop unneeded (double) casts [Eric]
Implement new algorithm [Paolo]
rewrite commit message [Markus]
stub cpu_get_clock in tests [Paolo]

Benoît

Benoît Canet (3):
throttle: Make NANOSECONDS_PER_SECOND an integer
timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse
util: Infrastructure for computing recent averages

 include/qemu/throttle.h      |   2 -
 include/qemu/timed-average.h |  60 +++++++++++++
 include/qemu/timer.h         |   2 +
 tests/Makefile               |   2 +
 tests/test-timed-average.c   |  89 ++++++++++++++++++
 util/Makefile.objs           |   1 +
 util/timed-average.c         | 208 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 362 insertions(+), 2 deletions(-)
 create mode 100644 include/qemu/timed-average.h
 create mode 100644 tests/test-timed-average.c
 create mode 100644 util/timed-average.c
Signature · 1 line
2.1.1
#1 lore raw

PATCH v3 1/3 throttle: Make NANOSECONDS_PER_SECOND an integer

We will want to reuse this define in the future by making it common to multiple QEMU modules.
It would be safer that this define be an integer so we avoid strange float rounding errors.
Do this conversion.

---
 include/qemu/throttle.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/throttle.h +1 -1
index b890613..8f9e611 100644
@@ -27,7 +27,7 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000.0
+#define NANOSECONDS_PER_SECOND  1000000000
 
 typedef enum {
     THROTTLE_BPS_TOTAL,
Signature · 1 line
2.1.1
Reviewed-by on v3 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/3] throttle: Make NANOSECONDS_PER_SECOND an integer

Quoted 10 lines · Benoît Canet

We will want to reuse this define in the future by making it common to multiple QEMU modules.
It would be safer that this define be an integer so we avoid strange float rounding errors.
Do this conversion.

---
 include/qemu/throttle.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
include/qemu/throttle.h +1 -1
index b890613..8f9e611 100644
@@ -27,7 +27,7 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000.0
+#define NANOSECONDS_PER_SECOND  1000000000
 
 typedef enum {
     THROTTLE_BPS_TOTAL,
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

PATCH v3 2/3 timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse

In reply to Benoît Canet
---
 include/qemu/throttle.h | 2 --
 include/qemu/timer.h    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/throttle.h +0 -2
index 8f9e611..1c639d2 100644
@@ -27,8 +27,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000
-
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
include/qemu/timer.h +2 -0
index 5f5210d..0884e72 100644
@@ -5,6 +5,8 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 
+#define NANOSECONDS_PER_SECOND  1000000000
+
 /* timers */
 
 #define SCALE_MS 1000000
Signature · 1 line
2.1.1
Reviewed-by on v3 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 26 lines
---
 include/qemu/throttle.h | 2 --
 include/qemu/timer.h    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/throttle.h +0 -2
index 8f9e611..1c639d2 100644
@@ -27,8 +27,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000
-
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
include/qemu/timer.h +0 -0
index 5f5210d..0884e72 100644

include/qemu/timer.h line 12

Quoted 8 lines
include/qemu/timer.h +2 -0
@@ -5,6 +5,8 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 
+#define NANOSECONDS_PER_SECOND  1000000000
+
 /* timers */
 
 #define SCALE_MS 1000000

Went through all uses, for the two that matter:

util/throttle.c:    leak = (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND;
util/throttle.c:    double wait = extra * NANOSECONDS_PER_SECOND;

the other operand is already double.

Reviewed-by on v3 2/3
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse

In reply to Benoît Canet

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 26 lines
---
 include/qemu/throttle.h | 2 --
 include/qemu/timer.h    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/throttle.h +0 -2
index 8f9e611..1c639d2 100644
@@ -27,8 +27,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000
-
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
include/qemu/timer.h +0 -0
index 5f5210d..0884e72 100644

include/qemu/timer.h line 12

Quoted 8 lines
include/qemu/timer.h +2 -0
@@ -5,6 +5,8 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 
+#define NANOSECONDS_PER_SECOND  1000000000
+
 /* timers */
 
 #define SCALE_MS 1000000

:)

#6 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse

Quoted 33 lines · Paolo Bonzini

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 32 lines
---
 include/qemu/throttle.h | 2 --
 include/qemu/timer.h    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/throttle.h +0 -2
index 8f9e611..1c639d2 100644
@@ -27,8 +27,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000
-
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
include/qemu/timer.h +2 -0
index 5f5210d..0884e72 100644
@@ -5,6 +5,8 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 
+#define NANOSECONDS_PER_SECOND  1000000000
+
 /* timers */
 
Quoted 6 lines

include/qemu/timer.h line 12

Quoted 2 lines
include/qemu/timer.h +0 -0
 #define SCALE_MS 1000000

:)

Two rev by for one commit are better than one but does this belong to the third commit ? :)

Best regards

Benoît

#7 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse

Il 29/09/2014 18:04, Benoît Canet ha scritto:

Quoted 39 lines · Paolo Bonzini
Quoted 38 lines

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 26 lines
---
 include/qemu/throttle.h | 2 --
 include/qemu/timer.h    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/throttle.h +0 -2
index 8f9e611..1c639d2 100644
@@ -27,8 +27,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000
-
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
include/qemu/timer.h +0 -0
index 5f5210d..0884e72 100644

include/qemu/timer.h line 12

Quoted 8 lines
include/qemu/timer.h +2 -0
@@ -5,6 +5,8 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 
+#define NANOSECONDS_PER_SECOND  1000000000
+
 /* timers */
 
 #define SCALE_MS 1000000
Quoted 4 lines
Quoted 1 line

:)

Two rev by for one commit are better than one but does this belong to the third commit ? :)

The rev-bys belong to the first and second. The third's on my list...

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] timers: Move NANOSECONDS_PER_SECONDS to timer.h for future reuse

Quoted 40 lines · Paolo Bonzini

Il 29/09/2014 18:04, Benoît Canet ha scritto:

Quoted 39 lines · Paolo Bonzini
Quoted 38 lines

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 26 lines
---
 include/qemu/throttle.h | 2 --
 include/qemu/timer.h    | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/throttle.h +0 -2
index 8f9e611..1c639d2 100644
@@ -27,8 +27,6 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
-#define NANOSECONDS_PER_SECOND  1000000000
-
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
include/qemu/timer.h +0 -0
index 5f5210d..0884e72 100644

include/qemu/timer.h line 12

Quoted 8 lines
include/qemu/timer.h +2 -0
@@ -5,6 +5,8 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 
+#define NANOSECONDS_PER_SECOND  1000000000
+
 /* timers */
 
 #define SCALE_MS 1000000
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

:)

Two rev by for one commit are better than one but does this belong to the third commit ? :)

The rev-bys belong to the first and second. The third's on my list...

I misread (freudian slip) the 20 seconds delta between the two rev-by as 20 min. 20 min was credible but 20 seconds is not despite you being fast :)

Best regards

Benoît

Quoted 2 lines

Paolo

#9 lore raw

PATCH v3 3/3 util: Infrastructure for computing recent averages

In reply to Benoît Canet

The module takes care of computing minimal and maximal values over the time slice duration.

---
 include/qemu/timed-average.h |  60 +++++++++++++
 tests/Makefile               |   2 +
 tests/test-timed-average.c   |  89 ++++++++++++++++++
 util/Makefile.objs           |   1 +
 util/timed-average.c         | 208 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 360 insertions(+)
 create mode 100644 include/qemu/timed-average.h
 create mode 100644 tests/test-timed-average.c
 create mode 100644 util/timed-average.c
include/qemu/timed-average.h new file +60 -0
new file mode 100644
index 0000000..8150ead
@@ -0,0 +1,60 @@
+/*
+ * QEMU timed average computation
+ *
+ * Copyright (C) Nodalink, EURL. 2014
+ *
+ * Author:
+ *   Benoît Canet <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) version 3 or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TIMED_AVERAGE_H
+#define TIMED_AVERAGE_H
+
+#include <stdint.h>
+
+#include "qemu/timer.h"
+
+typedef struct Window {
+    uint64_t      min;           /* minimal value accounted in the window */
+    uint64_t      max;           /* maximal value accounted in the window */
+    uint64_t      sum;           /* sum of the discrete values */
+    uint64_t      count;         /* number of values */
+    int64_t       expiration;    /* the end of the current window in ns */
+} Window;
+
+typedef struct TimedAverage {
+    Window        windows[2];    /* two overlapping windows of period
+                                  * nanoseconds with an offset of period / 2
+                                  * between them
+                                  */
+
+    uint64_t      period;        /* period in nanoseconds */
+    uint64_t      current;       /* the current window index: it's also the
+                                  * oldest window index
+                                  */
+    QEMUClockType clock_type;    /* the clock used */
+} TimedAverage;
+
+void timed_average_init(TimedAverage *ta, QEMUClockType clock_type,
+                        uint64_t period);
+
+void timed_average_account(TimedAverage *ta, uint64_t value);
+
+uint64_t timed_average_min(TimedAverage *ta);
+uint64_t timed_average_avg(TimedAverage *ta);
+uint64_t timed_average_max(TimedAverage *ta);
+
+#endif
tests/Makefile +2 -0
index 469c0a5..d3669f4 100644
@@ -64,6 +64,7 @@ gcov-files-check-qom-interface-y = qom/object.c
 check-unit-$(CONFIG_POSIX) += tests/test-vmstate$(EXESUF)
 check-unit-y += tests/test-qemu-opts$(EXESUF)
 gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c
+check-unit-y += tests/test-timed-average$(EXESUF)
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -259,6 +260,7 @@ tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
 tests/test-vmstate$(EXESUF): tests/test-vmstate.o \
 	vmstate.o qemu-file.o \
 	libqemuutil.a
+tests/test-timed-average$(EXESUF): tests/test-timed-average.o qemu-timer.o libqemuutil.a stubs/clock-warp.o stubs/cpu-get-icount.o stubs/notify-event.o
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-timed-average.c new file +89 -0

89 lines not on this page · raw message

util/Makefile.objs +1 -0
index 6b3c83b..97d82ce 100644
@@ -15,3 +15,4 @@ util-obj-y += throttle.o
 util-obj-y += getauxval.o
 util-obj-y += readline.o
 util-obj-y += rfifolock.o
+util-obj-y += timed-average.o
util/timed-average.c new file +208 -0

208 lines not on this page · raw message

Signature · 1 line
2.1.1
#10 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] util: Infrastructure for computing recent averages

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 2 lines

The module takes care of computing minimal and maximal values over the time slice duration.

The code looks good, just two comments:

Quoted 4 lines
util/timed-average.c new file +4 -0
+/* Get the average value
+ *
+ * @ta:  the timed average structure used
+ * @ret: the average value

util/timed-average.c line 193

Quoted 10 lines
util/timed-average.c new file +10 -0
+ */
+uint64_t timed_average_avg(TimedAverage *ta)
+{
+    Window *w;
+    check_expirations(ta);
+
+    w = current_window(ta);
+
+    if (w->count) {
+        return w->sum / w->count;

First, do we want this to return double?

Second, this will return the min/max/avg in an unknown amount of time between period/2 and period---on average period*3/4, e.g. 0.75 seconds for a period equal to one second.

Would it make sense to tweak the TimedAverage period to be higher, e.g. 1.33 seconds/80 seconds/80 minutes, so that the _average_ period is 1 second/1 minute/1 hour?

This only applies to how the code is used, not to TimedAverage itself; hence, feel free to post the patch with Reviewed-by once timed_average_avg's return type is changed to a double.

Paolo

Quoted 16 lines
util/timed-average.c new file +15 -0
+    }
+
+    return 0;
+}
+
+/* Get the maximum value
+ *
+ * @ta:  the timed average structure used
+ * @ret: the maximal value
+ */
+uint64_t timed_average_max(TimedAverage *ta)
+{
+    check_expirations(ta);
+    return current_window(ta)->max;
+}
#11 lore raw

Re: [Qemu-devel] [PATCH v3 3/3] util: Infrastructure for computing recent averages

Quoted 27 lines · Paolo Bonzini

Il 24/09/2014 17:21, Benoît Canet ha scritto:

Quoted 2 lines

The module takes care of computing minimal and maximal values over the time slice duration.

The code looks good, just two comments:

Quoted 4 lines
util/timed-average.c new file +4 -0
+/* Get the average value
+ *
+ * @ta:  the timed average structure used
+ * @ret: the average value

util/timed-average.c line 193

Quoted 10 lines
util/timed-average.c new file +10 -0
+ */
+uint64_t timed_average_avg(TimedAverage *ta)
+{
+    Window *w;
+    check_expirations(ta);
+
+    w = current_window(ta);
+
+    if (w->count) {
+        return w->sum / w->count;

First, do we want this to return double?

Second, this will return the min/max/avg in an unknown amount of time between period/2 and period---on average period*3/4, e.g. 0.75 seconds for a period equal to one second.

Quoted 7 lines

Would it make sense to tweak the TimedAverage period to be higher, e.g. 1.33 seconds/80 seconds/80 minutes, so that the _average_ period is 1 second/1 minute/1 hour?

This only applies to how the code is used, not to TimedAverage itself; hence, feel free to post the patch with Reviewed-by once timed_average_avg's return type is changed to a double.

This would require either changing _init period units or making it a float or baking the 1.33 ratio in timed average.

Which one would you prefer ?

Best regards

Benoît

Thanks for reviewing.

Quoted 20 lines

Paolo

Quoted 16 lines
util/timed-average.c new file +15 -0
+    }
+
+    return 0;
+}
+
+/* Get the maximum value
+ *
+ * @ta:  the timed average structure used
+ * @ret: the maximal value
+ */
+uint64_t timed_average_max(TimedAverage *ta)
+{
+    check_expirations(ta);
+    return current_window(ta)->max;
+}