Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,61 @@ void trackInstantaneousMetric(int metric, long long current_value, long long cur
server.inst_metric[metric].last_sample_value = current_value;
}

void displayUpdate(int pre_value, int current_value) {
serverLog(LL_WARNING, "This is for testing, previous item number is %d, and current item number is %d", pre_value, current_value);
}

void displayDataTypeArray(keysizeInfo *keysize_array, int length) {
serverLog(LL_WARNING, "Current array length is %d", length);
for (int i = 0; i < length; i++) {
serverLog(LL_WARNING, "Item %ld and value is %ld", keysize_array[i].element_size, keysize_array[i].num);
}
}

void decreaseDataTypeArrayPreviousValue(keysizeInfo *keysize_array, int low, int high, int value) {
if (keysize_array[low].element_size == value) {
keysize_array[low].num--;
} else if (keysize_array[high].element_size == value) {
keysize_array[high].num--;
} else {
while (low + 1 < high) {
int mid = low + (high - low) / 2;
if (value > keysize_array[mid].element_size) {
low = mid;
} else {
high = mid;
}
}
if (value == keysize_array[high].element_size) {
keysize_array[high].num--;
} else {
keysize_array[low].num--;
}
}
}

void increaseDataTypeArrayCurrentValue(keysizeInfo *keysize_array, int low, int high, int value) {
if (keysize_array[low].element_size == value) {
keysize_array[low].num++;
} else if (keysize_array[high].element_size == value) {
keysize_array[high].num++;
} else {
while (low + 1 < high) {
int mid = low + (high - low) / 2;
if (value > keysize_array[mid].element_size) {
low = mid;
} else {
high = mid;
}
}
if (value == keysize_array[high].element_size) {
keysize_array[high].num++;
} else {
keysize_array[low].num++;
}
}
}

/* Return the mean of all the samples. */
long long getInstantaneousMetric(int metric) {
int j;
Expand Down Expand Up @@ -2731,6 +2786,7 @@ void makeThreadKillable(void) {
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
}

#define INIT_ARRAY_SIZE 5
void initServer(void) {
int j;

Expand Down Expand Up @@ -2822,6 +2878,35 @@ void initServer(void) {
server.db[j].watched_keys = dictCreate(&keylistDictType);
server.db[j].id = j;
server.db[j].avg_ttl = 0;
server.db[j].lists_array_length = INIT_ARRAY_SIZE;
server.db[j].lists_number_of_elements = 0;
server.db[j].lists_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
server.db[j].sets_array_length = INIT_ARRAY_SIZE;
server.db[j].sets_number_of_elements = 0;
server.db[j].sets_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
server.db[j].hashes_array_length = INIT_ARRAY_SIZE;
server.db[j].hashes_number_of_elements = 0;
server.db[j].hashes_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
server.db[j].zsets_array_length = INIT_ARRAY_SIZE;
server.db[j].zsets_number_of_elements = 0;
server.db[j].zsets_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
server.db[j].strings_array_length = INIT_ARRAY_SIZE;
server.db[j].strings_number_of_elements = 0;
server.db[j].strings_array = zmalloc(sizeof(keysizeInfo) * INIT_ARRAY_SIZE);
int i = 1;
for (int count = 0; count < INIT_ARRAY_SIZE; count++) {
server.db[j].lists_array[count].element_size = i;
server.db[j].lists_array[count].num = 0;
server.db[j].sets_array[count].element_size = i;
server.db[j].sets_array[count].num = 0;
server.db[j].hashes_array[count].element_size = i;
server.db[j].hashes_array[count].num = 0;
server.db[j].zsets_array[count].element_size = i;
server.db[j].zsets_array[count].num = 0;
server.db[j].strings_array[count].element_size = i;
server.db[j].strings_array[count].num = 0;
i *= 2;
}
}
evictionPoolAlloc(); /* Initialize the LRU keys pool. */
/* Note that server.pubsub_channels was chosen to be a kvstore (with only one dict, which
Expand Down Expand Up @@ -5553,6 +5638,7 @@ dict *genInfoSectionDict(robj **argv, int argc, char **defaults, int *out_all, i
"errorstats",
"cluster",
"keyspace",
"keysizes",
NULL,
};
if (!defaults) defaults = default_sections;
Expand Down Expand Up @@ -6207,6 +6293,41 @@ sds genValkeyInfoString(dict *section_dict, int all_sections, int everything) {
}
}

/* Key size distribution*/
if (all_sections || (dictFind(section_dict, "keysizes") != NULL)) {
if (sections++) info = sdscat(info, "\r\n");
info = sdscatprintf(info, "# Keysizes\r\n");
for (j = 0; j < server.dbnum; j++) {
if (server.db[j].lists_number_of_elements != 0) {
info = sdscatprintf(info, "db%d_distrib_strings_sizes:", j);
for (int l = 0; l < server.db[j].strings_array_length; l++) {
info = sdscatprintf(info, "%ld=%ld,", server.db[j].strings_array[l].element_size, server.db[j].strings_array[l].num);
}
info = sdscatprintf(info, "\r\n");
info = sdscatprintf(info, "db%d_distrib_lists_items:", j);
for (int l = 0; l < server.db[j].lists_array_length; l++) {
info = sdscatprintf(info, "%ld=%ld,", server.db[j].lists_array[l].element_size, server.db[j].lists_array[l].num);
}
info = sdscatprintf(info, "\r\n");
info = sdscatprintf(info, "db%d_distrib_sets_items:", j);
for (int l = 0; l < server.db[j].sets_array_length; l++) {
info = sdscatprintf(info, "%ld=%ld,", server.db[j].sets_array[l].element_size, server.db[j].sets_array[l].num);
}
info = sdscatprintf(info, "\r\n");
info = sdscatprintf(info, "db%d_distrib_hashes_items:", j);
for (int l = 0; l < server.db[j].hashes_array_length; l++) {
info = sdscatprintf(info, "%ld=%ld,", server.db[j].hashes_array[l].element_size, server.db[j].hashes_array[l].num);
}
info = sdscatprintf(info, "\r\n");
info = sdscatprintf(info, "db%d_distrib_zsets_items:", j);
for (int l = 0; l < server.db[j].zsets_array_length; l++) {
info = sdscatprintf(info, "%ld=%ld,", server.db[j].zsets_array[l].element_size, server.db[j].zsets_array[l].num);
}
info = sdscatprintf(info, "\r\n");
}
}
}

/* Get info from modules.
* Returned when the user asked for "everything", "modules", or a specific module section.
* We're not aware of the module section names here, and we rather avoid the search when we can.
Expand Down
25 changes: 25 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,11 @@ typedef struct replBufBlock {
char buf[];
} replBufBlock;

typedef struct keysizeInfo {
long element_size;
long num;
} keysizeInfo;

/* Database representation. There are multiple databases identified
* by integers from 0 (the default database) up to the max configured
* database. The database number is the 'id' field in the structure. */
Expand All @@ -833,6 +838,21 @@ typedef struct serverDb {
int id; /* Database ID */
long long avg_ttl; /* Average TTL, just for stats */
unsigned long expires_cursor; /* Cursor of the active expire cycle. */
keysizeInfo *lists_array;
int lists_array_length;
unsigned long long lists_number_of_elements;
keysizeInfo *sets_array;
int sets_array_length;
unsigned long long sets_number_of_elements;
keysizeInfo *hashes_array;
int hashes_array_length;
unsigned long long hashes_number_of_elements;
keysizeInfo *zsets_array;
int zsets_array_length;
unsigned long long zsets_number_of_elements;
keysizeInfo *strings_array;
int strings_array_length;
unsigned long long strings_number_of_elements;
} serverDb;

/* forward declaration for functions ctx */
Expand Down Expand Up @@ -3212,6 +3232,10 @@ void *activeDefragAlloc(void *ptr);
robj *activeDefragStringOb(robj *ob);
void dismissSds(sds s);
void dismissMemoryInChild(void);
void displayUpdate(int pre_value, int current_value);
void displayDataTypeArray(keysizeInfo *keysize_array, int length);
void decreaseDataTypeArrayPreviousValue(keysizeInfo *keysize_array, int low, int high, int value);
void increaseDataTypeArrayCurrentValue(keysizeInfo *keysize_array, int low, int high, int value);

#define RESTART_SERVER_NONE 0
#define RESTART_SERVER_GRACEFULLY (1 << 0) /* Do proper shutdown. */
Expand Down Expand Up @@ -3518,6 +3542,7 @@ unsigned long evalScriptsMemory(void);
uint64_t evalGetCommandFlags(client *c, uint64_t orig_flags);
uint64_t fcallGetCommandFlags(client *c, uint64_t orig_flags);
int isInsideYieldingLongCommand(void);
void displayUpdate(int pre_value, int current_value);

/* Cache of recently used small arguments to avoid malloc calls. */
#define LUA_CMD_OBJCACHE_SIZE 32
Expand Down
Loading
Loading