MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / findHotKeys

Function findHotKeys

src/redis-cli.c:6713–6803  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6711
6712#define HOTKEYS_SAMPLE 16
6713static void findHotKeys(void) {
6714 redisReply *keys, *reply;
6715 unsigned long long counters[HOTKEYS_SAMPLE] = {0};
6716 sds hotkeys[HOTKEYS_SAMPLE] = {NULL};
6717 unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0;
6718 unsigned int arrsize = 0, i, k;
6719 double pct;
6720
6721 /* Total keys pre scanning */
6722 total_keys = getDbSize();
6723
6724 /* Status message */
6725 printf("\n# Scanning the entire keyspace to find hot keys as well as\n");
6726 printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n");
6727 printf("# per 100 SCAN commands (not usually needed).\n\n");
6728
6729 /* SCAN loop */
6730 do {
6731 /* Calculate approximate percentage completion */
6732 pct = 100 * (double)sampled/total_keys;
6733
6734 /* Grab some keys and point to the keys array */
6735 reply = sendScan(&it);
6736 keys = reply->element[1];
6737
6738 /* Reallocate our freqs array if we need to */
6739 if(keys->elements > arrsize) {
6740 freqs = zrealloc(freqs, sizeof(unsigned long long)*keys->elements, MALLOC_LOCAL);
6741
6742 if(!freqs) {
6743 fprintf(stderr, "Failed to allocate storage for keys!\n");
6744 exit(1);
6745 }
6746
6747 arrsize = keys->elements;
6748 }
6749
6750 getKeyFreqs(keys, freqs);
6751
6752 /* Now update our stats */
6753 for(i=0;i<keys->elements;i++) {
6754 sampled++;
6755 /* Update overall progress */
6756 if(sampled % 1000000 == 0) {
6757 printf("[%05.2f%%] Sampled %llu keys so far\n", pct, sampled);
6758 }
6759
6760 /* Use eviction pool here */
6761 k = 0;
6762 while (k < HOTKEYS_SAMPLE && freqs[i] > counters[k]) k++;
6763 if (k == 0) continue;
6764 k--;
6765 if (k == 0 || counters[k] == 0) {
6766 sdsfree(hotkeys[k]);
6767 } else {
6768 sdsfree(hotkeys[0]);
6769 memmove(counters,counters+1,sizeof(counters[0])*k);
6770 memmove(hotkeys,hotkeys+1,sizeof(hotkeys[0])*k);

Callers 1

mainFunction · 0.85

Calls 9

getDbSizeFunction · 0.85
sendScanFunction · 0.85
zreallocFunction · 0.85
getKeyFreqsFunction · 0.85
sdsfreeFunction · 0.85
sdscatreprFunction · 0.85
sdsemptyFunction · 0.85
freeReplyObjectFunction · 0.85
zfreeFunction · 0.85

Tested by

no test coverage detected