diff mbox series

[liburing,4/5] examples/sendzc: try to print stats on SIGINT

Message ID 08d8d03b0f4e17afb8643996b8d8f55a730d1a4f.1701789563.git.asml.silence@gmail.com (mailing list archive)
State New
Headers show
Series send-zc test/bench improvements | expand

Commit Message

Pavel Begunkov Dec. 5, 2023, 3:22 p.m. UTC
If interrupted in the middle of a long run, instead of silently
crashing, as we currently do, we can try to print intermediate stats.
That's a good use case, and it's always annoying loosing results when
you forget about it.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 examples/send-zerocopy.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
diff mbox series

Patch

diff --git a/examples/send-zerocopy.c b/examples/send-zerocopy.c
index 9725d0b..84d2323 100644
--- a/examples/send-zerocopy.c
+++ b/examples/send-zerocopy.c
@@ -39,6 +39,7 @@ 
 #include <sys/wait.h>
 #include <sys/mman.h>
 #include <linux/mman.h>
+#include <signal.h>
 
 #include "liburing.h"
 
@@ -82,6 +83,16 @@  static char *payload;
 static struct thread_data threads[MAX_THREADS];
 static pthread_barrier_t barrier;
 
+static bool should_stop = false;
+
+static void sigint_handler(int sig)
+{
+	/* kill if should_stop can't unblock threads fast enough */
+	if (should_stop)
+		_exit(-1);
+	should_stop = true;
+}
+
 /*
  * Implementation of error(3), prints an error message and exits.
  */
@@ -421,6 +432,8 @@  static void do_tx(struct thread_data *td, int domain, int type, int protocol)
 			}
 			io_uring_cqe_seen(&ring, cqe);
 		}
+		if (should_stop)
+			break;
 	} while ((++loop % 16 != 0) || gettimeofday_ms() < tstart + cfg_runtime_ms);
 
 	td->dt_ms = gettimeofday_ms() - tstart;
@@ -582,6 +595,9 @@  int main(int argc, char **argv)
 	if (cfg_rx)
 		do_setup_rx(cfg_family, cfg_type, 0);
 
+	if (!cfg_rx)
+		signal(SIGINT, sigint_handler);
+
 	for (i = 0; i < cfg_nr_threads; i++)
 		pthread_create(&threads[i].thread, NULL,
 				!cfg_rx ? do_test : do_rx, &threads[i]);