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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2011-2015 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "internal.h"
/**
* Timing data in libcouchbase is stored in a structure to make
* it easy to work with. It ill consume a fair amount of data,
* but it's only allocated when you enable it ;-)
* I decided I'd rather just make it easy to work with...
*/
struct lcb_histogram_st {
/** The highest value (number of ocurrences ) in all of the buckets */
lcb_U32 max;
/** Number of entries below a microsecond */
lcb_U32 nsec;
/**
* Entries between 1-1000 microseconds. Each array element refers to a
* 10 microsecond interval
*/
lcb_U32 usec[100];
/**
* Entries between 1-10 milliseconds. Each array entry refers to a 100
* microsecond interval.
*/
lcb_U32 lt10msec[100];
/**
* Entries between 10-1000 milliseconds. Each entry refers to a 10 millisecond
* interval
*/
lcb_U32 msec[100];
/**
* Seconds are collected per sec
*/
lcb_U32 sec[10];
};
LCB_INTERNAL_API
lcb_HISTOGRAM *
lcb_histogram_create(void)
{
return calloc(1, sizeof(lcb_HISTOGRAM));
}
LCB_INTERNAL_API
void
lcb_histogram_destroy(lcb_HISTOGRAM *hg)
{
free(hg);
}
LCB_INTERNAL_API
void
lcb_histogram_read(const lcb_HISTOGRAM *hg,
const void *cookie, lcb_HISTOGRAM_CALLBACK callback)
{
lcb_U32 max, start, ii, end;
max = hg->max;
/*
** @todo I should merge "empty" sets.. currently I'm only going to
** report the nonzero ones...
*/
if (hg->nsec) {
callback(cookie, LCB_TIMEUNIT_NSEC, 0, 999, hg->nsec, max);
}
start = 1;
for (ii = 0; ii < 100; ++ii) {
end = (ii + 1) * 10 - 1;
if (hg->usec[ii]) {
callback(cookie, LCB_TIMEUNIT_USEC, start, end, hg->usec[ii], max);
}
start = end + 1;
}
start = 1000;
for (ii = 0; ii < 100; ++ii) {
end = (ii + 1) * 100 - 1;
if (hg->lt10msec[ii]) {
callback(cookie, LCB_TIMEUNIT_USEC, start, end, hg->lt10msec[ii], max);
}
start = end + 1;
}
start = 1;
for (ii = 0; ii < 100; ++ii) {
end = (ii + 1) * 10 - 1;
if (hg->msec[ii]) {
callback(cookie, LCB_TIMEUNIT_MSEC, start, end, hg->msec[ii], max);
}
start = end + 1;
}
start = 1000;
for (ii = 1; ii < 9; ++ii) {
start = ii * 1000;
end = ((ii + 1) * 1000) - 1;
if (hg->sec[ii]) {
callback(cookie, LCB_TIMEUNIT_MSEC, start, end, hg->sec[ii], max);
}
}
if (hg->sec[9]) {
callback(cookie, LCB_TIMEUNIT_SEC, 9, 9999, hg->sec[9], max);
}
}
static void
default_timings_callback(const void *cookie,
lcb_timeunit_t timeunit,
lcb_uint32_t min_val, lcb_uint32_t max_val,
lcb_uint32_t total, lcb_uint32_t maxtotal) {
FILE* stream = (FILE*)cookie;
const char *unit = NULL;
int ii;
int num_hash;
fprintf(stream, "[%-4u - %-4u]", min_val, max_val);
if (timeunit == LCB_TIMEUNIT_NSEC) {
unit = "ns";
} else if (timeunit == LCB_TIMEUNIT_USEC) {
unit = "us";
} else if (timeunit == LCB_TIMEUNIT_MSEC) {
unit = "ms";
} else if (timeunit == LCB_TIMEUNIT_SEC) {
unit = "s";
} else {
unit = "?";
}
fprintf(stream, "%s |", unit);
num_hash = (int)((float)40.0 * (float)total / (float)maxtotal);
for (ii = 0; ii < num_hash; ++ii) {
putw('#', stream);
}
fprintf(stream, " - %u\n", total);
}
LCB_INTERNAL_API
void lcb_histogram_print(lcb_HISTOGRAM* hg, FILE* stream)
{
lcb_histogram_read(hg, stream, default_timings_callback);
}
LCB_INTERNAL_API
void
lcb_histogram_record(lcb_HISTOGRAM *hg, lcb_U64 delta)
{
lcb_U32 num;
if (delta < 1000) {
/* nsec */
if (++hg->nsec > hg->max) {
hg->max = hg->nsec;
}
} else if (delta < LCB_US2NS(1000)) {
/* micros */
delta /= LCB_US2NS(1);
if ((num = ++hg->usec[delta/10]) > hg->max) {
hg->max = num;
}
} else if (delta < LCB_US2NS(10000)) {
/* 1-10ms */
delta /= LCB_US2NS(1);
assert(delta <= 10000);
if ((num = ++hg->lt10msec[delta/100]) > hg->max) {
hg->max = num;
}
} else if (delta < LCB_S2NS(1)) {
delta /= LCB_US2NS(1000);
if ((num = ++hg->msec[delta/10]) > hg->max) {
hg->max = num;
}
} else {
delta /= LCB_S2NS(1);
if (delta > 9) {
delta = 9;
}
if ((num = ++hg->sec[delta]) > hg->max) {
hg->max = num;
}
}
}