naver / arcus-memcached

ARCUS memory cache server
https://github.com/naver/arcus
Apache License 2.0
70 stars 55 forks source link

INTERNAL: Refactor lqdetect show #780

Open cheesecrust opened 3 months ago

cheesecrust commented 3 months ago

πŸ”— Related Issue

⌨️ What I did

ing-eoking commented 3 months ago

기쑴의 μ½”λ“œλŠ” #ifdef, #endif 둜 λ‚¨κ²¨λ‘μ—ˆμŠ΅λ‹ˆλ‹€.

κΈ°μ‘΄ μ½”λ“œλ₯Ό λ‚¨κ²¨λ‘λŠ” μ΄μœ λŠ” λ­”κ°€μš”?

cheesecrust commented 3 months ago

λ”°λΌμ„œ ν•¨μˆ˜ 호좜 μ‹œ κ°€μ§ˆ 수 μžˆλŠ” 졜μž₯ 길이 λ§ŒνΌμ„ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήν•˜κ³  write_and_free λ₯Ό μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μƒλ‹Ήνžˆ 큰 λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ήν•˜κ³  ν•΄μ œν•˜κ²Œ λ˜λŠ”λ°, 생각해 λ³Έ λ‹€λ₯Έ κ΅¬ν˜„ λ°©μ•ˆμ΄ μžˆλ‚˜μš”?

방식을 λ°”κΎΈμ–΄μ„œ λ¨Όμ € 좜λ ₯ν•  λ¬Έμžμ—΄μ˜ 길이λ₯Ό 잰 λ‹€μŒμ— 그에 맞게 ν• λ‹Ήν•˜λŠ” λ°©μ‹μœΌλ‘œ κ΅¬ν˜„μ„ λ°”κΎΈμ—ˆμŠ΅λ‹ˆλ‹€.

cheesecrust commented 3 months ago
        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;
       nxt += snprintf(nxt, bytes - (nxt - result.value), "%s : %u\n", command_str[i], ldb.ntotal);
       if (ldb.ntotal != 0) {
           nxt += snprintf(nxt, ldb.offset + 1, "%s", ldb.data);
       }
   }
namsic commented 3 months ago

μ•„λž˜ ν˜•νƒœμ˜ κ΅¬ν˜„μ„ κ²€ν† ν•΄ μ£ΌκΈ° λ°”λžλ‹ˆλ‹€.

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;
}
cheesecrust commented 3 months ago

μ•„λž˜ ν˜•νƒœμ˜ κ΅¬ν˜„μ„ κ²€ν† ν•΄ μ£ΌκΈ° λ°”λžλ‹ˆλ‹€.

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;
}

μœ„λ₯Ό κ²€ν† ν•˜μ—¬ μ•„λž˜μ™€ 같이 λ³€κ²½ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

   if (result.value != NULL) {
       int offset = 0;
       for (int i = 0; i < LQ_CMD_NUM; i++) {
           struct lq_detect_buffer ldb = lqdetect.buffer[i];
           offset += snprintf(result.value + offset, bytes - offset, "%s : %u\n", command_str[i], ldb.ntotal);
           if (ldb.ntotal != 0) {
               offset += snprintf(result.value + offset, ldb.offset + 1, "%s", ldb.data);
           }
       }
       result.length = offset;
   }