From 00a9d6b3147143b7ed3f208f3924ff46c31e1fd9 Mon Sep 17 00:00:00 2001 From: Ofir Luzon Date: Sun, 22 May 2022 00:55:26 -0700 Subject: [PATCH] Add SIGINT handler to redis-cli --bigkeys, --memkeys, --hotkeys, --scan (#10736) Finish current loop and display the scanned keys summery on SIGINT (Ctrl-C) signal. It will also prepend the current scanned percentage to the scanned keys summery 1st line. In this commit I've renamed and relocated `intrinsicLatencyModeStop` function as I'm using the exact same logic. --- src/redis-cli.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/redis-cli.c b/src/redis-cli.c index 1cd1ad85c..485076792 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -8220,6 +8220,11 @@ static void getKeySizes(redisReply *keys, typeinfo **types, } } +static void longStatLoopModeStop(int s) { + UNUSED(s); + force_cancel_loop = 1; +} + static void findBigKeys(int memkeys, unsigned memkeys_samples) { unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0, scan_loops = 0; redisReply *reply, *keys; @@ -8237,6 +8242,7 @@ static void findBigKeys(int memkeys, unsigned memkeys_samples) { typeinfo_add(types_dict, "zset", &type_zset); typeinfo_add(types_dict, "stream", &type_stream); + signal(SIGINT, longStatLoopModeStop); /* Total keys pre scanning */ total_keys = getDbSize(); @@ -8315,14 +8321,14 @@ static void findBigKeys(int memkeys, unsigned memkeys_samples) { } freeReplyObject(reply); - } while(it != 0); + } while(force_cancel_loop == 0 && it != 0); if(types) zfree(types); if(sizes) zfree(sizes); /* We're done */ printf("\n-------- summary -------\n\n"); - + if (force_cancel_loop) printf("[%05.2f%%] ", pct); printf("Sampled %llu keys in the keyspace!\n", sampled); printf("Total key length in bytes is %llu (avg len %.2f)\n\n", totlen, totlen ? (double)totlen/sampled : 0); @@ -8401,6 +8407,7 @@ static void findHotKeys(void) { unsigned int arrsize = 0, i, k; double pct; + signal(SIGINT, longStatLoopModeStop); /* Total keys pre scanning */ total_keys = getDbSize(); @@ -8466,13 +8473,13 @@ static void findHotKeys(void) { } freeReplyObject(reply); - } while(it != 0); + } while(force_cancel_loop ==0 && it != 0); if (freqs) zfree(freqs); /* We're done */ printf("\n-------- summary -------\n\n"); - + if(force_cancel_loop)printf("[%05.2f%%] ",pct); printf("Sampled %llu keys in the keyspace!\n", sampled); for (i=1; i<= HOTKEYS_SAMPLE; i++) { @@ -8643,7 +8650,7 @@ static void statMode(void) { static void scanMode(void) { redisReply *reply; unsigned long long cur = 0; - + signal(SIGINT, longStatLoopModeStop); do { reply = sendScan(&cur); for (unsigned int j = 0; j < reply->element[1]->elements; j++) { @@ -8658,7 +8665,7 @@ static void scanMode(void) { } freeReplyObject(reply); if (config.interval) usleep(config.interval); - } while(cur != 0); + } while(force_cancel_loop == 0 && cur != 0); exit(0); } @@ -8785,11 +8792,6 @@ unsigned long compute_something_fast(void) { return output; } -static void intrinsicLatencyModeStop(int s) { - UNUSED(s); - force_cancel_loop = 1; -} - static void sigIntHandler(int s) { UNUSED(s); @@ -8807,7 +8809,7 @@ static void intrinsicLatencyMode(void) { run_time = (long long)config.intrinsic_latency_duration * 1000000; test_end = ustime() + run_time; - signal(SIGINT, intrinsicLatencyModeStop); + signal(SIGINT, longStatLoopModeStop); while(1) { long long start, end, latency;