qemu-patch-raspberry4/include/qemu/qdist.h
Emilio G. Cota bf3afd5f41 qdist: add module to represent frequency distributions of data
Sometimes it is useful to have a quick histogram to represent a certain
distribution -- for example, when investigating a performance regression
in a hash table due to inadequate hashing.

The appended allows us to easily represent a distribution using Unicode
characters. Further, the data structure keeping track of the distribution
is so simple that obtaining its values for off-line processing is trivial.

Example, taking the last 10 commits to QEMU:

 Characters in commit title  Count
-----------------------------------
                         39      1
                         48      1
                         53      1
                         54      2
                         57      1
                         61      1
                         67      1
                         78      1
                         80      1
qdist_init(&dist);
qdist_inc(&dist, 39);
[...]
qdist_inc(&dist, 80);

char *str = qdist_pr(&dist, 9, QDIST_PR_LABELS);
// -> [39.0,43.6)▂▂ █▂ ▂ ▄[75.4,80.0]
g_free(str);

char *str = qdist_pr(&dist, 4, QDIST_PR_LABELS);
// -> [39.0,49.2)▁█▁▁[69.8,80.0]
g_free(str);

Reviewed-by: Richard Henderson <rth@twiddle.net>
Signed-off-by: Emilio G. Cota <cota@braap.org>
Message-Id: <1465412133-3029-9-git-send-email-cota@braap.org>
Signed-off-by: Richard Henderson <rth@twiddle.net>
2016-06-11 23:10:19 +00:00

64 lines
2 KiB
C

/*
* Copyright (C) 2016, Emilio G. Cota <cota@braap.org>
*
* License: GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef QEMU_QDIST_H
#define QEMU_QDIST_H
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/bitops.h"
/*
* Samples with the same 'x value' end up in the same qdist_entry,
* e.g. inc(0.1) and inc(0.1) end up as {x=0.1, count=2}.
*
* Binning happens only at print time, so that we retain the flexibility to
* choose the binning. This might not be ideal for workloads that do not care
* much about precision and insert many samples all with different x values;
* in that case, pre-binning (e.g. entering both 0.115 and 0.097 as 0.1)
* should be considered.
*/
struct qdist_entry {
double x;
unsigned long count;
};
struct qdist {
struct qdist_entry *entries;
size_t n;
size_t size;
};
#define QDIST_PR_BORDER BIT(0)
#define QDIST_PR_LABELS BIT(1)
/* the remaining options only work if PR_LABELS is set */
#define QDIST_PR_NODECIMAL BIT(2)
#define QDIST_PR_PERCENT BIT(3)
#define QDIST_PR_100X BIT(4)
#define QDIST_PR_NOBINRANGE BIT(5)
void qdist_init(struct qdist *dist);
void qdist_destroy(struct qdist *dist);
void qdist_add(struct qdist *dist, double x, long count);
void qdist_inc(struct qdist *dist, double x);
double qdist_xmin(const struct qdist *dist);
double qdist_xmax(const struct qdist *dist);
double qdist_avg(const struct qdist *dist);
unsigned long qdist_sample_count(const struct qdist *dist);
size_t qdist_unique_entries(const struct qdist *dist);
/* callers must free the returned string with g_free() */
char *qdist_pr_plain(const struct qdist *dist, size_t n_groups);
/* callers must free the returned string with g_free() */
char *qdist_pr(const struct qdist *dist, size_t n_groups, uint32_t opt);
/* Only qdist code and test code should ever call this function */
void qdist_bin__internal(struct qdist *to, const struct qdist *from, size_t n);
#endif /* QEMU_QDIST_H */