ERL_NIF_TERM _hh_log_csv(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { char fname[64]; hh_ctx_t* ctx = NULL; ErlNifResourceType* ctx_type = get_hh_ctx_type(env); if (argc != 2 || ctx_type == NULL || !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx) || !enif_get_string(env, argv[1], fname, 64, ERL_NIF_LATIN1)) { return enif_make_badarg(env); } FILE* stream = fopen(fname,"w+"); if (stream == NULL) { return make_error(env, "cannot_create_or_write_to_file"); } if (ctx != NULL) { hdr_percentiles_print(ctx->data, stream, 5, 1.0, CSV); if (fclose(stream) != 0) { return make_error(env, "bad_file"); } return ATOM_OK; } return make_error(env, "bad_hdr_histogram_nif_impl"); }
int main(int argc, char** argv) { int rc = 0; FILE* f; if (argc == 1) { f = stdin; } else { f = fopen(argv[1], "r"); } if (!f) { fprintf(stderr, "Failed to open file(%s):%s\n", argv[1], strerror(errno)); return -1; } struct hdr_log_reader reader; if (hdr_log_reader_init(&reader)) { fprintf(stderr, "Failed to init reader\n"); return -1; } struct hdr_histogram* h = NULL; hdr_timespec timestamp; hdr_timespec interval; rc = hdr_log_read_header(&reader, f); if(rc) { fprintf(stderr, "Failed to read header: %s\n", hdr_strerror(rc)); return -1; } while (true) { rc = hdr_log_read(&reader, f, &h, ×tamp, &interval); if (0 == rc) { hdr_percentiles_print(h, stdout, 5, 1.0, CLASSIC); } else if (EOF == rc) { break; } else { fprintf(stderr, "Failed to print histogram: %s\n", hdr_strerror(rc)); return -1; } } return 0; }
int main(int argc, char **argv) { int i; struct hdr_histogram* raw_histogram = NULL; struct hdr_histogram* cor_histogram = NULL; hdr_alloc(100000000, 3, &raw_histogram); hdr_alloc(100000000, 3, &cor_histogram); for (i = 0; i < 10000; i++) { hdr_record_value(raw_histogram, 1000L); hdr_record_corrected_value(cor_histogram, 1000L, 10000L); } hdr_record_value(raw_histogram, 100000000L); hdr_record_corrected_value(cor_histogram, 100000000L, 10000L); hdr_percentiles_print(raw_histogram, stdout, 5, 1.0, CSV); hdr_percentiles_print(cor_histogram, stdout, 5, 1.0, CSV); }
// we rewrite the file each time. void print_full_histogram(FILE *out, struct hdr_histogram *cumulative) { struct timeval now; gettimeofday(&now,NULL); fseek(out, 0, SEEK_SET); fprintf(out,"c_hiccup histogram report: %s\n------------------------\n",ctime(&now.tv_sec)); hdr_percentiles_print(cumulative, out, 5, 1.0, CSV); fflush(out); return; }
void report() { unsigned int current_sec = start > 0 ? time(NULL) - start + 1 : 0; int first_zero = -1; unsigned int valid_cnt = 0; unsigned int valid_pps[MAX_SECONDS]; fprintf(pps_output, "# seconds \t k packets\n"); if (current_sec > MAX_SECONDS) current_sec = MAX_SECONDS; for (int i = 0; i < current_sec; ++i) { // Omit trailing zeros if (pps[i] == 0) { if (first_zero < 0) first_zero = i; continue; } if (first_zero >= 0) { for (int j = first_zero; j < i; ++j) { fprintf(pps_output, "%d \t %d\n", j, 0); valid_pps[valid_cnt++] = 0; } first_zero = -1; } fprintf(pps_output, "%d \t %d\n", i, pps[i]); valid_pps[valid_cnt++] = pps[i]; } qsort(valid_pps, valid_cnt, sizeof(unsigned int), intcmp); hdr_percentiles_print(hist, lat_output, 5, 1.0, CLASSIC); printf("TOTAL_PACKETS=%"PRIu64"\n", received_packets); if (received_packets > 0) { printf("MEDIAN_PPS=%u\n", valid_pps[valid_cnt / 2]); printf("MAX_PPS=%u\n", valid_pps[valid_cnt - 1]); printf("LAT_50=%"PRIu64"\n", hdr_value_at_percentile(hist, 50.0)); printf("LAT_75=%"PRIu64"\n", hdr_value_at_percentile(hist, 75.0)); printf("LAT_90=%"PRIu64"\n", hdr_value_at_percentile(hist, 90.0)); printf("LAT_99=%"PRIu64"\n", hdr_value_at_percentile(hist, 99.0)); printf("LAT_99_9=%"PRIu64"\n", hdr_value_at_percentile(hist, 99.9)); } close(sock_raw); }
ERL_NIF_TERM _hh_print_csv(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { hh_ctx_t* ctx = NULL; ErlNifResourceType* ctx_type = get_hh_ctx_type(env); if (ctx_type != NULL && !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx)) { return enif_make_badarg(env); } if (ctx != NULL) { hdr_percentiles_print(ctx->data, stdout, 5, 1.0, CSV); return ATOM_OK; } return make_error(env, "bad_hdr_histogram_nif_impl"); }