Skip to content

Commit 93600bd

Browse files
authored
Merge pull request #14 from xapi-project/private/asultanov/monotonic-timestamp
librrd: Get timestamp at a higher precision
2 parents 9bd07a7 + f3d6e3a commit 93600bd

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ considered private to the library.
107107

108108
The name of the plugin is descriptive, as whether it reports data
109109
for a single machine (`RRD_LOCAL_DOMAIN`) or multiple
110-
(`RRD_INTER_DOMAIN`). The second parameter of `rrd_sample` is typically
111-
NULL. If it isn't, it us used to obtain a timestamp instead of using
112-
time(3).
110+
(`RRD_INTER_DOMAIN`). The second parameter of `rrd_sample` is deprecated
111+
and will be ignored (and should be NULL).
113112

114113
When a plugin is opened, the file at `path` is being created and it is
115114
removed when the plugin is closed.

librrd.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ json_for_plugin(RRD_PLUGIN * plugin)
218218
}
219219
return root_json;
220220
}
221+
222+
double get_timestamp()
223+
{
224+
struct timeval tp;
225+
gettimeofday(&tp, NULL);
226+
return (double) tp.tv_sec + (double) tp.tv_usec / 1e6;
227+
}
228+
221229
/*
222230
* initialise the buffer that we update and write out to a file. Once
223231
* initialised, it is kept up to date by sample().
@@ -263,7 +271,7 @@ initialise(RRD_PLUGIN * plugin)
263271
memcpy(&header->rrd_magic, MAGIC, MAGIC_SIZE);
264272
header->rrd_checksum_value = htonl(0x01234567);
265273
header->rrd_header_datasources = htonl(plugin->n);
266-
header->rrd_timestamp = htonll((uint64_t) time(NULL));
274+
header->rrd_timestamp = htonll(get_timestamp());
267275
if (header->rrd_timestamp == -1) {
268276
free(plugin->buf);
269277
plugin->buf_size = 0;
@@ -450,7 +458,7 @@ rrd_sample(RRD_PLUGIN * plugin, time_t(*t) (time_t *))
450458
* update timestamp, calculate crc
451459
*/
452460

453-
header->rrd_timestamp = htonll((uint64_t) (t ? t(NULL) : time(NULL)));
461+
header->rrd_timestamp = htonll(get_timestamp());
454462
uint32_t crc = crc32(0L, Z_NULL, 0);
455463
crc = crc32(crc,
456464
(unsigned char *)&header->rrd_timestamp,

librrd.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
#include <stdint.h>
26-
#include <time.h>
26+
#include <sys/time.h>
2727

2828
#define RRD_MAX_SOURCES 16
2929

@@ -131,8 +131,6 @@ int rrd_del_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);
131131
* calling rrd_sample(plugin) triggers that all data sources are sampled
132132
* and the results are reported to the RRD daemon. This function needs
133133
* to be called every 5 seconds by the client. The second parameter is
134-
* typically NULL. If it isn't, it will be used instead of time(3) to
135-
* obtain the time stamp that is written to the RRD file. This can be
136-
* used to create RRD files that don't depend on the current time.
134+
* deprecated and will be ignored (and should be NULL).
137135
*/
138136
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));

0 commit comments

Comments
 (0)