xref: /qemu/include/qemu/timed-average.h (revision 7a4e543d)
1 /*
2  * QEMU timed average computation
3  *
4  * Copyright (C) Nodalink, EURL. 2014
5  * Copyright (C) Igalia, S.L. 2015
6  *
7  * Authors:
8  *   Benoît Canet <benoit.canet@nodalink.com>
9  *   Alberto Garcia <berto@igalia.com>
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or
14  * (at your option) version 3 or any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef TIMED_AVERAGE_H
26 #define TIMED_AVERAGE_H
27 
28 #include <stdint.h>
29 
30 #include "qemu/timer.h"
31 
32 typedef struct TimedAverageWindow TimedAverageWindow;
33 typedef struct TimedAverage TimedAverage;
34 
35 /* All fields of both structures are private */
36 
37 struct TimedAverageWindow {
38     uint64_t      min;             /* minimum value accounted in the window */
39     uint64_t      max;             /* maximum value accounted in the window */
40     uint64_t      sum;             /* sum of all values */
41     uint64_t      count;           /* number of values */
42     int64_t       expiration;      /* the end of the current window in ns */
43 };
44 
45 struct TimedAverage {
46     uint64_t           period;     /* period in nanoseconds */
47     TimedAverageWindow windows[2]; /* two overlapping windows of with
48                                     * an offset of period / 2 between them */
49     unsigned           current;    /* the current window index: it's also the
50                                     * oldest window index */
51     QEMUClockType      clock_type; /* the clock used */
52 };
53 
54 void timed_average_init(TimedAverage *ta, QEMUClockType clock_type,
55                         uint64_t period);
56 
57 void timed_average_account(TimedAverage *ta, uint64_t value);
58 
59 uint64_t timed_average_min(TimedAverage *ta);
60 uint64_t timed_average_avg(TimedAverage *ta);
61 uint64_t timed_average_max(TimedAverage *ta);
62 uint64_t timed_average_sum(TimedAverage *ta, uint64_t *elapsed);
63 
64 #endif
65