Skip to content
Snippets Groups Projects
Commit 676677de authored by Tom Gundersen's avatar Tom Gundersen Committed by David Herrmann
Browse files

util/metrics: take clock id as argument to init


We may want to use the metrics infratsructure with different clocks.

Signed-off-by: default avatarTom Gundersen <teg@jklm.no>
parent 6dc7d010
No related branches found
No related tags found
No related merge requests found
......@@ -51,7 +51,7 @@ struct Bus {
.wildcard_matches = MATCH_REGISTRY_INIT((_x).wildcard_matches), \
.sender_matches = MATCH_REGISTRY_INIT((_x).sender_matches), \
.peers = PEER_REGISTRY_INIT, \
.metrics = METRICS_INIT, \
.metrics = METRICS_INIT(CLOCK_THREAD_CPUTIME_ID), \
}
int bus_init(Bus *bus,
......
......@@ -21,13 +21,13 @@
#include <time.h>
#include "util/metrics.h"
void metrics_init(Metrics *metrics) {
*metrics = (Metrics)METRICS_INIT;
void metrics_init(Metrics *metrics, clockid_t id) {
*metrics = (Metrics)METRICS_INIT(id);
}
void metrics_deinit(Metrics *metrics) {
assert(!metrics->timestamp);
metrics_init(metrics);
metrics_init(metrics, metrics->id);
}
/**
......@@ -37,11 +37,11 @@ void metrics_deinit(Metrics *metrics) {
*
* Return: the timestamp in nano seconds.
*/
uint64_t metrics_get_time(void) {
uint64_t metrics_get_time(Metrics *metrics) {
struct timespec ts;
int r;
r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
r = clock_gettime(metrics->id, &ts);
assert(r >= 0);
return ts.tv_sec * UINT64_C(1000000000) + ts.tv_nsec;
......@@ -58,7 +58,7 @@ uint64_t metrics_get_time(void) {
void metrics_sample_add(Metrics *metrics, uint64_t timestamp) {
uint64_t sample, average_old;
sample = metrics_get_time() - timestamp;
sample = metrics_get_time(metrics) - timestamp;
metrics->count ++;
metrics->sum += sample;
......@@ -83,7 +83,7 @@ void metrics_sample_add(Metrics *metrics, uint64_t timestamp) {
*/
void metrics_sample_start(Metrics *metrics) {
assert(!metrics->timestamp);
metrics->timestamp = metrics_get_time();
metrics->timestamp = metrics_get_time(metrics);
}
/**
......
......@@ -6,6 +6,7 @@
#include <c-macro.h>
#include <stdlib.h>
#include <time.h>
typedef struct Metrics Metrics;
......@@ -17,18 +18,20 @@ struct Metrics {
uint64_t average;
/* internal state */
clockid_t id;
uint64_t timestamp;
uint64_t sum_of_squares;
};
#define METRICS_INIT { \
#define METRICS_INIT(_id) { \
.minimum = (uint64_t) -1, \
.id = (_id), \
}
void metrics_init(Metrics *metrics);
void metrics_init(Metrics *metrics, clockid_t id);
void metrics_deinit(Metrics *metrics);
uint64_t metrics_get_time(void);
uint64_t metrics_get_time(Metrics *metrics);
void metrics_sample_add(Metrics *metrics, uint64_t timestamp);
void metrics_sample_start(Metrics *metrics);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment