| 7825 | |
| 7826 | #define HOTKEYS_SAMPLE 16 |
| 7827 | static void findHotKeys(void) { |
| 7828 | redisReply *keys, *reply; |
| 7829 | unsigned long long counters[HOTKEYS_SAMPLE] = {0}; |
| 7830 | sds hotkeys[HOTKEYS_SAMPLE] = {NULL}; |
| 7831 | unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0; |
| 7832 | unsigned int arrsize = 0, i, k; |
| 7833 | double pct; |
| 7834 | |
| 7835 | /* Total keys pre scanning */ |
| 7836 | total_keys = getDbSize(); |
| 7837 | |
| 7838 | /* Status message */ |
| 7839 | printf("\n# Scanning the entire keyspace to find hot keys as well as\n"); |
| 7840 | printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n"); |
| 7841 | printf("# per 100 SCAN commands (not usually needed).\n\n"); |
| 7842 | |
| 7843 | /* SCAN loop */ |
| 7844 | do { |
| 7845 | /* Calculate approximate percentage completion */ |
| 7846 | pct = 100 * (double)sampled/total_keys; |
| 7847 | |
| 7848 | /* Grab some keys and point to the keys array */ |
| 7849 | reply = sendScan(&it); |
| 7850 | keys = reply->element[1]; |
| 7851 | |
| 7852 | /* Reallocate our freqs array if we need to */ |
| 7853 | if(keys->elements > arrsize) { |
| 7854 | freqs = zrealloc(freqs, sizeof(unsigned long long)*keys->elements); |
| 7855 | |
| 7856 | if(!freqs) { |
| 7857 | fprintf(stderr, "Failed to allocate storage for keys!\n"); |
| 7858 | exit(1); |
| 7859 | } |
| 7860 | |
| 7861 | arrsize = keys->elements; |
| 7862 | } |
| 7863 | |
| 7864 | getKeyFreqs(keys, freqs); |
| 7865 | |
| 7866 | /* Now update our stats */ |
| 7867 | for(i=0;i<keys->elements;i++) { |
| 7868 | sampled++; |
| 7869 | /* Update overall progress */ |
| 7870 | if(sampled % 1000000 == 0) { |
| 7871 | printf("[%05.2f%%] Sampled %llu keys so far\n", pct, sampled); |
| 7872 | } |
| 7873 | |
| 7874 | /* Use eviction pool here */ |
| 7875 | k = 0; |
| 7876 | while (k < HOTKEYS_SAMPLE && freqs[i] > counters[k]) k++; |
| 7877 | if (k == 0) continue; |
| 7878 | k--; |
| 7879 | if (k == 0 || counters[k] == 0) { |
| 7880 | sdsfree(hotkeys[k]); |
| 7881 | } else { |
| 7882 | sdsfree(hotkeys[0]); |
| 7883 | memmove(counters,counters+1,sizeof(counters[0])*k); |
| 7884 | memmove(hotkeys,hotkeys+1,sizeof(hotkeys[0])*k); |
no test coverage detected