Skip to content

Commit e414a5a

Browse files
committed
poc commit
1 parent 25ef0b3 commit e414a5a

File tree

7 files changed

+540
-11
lines changed

7 files changed

+540
-11
lines changed

src/server.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,61 @@ void trackInstantaneousMetric(int metric, long long current_value, long long cur
889889
server.inst_metric[metric].last_sample_value = current_value;
890890
}
891891

892+
void displayUpdate(int pre_value, int current_value) {
893+
serverLog(LL_WARNING, "This is for testing, previous item number is %d, and current item number is %d", pre_value, current_value);
894+
}
895+
896+
void displayDataTypeArray(keysizeInfo *keysize_array, int length) {
897+
serverLog(LL_WARNING, "Current array length is %d", length);
898+
for (int i = 0; i < length; i++) {
899+
serverLog(LL_WARNING, "Item %ld and value is %ld", keysize_array[i].element_size, keysize_array[i].num);
900+
}
901+
}
902+
903+
void decreaseDataTypeArrayPreviousValue(keysizeInfo *keysize_array, int low, int high, int value) {
904+
if (keysize_array[low].element_size == value) {
905+
keysize_array[low].num--;
906+
} else if (keysize_array[high].element_size == value) {
907+
keysize_array[high].num--;
908+
} else {
909+
while (low + 1 < high) {
910+
int mid = low + (high - low) / 2;
911+
if (value > keysize_array[mid].element_size) {
912+
low = mid;
913+
} else {
914+
high = mid;
915+
}
916+
}
917+
if (value == keysize_array[high].element_size) {
918+
keysize_array[high].num--;
919+
} else {
920+
keysize_array[low].num--;
921+
}
922+
}
923+
}
924+
925+
void increaseDataTypeArrayCurrentValue(keysizeInfo *keysize_array, int low, int high, int value) {
926+
if (keysize_array[low].element_size == value) {
927+
keysize_array[low].num++;
928+
} else if (keysize_array[high].element_size == value) {
929+
keysize_array[high].num++;
930+
} else {
931+
while (low + 1 < high) {
932+
int mid = low + (high - low) / 2;
933+
if (value > keysize_array[mid].element_size) {
934+
low = mid;
935+
} else {
936+
high = mid;
937+
}
938+
}
939+
if (value == keysize_array[high].element_size) {
940+
keysize_array[high].num++;
941+
} else {
942+
keysize_array[low].num++;
943+
}
944+
}
945+
}
946+
892947
/* Return the mean of all the samples. */
893948
long long getInstantaneousMetric(int metric) {
894949
int j;
@@ -2740,6 +2795,7 @@ void makeThreadKillable(void) {
27402795
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
27412796
}
27422797

2798+
#define INIT_ARRAY_SIZE 5
27432799
void initServer(void) {
27442800
int j;
27452801

@@ -2832,6 +2888,35 @@ void initServer(void) {
28322888
server.db[j].watched_keys = dictCreate(&keylistDictType);
28332889
server.db[j].id = j;
28342890
server.db[j].avg_ttl = 0;
2891+
server.db[j].lists_array_length = INIT_ARRAY_SIZE;
2892+
server.db[j].lists_number_of_elements = 0;
2893+
server.db[j].lists_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
2894+
server.db[j].sets_array_length = INIT_ARRAY_SIZE;
2895+
server.db[j].sets_number_of_elements = 0;
2896+
server.db[j].sets_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
2897+
server.db[j].hashes_array_length = INIT_ARRAY_SIZE;
2898+
server.db[j].hashes_number_of_elements = 0;
2899+
server.db[j].hashes_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
2900+
server.db[j].zsets_array_length = INIT_ARRAY_SIZE;
2901+
server.db[j].zsets_number_of_elements = 0;
2902+
server.db[j].zsets_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
2903+
server.db[j].strings_array_length = INIT_ARRAY_SIZE;
2904+
server.db[j].strings_number_of_elements = 0;
2905+
server.db[j].strings_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
2906+
int i = 1;
2907+
for (int count = 0; count < INIT_ARRAY_SIZE; count++) {
2908+
server.db[j].lists_array[count].element_size = i;
2909+
server.db[j].lists_array[count].num = 0;
2910+
server.db[j].sets_array[count].element_size = i;
2911+
server.db[j].sets_array[count].num = 0;
2912+
server.db[j].hashes_array[count].element_size = i;
2913+
server.db[j].hashes_array[count].num = 0;
2914+
server.db[j].zsets_array[count].element_size = i;
2915+
server.db[j].zsets_array[count].num = 0;
2916+
server.db[j].strings_array[count].element_size = i;
2917+
server.db[j].strings_array[count].num = 0;
2918+
i *= 2;
2919+
}
28352920
}
28362921
evictionPoolAlloc(); /* Initialize the LRU keys pool. */
28372922
/* Note that server.pubsub_channels was chosen to be a kvstore (with only one dict, which
@@ -5566,6 +5651,7 @@ dict *genInfoSectionDict(robj **argv, int argc, char **defaults, int *out_all, i
55665651
"errorstats",
55675652
"cluster",
55685653
"keyspace",
5654+
"keysizes",
55695655
NULL,
55705656
};
55715657
if (!defaults) defaults = default_sections;
@@ -6220,6 +6306,41 @@ sds genValkeyInfoString(dict *section_dict, int all_sections, int everything) {
62206306
}
62216307
}
62226308

6309+
/* Key size distribution*/
6310+
if (all_sections || (dictFind(section_dict, "keysizes") != NULL)) {
6311+
if (sections++) info = sdscat(info, "\r\n");
6312+
info = sdscatprintf(info, "# Keysizes\r\n");
6313+
for (j = 0; j < server.dbnum; j++) {
6314+
if (server.db[j].lists_number_of_elements != 0) {
6315+
info = sdscatprintf(info, "db%d_distrib_strings_sizes:", j);
6316+
for (int l = 0; l < server.db[j].strings_array_length; l++) {
6317+
info = sdscatprintf(info, "%ld=%ld,", server.db[j].strings_array[l].element_size, server.db[j].strings_array[l].num);
6318+
}
6319+
info = sdscatprintf(info, "\r\n");
6320+
info = sdscatprintf(info, "db%d_distrib_lists_items:", j);
6321+
for (int l = 0; l < server.db[j].lists_array_length; l++) {
6322+
info = sdscatprintf(info, "%ld=%ld,", server.db[j].lists_array[l].element_size, server.db[j].lists_array[l].num);
6323+
}
6324+
info = sdscatprintf(info, "\r\n");
6325+
info = sdscatprintf(info, "db%d_distrib_sets_items:", j);
6326+
for (int l = 0; l < server.db[j].sets_array_length; l++) {
6327+
info = sdscatprintf(info, "%ld=%ld,", server.db[j].sets_array[l].element_size, server.db[j].sets_array[l].num);
6328+
}
6329+
info = sdscatprintf(info, "\r\n");
6330+
info = sdscatprintf(info, "db%d_distrib_hashes_items:", j);
6331+
for (int l = 0; l < server.db[j].hashes_array_length; l++) {
6332+
info = sdscatprintf(info, "%ld=%ld,", server.db[j].hashes_array[l].element_size, server.db[j].hashes_array[l].num);
6333+
}
6334+
info = sdscatprintf(info, "\r\n");
6335+
info = sdscatprintf(info, "db%d_distrib_zsets_items:", j);
6336+
for (int l = 0; l < server.db[j].zsets_array_length; l++) {
6337+
info = sdscatprintf(info, "%ld=%ld,", server.db[j].zsets_array[l].element_size, server.db[j].zsets_array[l].num);
6338+
}
6339+
info = sdscatprintf(info, "\r\n");
6340+
}
6341+
}
6342+
}
6343+
62236344
/* Get info from modules.
62246345
* Returned when the user asked for "everything", "modules", or a specific module section.
62256346
* We're not aware of the module section names here, and we rather avoid the search when we can.

src/server.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ typedef struct replBufBlock {
819819
char buf[];
820820
} replBufBlock;
821821

822+
typedef struct keysizeInfo {
823+
long element_size;
824+
long num;
825+
} keysizeInfo;
826+
822827
/* Database representation. There are multiple databases identified
823828
* by integers from 0 (the default database) up to the max configured
824829
* database. The database number is the 'id' field in the structure. */
@@ -834,6 +839,21 @@ typedef struct serverDb {
834839
int id; /* Database ID */
835840
long long avg_ttl; /* Average TTL, just for stats */
836841
unsigned long expires_cursor; /* Cursor of the active expire cycle. */
842+
keysizeInfo *lists_array;
843+
int lists_array_length;
844+
unsigned long long lists_number_of_elements;
845+
keysizeInfo *sets_array;
846+
int sets_array_length;
847+
unsigned long long sets_number_of_elements;
848+
keysizeInfo *hashes_array;
849+
int hashes_array_length;
850+
unsigned long long hashes_number_of_elements;
851+
keysizeInfo *zsets_array;
852+
int zsets_array_length;
853+
unsigned long long zsets_number_of_elements;
854+
keysizeInfo *strings_array;
855+
int strings_array_length;
856+
unsigned long long strings_number_of_elements;
837857
} serverDb;
838858

839859
/* forward declaration for functions ctx */
@@ -3227,6 +3247,10 @@ void *activeDefragAlloc(void *ptr);
32273247
robj *activeDefragStringOb(robj *ob);
32283248
void dismissSds(sds s);
32293249
void dismissMemoryInChild(void);
3250+
void displayUpdate(int pre_value, int current_value);
3251+
void displayDataTypeArray(keysizeInfo *keysize_array, int length);
3252+
void decreaseDataTypeArrayPreviousValue(keysizeInfo *keysize_array, int low, int high, int value);
3253+
void increaseDataTypeArrayCurrentValue(keysizeInfo *keysize_array, int low, int high, int value);
32303254

32313255
#define RESTART_SERVER_NONE 0
32323256
#define RESTART_SERVER_GRACEFULLY (1 << 0) /* Do proper shutdown. */

0 commit comments

Comments
 (0)