| 1 | // SPDX-License-Identifier: GPL-2.0-or-latercxx . . $ `<file>/src/lib/histogram.c`/ |
| 2 | /* |
| 3 | * Latency histograms |
| 4 | * |
| 5 | * Copyright 2023 Red Hat Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <errno.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include "histogram.h"cxx . . $ `<file>/src/include/histogram.h`/ |
| 13 | |
| 14 | int hist_initcxx . . $ hist_init(8be1cec40e435a01).(struct histogramcxx . . $ histogram# *h, unsigned long width, unsigned long num) |
| 15 | { |
| 16 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(h, 0, sizeof(*h)); |
| 17 | h->widthcxx . . $ histogram#width. = width; |
| 18 | h->numcxx . . $ histogram#num. = num; |
| 19 | |
| 20 | h->bucketscxx . . $ histogram#buckets. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num, sizeof(unsigned long)); |
| 21 | if (!h->bucketscxx . . $ histogram#buckets.) |
| 22 | return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | int hist_init_oflowcxx . . $ hist_init_oflow(e5685fb6a6b593bd).(struct histogramcxx . . $ histogram# *h, unsigned long num) |
| 28 | { |
| 29 | h->oflow_bufsizecxx . . $ histogram#oflow_bufsize. = num; |
| 30 | h->oflowscxx . . $ histogram#oflows. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num, sizeof(unsigned long)); |
| 31 | if (!h->oflowscxx . . $ histogram#oflows.) |
| 32 | return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | void hist_destroycxx . . $ hist_destroy(99493587c9cec4dd).(struct histogramcxx . . $ histogram# *h) |
| 38 | { |
| 39 | freecxx . . $ free(5d22bdacc48458e8).(h->oflowscxx . . $ histogram#oflows.); |
| 40 | h->oflowscxx . . $ histogram#oflows. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 41 | freecxx . . $ free(5d22bdacc48458e8).(h->bucketscxx . . $ histogram#buckets.); |
| 42 | h->bucketscxx . . $ histogram#buckets. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 43 | } |
| 44 | |
| 45 | int hist_samplecxx . . $ hist_sample(e5685fb6a6b593bd).(struct histogramcxx . . $ histogram# *h, uint64_tcxx . . $ uint64_t# sample) |
| 46 | { |
| 47 | unsigned long bucket = sample / h->widthcxx . . $ histogram#width.; |
| 48 | unsigned long extra; |
| 49 | unsigned long event = h->eventscxx . . $ histogram#events.++; |
| 50 | int ret; |
| 51 | |
| 52 | if (bucket < h->numcxx . . $ histogram#num.) { |
| 53 | h->bucketscxx . . $ histogram#buckets.[bucket]++; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | ret = HIST_OVERFLOWcxx . . $ `src/include/histogram.h:24:9`!; |
| 58 | extra = bucket - h->numcxx . . $ histogram#num.; |
| 59 | if (h->oflow_magnitudecxx . . $ histogram#oflow_magnitude. + extra > h->oflow_magnitudecxx . . $ histogram#oflow_magnitude.) |
| 60 | h->oflow_magnitudecxx . . $ histogram#oflow_magnitude. += extra; |
| 61 | else |
| 62 | ret |= HIST_OVERFLOW_MAGcxx . . $ `src/include/histogram.h:25:9`!; |
| 63 | |
| 64 | if (h->oflowscxx . . $ histogram#oflows.) { |
| 65 | if (h->oflow_countcxx . . $ histogram#oflow_count. < h->oflow_bufsizecxx . . $ histogram#oflow_bufsize.) |
| 66 | h->oflowscxx . . $ histogram#oflows.[h->oflow_countcxx . . $ histogram#oflow_count.] = event; |
| 67 | else |
| 68 | ret |= HIST_OVERFLOW_LOGcxx . . $ `src/include/histogram.h:26:9`!; |
| 69 | } |
| 70 | |
| 71 | h->oflow_countcxx . . $ histogram#oflow_count.++; |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | int hset_initcxx . . $ hset_init(f7593c26bdc66446).(struct histosetcxx . . $ histoset# *hs, unsigned long num_histos, |
| 76 | unsigned long bucket_width, unsigned long num_buckets, |
| 77 | unsigned long overflow) |
| 78 | { |
| 79 | unsigned long i; |
| 80 | |
| 81 | if (num_histos == 0) |
| 82 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 83 | |
| 84 | hs->num_histoscxx . . $ histoset#num_histos. = num_histos; |
| 85 | hs->num_bucketscxx . . $ histoset#num_buckets. = num_buckets; |
| 86 | hs->histoscxx . . $ histoset#histos. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num_histos, sizeof(struct histogramcxx . . $ histogram#)); |
| 87 | if (!hs->histoscxx . . $ histoset#histos.) |
| 88 | return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 89 | |
| 90 | for (i = 0; i < num_histos; i++) { |
| 91 | if (hist_initcxx . . $ hist_init(8be1cec40e435a01).(&hs->histoscxx . . $ histoset#histos.[i], bucket_width, num_buckets)) |
| 92 | goto fail; |
| 93 | if (overflow && hist_init_oflowcxx . . $ hist_init_oflow(e5685fb6a6b593bd).(&hs->histoscxx . . $ histoset#histos.[i], overflow)) |
| 94 | goto fail; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | |
| 99 | fail: |
| 100 | hset_destroycxx . . $ hset_destroy(33dbd104c143235e).(hs); |
| 101 | return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 102 | } |
| 103 | |
| 104 | void hset_destroycxx . . $ hset_destroy(33dbd104c143235e).(struct histosetcxx . . $ histoset# *hs) |
| 105 | { |
| 106 | unsigned long i; |
| 107 | |
| 108 | if (hs->histoscxx . . $ histoset#histos.) { |
| 109 | for (i = 0; i < hs->num_histoscxx . . $ histoset#num_histos.; i++) |
| 110 | hist_destroycxx . . $ hist_destroy(99493587c9cec4dd).(&hs->histoscxx . . $ histoset#histos.[i]); |
| 111 | } |
| 112 | |
| 113 | freecxx . . $ free(5d22bdacc48458e8).(hs->histoscxx . . $ histoset#histos.); |
| 114 | hs->histoscxx . . $ histoset#histos. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 115 | } |
| 116 | |
| 117 | void hset_print_bucketcxx . . $ hset_print_bucket(31a571358aba96cf).(struct histosetcxx . . $ histoset# *hs, FILEcxx . . $ FILE# *f, const char *pre, |
| 118 | unsigned long bucket, unsigned long flags) |
| 119 | { |
| 120 | unsigned long long sum = 0; |
| 121 | unsigned long i; |
| 122 | |
| 123 | if (bucket >= hs->num_bucketscxx . . $ histoset#num_buckets.) |
| 124 | return; |
| 125 | |
| 126 | for (i = 0; i < hs->num_histoscxx . . $ histoset#num_histos.; i++) |
| 127 | sum += hs->histoscxx . . $ histoset#histos.[i].bucketscxx . . $ histogram#buckets.[bucket]; |
| 128 | |
| 129 | if (sum == 0) |
| 130 | return; |
| 131 | if (pre) |
| 132 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "%s", pre); |
| 133 | |
| 134 | for (i = 0; i < hs->num_histoscxx . . $ histoset#num_histos.; i++) { |
| 135 | unsigned long val = hs->histoscxx . . $ histoset#histos.[i].bucketscxx . . $ histogram#buckets.[bucket]; |
| 136 | |
| 137 | if (i != 0) |
| 138 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "\t"); |
| 139 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "%06lu", val); |
| 140 | } |
| 141 | |
| 142 | if (flags & HSET_PRINT_SUMcxx . . $ `src/include/histogram.h:33:9`!) |
| 143 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "\t%06llu", sum); |
| 144 | |
| 145 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "\n"); |
| 146 | } |
| 147 | |
| 148 | void hist_print_jsoncxx . . $ hist_print_json(6acc875be05641a4).(struct histogramcxx . . $ histogram# *h, FILEcxx . . $ FILE# *f) |
| 149 | { |
| 150 | unsigned long i; |
| 151 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! comma = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 152 | |
| 153 | for (i = 0; i < h->numcxx . . $ histogram#num.; i++) { |
| 154 | unsigned long val = h->bucketscxx . . $ histogram#buckets.[i]; |
| 155 | |
| 156 | if (val != 0) { |
| 157 | if (comma) |
| 158 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, ","); |
| 159 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "\n \"%lu\": %lu", i, val); |
| 160 | comma = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "\n"); |
| 165 | } |
| 166 | |
| 167 | void hist_print_oflowscxx . . $ hist_print_oflows(6acc875be05641a4).(struct histogramcxx . . $ histogram# *h, FILEcxx . . $ FILE# *f) |
| 168 | { |
| 169 | unsigned long i; |
| 170 | |
| 171 | for (i = 0; i < h->oflow_countcxx . . $ histogram#oflow_count.; i++) { |
| 172 | if (i >= h->oflow_bufsizecxx . . $ histogram#oflow_bufsize.) |
| 173 | break; |
| 174 | if (i != 0) |
| 175 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " "); |
| 176 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, "%05lu", h->oflowscxx . . $ histogram#oflows.[i]); |
| 177 | } |
| 178 | |
| 179 | if (i >= h->oflow_bufsizecxx . . $ histogram#oflow_bufsize.) |
| 180 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(f, " # %05lu others", h->oflow_countcxx . . $ histogram#oflow_count. - h->oflow_bufsizecxx . . $ histogram#oflow_bufsize.); |
| 181 | } |