Repository: valkey-io/valkey
File: src/valkey-check-rdb.c (C)

Code before:
        snprintf(value, value_len, "%.2lf", (stats->elements > 0 ? (float)stats->all_elements_size / (float)stats->elements : 0));
    } else if (!strcasecmp(field_string, "elements.size.p99")) {
        snprintf(value, value_len, "%.2lf", (float)hdr_value_at_percentile(stats->element_size_histogram, 99.0));
    } else if (!strcasecmp(field_string, "elements.size.p90")) {
        snprintf(value, value_len, "%.2lf", (float)hdr_value_at_percentile(stats->element_size_histogram, 90.0));
    } else if (!strcasecmp(field_string, "elements.size.p50")) {
        snprintf(value, value_len, "%.2lf", (float)hdr_value_at_percentile(stats->element_size_histogram, 50.0));
    }
}

/* Show a few stats collected into 'rdbstate' */
void rdbShowGenericInfo(void) {
    printf("[info] %lu keys read\n", rdbstate.keys);
    printf("[info] %lu expires\n", rdbstate.expires);
    printf("[info] %lu already expired\n", rdbstate.already_expired);
    printf("[info] %lu functions\n", rdbstate.functions_num);
    if (rdbstate.lua_scripts) {
        printf("[info] %lu lua scripts read\n", rdbstate.lua_scripts);
    }

    char buffer[64];
    int stats_fd = -1;
    int original_stdout_fd = -1;
    if (rdbCheckStats) {
        if (rdbCheckOutput) {
            original_stdout_fd = dup(STDOUT_FILENO);
            stats_fd = open(rdbstate.stats_output, O_WRONLY | O_CREAT, 0644);
            if (stats_fd == -1) {
                fprintf(stderr, "Cannot open output file: '%s': %s\n", rdbstate.stats_output, strerror(errno));
                exit(1);
            } else {
                dup2(stats_fd, STDOUT_FILENO);
            }
        }

        char field_string[80];
        for (int dbid = 0; dbid <= rdbstate.databases; dbid++) {
            for (size_t i = 0; stats_field_string[i] != NULL; i++) {
                if (rdbstate.format == OUTPUT_FORMAT_TABLE) {
                    snprintf(field_string, sizeof(field_string), "db.%d.%s", dbid, stats_field_string[i]);
                    printf("%-30s", field_string);
                } else if (rdbstate.format == OUTPUT_FORMAT_CSV) {
                    printf("db.%d.%s", dbid, stats_field_string[i]);
                }

                for (size_t obj_type = 0; obj_type < OBJ_TYPE_MAX; obj_type++) {
                    const size_t stats_idx = obj_type + dbid * OBJ_TYPE_MAX;
                    rdbStats *stats = rdbstate.stats[stats_idx];

                    if (rdbstate.format == OUTPUT_FORMAT_INFO) {
                        if (i == 0) continue;

Review comment (refactor):
I was slightly confused by this variable name. The original stdout fd is actually STDOUT_FILENO. This is a copy AKA duplicate. It's another file descriptor for the same file.

Maybe rename it to `original_stdout_dup` or `saved_stdout`? Not sure if it make any difference.

Code after:
        snprintf(value, value_len, "%.2lf", (stats->elements > 0 ? (float)stats->all_elements_size / (float)stats->elements : 0));
    } else if (!strcasecmp(field_string, "elements.size.p99")) {
        snprintf(value, value_len, "%.2lf", (float)hdr_value_at_percentile(stats->element_size_histogram, 99.0));
    } else if (!strcasecmp(field_string, "elements.size.p90")) {
        snprintf(value, value_len, "%.2lf", (float)hdr_value_at_percentile(stats->element_size_histogram, 90.0));
    } else if (!strcasecmp(field_string, "elements.size.p50")) {
        snprintf(value, value_len, "%.2lf", (float)hdr_value_at_percentile(stats->element_size_histogram, 50.0));
    }
}

/* Show a few stats collected into 'rdbstate' */
void rdbShowGenericInfo(void) {
    printf("[info] %lu keys read\n", rdbstate.keys);
    printf("[info] %lu expires\n", rdbstate.expires);
    printf("[info] %lu already expired\n", rdbstate.already_expired);
    printf("[info] %lu functions\n", rdbstate.functions_num);
    if (rdbstate.lua_scripts) {
        printf("[info] %lu lua scripts read\n", rdbstate.lua_scripts);
    }

    char buffer[64];
    int stats_fd = -1;
    int saved_stdout = -1;
    if (rdbCheckStats) {
        if (rdbCheckOutput) {
            saved_stdout = dup(STDOUT_FILENO);
            stats_fd = open(rdbstate.stats_output, O_WRONLY | O_CREAT, 0644);
            if (stats_fd == -1) {
                fprintf(stderr, "Cannot open output file: '%s': %s\n", rdbstate.stats_output, strerror(errno));
                exit(1);
            } else {
                dup2(stats_fd, STDOUT_FILENO);
            }
        }

        char field_string[80];
        for (int dbid = 0; dbid <= rdbstate.databases; dbid++) {
            for (size_t i = 0; stats_field_string[i] != NULL; i++) {
                if (rdbstate.format == OUTPUT_FORMAT_TABLE) {
                    snprintf(field_string, sizeof(field_string), "db.%d.%s", dbid, stats_field_string[i]);
                    printf("%-30s", field_string);
                } else if (rdbstate.format == OUTPUT_FORMAT_CSV) {
                    printf("db.%d.%s", dbid, stats_field_string[i]);
                }

                for (size_t obj_type = 0; obj_type < OBJ_TYPE_MAX; obj_type++) {
                    const size_t stats_idx = obj_type + dbid * OBJ_TYPE_MAX;
                    rdbStats *stats = rdbstate.stats[stats_idx];

                    if (rdbstate.format == OUTPUT_FORMAT_INFO) {
                        if (i == 0) continue;