Skip to content

Commit b6e9fc1

Browse files
committed
CP-18272 change signature of rrd_sample to support testing
The commit changes the signature of rrd_sample to int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*)); The caller can provide a function to obtain a time stamp or provide NULL (the typical case). This permits to control the time stamps in RRD files for debugging and testing. Signed-off-by: Christian Lindig <[email protected]>
1 parent 8d9413a commit b6e9fc1

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

Makefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ indent: librrd.h librrd.c rrdtest.c
5555
depend: librrd.c rrdtest.c
5656
$(CC) -MM $^
5757

58-
.PHONY: parson
59-
parson:
60-
# git submodule add https://github.com/kgabis/parson.git
61-
git submodule init
62-
git submodule update
63-
6458
%.o: %.c
6559
$(CC) $(CFLAGS) -c -o $@ $<
6660

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ library:
4242
int rrd_close(RRD_PLUGIN * plugin);
4343
int rrd_add_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);
4444
int rrd_del_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);
45-
int rrd_sample(RRD_PLUGIN * plugin);
45+
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));
4646

4747
A plugin reports streams of data to the RRD service. Each such
4848
stream is represented as an `RRD_SOURCE` value. An `RRD_PLUGIN`
@@ -83,7 +83,7 @@ All strings are in UTF8 encoding. The library implements the following
8383
policy to manage memory: it does not free any memory that is hasn't
8484
itself allocated. This means, if the client passes dynamically allocated
8585
data into the library, it is the client's responsibility to de-allocate
86-
it.
86+
it.
8787

8888
## Open, Sample, Close
8989

@@ -102,12 +102,14 @@ considered private to the library.
102102
<<function declarations>>=
103103
RRD_PLUGIN *rrd_open(char *name, rrd_domain_t domain, char *path);
104104
int rrd_close(RRD_PLUGIN * plugin);
105-
int rrd_sample(RRD_PLUGIN * plugin);
105+
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));
106106

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`).
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).
111113

112114
## Data Sources
113115

librrd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <stdlib.h>
2727
#include <stdio.h>
2828
#include <zlib.h>
29-
#include <time.h>
3029
#include <assert.h>
3130
#include <sys/types.h>
3231
#include <sys/stat.h>
@@ -400,7 +399,7 @@ write_exact(int fd, const void *data, size_t size)
400399
* first.
401400
*/
402401
int
403-
rrd_sample(RRD_PLUGIN * plugin)
402+
rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*))
404403
{
405404
assert(plugin);
406405
JSON_Value *json;
@@ -440,7 +439,8 @@ rrd_sample(RRD_PLUGIN * plugin)
440439
/*
441440
* update timestamp, calculate crc
442441
*/
443-
header->rrd_timestamp = htonll((uint64_t) time(NULL));
442+
443+
header->rrd_timestamp = htonll((uint64_t) (t ? t(NULL) : time(NULL)));
444444
uint32_t crc = crc32(0L, Z_NULL, 0);
445445
crc = crc32(crc,
446446
(unsigned char *) &header->rrd_timestamp,

librrd.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

2525
#include <stdint.h>
26+
#include <time.h>
2627
#include "parson/parson.h"
2728

2829
#define RRD_MAX_SOURCES 128
@@ -124,7 +125,10 @@ int rrd_del_src(RRD_PLUGIN * plugin, RRD_SOURCE * source);
124125

125126
/*
126127
* calling rrd_sample(plugin) triggers that all data sources are sampled
127-
* and the results are reported to the RRD daemon. This function needs to
128-
* be called every 5 seconds by the client,
128+
* and the results are reported to the RRD daemon. This function needs
129+
* to be called every 5 seconds by the client. The second parameter is
130+
* typically NULL. If it isn't, it will be used instead of time(3) to
131+
* obtain the time stamp that is written to the RRD file. This can be
132+
* used to create RRD files that don't depend on the current time.
129133
*/
130-
int rrd_sample(RRD_PLUGIN * plugin);
134+
int rrd_sample(RRD_PLUGIN * plugin, time_t (*t)(time_t*));

rrdclient.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ main(int argc, char **argv)
7373
int rc;
7474
while (fgets(line, sizeof(line), stdin) != NULL) {
7575
v.int64 = (int64_t) atol(line);
76-
rc = rrd_sample(plugin);
76+
rc = rrd_sample(plugin, NULL);
7777
assert(rc == RRD_OK);
7878
}
7979

rrdtest.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ main(int argc, char **argv)
8282

8383
printf("adding source: %s\n", src[0].name);
8484
rrd_add_src(plugin, &src[0]);
85-
rc = rrd_sample(plugin);
85+
rc = rrd_sample(plugin, NULL);
8686
assert(rc == RRD_OK);
8787

8888
src[1].name = "second";
@@ -99,12 +99,12 @@ main(int argc, char **argv)
9999

100100
printf("adding source: %s\n", src[1].name);
101101
rrd_add_src(plugin, &src[1]);
102-
rc = rrd_sample(plugin);
102+
rc = rrd_sample(plugin, NULL);
103103
assert(rc == RRD_OK);
104104

105105
printf("removing source: %s\n", src[0].name);
106106
rrd_del_src(plugin, &src[0]);
107-
rc = rrd_sample(plugin);
107+
rc = rrd_sample(plugin, NULL);
108108
assert(rc == RRD_OK);
109109

110110
printf("removing source: %s\n", src[1].name);

0 commit comments

Comments
 (0)