7 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
24 #ifdef CVMFS_NAMESPACE_GUARD
25 namespace CVMFS_NAMESPACE_GUARD {
33 if (end.tv_usec < start.tv_usec) {
34 const int64_t nsec = (end.tv_usec - start.tv_usec) / 1000000 + 1;
35 start.tv_usec -= 1000000 * nsec;
38 if (end.tv_usec - start.tv_usec > 1000000) {
39 const int64_t nsec = (end.tv_usec - start.tv_usec) / 1000000;
40 start.tv_usec += 1000000 * nsec;
46 const uint64_t elapsed_usec =
47 ((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec);
48 return static_cast<double>(elapsed_usec) / 1000000.0;
55 gettimeofday(&start_, NULL);
63 gettimeofday(&end_, NULL);
87 return static_cast<unsigned int>(floor(log10(static_cast<double>(n)))) + 1;
90 static std::string
GenerateStars(
unsigned int n) {
return std::string(n,
'*'); }
96 this->bins_.assign(nbins + 1, 0);
97 this->boundary_values_.assign(nbins + 1, 0);
100 for (i = 1; i <= nbins; i++) {
101 this->boundary_values_[i] = (1 << ((i - 1) + 1));
110 const uint64_t total = this->N();
112 uint64_t pivot =
static_cast<uint64_t
>(
static_cast<float>(total) * n);
113 float normalized_pivot = 0.0;
117 for (i = 1; i <= this->bins_.size() - 1; i++) {
118 const unsigned int bin_value =
119 static_cast<unsigned int>(atomic_read32(&(this->bins_[i])));
120 if (pivot <= bin_value) {
121 normalized_pivot =
static_cast<float>(pivot)
122 / static_cast<float>(bin_value);
127 if (i >= this->bins_.size()) {
128 return this->boundary_values_[this->bins_.size() - 1];
132 const unsigned int min_value = this->boundary_values_[i - 1];
133 const unsigned int max_value = this->boundary_values_[i];
136 +
static_cast<unsigned int>(
static_cast<float>(max_value - min_value)
143 unsigned int max_left_boundary_count = 1;
144 unsigned int max_right_boundary_count = 1;
145 unsigned int max_value_count = 1;
146 unsigned int max_stars = 0;
147 unsigned int max_bins = 0;
148 const unsigned int total_stars = 38;
149 uint64_t total_sum_of_bins = 0;
151 for (i = 1; i <= this->bins_.size() - 1; i++) {
152 max_left_boundary_count = std::max(max_left_boundary_count,
154 max_right_boundary_count = std::max(max_right_boundary_count,
156 max_value_count = std::max(max_value_count,
159 max_bins, static_cast<unsigned int>(atomic_read32(&(this->bins_[i]))));
160 total_sum_of_bins +=
static_cast<unsigned int>(
161 atomic_read32(&(this->bins_[i])));
165 max_bins, static_cast<unsigned int>(atomic_read32(&(this->bins_[0]))));
166 total_sum_of_bins +=
static_cast<unsigned int>(
167 atomic_read32(&(this->bins_[0])));
169 if (total_sum_of_bins != 0) {
170 max_stars = max_bins * total_stars / total_sum_of_bins;
173 const std::string
format =
175 StringifyUint(max_left_boundary_count < 2 ? 2 : max_left_boundary_count) +
176 "d -> %" +
StringifyUint(max_right_boundary_count) +
"d : %" +
180 const std::string title_format =
183 (max_left_boundary_count < 2 ? 2 : max_left_boundary_count) +
184 max_right_boundary_count + 4) +
188 const std::string overflow_format =
190 StringifyUint(max_left_boundary_count + max_right_boundary_count + 5) +
194 const std::string total_format =
196 StringifyUint(max_left_boundary_count + max_right_boundary_count + 5 < 8
198 : max_left_boundary_count + max_right_boundary_count +
202 std::string result_string =
"";
204 const unsigned int kBufSize = 300;
205 char buffer[kBufSize];
206 memset(buffer, 0,
sizeof(buffer));
209 buffer, kBufSize, title_format.c_str(),
"nsec",
"count",
"distribution");
210 result_string += buffer;
211 memset(buffer, 0,
sizeof(buffer));
213 for (i = 1; i <= this->bins_.size() - 1; i++) {
214 unsigned int n_of_stars = 0;
215 if (total_sum_of_bins != 0) {
216 n_of_stars =
static_cast<unsigned int>(atomic_read32(&(this->bins_[i])))
217 * total_stars / total_sum_of_bins;
223 boundary_values_[i - 1],
224 boundary_values_[i] - 1,
225 static_cast<unsigned int>(atomic_read32(&this->bins_[i])),
227 result_string += buffer;
228 memset(buffer, 0,
sizeof(buffer));
231 unsigned int n_of_stars = 0;
232 if (total_sum_of_bins != 0) {
233 n_of_stars =
static_cast<unsigned int>(atomic_read32(&(this->bins_[0])))
234 * total_stars / total_sum_of_bins;
239 overflow_format.c_str(),
241 static_cast<unsigned int>(atomic_read32(&(this->bins_[0]))),
243 result_string += buffer;
244 memset(buffer, 0,
sizeof(buffer));
246 snprintf(buffer, kBufSize, total_format.c_str(),
"total", total_sum_of_bins);
247 result_string += buffer;
248 memset(buffer, 0,
sizeof(buffer));
250 const float qs[15] = {.1, .2, .25, .3, .4, .5, .6, .7,
251 .75, .8, .9, .95, .99, .995, .999};
252 snprintf(buffer, kBufSize,
254 "%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,"
255 "%0.4f,%0.4f,%0.4f,%0.4f\n"
256 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n"
258 "\n-----------------------\n",
259 qs[0], qs[1], qs[2], qs[3], qs[4], qs[5], qs[6], qs[7], qs[8], qs[9],
260 qs[10], qs[11], qs[12], qs[13], qs[14], GetQuantile(qs[0]),
261 GetQuantile(qs[1]), GetQuantile(qs[2]), GetQuantile(qs[3]),
262 GetQuantile(qs[4]), GetQuantile(qs[5]), GetQuantile(qs[6]),
263 GetQuantile(qs[7]), GetQuantile(qs[8]), GetQuantile(qs[9]),
264 GetQuantile(qs[10]), GetQuantile(qs[11]), GetQuantile(qs[12]),
265 GetQuantile(qs[13]), GetQuantile(qs[14]));
267 result_string += buffer;
268 memset(buffer, 0,
sizeof(buffer));
270 return result_string;
274 printf(
"%s", this->ToString().c_str());
277 #ifdef CVMFS_NAMESPACE_GUARD
std::vector< atomic_int32 > bins_
double DiffTimeSeconds(struct timeval start, struct timeval end)
static std::string GenerateStars(unsigned int n)
assert((mem||(size==0))&&"Out Of Memory")
Log2Histogram(unsigned int nbins)
std::string StringifyUint(const uint64_t value)
unsigned int GetQuantile(float n)
static unsigned int CountDigits(uint64_t n)
void PrintLog2Histogram()
CVMFS_EXPORT const LogSource const int const char * format
std::vector< atomic_int32 > GetBins(const Log2Histogram &h)