cheloha@ switched the bgpctl code to use struct timespec and a monotonic clock. Adjust the ometric code to use a timespec internally so that there is no need to convert from timespec to timeval.
-- :wq Claudio Index: ometric.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/ometric.c,v retrieving revision 1.7 diff -u -p -r1.7 ometric.c --- ometric.c 6 Dec 2022 17:38:41 -0000 1.7 +++ ometric.c 9 Dec 2022 11:52:29 -0000 @@ -43,7 +43,7 @@ struct olabels { enum ovalue_type { OVT_INTEGER, OVT_DOUBLE, - OVT_TIMEVAL, + OVT_TIMESPEC, }; struct ovalue { @@ -52,7 +52,7 @@ struct ovalue { union { unsigned long long i; double f; - struct timeval tv; + struct timespec ts; } value; enum ovalue_type valtype; }; @@ -316,9 +316,9 @@ ometric_output_value(FILE *out, const st return fprintf(out, "%llu", ov->value.i); case OVT_DOUBLE: return fprintf(out, "%g", ov->value.f); - case OVT_TIMEVAL: - return fprintf(out, "%lld.%06ld", - (long long)ov->value.tv.tv_sec, (long)ov->value.tv.tv_usec); + case OVT_TIMESPEC: + return fprintf(out, "%lld.%09ld", + (long long)ov->value.ts.tv_sec, ov->value.ts.tv_nsec); } return -1; } @@ -430,10 +430,10 @@ ometric_set_float(struct ometric *om, do } /* - * Set an timeval value with label ol. ol can be NULL. + * Set an timespec value with label ol. ol can be NULL. */ void -ometric_set_timeval(struct ometric *om, const struct timeval *tv, +ometric_set_timespec(struct ometric *om, const struct timespec *ts, struct olabels *ol) { struct ovalue *ov; @@ -444,8 +444,8 @@ ometric_set_timeval(struct ometric *om, if ((ov = malloc(sizeof(*ov))) == NULL) err(1, NULL); - ov->value.tv = *tv; - ov->valtype = OVT_TIMEVAL; + ov->value.ts = *ts; + ov->valtype = OVT_TIMESPEC; ov->labels = olabels_ref(ol); STAILQ_INSERT_TAIL(&om->vals, ov, entry); @@ -512,12 +512,12 @@ ometric_set_int_with_labels(struct ometr } void -ometric_set_timeval_with_labels(struct ometric *om, struct timeval *tv, +ometric_set_timespec_with_labels(struct ometric *om, struct timespec *ts, const char **keys, const char **values, struct olabels *ol) { struct olabels *extra; extra = olabels_add_extras(ol, keys, values); - ometric_set_timeval(om, tv, extra); + ometric_set_timespec(om, ts, extra); olabels_free(extra); } Index: ometric.h =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/ometric.h,v retrieving revision 1.4 diff -u -p -r1.4 ometric.h --- ometric.h 6 Dec 2022 11:27:58 -0000 1.4 +++ ometric.h 9 Dec 2022 11:49:11 -0000 @@ -41,13 +41,13 @@ int ometric_output_all(FILE *); /* functions to set gauge and counter metrics */ void ometric_set_int(struct ometric *, uint64_t, struct olabels *); void ometric_set_float(struct ometric *, double, struct olabels *); -void ometric_set_timeval(struct ometric *, const struct timeval *, +void ometric_set_timespec(struct ometric *, const struct timespec *, struct olabels *); void ometric_set_info(struct ometric *, const char **, const char **, struct olabels *); void ometric_set_state(struct ometric *, const char *, struct olabels *); void ometric_set_int_with_labels(struct ometric *, uint64_t, const char **, const char **, struct olabels *); -void ometric_set_timeval_with_labels(struct ometric *, struct timeval *, +void ometric_set_timespec_with_labels(struct ometric *, struct timespec *, const char **, const char **, struct olabels *); #define OKV(...) (const char *[]){ __VA_ARGS__, NULL } Index: output_ometric.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/output_ometric.c,v retrieving revision 1.9 diff -u -p -r1.9 output_ometric.c --- output_ometric.c 8 Dec 2022 17:24:39 -0000 1.9 +++ output_ometric.c 9 Dec 2022 11:51:10 -0000 @@ -322,13 +322,11 @@ static void ometric_tail(void) { struct timespec elapsed_time; - struct timeval tv; clock_gettime(CLOCK_MONOTONIC, &end_time); timespecsub(&end_time, &start_time, &elapsed_time); - TIMESPEC_TO_TIMEVAL(&tv, &elapsed_time); - ometric_set_timeval(bgpd_scrape_time, &tv, NULL); + ometric_set_timespec(bgpd_scrape_time, &elapsed_time, NULL); ometric_output_all(stdout); ometric_free_all();