diff mbox series

[RFC,bpf-next,12/15] samples: bpf: subtract time spent in collection from polling interval

Message ID 20210528235250.2635167-13-memxor@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series Improve XDP samples usability and output | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 1 maintainers not CCed: hawk@kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: line length of 90 exceeds 80 columns
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Kumar Kartikeya Dwivedi May 28, 2021, 11:52 p.m. UTC
This improves sleeping precision and reduces the possibility of
reporting incorrect statistics to the user.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 samples/bpf/xdp_redirect_cpu_user.c |  7 ++++++-
 samples/bpf/xdp_sample_user.c       | 27 ++++++++++++++++++++++++++-
 samples/bpf/xdp_sample_user.h       |  2 ++
 3 files changed, 34 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/samples/bpf/xdp_redirect_cpu_user.c b/samples/bpf/xdp_redirect_cpu_user.c
index 4c9f32229508..103ac5c24163 100644
--- a/samples/bpf/xdp_redirect_cpu_user.c
+++ b/samples/bpf/xdp_redirect_cpu_user.c
@@ -214,6 +214,9 @@  static void __stats_poll(int interval, bool use_separators, char *prog_name,
 		setlocale(LC_NUMERIC, "en_US");
 
 	for (;;) {
+		struct timespec ots, nts;
+
+		clock_gettime(CLOCK_MONOTONIC, &ots);
 		swap(&prev, &record);
 		sample_stats_collect(mask, record);
 		sample_stats_print(mask, record, prev, NULL, interval);
@@ -224,7 +227,9 @@  static void __stats_poll(int interval, bool use_separators, char *prog_name,
 		if (sample_log_level & LL_DEFAULT)
 			printf("\n");
 		fflush(stdout);
-		sleep(interval);
+		clock_gettime(CLOCK_MONOTONIC, &nts);
+		sample_calc_timediff(&nts, &ots, interval);
+		nanosleep(&nts, NULL);
 		if (stress_mode)
 			stress_cpumap(value);
 		sample_reset_mode();
diff --git a/samples/bpf/xdp_sample_user.c b/samples/bpf/xdp_sample_user.c
index 909257ffe54c..96d36c708ee3 100644
--- a/samples/bpf/xdp_sample_user.c
+++ b/samples/bpf/xdp_sample_user.c
@@ -923,6 +923,26 @@  void sample_stats_print(int mask, struct stats_record *cur,
 	stats_print(prog_name, mask, cur, prev, &out);
 }
 
+static void calc_timediff(struct timespec *cur, const struct timespec *prev)
+{
+	if (cur->tv_nsec - prev->tv_nsec < 0) {
+		cur->tv_sec = cur->tv_sec - prev->tv_sec - 1;
+		cur->tv_nsec = cur->tv_nsec - prev->tv_nsec + NANOSEC_PER_SEC;
+	} else {
+		cur->tv_sec -= prev->tv_sec;
+		cur->tv_nsec -= prev->tv_nsec;
+	}
+}
+
+void sample_calc_timediff(struct timespec *cur, const struct timespec *prev, int interval)
+{
+	struct timespec ts = { .tv_sec = interval };
+
+	calc_timediff(cur, prev);
+	calc_timediff(&ts, cur);
+	*cur = ts;
+}
+
 void sample_stats_poll(int interval, int mask, char *prog_name, int use_separators)
 {
 	struct stats_record *record, *prev;
@@ -936,11 +956,16 @@  void sample_stats_poll(int interval, int mask, char *prog_name, int use_separato
 		setlocale(LC_NUMERIC, "en_US");
 
 	for (;;) {
+		struct timespec ots, nts;
+
+		clock_gettime(CLOCK_MONOTONIC, &ots);
 		swap(&prev, &record);
 		sample_stats_collect(mask, record);
 		sample_stats_print(mask, record, prev, prog_name, interval);
 		fflush(stdout);
-		sleep(interval);
+		clock_gettime(CLOCK_MONOTONIC, &nts);
+		sample_calc_timediff(&nts, &ots, interval);
+		nanosleep(&nts, NULL);
 		sample_reset_mode();
 	}
 
diff --git a/samples/bpf/xdp_sample_user.h b/samples/bpf/xdp_sample_user.h
index abe4ec25c310..588bd2f15352 100644
--- a/samples/bpf/xdp_sample_user.h
+++ b/samples/bpf/xdp_sample_user.h
@@ -161,6 +161,8 @@  void sample_stats_print(int mask, struct stats_record *cur,
 void sample_stats_collect(int mask, struct stats_record *rec);
 void sample_summary_update(struct sample_output *out, int interval);
 void sample_summary_print(void);
+void sample_calc_timediff(struct timespec *cur, const struct timespec *prev,
+			  int interval);
 void sample_stats_poll(int interval, int mask, char *prog_name,
 		       int use_separators);
 void sample_stats_print_cpumap_remote(struct stats_record *stats_rec,