summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/include/impl/histogram.h
blob: 92ddeb9e576b4cc6c4fc20924da4deced3d120c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * histograms.
 *
 * Copyright (c) 2009 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if !defined (__PGM_IMPL_FRAMEWORK_H_INSIDE__) && !defined (PGM_COMPILATION)
#	error "Only <framework.h> can be included directly."
#endif

#ifndef __PGM_IMPL_HISTOGRAM_H__
#define __PGM_IMPL_HISTOGRAM_H__

#include <pgm/types.h>
#include <pgm/time.h>
#include <impl/slist.h>
#include <impl/string.h>

PGM_BEGIN_DECLS

typedef int pgm_sample_t;
typedef int pgm_count_t;

struct pgm_sample_set_t {
	pgm_count_t*	counts;
	unsigned	counts_len;
	int64_t		sum;
	int64_t		square_sum;
};

typedef struct pgm_sample_set_t pgm_sample_set_t;

struct pgm_histogram_t {
	const char* restrict	histogram_name;
	unsigned		bucket_count;
	pgm_sample_t		declared_min;
	pgm_sample_t		declared_max;
	pgm_sample_t* restrict	ranges;
	pgm_sample_set_t	sample;
	bool			is_registered;
	pgm_slist_t		histograms_link;
};

typedef struct pgm_histogram_t pgm_histogram_t;

#define PGM_HISTOGRAM_DEFINE(name, minimum, maximum, count) \
		static pgm_count_t counts[ (count) ]; \
		static pgm_sample_t ranges[ (count) + 1 ]; \
		static pgm_histogram_t counter = { \
			.histogram_name		= (name), \
			.bucket_count		= (count), \
			.declared_min		= (minimum), \
			.declared_max		= (maximum), \
			.ranges			= ranges, \
			.sample = { \
				.counts 		= counts, \
				.counts_len		= (count), \
				.sum			= 0, \
				.square_sum		= 0 \
			}, \
			.is_registered		= FALSE \
		}

#ifdef CONFIG_HISTOGRAMS

#	define PGM_HISTOGRAM_TIMES(name, sample) do { \
		PGM_HISTOGRAM_DEFINE(name, pgm_msecs(1), pgm_secs(10), 50); \
		if (!counter.is_registered) { \
			memset (counts, 0, sizeof(counts)); \
			memset (ranges, 0, sizeof(ranges)); \
			pgm_histogram_init (&counter); \
		} \
		pgm_histogram_add_time (&counter, sample); \
	} while (0)

#	define PGM_HISTOGRAM_COUNTS(name, sample) do { \
		PGM_HISTOGRAM_DEFINE(name, 1, 1000000, 50); \
		if (!counter.is_registered) { \
			memset (counts, 0, sizeof(counts)); \
			memset (ranges, 0, sizeof(ranges)); \
			pgm_histogram_init (&counter); \
		} \
		pgm_histogram_add (&counter, (sample)); \
	} while (0)

#else /* !CONFIG_HISTOGRAMS */

#	define PGM_HISTOGRAM_TIMES(name, sample)
#	define PGM_HISTOGRAM_COUNTS(name, sample)

#endif /* !CONFIG_HISTOGRAMS */


extern pgm_slist_t*	pgm_histograms;

void pgm_histogram_init (pgm_histogram_t*);
void pgm_histogram_add (pgm_histogram_t*, int);
void pgm_histogram_write_html_graph_all (pgm_string_t*);

static inline
void
pgm_histogram_add_time (
	pgm_histogram_t*const	histogram,
	pgm_time_t		sample_time
	)
{
	pgm_histogram_add (histogram, (int)pgm_to_msecs (sample_time));
}

PGM_END_DECLS

#endif /* __PGM_IMPL_HISTOGRAM_H__ */

/* eof */