]> git.proxmox.com Git - mirror_ovs.git/blob - lib/stopwatch.h
stopwatch: Add API for waiting until samples have been processed
[mirror_ovs.git] / lib / stopwatch.h
1 /* Copyright (c) 2017 Red Hat, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef STOPWATCH_H
17 #define STOPWATCH_H 1
18
19 #include <stdbool.h>
20
21 enum stopwatch_units {
22 SW_MS,
23 SW_US,
24 SW_NS,
25 };
26
27 struct stopwatch_stats {
28 unsigned long long count; /* Total number of samples. */
29 enum stopwatch_units unit; /* Unit of following values. */
30 unsigned long long max; /* Maximum value. */
31 unsigned long long min; /* Minimum value. */
32 double pctl_95; /* 95th percentile. */
33 double ewma_50; /* Exponentially weighted moving average (alpha 0.50). */
34 double ewma_1; /* Exponentially weighted moving average (alpha 0.01). */
35 };
36
37 /* Create a new stopwatch.
38 * The "units" are not used for any calculations but are printed when
39 * statistics are requested.
40 */
41 void stopwatch_create(const char *name, enum stopwatch_units units);
42
43 /* Start a stopwatch. */
44 void stopwatch_start(const char *name, unsigned long long ts);
45
46 /* Stop a stopwatch. The elapsed time will be used for updating statistics
47 * for this stopwatch.
48 */
49 void stopwatch_stop(const char *name, unsigned long long ts);
50
51 /* Retrieve statistics calculated from collected samples */
52 bool stopwatch_get_stats(const char *name, struct stopwatch_stats *stats);
53
54 /* Block until all enqueued samples have been processed. */
55 void stopwatch_sync(void);
56
57 #endif /* stopwatch.h */