|
154 | 154 | #define LOG_COLOR_YELLOW "33;1m"
|
155 | 155 | #define LOG_COLOR_RESET "0m"
|
156 | 156 |
|
| 157 | +#define HOTKEYS_COUNT 16 |
| 158 | + |
157 | 159 | /* cliConnect() flags. */
|
158 | 160 | #define CC_FORCE (1 << 0) /* Re-connect if already connected. */
|
159 | 161 | #define CC_QUIET (1 << 1) /* Don't log connecting errors. */
|
@@ -249,6 +251,7 @@ static struct config {
|
249 | 251 | int memkeys;
|
250 | 252 | unsigned memkeys_samples;
|
251 | 253 | int hotkeys;
|
| 254 | + unsigned hotkeys_count; |
252 | 255 | int stdin_lastarg; /* get last arg from stdin. (-x option) */
|
253 | 256 | int stdin_tag_arg; /* get <tag> arg from stdin. (-X option) */
|
254 | 257 | char *stdin_tag_name; /* Placeholder(tag name) for user input. */
|
@@ -2687,6 +2690,10 @@ static int parseOptions(int argc, char **argv) {
|
2687 | 2690 | config.memkeys_samples = atoi(argv[++i]);
|
2688 | 2691 | } else if (!strcmp(argv[i], "--hotkeys")) {
|
2689 | 2692 | 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]); |
2690 | 2697 | } else if (!strcmp(argv[i], "--eval") && !lastarg) {
|
2691 | 2698 | config.eval = argv[++i];
|
2692 | 2699 | } else if (!strcmp(argv[i], "--ldb")) {
|
@@ -3016,7 +3023,10 @@ static void usage(int err) {
|
3016 | 3023 | " --memkeys-samples <n> Sample keys looking for keys consuming a lot of memory.\n"
|
3017 | 3024 | " And define number of key elements to sample\n"
|
3018 | 3025 | " --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" |
3020 | 3030 | " --scan List all keys using the SCAN command.\n"
|
3021 | 3031 | " --pattern <pat> Keys pattern when using the --scan, --bigkeys or --hotkeys\n"
|
3022 | 3032 | " options (default: *).\n"
|
@@ -9010,15 +9020,22 @@ static void getKeyFreqs(redisReply *keys, unsigned long long *freqs) {
|
9010 | 9020 | }
|
9011 | 9021 | }
|
9012 | 9022 |
|
9013 |
| -#define HOTKEYS_SAMPLE 16 |
9014 | 9023 | static void findHotKeys(void) {
|
9015 | 9024 | 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; |
9018 | 9027 | unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0, scan_loops = 0;
|
9019 | 9028 | unsigned int arrsize = 0, i, k;
|
9020 | 9029 | double pct;
|
9021 | 9030 |
|
| 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 | + |
9022 | 9039 | signal(SIGINT, longStatLoopModeStop);
|
9023 | 9040 | /* Total keys pre scanning */
|
9024 | 9041 | total_keys = getDbSize();
|
@@ -9065,7 +9082,7 @@ static void findHotKeys(void) {
|
9065 | 9082 |
|
9066 | 9083 | /* Use eviction pool here */
|
9067 | 9084 | k = 0;
|
9068 |
| - while (k < HOTKEYS_SAMPLE && freqs[i] > counters[k]) k++; |
| 9085 | + while (k < config.hotkeys_count && freqs[i] > counters[k]) k++; |
9069 | 9086 | if (k == 0) continue;
|
9070 | 9087 | k--;
|
9071 | 9088 | if (k == 0 || counters[k] == 0) {
|
@@ -9095,13 +9112,15 @@ static void findHotKeys(void) {
|
9095 | 9112 | if (force_cancel_loop) printf("[%05.2f%%] ", pct);
|
9096 | 9113 | printf("Sampled %llu keys in the keyspace!\n", sampled);
|
9097 | 9114 |
|
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; |
9100 | 9117 | if (counters[k] > 0) {
|
9101 | 9118 | printf("hot key found with counter: %llu\tkeyname: %s\n", counters[k], hotkeys[k]);
|
9102 | 9119 | sdsfree(hotkeys[k]);
|
9103 | 9120 | }
|
9104 | 9121 | }
|
| 9122 | + if (counters) zfree(counters); |
| 9123 | + if (hotkeys) zfree(hotkeys); |
9105 | 9124 |
|
9106 | 9125 | exit(0);
|
9107 | 9126 | }
|
|
0 commit comments