Skip to content

Commit 83d2979

Browse files
hwwarezuiderkwast
andauthored
Add --hotkeys-count option for Valkey-cli (#1933)
Now we only have --hotkeys option, and the default output value is 16. This PR add one more option for hotkeys as "--hotkeys-count", users have chance to specific how many output items they want. Example 1: ./src/valkey-cli --hotkeys-count 10 [00.00%] Hot key '"memtier-3308"' found so far with counter 5 [00.00%] Hot key '"memtier-23516"' found so far with counter 5 [00.00%] Hot key '"memtier-16678"' found so far with counter 5 [00.00%] Hot key '"memtier-25001"' found so far with counter 5 [00.00%] Hot key '"memtier-63829"' found so far with counter 5 [00.00%] Hot key '"memtier-21281"' found so far with counter 5 [00.00%] Hot key '"memtier-81379"' found so far with counter 5 [00.00%] Hot key '"memtier-30335"' found so far with counter 5 [00.00%] Hot key '"memtier-87521"' found so far with counter 5 [00.00%] Hot key '"memtier-42166"' found so far with counter 5 -------- summary ------- Sampled 100000 keys in the keyspace! hot key found with counter: 5 keyname: "memtier-3308" hot key found with counter: 5 keyname: "memtier-23516" hot key found with counter: 5 keyname: "memtier-16678" hot key found with counter: 5 keyname: "memtier-25001" hot key found with counter: 5 keyname: "memtier-63829" hot key found with counter: 5 keyname: "memtier-21281" hot key found with counter: 5 keyname: "memtier-81379" hot key found with counter: 5 keyname: "memtier-30335" hot key found with counter: 5 keyname: "memtier-87521" hot key found with counter: 5 keyname: "memtier-42166" Example 2: ./src/valkey-cli --hotkeys-count 20 [00.00%] Hot key '"memtier-3308"' found so far with counter 5 [00.00%] Hot key '"memtier-23516"' found so far with counter 5 [00.00%] Hot key '"memtier-16678"' found so far with counter 5 [00.00%] Hot key '"memtier-25001"' found so far with counter 5 [00.00%] Hot key '"memtier-63829"' found so far with counter 5 [00.00%] Hot key '"memtier-21281"' found so far with counter 5 [00.00%] Hot key '"memtier-81379"' found so far with counter 5 [00.00%] Hot key '"memtier-30335"' found so far with counter 5 [00.00%] Hot key '"memtier-87521"' found so far with counter 5 [00.00%] Hot key '"memtier-42166"' found so far with counter 5 [00.00%] Hot key '"memtier-6370"' found so far with counter 5 [00.01%] Hot key '"memtier-37381"' found so far with counter 5 [00.01%] Hot key '"memtier-95045"' found so far with counter 5 [00.01%] Hot key '"memtier-14027"' found so far with counter 5 [00.01%] Hot key '"memtier-60370"' found so far with counter 5 [00.01%] Hot key '"memtier-98622"' found so far with counter 5 [00.01%] Hot key '"memtier-18642"' found so far with counter 5 [00.01%] Hot key '"memtier-39260"' found so far with counter 5 [00.01%] Hot key '"memtier-64371"' found so far with counter 5 [00.01%] Hot key '"memtier-27099"' found so far with counter 5 -------- summary ------- Sampled 100000 keys in the keyspace! hot key found with counter: 5 keyname: "memtier-3308" hot key found with counter: 5 keyname: "memtier-23516" hot key found with counter: 5 keyname: "memtier-16678" hot key found with counter: 5 keyname: "memtier-25001" hot key found with counter: 5 keyname: "memtier-63829" hot key found with counter: 5 keyname: "memtier-21281" hot key found with counter: 5 keyname: "memtier-81379" hot key found with counter: 5 keyname: "memtier-30335" hot key found with counter: 5 keyname: "memtier-87521" hot key found with counter: 5 keyname: "memtier-42166" hot key found with counter: 5 keyname: "memtier-6370" hot key found with counter: 5 keyname: "memtier-37381" hot key found with counter: 5 keyname: "memtier-95045" hot key found with counter: 5 keyname: "memtier-14027" hot key found with counter: 5 keyname: "memtier-60370" hot key found with counter: 5 keyname: "memtier-98622" hot key found with counter: 5 keyname: "memtier-18642" hot key found with counter: 5 keyname: "memtier-39260" hot key found with counter: 5 keyname: "memtier-64371" hot key found with counter: 5 keyname: "memtier-27099" --------- Signed-off-by: hwware <wen.hui.ware@gmail.com> Signed-off-by: Wen Hui <wen.hui.ware@gmail.com> Co-authored-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
1 parent 3884650 commit 83d2979

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/valkey-cli.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
#define LOG_COLOR_YELLOW "33;1m"
155155
#define LOG_COLOR_RESET "0m"
156156

157+
#define HOTKEYS_COUNT 16
158+
157159
/* cliConnect() flags. */
158160
#define CC_FORCE (1 << 0) /* Re-connect if already connected. */
159161
#define CC_QUIET (1 << 1) /* Don't log connecting errors. */
@@ -249,6 +251,7 @@ static struct config {
249251
int memkeys;
250252
unsigned memkeys_samples;
251253
int hotkeys;
254+
unsigned hotkeys_count;
252255
int stdin_lastarg; /* get last arg from stdin. (-x option) */
253256
int stdin_tag_arg; /* get <tag> arg from stdin. (-X option) */
254257
char *stdin_tag_name; /* Placeholder(tag name) for user input. */
@@ -2687,6 +2690,10 @@ static int parseOptions(int argc, char **argv) {
26872690
config.memkeys_samples = atoi(argv[++i]);
26882691
} else if (!strcmp(argv[i], "--hotkeys")) {
26892692
config.hotkeys = 1;
2693+
config.hotkeys_count = HOTKEYS_COUNT;
2694+
} else if (!strcmp(argv[i], "--hotkeys-count") && !lastarg) {
2695+
config.hotkeys = 1;
2696+
config.hotkeys_count = atoi(argv[++i]);
26902697
} else if (!strcmp(argv[i], "--eval") && !lastarg) {
26912698
config.eval = argv[++i];
26922699
} else if (!strcmp(argv[i], "--ldb")) {
@@ -3016,7 +3023,10 @@ static void usage(int err) {
30163023
" --memkeys-samples <n> Sample keys looking for keys consuming a lot of memory.\n"
30173024
" And define number of key elements to sample\n"
30183025
" --hotkeys Sample keys looking for hot keys.\n"
3019-
" only works when maxmemory-policy is *lfu.\n"
3026+
" Only works when maxmemory-policy is *lfu.\n"
3027+
" This is equivalent to --hotkeys-count 16.\n"
3028+
" --hotkeys-count <n> Sample keys looking for the n most hot keys.\n"
3029+
" Only works when maxmemory-policy is *lfu.\n"
30203030
" --scan List all keys using the SCAN command.\n"
30213031
" --pattern <pat> Keys pattern when using the --scan, --bigkeys or --hotkeys\n"
30223032
" options (default: *).\n"
@@ -9010,15 +9020,22 @@ static void getKeyFreqs(redisReply *keys, unsigned long long *freqs) {
90109020
}
90119021
}
90129022

9013-
#define HOTKEYS_SAMPLE 16
90149023
static void findHotKeys(void) {
90159024
redisReply *keys, *reply;
9016-
unsigned long long counters[HOTKEYS_SAMPLE] = {0};
9017-
sds hotkeys[HOTKEYS_SAMPLE] = {NULL};
9025+
unsigned long long *counters = NULL;
9026+
sds *hotkeys = NULL;
90189027
unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0, scan_loops = 0;
90199028
unsigned int arrsize = 0, i, k;
90209029
double pct;
90219030

9031+
counters = zrealloc(counters, sizeof(unsigned long long) * config.hotkeys_count);
9032+
hotkeys = zrealloc(hotkeys, sizeof(sds) * config.hotkeys_count);
9033+
unsigned long long nums;
9034+
for (nums = 0; nums < config.hotkeys_count; nums++) {
9035+
counters[nums] = 0;
9036+
hotkeys[nums] = NULL;
9037+
}
9038+
90229039
signal(SIGINT, longStatLoopModeStop);
90239040
/* Total keys pre scanning */
90249041
total_keys = getDbSize();
@@ -9065,7 +9082,7 @@ static void findHotKeys(void) {
90659082

90669083
/* Use eviction pool here */
90679084
k = 0;
9068-
while (k < HOTKEYS_SAMPLE && freqs[i] > counters[k]) k++;
9085+
while (k < config.hotkeys_count && freqs[i] > counters[k]) k++;
90699086
if (k == 0) continue;
90709087
k--;
90719088
if (k == 0 || counters[k] == 0) {
@@ -9095,13 +9112,15 @@ static void findHotKeys(void) {
90959112
if (force_cancel_loop) printf("[%05.2f%%] ", pct);
90969113
printf("Sampled %llu keys in the keyspace!\n", sampled);
90979114

9098-
for (i = 1; i <= HOTKEYS_SAMPLE; i++) {
9099-
k = HOTKEYS_SAMPLE - i;
9115+
for (i = 1; i <= config.hotkeys_count; i++) {
9116+
k = config.hotkeys_count - i;
91009117
if (counters[k] > 0) {
91019118
printf("hot key found with counter: %llu\tkeyname: %s\n", counters[k], hotkeys[k]);
91029119
sdsfree(hotkeys[k]);
91039120
}
91049121
}
9122+
if (counters) zfree(counters);
9123+
if (hotkeys) zfree(hotkeys);
91059124

91069125
exit(0);
91079126
}

0 commit comments

Comments
 (0)