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 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 int64_t nsec = (end.tv_usec - start.tv_usec) / 1000000;
40 start.tv_usec += 1000000 * nsec;
46 uint64_t elapsed_usec = ((end.tv_sec - start.tv_sec)*1000000) +
47 (end.tv_usec - start.tv_usec);
48 return static_cast<double>(elapsed_usec)/1000000.0;
56 gettimeofday(&start_, NULL);
64 gettimeofday(&end_, NULL);
88 return static_cast<unsigned int>(floor(log10(static_cast<double>(n)))) + 1;
92 return std::string(n,
'*');
99 this->bins_.assign(nbins + 1, 0);
100 this->boundary_values_.assign(nbins + 1, 0);
103 for (i = 1; i <= nbins; i++) {
104 this->boundary_values_[i] = (1 << ((i - 1) + 1));
113 uint64_t total = this->N();
115 uint64_t pivot =
static_cast<uint64_t
>(
static_cast<float>(total) * n);
116 float normalized_pivot = 0.0;
120 for (i = 1; i <= this->bins_.size() - 1; i++) {
121 unsigned int bin_value =
122 static_cast<unsigned int>(atomic_read32(&(this->bins_[i])));
123 if (pivot <= bin_value) {
125 static_cast<float>(pivot) / static_cast<float>(bin_value);
130 if (i >= this->bins_.size()) {
131 return this->boundary_values_[this->bins_.size() - 1];
135 unsigned int min_value = this->boundary_values_[i - 1];
136 unsigned int max_value = this->boundary_values_[i];
138 return min_value +
static_cast<unsigned int>(
139 static_cast<float>(max_value - min_value) * normalized_pivot);
145 unsigned int max_left_boundary_count = 1;
146 unsigned int max_right_boundary_count = 1;
147 unsigned int max_value_count = 1;
148 unsigned int max_stars = 0;
149 unsigned int max_bins = 0;
150 unsigned int total_stars = 38;
151 uint64_t total_sum_of_bins = 0;
153 for (i = 1; i <= this->bins_.size() - 1; i++) {
154 max_left_boundary_count = std::max(max_left_boundary_count,
156 max_right_boundary_count = std::max(max_right_boundary_count,
158 max_value_count = std::max(max_value_count,
160 max_bins = std::max(max_bins, static_cast<unsigned int>(
161 atomic_read32(&(this->bins_[i]))));
163 static_cast<unsigned int>(atomic_read32(&(this->bins_[i])));
166 max_bins = std::max(max_bins, static_cast<unsigned int>(
167 atomic_read32(&(this->bins_[0]))));
169 static_cast<unsigned int>(atomic_read32(&(this->bins_[0])));
171 if (total_sum_of_bins != 0) {
172 max_stars = max_bins * total_stars / total_sum_of_bins;
176 2 : max_left_boundary_count) +
181 std::string title_format =
" %" +
183 2 : max_left_boundary_count) +
184 max_right_boundary_count +
190 std::string overflow_format =
"%" +
192 max_right_boundary_count +
198 std::string total_format =
"%" +
200 max_right_boundary_count +
201 5 < 8 ? 8 : max_left_boundary_count +
202 max_right_boundary_count + 5) +
205 std::string result_string =
"";
207 const unsigned int kBufSize = 300;
208 char buffer[kBufSize];
209 memset(buffer, 0,
sizeof(buffer));
213 title_format.c_str(),
217 result_string += buffer;
218 memset(buffer, 0,
sizeof(buffer));
220 for (i = 1; i <= this->bins_.size() - 1; i++) {
221 unsigned int n_of_stars = 0;
222 if (total_sum_of_bins != 0) {
223 n_of_stars =
static_cast<unsigned int>(atomic_read32(&(this->bins_[i]))) *
224 total_stars / total_sum_of_bins;
230 boundary_values_[i - 1],
231 boundary_values_[i] - 1,
232 static_cast<unsigned int>(atomic_read32(&this->bins_[i])),
234 result_string += buffer;
235 memset(buffer, 0,
sizeof(buffer));
238 unsigned int n_of_stars = 0;
239 if (total_sum_of_bins != 0) {
240 n_of_stars =
static_cast<unsigned int>(atomic_read32(&(this->bins_[0])))
241 * total_stars / total_sum_of_bins;
246 overflow_format.c_str(),
248 static_cast<unsigned int>(atomic_read32(&(this->bins_[0]))),
250 result_string += buffer;
251 memset(buffer, 0,
sizeof(buffer));
255 total_format.c_str(),
258 result_string += buffer;
259 memset(buffer, 0,
sizeof(buffer));
261 float qs[15] = {.1, .2, .25, .3, .4, .5, .6, .7,
262 .75, .8, .9, .95, .99, .995, .999};
263 snprintf(buffer, kBufSize,
265 "%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,%0.4f,"
266 "%0.4f,%0.4f,%0.4f,%0.4f\n"
267 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n"
269 "\n-----------------------\n",
270 qs[0], qs[1], qs[2], qs[3], qs[4], qs[5], qs[6], qs[7], qs[8], qs[9],
271 qs[10], qs[11], qs[12], qs[13], qs[14], GetQuantile(qs[0]),
272 GetQuantile(qs[1]), GetQuantile(qs[2]), GetQuantile(qs[3]),
273 GetQuantile(qs[4]), GetQuantile(qs[5]), GetQuantile(qs[6]),
274 GetQuantile(qs[7]), GetQuantile(qs[8]), GetQuantile(qs[9]),
275 GetQuantile(qs[10]), GetQuantile(qs[11]), GetQuantile(qs[12]),
276 GetQuantile(qs[13]), GetQuantile(qs[14]));
278 result_string += buffer;
279 memset(buffer, 0,
sizeof(buffer));
281 return result_string;
285 printf(
"%s", this->ToString().c_str());
288 #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()
const int const char * format
std::vector< atomic_int32 > GetBins(const Log2Histogram &h)