| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 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" |
| 13 | |
| 14 | int hist_init(struct histogram *h, unsigned long width, unsigned long num) |
| 15 | { |
| 16 | memset(h, 0, sizeof(*h)); |
| 17 | h->width = width; |
| 18 | h->num = num; |
| 19 | |
| 20 | h->buckets = calloc(num, sizeof(unsigned long)); |
| 21 | if (!h->buckets) |
| 22 | return -ENOMEM; |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | int hist_init_oflow(struct histogram *h, unsigned long num) |
| 28 | { |
| 29 | h->oflow_bufsize = num; |
| 30 | h->oflows = calloc(num, sizeof(unsigned long)); |
| 31 | if (!h->oflows) |
| 32 | return -ENOMEM; |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | void hist_destroy(struct histogram *h) |
| 38 | { |
| 39 | free(h->oflows); |
| 40 | h->oflows = NULL; |
| 41 | free(h->buckets); |
| 42 | h->buckets = NULL; |
| 43 | } |
| 44 | |
| 45 | int hist_sample(struct histogram *h, uint64_t sample) |
| 46 | { |
| 47 | unsigned long bucket = sample / h->width; |
| 48 | unsigned long extra; |
| 49 | unsigned long event = h->events++; |
| 50 | int ret; |
| 51 | |
| 52 | if (bucket < h->num) { |
| 53 | h->buckets[bucket]++; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | ret = HIST_OVERFLOW; |
| 58 | extra = bucket - h->num; |
| 59 | if (h->oflow_magnitude + extra > h->oflow_magnitude) |
| 60 | h->oflow_magnitude += extra; |
| 61 | else |
| 62 | ret |= HIST_OVERFLOW_MAG; |
| 63 | |
| 64 | if (h->oflows) { |
| 65 | if (h->oflow_count < h->oflow_bufsize) |
| 66 | h->oflows[h->oflow_count] = event; |
| 67 | else |
| 68 | ret |= HIST_OVERFLOW_LOG; |
| 69 | } |
| 70 | |
| 71 | h->oflow_count++; |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | int hset_init(struct 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 -EINVAL; |
| 83 | |
| 84 | hs->num_histos = num_histos; |
| 85 | hs->num_buckets = num_buckets; |
| 86 | hs->histos = calloc(num_histos, sizeof(struct histogram)); |
| 87 | if (!hs->histos) |
| 88 | return -ENOMEM; |
| 89 | |
| 90 | for (i = 0; i < num_histos; i++) { |
| 91 | if (hist_init(&hs->histos[i], bucket_width, num_buckets)) |
| 92 | goto fail; |
| 93 | if (overflow && hist_init_oflow(&hs->histos[i], overflow)) |
| 94 | goto fail; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | |
| 99 | fail: |
| 100 | hset_destroy(hs); |
| 101 | return -ENOMEM; |
| 102 | } |
| 103 | |
| 104 | void hset_destroy(struct histoset *hs) |
| 105 | { |
| 106 | unsigned long i; |
| 107 | |
| 108 | if (hs->histos) { |
| 109 | for (i = 0; i < hs->num_histos; i++) |
| 110 | hist_destroy(&hs->histos[i]); |
| 111 | } |
| 112 | |
| 113 | free(hs->histos); |
| 114 | hs->histos = NULL; |
| 115 | } |
| 116 | |
| 117 | void hset_print_bucket(struct histoset *hs, 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_buckets) |
| 124 | return; |
| 125 | |
| 126 | for (i = 0; i < hs->num_histos; i++) |
| 127 | sum += hs->histos[i].buckets[bucket]; |
| 128 | |
| 129 | if (sum == 0) |
| 130 | return; |
| 131 | if (pre) |
| 132 | fprintf(f, "%s", pre); |
| 133 | |
| 134 | for (i = 0; i < hs->num_histos; i++) { |
| 135 | unsigned long val = hs->histos[i].buckets[bucket]; |
| 136 | |
| 137 | if (i != 0) |
| 138 | fprintf(f, "\t"); |
| 139 | fprintf(f, "%06lu", val); |
| 140 | } |
| 141 | |
| 142 | if (flags & HSET_PRINT_SUM) |
| 143 | fprintf(f, "\t%06llu", sum); |
| 144 | |
| 145 | fprintf(f, "\n"); |
| 146 | } |
| 147 | |
| 148 | void hist_print_json(struct histogram *h, FILE *f) |
| 149 | { |
| 150 | unsigned long i; |
| 151 | bool comma = false; |
| 152 | |
| 153 | for (i = 0; i < h->num; i++) { |
| 154 | unsigned long val = h->buckets[i]; |
| 155 | |
| 156 | if (val != 0) { |
| 157 | if (comma) |
| 158 | fprintf(f, ","); |
| 159 | fprintf(f, "\n \"%lu\": %lu", i, val); |
| 160 | comma = true; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fprintf(f, "\n"); |
| 165 | } |
| 166 | |
| 167 | void hist_print_oflows(struct histogram *h, FILE *f) |
| 168 | { |
| 169 | unsigned long i; |
| 170 | |
| 171 | for (i = 0; i < h->oflow_count; i++) { |
| 172 | if (i >= h->oflow_bufsize) |
| 173 | break; |
| 174 | if (i != 0) |
| 175 | fprintf(f, " "); |
| 176 | fprintf(f, "%05lu", h->oflows[i]); |
| 177 | } |
| 178 | |
| 179 | if (i >= h->oflow_bufsize) |
| 180 | fprintf(f, " # %05lu others", h->oflow_count - h->oflow_bufsize); |
| 181 | } |