-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
INTERNAL: Refactor lqdetect show #780
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ°λΌμ ν¨μ νΈμΆ μ κ°μ§ μ μλ μ΅μ₯ κΈΈμ΄ λ§νΌμ λ©λͺ¨λ¦¬μ ν λΉνκ³
write_and_free
λ₯Ό μ¬μ©νμμ΅λλ€.
μλΉν ν° λ©λͺ¨λ¦¬λ₯Ό ν λΉνκ³ ν΄μ νκ² λλλ°, μκ°ν΄ λ³Έ λ€λ₯Έ ꡬν λ°©μμ΄ μλμ?
λ°©μμ λ°κΎΈμ΄μ λ¨Όμ μΆλ ₯ν λ¬Έμμ΄μ κΈΈμ΄λ₯Ό μ° λ€μμ κ·Έμ λ§κ² ν λΉνλ λ°©μμΌλ‘ ꡬνμ λ°κΎΈμμ΅λλ€. |
0c1130a
to
db6aee3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μλμ κ°μ λ°©μμ μ΄λ€κ°μ?
field_t lqdetect_result_get(void)
{
int hdrlen = 32;
field_t result = {NULL, 0};
pthread_mutex_lock(&lqdetect.lock);
int bytes = hdrlen * LQ_CMD_NUM;
for (int i = 0; i < LQ_CMD_NUM; i++) {
bytes += lqdetect.buffer[i].offset;
}
if ((result.value = (char*)malloc(bytes)) != NULL) {
char *nxt = result.value;
for (int i = 0; i < LQ_CMD_NUM; i++) {
struct lq_detect_buffer ldb = lqdetect.buffer[i];
nxt += snprintf(nxt, bytes, "%s : %u\n", command_str[i], ldb.ntotal);
bytes -= (nxt - result.value);
if (ldb.ntotal != 0) {
int length = bytes < ldb.offset ? bytes : ldb.offset;
nxt += snprintf(nxt, length, "%s", ldb.data);
bytes -= length;
}
}
result.length = nxt - result.value;
}
pthread_mutex_unlock(&lqdetect.lock);
return result;
}
|
μλ ννμ ꡬνμ κ²ν ν΄ μ£ΌκΈ° λ°λλλ€. int main(int argc, char **argv) {
int buflen = 200;
char *buf = (char*)malloc(buflen);
if (!buf) {
return 1;
}
int offset = 0;
int i;
for (i=0; i<20; i++) {
offset += snprintf(buf + offset, buflen - offset, "%d,", i);
//if (offset >= buflen) {
// break;
//}
}
printf("%s\n", buf);
free(buf);
return 0;
} |
μλ₯Ό κ²ν νμ¬ μλμ κ°μ΄ λ³κ²½νμμ΅λλ€.
|
d221555
to
c7f59dc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 μλ£
lqdetect.c
Outdated
if (str != NULL) { | ||
int offset = 0; | ||
for (int i = 0; i < LQ_CMD_NUM; i++) { | ||
struct lq_detect_buffer ldb = lqdetect.buffer[i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μμ μ½λλ struct lq_detect_buffer μ체λ₯Ό copyνλ κ²μ΄λ―λ‘,
μλμ κ°μ΄ pointer μ¬μ©ν©μλ€.
struct lq_detect_buffer *ldb = &lqdetect.buffer[i];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ°μνμμ΅λλ€.
lqdetect.c
Outdated
} | ||
*size = offset; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ½λ©νΈμ λλ€.
- hdrlen λ³μλ₯Ό λ°λ‘ λ νμκ° μμ κ² κ°κ³ ,
- bytes λμ μ length μ©μ΄κ° λμ κ² κ°κ³ ,
- size μΆλ ₯ μΈμλ νμ κ°μ΄ μ€μ λλ©΄ μ’κ² μ.
κ±°μ λμΌν μ½λμ΄μ§λ§, μλ μ½λμ΄λ©΄ μ’κ² μ΅λλ€.
char *lqdetect_result_get(int *size)
{
int offset = 0;
int length = 32 * LQ_CMD_NUM; // header length
char *str;
pthread_mutex_lock(&lqdetect.lock);
for (int i = 0; i < LQ_CMD_NUM; i++) {
length += lqdetect.buffer[i].offset;
}
str = (char*)malloc(length);
if (str != NULL) {
for (int i = 0; i < LQ_CMD_NUM; i++) {
struct lq_detect_buffer *ldb = &lqdetect.buffer[i];
offset += snprintf(str + offset, length - offset, "%s : %u\n", command_str[i], ldb->ntotal);
if (ldb->ntotal > 0) {
offset += snprintf(str + offset, length - offset, "%s", ldb->data);
}
}
}
pthread_mutex_unlock(&lqdetect.lock);
*size = offset;
return str;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@namsic
μ°Έκ³ μ¬νμΌλ‘,
μλ buffer ꡬ쑰μμ keypos, keylen νλκ° μλ€λ©΄,
νλμ buffer ꡬ쑰 ν¬κΈ°λ 24 λ°μ΄νΈμ΄λ©΄ μΆ©λΆνλ€.
struct lq_detect_buffer {
char *data;
uint32_t offset;
uint32_t ntotal;
uint32_t nsaved;
uint32_t keypos[LQ_SAVE_CNT];
uint32_t keylen[LQ_SAVE_CNT];
};
buffer ν¬κΈ°κ° μλ€λ©΄(or νμν νλλ§ λ³΅μ¬νλ€λ©΄),
lqdect locking μνμμ
- LQ_CMD_NUM(10) κ°μ bufferλ₯Ό κ·Έλλ‘ copyν΄ λκ³ ,
- refcount μ¦κ°μμΌ λ λ€μ
without locking μνμμ
- 볡μ¬ν΄ λ buffer μ 보λ₯Ό κΈ°λ°μΌλ‘ str λ³μλ₯Ό λΉλν ν
λ€μ lqdect locking μνμμ recount κ°μμν¨ νμ
str λ³μλ₯Ό 리ν΄ν μ μλ€.
μ΄λ κ² μ΅μ νν νμλ μκ² μ£ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lockμ μ‘κ³ μλ μκ°μ μ΅μννκΈ° μν κ΄μ μμμ μ΅μ νμΈκ°μ?
μ μκ°μ str λ³μλ₯Ό λΉλνλ κ²μ΄ νΉλ³ν μκ°μ λ§μ΄ νμλ‘ νλ λμμ μλ κ² κ°μμ,
ꡬνμ λ¨μνκ² κ°μ Έκ°λ νΈμ΄ λμ κ²μΌλ‘ μκ°ν©λλ€.
11c25ad
to
7e9bde5
Compare
refactor lqdetect show filed_t to char *
π Related Issue
β¨οΈ What I did
<value, length>
ꡬ쑰μ λ°°μ΄μ 리ν΄νλ λ‘μ§μμ κ°λ¨νκ²filed_t
νλλ§μ λ°ννλ λ‘μ§μΌλ‘ λ³κ²½νμμ΅λλ€.total_offset
μ μ μΈνμ¬ buffer μ offset μ΄ μ¦κ°ν λ κ°μ΄ μ¦κ°νμ¬ μ΄λ₯Ό νμ©νμ΅λλ€.SERVER ERROR out of memory writing show response.
λ₯Ό μΆλ ₯νλλ‘ νμμ΅λλ€.