Skip to content

Commit

Permalink
INTERNAL: Remove keypos, keylen field from struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesecrust committed Nov 1, 2024
1 parent 3624d83 commit 67170ce
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions lqdetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#define LQ_THRESHOLD_DEFAULT 4000
#define LQ_QUERY_SIZE (64*2+64) /* bop get (longest query) : "<longest bkey>..<longest bkey> efilter <offset> <count> delete" */
#define LQ_KEY_SIZE 251 /* the max size of key string */
#define LQ_SAVE_CNT 20 /* save key count */
#define LQ_INPUT_SIZE 500 /* the size of input(time, ip, command, argument) */
#define LQ_STAT_STRLEN 300 /* max length of stats */
Expand Down Expand Up @@ -56,6 +57,7 @@ struct lq_detect_stats {
/* lqdetect argument structure */
struct lq_detect_argument {
char query[LQ_QUERY_SIZE];
char key[LQ_KEY_SIZE];
uint32_t count;
uint32_t overhead;
};
Expand All @@ -66,8 +68,6 @@ struct lq_detect_buffer {
uint32_t offset;
uint32_t ntotal;
uint32_t nsaved;
uint32_t keypos[LQ_SAVE_CNT];
uint32_t keylen[LQ_SAVE_CNT];
};

/* lqdetect global structure */
Expand All @@ -80,10 +80,9 @@ struct lq_detect_global {
};
struct lq_detect_global lqdetect;

static bool is_command_duplicated(char *key, int keylen, enum lq_detect_command cmd, struct lq_detect_argument *arg)
static bool is_command_duplicated(enum lq_detect_command cmd, struct lq_detect_argument *arg)
{
int count = lqdetect.buffer[cmd].nsaved;
struct lq_detect_buffer *buf = &lqdetect.buffer[cmd];

switch (cmd) {
case LQCMD_LOP_INSERT:
Expand All @@ -105,8 +104,7 @@ static bool is_command_duplicated(char *key, int keylen, enum lq_detect_command
for (int ii = 0; ii < count; ii++) {
if (strcmp(lqdetect.arg[cmd][ii].query, arg->query) == 0) {
if (arg->count > 0) return true;
if (buf->keylen[ii] == keylen &&
memcmp(buf->data + buf->keypos[ii], key, keylen) == 0) {
if (strcmp(lqdetect.arg[cmd][ii].key, arg->key) == 0) {
return true;
}
}
Expand All @@ -116,41 +114,30 @@ static bool is_command_duplicated(char *key, int keylen, enum lq_detect_command
return false;
}

static void do_lqdetect_write(char *client_ip, char *key,
enum lq_detect_command cmd, struct lq_detect_argument *arg)
static void do_lqdetect_write(char *client_ip, enum lq_detect_command cmd,
struct lq_detect_argument *arg)
{
struct tm *ptm;
struct timeval val;
struct lq_detect_buffer *buffer = &lqdetect.buffer[cmd];
uint32_t nsaved = buffer->nsaved;
uint32_t length, keylen = strlen(key);
char keybuf[251];
char *keyptr = key;
uint32_t length = ((nsaved+1) * LQ_INPUT_SIZE);

if (keylen > 250) { /* long key string */
keylen = snprintf(keybuf, sizeof(keybuf), "%.*s...%.*s",
124, key, 123, (key + keylen - 123));
keyptr = keybuf;
}

if (is_command_duplicated(keyptr, keylen, cmd, arg) == true) {
if (is_command_duplicated(cmd, arg) == true) {
return;
}

gettimeofday(&val, NULL);
ptm = localtime(&val.tv_sec);

length = ((nsaved+1) * LQ_INPUT_SIZE);
snprintf(buffer->data + buffer->offset, length - buffer->offset,
"%02d:%02d:%02d.%06ld %s <%u> %s ",
ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (long)val.tv_usec,
client_ip, arg->overhead, command_str[cmd]);
buffer->offset += strlen(buffer->data + buffer->offset);
buffer->keypos[nsaved] = buffer->offset;
buffer->keylen[nsaved] = keylen;

snprintf(buffer->data + buffer->offset, length - buffer->offset,
"%s %s\n", keyptr, arg->query);
"%s %s\n", arg->key, arg->query);
buffer->offset += strlen(buffer->data + buffer->offset);
lqdetect.arg[cmd][nsaved] = *arg;
buffer->nsaved += 1;
Expand All @@ -165,16 +152,16 @@ static void do_lqdetect_stop(int cause)
lqdetect_in_use = false;
}

static void do_lqdetect_save_cmd(char *client_ip, char* key,
enum lq_detect_command cmd, struct lq_detect_argument *arg)
static void do_lqdetect_save_cmd(char *client_ip, enum lq_detect_command cmd,
struct lq_detect_argument *arg)
{
assert(cmd >= LQCMD_SOP_GET && cmd <= LQCMD_BOP_GBP);
pthread_mutex_lock(&lqdetect.lock);
if (lqdetect_in_use) {
lqdetect.buffer[cmd].ntotal++;
if (lqdetect.buffer[cmd].nsaved < LQ_SAVE_CNT) {
/* write to buffer */
do_lqdetect_write(client_ip, key, cmd, arg);
do_lqdetect_write(client_ip, cmd, arg);
/* internal stop */
if (lqdetect.buffer[cmd].nsaved >= LQ_SAVE_CNT) {
lqdetect.overflow_cnt++;
Expand Down Expand Up @@ -215,6 +202,18 @@ static int do_make_bkeystring(char *buffer, const bkey_range *bkrange, const efl
return (bufptr - buffer);
}

static void do_make_key(char *key, char *buffer)
{
uint32_t keylen = strlen(key);

if (keylen >= LQ_KEY_SIZE) { /* long key string */
snprintf(buffer, LQ_KEY_SIZE, "%.*s...%.*s",
124, key, 123, (key + keylen - 123));
} else {
snprintf(buffer, LQ_KEY_SIZE, "%s", key);
}
}

/* external functions */
int lqdetect_init(EXTENSION_LOGGER_DESCRIPTOR *logger)
{
Expand Down Expand Up @@ -354,8 +353,9 @@ void lqdetect_lop_insert(char *client_ip, char *key, int coll_index)
if (overhead >= lqdetect.stats.threshold) {
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%d", coll_index);
do_make_key(key, argument.key);
argument.overhead = overhead;
do_lqdetect_save_cmd(client_ip, key, LQCMD_LOP_INSERT, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_LOP_INSERT, &argument);
}
}

Expand All @@ -367,8 +367,9 @@ void lqdetect_lop_delete(char *client_ip, char *key, uint32_t del_count,
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%d..%d%s", from_index, to_index,
drop_if_empty ? " drop" : "");
do_make_key(key, argument.key);
argument.overhead = overhead;
do_lqdetect_save_cmd(client_ip, key, LQCMD_LOP_DELETE, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_LOP_DELETE, &argument);
}
}

Expand All @@ -380,8 +381,9 @@ void lqdetect_lop_get(char *client_ip, char *key, uint32_t elem_count,
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%d..%d%s", from_index, to_index,
drop_if_empty ? " drop" : (delete ? " delete" : ""));
do_make_key(key, argument.key);
argument.overhead = overhead;
do_lqdetect_save_cmd(client_ip, key, LQCMD_LOP_GET, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_LOP_GET, &argument);
}
}

Expand All @@ -392,9 +394,10 @@ void lqdetect_sop_get(char *client_ip, char *key, uint32_t elem_count,
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%u%s", count,
drop_if_empty ? " drop" : (delete ? " delete" : ""));
do_make_key(key, argument.key);
argument.overhead = elem_count;
argument.count = count;
do_lqdetect_save_cmd(client_ip, key, LQCMD_SOP_GET, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_SOP_GET, &argument);
}
}

Expand All @@ -405,9 +408,10 @@ void lqdetect_mop_get(char *client_ip, char *key, uint32_t elem_count,
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%u%s", coll_numkeys,
drop_if_empty ? " drop" : (delete ? " delete" : ""));
do_make_key(key, argument.key);
argument.overhead = elem_count;
argument.count = coll_numkeys;
do_lqdetect_save_cmd(client_ip, key, LQCMD_MOP_GET, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_MOP_GET, &argument);
}
}

Expand All @@ -418,9 +422,10 @@ void lqdetect_mop_delete(char *client_ip, char *key, uint32_t del_count,
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%u%s", coll_numkeys,
drop_if_empty ? " drop" : "");
do_make_key(key, argument.key);
argument.overhead = del_count;
argument.count = coll_numkeys;
do_lqdetect_save_cmd(client_ip, key, LQCMD_MOP_DELETE, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_MOP_DELETE, &argument);
}
}

Expand All @@ -431,8 +436,9 @@ void lqdetect_bop_gbp(char *client_ip, char *key, uint32_t elem_count,
struct lq_detect_argument argument;
snprintf(argument.query, LQ_QUERY_SIZE, "%u..%u %s", from_posi, to_posi,
order == BTREE_ORDER_ASC ? "asc" : "desc");
do_make_key(key, argument.key);
argument.overhead = elem_count;
do_lqdetect_save_cmd(client_ip, key, LQCMD_BOP_GBP, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_BOP_GBP, &argument);
}
}

Expand All @@ -445,8 +451,9 @@ void lqdetect_bop_get(char *client_ip, char *key, uint32_t access_count,
int nwrite = do_make_bkeystring(argument.query, bkrange, efilter);
snprintf(argument.query + nwrite, LQ_QUERY_SIZE - nwrite, " %u %u%s",
offset, count, drop_if_empty ? " drop" : (delete ? " delete" : ""));
do_make_key(key, argument.key);
argument.overhead = access_count;
do_lqdetect_save_cmd(client_ip, key, LQCMD_BOP_GET, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_BOP_GET, &argument);
}
}

Expand All @@ -456,8 +463,9 @@ void lqdetect_bop_count(char *client_ip, char *key, uint32_t access_count,
if (access_count >= lqdetect.stats.threshold) {
struct lq_detect_argument argument;
do_make_bkeystring(argument.query, bkrange, efilter);
do_make_key(key, argument.key);
argument.overhead = access_count;
do_lqdetect_save_cmd(client_ip, key, LQCMD_BOP_COUNT, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_BOP_COUNT, &argument);
}
}

Expand All @@ -470,8 +478,9 @@ void lqdetect_bop_delete(char *client_ip, char *key, uint32_t access_count,
int nwrite = do_make_bkeystring(argument.query, bkrange, efilter);
snprintf(argument.query + nwrite, LQ_QUERY_SIZE - nwrite, " %u%s",
count, drop_if_empty ? " drop" : "");
do_make_key(key, argument.key);
argument.count = count;
argument.overhead = access_count;
do_lqdetect_save_cmd(client_ip, key, LQCMD_BOP_DELETE, &argument);
do_lqdetect_save_cmd(client_ip, LQCMD_BOP_DELETE, &argument);
}
}

0 comments on commit 67170ce

Please sign in to comment.