blob: 687a5496b2bd3e8b9965ec7da79ca66adff6b970 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5// Histogram is an object that aggregates statistics, and can summarize them in
6// various forms, including ASCII graphical, HTML, and numerically (as a
7// vector of numbers corresponding to each of the aggregating buckets).
8// See header file for details and examples.
9
10#include "base/histogram.h"
11
12#include <math.h>
13#include <string>
14
15#include "base/logging.h"
[email protected]3f383852009-04-03 18:18:5516#include "base/pickle.h"
initial.commitd7cae122008-07-26 21:49:3817#include "base/string_util.h"
18
[email protected]e1acf6f2008-10-27 20:43:3319using base::TimeDelta;
20
initial.commitd7cae122008-07-26 21:49:3821typedef Histogram::Count Count;
22
[email protected]b16ef312008-08-19 18:36:2323// static
24const int Histogram::kHexRangePrintingFlag = 0x8000;
25
[email protected]553dba62009-02-24 19:08:2326Histogram::Histogram(const char* name, Sample minimum,
initial.commitd7cae122008-07-26 21:49:3827 Sample maximum, size_t bucket_count)
[email protected]e6f02ab2009-04-10 22:29:2928 : histogram_name_(name),
initial.commitd7cae122008-07-26 21:49:3829 declared_min_(minimum),
30 declared_max_(maximum),
31 bucket_count_(bucket_count),
32 flags_(0),
33 ranges_(bucket_count + 1, 0),
34 sample_(),
35 registered_(false) {
36 Initialize();
37}
38
[email protected]553dba62009-02-24 19:08:2339Histogram::Histogram(const char* name, TimeDelta minimum,
initial.commitd7cae122008-07-26 21:49:3840 TimeDelta maximum, size_t bucket_count)
[email protected]e6f02ab2009-04-10 22:29:2941 : histogram_name_(name),
initial.commitd7cae122008-07-26 21:49:3842 declared_min_(static_cast<int> (minimum.InMilliseconds())),
43 declared_max_(static_cast<int> (maximum.InMilliseconds())),
44 bucket_count_(bucket_count),
45 flags_(0),
46 ranges_(bucket_count + 1, 0),
47 sample_(),
48 registered_(false) {
49 Initialize();
50}
51
52Histogram::~Histogram() {
53 if (registered_)
[email protected]55e57d42009-02-25 06:10:1754 StatisticsRecorder::UnRegister(this);
initial.commitd7cae122008-07-26 21:49:3855 // Just to make sure most derived class did this properly...
56 DCHECK(ValidateBucketRanges());
57}
58
initial.commitd7cae122008-07-26 21:49:3859void Histogram::Add(int value) {
60 if (!registered_)
[email protected]55e57d42009-02-25 06:10:1761 registered_ = StatisticsRecorder::Register(this);
initial.commitd7cae122008-07-26 21:49:3862 if (value >= kSampleType_MAX)
63 value = kSampleType_MAX - 1;
initial.commitd7cae122008-07-26 21:49:3864 if (value < 0)
65 value = 0;
66 size_t index = BucketIndex(value);
67 DCHECK(value >= ranges(index));
68 DCHECK(value < ranges(index + 1));
69 Accumulate(value, 1, index);
70}
71
[email protected]55e57d42009-02-25 06:10:1772void Histogram::AddSampleSet(const SampleSet& sample) {
73 sample_.Add(sample);
74}
75
initial.commitd7cae122008-07-26 21:49:3876// The following methods provide a graphical histogram display.
77void Histogram::WriteHTMLGraph(std::string* output) const {
78 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
79 output->append("<PRE>");
80 WriteAscii(true, "<br>", output);
81 output->append("</PRE>");
82}
83
84void Histogram::WriteAscii(bool graph_it, const std::string& newline,
85 std::string* output) const {
86 // Get local (stack) copies of all effectively volatile class data so that we
87 // are consistent across our output activities.
88 SampleSet snapshot;
89 SnapshotSample(&snapshot);
90 Count sample_count = snapshot.TotalCount();
91
92 WriteAsciiHeader(snapshot, sample_count, output);
93 output->append(newline);
94
95 // Prepare to normalize graphical rendering of bucket contents.
96 double max_size = 0;
97 if (graph_it)
98 max_size = GetPeakBucketSize(snapshot);
99
100 // Calculate space needed to print bucket range numbers. Leave room to print
101 // nearly the largest bucket range without sliding over the histogram.
[email protected]e2951cf2008-09-24 23:51:25102 size_t largest_non_empty_bucket = bucket_count() - 1;
103 while (0 == snapshot.counts(largest_non_empty_bucket)) {
initial.commitd7cae122008-07-26 21:49:38104 if (0 == largest_non_empty_bucket)
105 break; // All buckets are empty.
[email protected]55e57d42009-02-25 06:10:17106 --largest_non_empty_bucket;
initial.commitd7cae122008-07-26 21:49:38107 }
108
109 // Calculate largest print width needed for any of our bucket range displays.
110 size_t print_width = 1;
[email protected]e2951cf2008-09-24 23:51:25111 for (size_t i = 0; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38112 if (snapshot.counts(i)) {
113 size_t width = GetAsciiBucketRange(i).size() + 1;
114 if (width > print_width)
115 print_width = width;
116 }
117 }
118
119 int64 remaining = sample_count;
120 int64 past = 0;
121 // Output the actual histogram graph.
[email protected]55e57d42009-02-25 06:10:17122 for (size_t i = 0; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38123 Count current = snapshot.counts(i);
124 if (!current && !PrintEmptyBucket(i))
125 continue;
126 remaining -= current;
127 StringAppendF(output, "%#*s ", print_width, GetAsciiBucketRange(i).c_str());
[email protected]e2951cf2008-09-24 23:51:25128 if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) {
129 while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1))
[email protected]55e57d42009-02-25 06:10:17130 ++i;
initial.commitd7cae122008-07-26 21:49:38131 output->append("... ");
132 output->append(newline);
133 continue; // No reason to plot emptiness.
134 }
135 double current_size = GetBucketSize(current, i);
136 if (graph_it)
137 WriteAsciiBucketGraph(current_size, max_size, output);
138 WriteAsciiBucketContext(past, current, remaining, i, output);
139 output->append(newline);
140 past += current;
141 }
142 DCHECK(past == sample_count);
143}
144
145bool Histogram::ValidateBucketRanges() const {
146 // Standard assertions that all bucket ranges should satisfy.
147 DCHECK(ranges_.size() == bucket_count_ + 1);
148 DCHECK(0 == ranges_[0]);
149 DCHECK(declared_min() == ranges_[1]);
150 DCHECK(declared_max() == ranges_[bucket_count_ - 1]);
151 DCHECK(kSampleType_MAX == ranges_[bucket_count_]);
152 return true;
153}
154
155void Histogram::Initialize() {
156 sample_.Resize(*this);
157 if (declared_min_ <= 0)
158 declared_min_ = 1;
159 if (declared_max_ >= kSampleType_MAX)
160 declared_max_ = kSampleType_MAX - 1;
161 DCHECK(declared_min_ > 0); // We provide underflow bucket.
[email protected]e2951cf2008-09-24 23:51:25162 DCHECK(declared_min_ <= declared_max_);
initial.commitd7cae122008-07-26 21:49:38163 DCHECK(1 < bucket_count_);
164 size_t maximal_bucket_count = declared_max_ - declared_min_ + 2;
165 DCHECK(bucket_count_ <= maximal_bucket_count);
166 DCHECK(0 == ranges_[0]);
167 ranges_[bucket_count_] = kSampleType_MAX;
168 InitializeBucketRange();
169 DCHECK(ValidateBucketRanges());
[email protected]55e57d42009-02-25 06:10:17170 registered_ = StatisticsRecorder::Register(this);
initial.commitd7cae122008-07-26 21:49:38171}
172
173// Calculate what range of values are held in each bucket.
174// We have to be careful that we don't pick a ratio between starting points in
175// consecutive buckets that is sooo small, that the integer bounds are the same
176// (effectively making one bucket get no values). We need to avoid:
177// (ranges_[i] == ranges_[i + 1]
178// To avoid that, we just do a fine-grained bucket width as far as we need to
179// until we get a ratio that moves us along at least 2 units at a time. From
180// that bucket onward we do use the exponential growth of buckets.
181void Histogram::InitializeBucketRange() {
182 double log_max = log(static_cast<double>(declared_max()));
183 double log_ratio;
184 double log_next;
185 size_t bucket_index = 1;
186 Sample current = declared_min();
187 SetBucketRange(bucket_index, current);
188 while (bucket_count() > ++bucket_index) {
189 double log_current;
190 log_current = log(static_cast<double>(current));
191 // Calculate the count'th root of the range.
192 log_ratio = (log_max - log_current) / (bucket_count() - bucket_index);
193 // See where the next bucket would start.
194 log_next = log_current + log_ratio;
195 int next;
196 next = static_cast<int>(floor(exp(log_next) + 0.5));
197 if (next > current)
198 current = next;
199 else
[email protected]55e57d42009-02-25 06:10:17200 ++current; // Just do a narrow bucket, and keep trying.
initial.commitd7cae122008-07-26 21:49:38201 SetBucketRange(bucket_index, current);
202 }
203
204 DCHECK(bucket_count() == bucket_index);
205}
206
207size_t Histogram::BucketIndex(Sample value) const {
208 // Use simple binary search. This is very general, but there are better
209 // approaches if we knew that the buckets were linearly distributed.
210 DCHECK(ranges(0) <= value);
211 DCHECK(ranges(bucket_count()) > value);
212 size_t under = 0;
213 size_t over = bucket_count();
214 size_t mid;
215
216 do {
217 DCHECK(over >= under);
218 mid = (over + under)/2;
219 if (mid == under)
220 break;
221 if (ranges(mid) <= value)
222 under = mid;
223 else
224 over = mid;
225 } while (true);
226
227 DCHECK(ranges(mid) <= value && ranges(mid+1) > value);
228 return mid;
229}
230
231// Use the actual bucket widths (like a linear histogram) until the widths get
232// over some transition value, and then use that transition width. Exponentials
233// get so big so fast (and we don't expect to see a lot of entries in the large
234// buckets), so we need this to make it possible to see what is going on and
235// not have 0-graphical-height buckets.
236double Histogram::GetBucketSize(Count current, size_t i) const {
237 DCHECK(ranges(i + 1) > ranges(i));
238 static const double kTransitionWidth = 5;
239 double denominator = ranges(i + 1) - ranges(i);
240 if (denominator > kTransitionWidth)
241 denominator = kTransitionWidth; // Stop trying to normalize.
242 return current/denominator;
243}
244
245//------------------------------------------------------------------------------
246// The following two methods can be overridden to provide a thread safe
247// version of this class. The cost of locking is low... but an error in each
248// of these methods has minimal impact. For now, I'll leave this unlocked,
249// and I don't believe I can loose more than a count or two.
250// The vectors are NOT reallocated, so there is no risk of them moving around.
251
252// Update histogram data with new sample.
253void Histogram::Accumulate(Sample value, Count count, size_t index) {
254 // Note locking not done in this version!!!
255 sample_.Accumulate(value, count, index);
256}
257
258// Do a safe atomic snapshot of sample data.
259// This implementation assumes we are on a safe single thread.
260void Histogram::SnapshotSample(SampleSet* sample) const {
261 // Note locking not done in this version!!!
262 *sample = sample_;
263}
264
265//------------------------------------------------------------------------------
266// Accessor methods
267
268void Histogram::SetBucketRange(size_t i, Sample value) {
269 DCHECK(bucket_count_ > i);
270 ranges_[i] = value;
271}
272
273//------------------------------------------------------------------------------
274// Private methods
275
276double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const {
277 double max = 0;
[email protected]55e57d42009-02-25 06:10:17278 for (size_t i = 0; i < bucket_count() ; ++i) {
initial.commitd7cae122008-07-26 21:49:38279 double current_size = GetBucketSize(snapshot.counts(i), i);
280 if (current_size > max)
281 max = current_size;
282 }
283 return max;
284}
285
286void Histogram::WriteAsciiHeader(const SampleSet& snapshot,
287 Count sample_count,
288 std::string* output) const {
289 StringAppendF(output,
290 "Histogram: %s recorded %ld samples",
291 histogram_name().c_str(),
292 sample_count);
293 if (0 == sample_count) {
294 DCHECK(0 == snapshot.sum());
295 } else {
296 double average = static_cast<float>(snapshot.sum()) / sample_count;
297 double variance = static_cast<float>(snapshot.square_sum())/sample_count
298 - average * average;
299 double standard_deviation = sqrt(variance);
300
301 StringAppendF(output,
302 ", average = %.1f, standard deviation = %.1f",
303 average, standard_deviation);
304 }
305 if (flags_ & ~kHexRangePrintingFlag )
306 StringAppendF(output, " (flags = 0x%x)", flags_ & ~kHexRangePrintingFlag);
307}
308
309void Histogram::WriteAsciiBucketContext(const int64 past,
310 const Count current,
311 const int64 remaining,
312 const size_t i,
313 std::string* output) const {
314 double scaled_sum = (past + current + remaining) / 100.0;
315 WriteAsciiBucketValue(current, scaled_sum, output);
316 if (0 < i) {
317 double percentage = past / scaled_sum;
318 StringAppendF(output, " {%3.1f%%}", percentage);
319 }
320}
321
322const std::string Histogram::GetAsciiBucketRange(size_t i) const {
323 std::string result;
324 if (kHexRangePrintingFlag & flags_)
[email protected]e2951cf2008-09-24 23:51:25325 StringAppendF(&result, "%#x", ranges(i));
initial.commitd7cae122008-07-26 21:49:38326 else
[email protected]e2951cf2008-09-24 23:51:25327 StringAppendF(&result, "%d", ranges(i));
initial.commitd7cae122008-07-26 21:49:38328 return result;
329}
330
331void Histogram::WriteAsciiBucketValue(Count current, double scaled_sum,
332 std::string* output) const {
333 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
334}
335
336void Histogram::WriteAsciiBucketGraph(double current_size, double max_size,
337 std::string* output) const {
338 const int k_line_length = 72; // Maximal horizontal width of graph.
339 int x_count = static_cast<int>(k_line_length * (current_size / max_size)
340 + 0.5);
341 int x_remainder = k_line_length - x_count;
342
343 while (0 < x_count--)
344 output->append("-");
345 output->append("O");
346 while (0 < x_remainder--)
347 output->append(" ");
348}
349
[email protected]55e57d42009-02-25 06:10:17350// static
351std::string Histogram::SerializeHistogramInfo(const Histogram& histogram,
352 const SampleSet& snapshot) {
353 Pickle pickle;
354
355 pickle.WriteString(histogram.histogram_name());
356 pickle.WriteInt(histogram.declared_min());
357 pickle.WriteInt(histogram.declared_max());
358 pickle.WriteSize(histogram.bucket_count());
359 pickle.WriteInt(histogram.histogram_type());
360 pickle.WriteInt(histogram.flags());
361
362 snapshot.Serialize(&pickle);
363 return std::string(static_cast<const char*>(pickle.data()), pickle.size());
364}
365
366// static
367void Histogram::DeserializeHistogramList(
368 const std::vector<std::string>& histograms) {
369 for (std::vector<std::string>::const_iterator it = histograms.begin();
370 it < histograms.end();
371 ++it) {
372 DeserializeHistogramInfo(*it);
373 }
374}
375
376// static
377bool Histogram::DeserializeHistogramInfo(const std::string& histogram_info) {
378 if (histogram_info.empty()) {
379 return false;
380 }
381
382 Pickle pickle(histogram_info.data(),
383 static_cast<int>(histogram_info.size()));
384 void* iter = NULL;
385 size_t bucket_count;
386 int declared_min;
387 int declared_max;
388 int histogram_type;
389 int flags;
390 std::string histogram_name;
391 SampleSet sample;
392
393 if (!pickle.ReadString(&iter, &histogram_name) ||
394 !pickle.ReadInt(&iter, &declared_min) ||
395 !pickle.ReadInt(&iter, &declared_max) ||
396 !pickle.ReadSize(&iter, &bucket_count) ||
397 !pickle.ReadInt(&iter, &histogram_type) ||
398 !pickle.ReadInt(&iter, &flags) ||
399 !sample.Histogram::SampleSet::Deserialize(&iter, pickle)) {
400 LOG(ERROR) << "Picke error decoding Histogram: " << histogram_name;
401 return false;
402 }
403
404 Histogram* render_histogram =
405 StatisticsRecorder::GetHistogram(histogram_name);
406
407 if (render_histogram == NULL) {
408 if (histogram_type == EXPONENTIAL) {
409 render_histogram = new Histogram(histogram_name.c_str(),
410 declared_min,
411 declared_max,
412 bucket_count);
413 } else if (histogram_type == LINEAR) {
414 render_histogram = reinterpret_cast<Histogram*>
415 (new LinearHistogram(histogram_name.c_str(),
416 declared_min,
417 declared_max,
418 bucket_count));
419 } else {
420 LOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: " <<
421 histogram_type;
422 return false;
423 }
424 DCHECK(!(flags & kRendererHistogramFlag));
425 render_histogram->SetFlags(flags | kRendererHistogramFlag);
426 }
427
428 DCHECK(declared_min == render_histogram->declared_min());
429 DCHECK(declared_max == render_histogram->declared_max());
430 DCHECK(bucket_count == render_histogram->bucket_count());
431 DCHECK(histogram_type == render_histogram->histogram_type());
432
433 if (render_histogram->flags() & kRendererHistogramFlag) {
434 render_histogram->AddSampleSet(sample);
435 } else {
436 DLOG(INFO) << "Single thread mode, histogram observed and not copied: " <<
437 histogram_name;
438 }
439
440 return true;
441}
442
443
initial.commitd7cae122008-07-26 21:49:38444//------------------------------------------------------------------------------
445// Methods for the Histogram::SampleSet class
446//------------------------------------------------------------------------------
447
448Histogram::SampleSet::SampleSet()
449 : counts_(),
450 sum_(0),
451 square_sum_(0) {
452}
453
454void Histogram::SampleSet::Resize(const Histogram& histogram) {
455 counts_.resize(histogram.bucket_count(), 0);
456}
457
458void Histogram::SampleSet::CheckSize(const Histogram& histogram) const {
459 DCHECK(counts_.size() == histogram.bucket_count());
460}
461
462
463void Histogram::SampleSet::Accumulate(Sample value, Count count,
464 size_t index) {
465 DCHECK(count == 1 || count == -1);
466 counts_[index] += count;
467 sum_ += count * value;
468 square_sum_ += (count * value) * static_cast<int64>(value);
469 DCHECK(counts_[index] >= 0);
470 DCHECK(sum_ >= 0);
471 DCHECK(square_sum_ >= 0);
472}
473
474Count Histogram::SampleSet::TotalCount() const {
475 Count total = 0;
476 for (Counts::const_iterator it = counts_.begin();
477 it != counts_.end();
[email protected]55e57d42009-02-25 06:10:17478 ++it) {
initial.commitd7cae122008-07-26 21:49:38479 total += *it;
480 }
481 return total;
482}
483
484void Histogram::SampleSet::Add(const SampleSet& other) {
485 DCHECK(counts_.size() == other.counts_.size());
486 sum_ += other.sum_;
487 square_sum_ += other.square_sum_;
[email protected]55e57d42009-02-25 06:10:17488 for (size_t index = 0; index < counts_.size(); ++index)
initial.commitd7cae122008-07-26 21:49:38489 counts_[index] += other.counts_[index];
490}
491
492void Histogram::SampleSet::Subtract(const SampleSet& other) {
493 DCHECK(counts_.size() == other.counts_.size());
494 // Note: Race conditions in snapshotting a sum or square_sum may lead to
495 // (temporary) negative values when snapshots are later combined (and deltas
496 // calculated). As a result, we don't currently CHCEK() for positive values.
497 sum_ -= other.sum_;
498 square_sum_ -= other.square_sum_;
[email protected]55e57d42009-02-25 06:10:17499 for (size_t index = 0; index < counts_.size(); ++index) {
initial.commitd7cae122008-07-26 21:49:38500 counts_[index] -= other.counts_[index];
501 DCHECK(counts_[index] >= 0);
502 }
503}
504
[email protected]55e57d42009-02-25 06:10:17505bool Histogram::SampleSet::Serialize(Pickle* pickle) const {
506 pickle->WriteInt64(sum_);
507 pickle->WriteInt64(square_sum_);
508 pickle->WriteSize(counts_.size());
509
510 for (size_t index = 0; index < counts_.size(); ++index) {
511 pickle->WriteInt(counts_[index]);
512 }
513
514 return true;
515}
516
517bool Histogram::SampleSet::Deserialize(void** iter, const Pickle& pickle) {
518 DCHECK(counts_.size() == 0);
519 DCHECK(sum_ == 0);
520 DCHECK(square_sum_ == 0);
521
522 size_t counts_size;
523
524 if (!pickle.ReadInt64(iter, &sum_) ||
525 !pickle.ReadInt64(iter, &square_sum_) ||
526 !pickle.ReadSize(iter, &counts_size)) {
527 return false;
528 }
529
530 if (counts_size <= 0)
531 return false;
532
533 counts_.resize(counts_size, 0);
534 for (size_t index = 0; index < counts_size; ++index) {
535 if (!pickle.ReadInt(iter, &counts_[index])) {
536 return false;
537 }
538 }
539
540 return true;
541}
542
initial.commitd7cae122008-07-26 21:49:38543//------------------------------------------------------------------------------
544// LinearHistogram: This histogram uses a traditional set of evenly spaced
545// buckets.
546//------------------------------------------------------------------------------
547
[email protected]55e57d42009-02-25 06:10:17548LinearHistogram::LinearHistogram(const char* name, Sample minimum,
549 Sample maximum, size_t bucket_count)
initial.commitd7cae122008-07-26 21:49:38550 : Histogram(name, minimum >= 1 ? minimum : 1, maximum, bucket_count) {
551 InitializeBucketRange();
552 DCHECK(ValidateBucketRanges());
553}
554
[email protected]553dba62009-02-24 19:08:23555LinearHistogram::LinearHistogram(const char* name,
initial.commitd7cae122008-07-26 21:49:38556 TimeDelta minimum, TimeDelta maximum, size_t bucket_count)
557 : Histogram(name, minimum >= TimeDelta::FromMilliseconds(1) ?
558 minimum : TimeDelta::FromMilliseconds(1),
559 maximum, bucket_count) {
560 // Do a "better" (different) job at init than a base classes did...
561 InitializeBucketRange();
562 DCHECK(ValidateBucketRanges());
563}
564
[email protected]5059e3a2009-01-14 16:01:03565void LinearHistogram::SetRangeDescriptions(
566 const DescriptionPair descriptions[]) {
initial.commitd7cae122008-07-26 21:49:38567 for (int i =0; descriptions[i].description; ++i) {
568 bucket_description_[descriptions[i].sample] = descriptions[i].description;
569 }
570}
571
572const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
573 int range = ranges(i);
574 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
575 if (it == bucket_description_.end())
576 return Histogram::GetAsciiBucketRange(i);
577 return it->second;
578}
579
580bool LinearHistogram::PrintEmptyBucket(size_t index) const {
581 return bucket_description_.find(ranges(index)) == bucket_description_.end();
582}
583
584
585void LinearHistogram::InitializeBucketRange() {
586 DCHECK(0 < declared_min()); // 0 is the underflow bucket here.
587 double min = declared_min();
588 double max = declared_max();
589 size_t i;
[email protected]55e57d42009-02-25 06:10:17590 for (i = 1; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38591 double linear_range = (min * (bucket_count() -1 - i) + max * (i - 1)) /
592 (bucket_count() - 2);
593 SetBucketRange(i, static_cast<int> (linear_range + 0.5));
594 }
595}
596
597// Find bucket to increment for sample value.
598size_t LinearHistogram::BucketIndex(Sample value) const {
599 if (value < declared_min()) return 0;
600 if (value >= declared_max()) return bucket_count() - 1;
601 size_t index;
602 index = static_cast<size_t>(((value - declared_min()) * (bucket_count() - 2))
603 / (declared_max() - declared_min()) + 1);
604 DCHECK(1 <= index && bucket_count() > index);
605 return index;
606}
607
608double LinearHistogram::GetBucketSize(Count current, size_t i) const {
609 DCHECK(ranges(i + 1) > ranges(i));
610 // Adjacent buckets with different widths would have "surprisingly" many (few)
611 // samples in a histogram if we didn't normalize this way.
612 double denominator = ranges(i + 1) - ranges(i);
613 return current/denominator;
614}
615
616//------------------------------------------------------------------------------
617// This section provides implementation for ThreadSafeHistogram.
618//------------------------------------------------------------------------------
619
[email protected]553dba62009-02-24 19:08:23620ThreadSafeHistogram::ThreadSafeHistogram(const char* name, Sample minimum,
initial.commitd7cae122008-07-26 21:49:38621 Sample maximum, size_t bucket_count)
622 : Histogram(name, minimum, maximum, bucket_count),
623 lock_() {
624 }
625
626void ThreadSafeHistogram::Remove(int value) {
627 if (value >= kSampleType_MAX)
628 value = kSampleType_MAX - 1;
initial.commitd7cae122008-07-26 21:49:38629 size_t index = BucketIndex(value);
630 Accumulate(value, -1, index);
631}
632
633void ThreadSafeHistogram::Accumulate(Sample value, Count count, size_t index) {
634 AutoLock lock(lock_);
635 Histogram::Accumulate(value, count, index);
636}
637
[email protected]6df40742009-03-19 22:24:50638void ThreadSafeHistogram::SnapshotSample(SampleSet* sample) const {
initial.commitd7cae122008-07-26 21:49:38639 AutoLock lock(lock_);
640 Histogram::SnapshotSample(sample);
641};
642
643
644//------------------------------------------------------------------------------
645// The next section handles global (central) support for all histograms, as well
646// as startup/teardown of this service.
647//------------------------------------------------------------------------------
648
649// This singleton instance should be started during the single threaded portion
650// of main(), and hence it is not thread safe. It initializes globals to
651// provide support for all future calls.
652StatisticsRecorder::StatisticsRecorder() {
653 DCHECK(!histograms_);
654 lock_ = new Lock;
655 histograms_ = new HistogramMap;
656}
657
658StatisticsRecorder::~StatisticsRecorder() {
659 DCHECK(histograms_);
660
661 if (dump_on_exit_) {
662 std::string output;
663 WriteGraph("", &output);
664 LOG(INFO) << output;
665 }
666
667 // Clean up.
668 delete histograms_;
669 histograms_ = NULL;
670 delete lock_;
671 lock_ = NULL;
672}
673
674// static
675bool StatisticsRecorder::WasStarted() {
676 return NULL != histograms_;
677}
678
679// static
[email protected]55e57d42009-02-25 06:10:17680bool StatisticsRecorder::Register(Histogram* histogram) {
initial.commitd7cae122008-07-26 21:49:38681 if (!histograms_)
682 return false;
[email protected]55e57d42009-02-25 06:10:17683 const std::string name = histogram->histogram_name();
initial.commitd7cae122008-07-26 21:49:38684 AutoLock auto_lock(*lock_);
[email protected]5059e3a2009-01-14 16:01:03685
[email protected]2b65dc32009-01-15 20:45:59686 DCHECK(histograms_->end() == histograms_->find(name)) << name << " is already"
687 "registered as a histogram. Check for duplicate use of the name, or a "
688 "race where a static initializer could be run by several threads.";
[email protected]55e57d42009-02-25 06:10:17689 (*histograms_)[name] = histogram;
initial.commitd7cae122008-07-26 21:49:38690 return true;
691}
692
693// static
[email protected]55e57d42009-02-25 06:10:17694void StatisticsRecorder::UnRegister(Histogram* histogram) {
initial.commitd7cae122008-07-26 21:49:38695 if (!histograms_)
696 return;
[email protected]55e57d42009-02-25 06:10:17697 const std::string name = histogram->histogram_name();
initial.commitd7cae122008-07-26 21:49:38698 AutoLock auto_lock(*lock_);
699 DCHECK(histograms_->end() != histograms_->find(name));
700 histograms_->erase(name);
701 if (dump_on_exit_) {
702 std::string output;
[email protected]55e57d42009-02-25 06:10:17703 histogram->WriteAscii(true, "\n", &output);
initial.commitd7cae122008-07-26 21:49:38704 LOG(INFO) << output;
705 }
706}
707
708// static
709void StatisticsRecorder::WriteHTMLGraph(const std::string& query,
710 std::string* output) {
711 if (!histograms_)
712 return;
713 output->append("<html><head><title>About Histograms");
714 if (!query.empty())
715 output->append(" - " + query);
716 output->append("</title>"
717 // We'd like the following no-cache... but it doesn't work.
718 // "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"
719 "</head><body>");
720
721 Histograms snapshot;
722 GetSnapshot(query, &snapshot);
723 for (Histograms::iterator it = snapshot.begin();
724 it != snapshot.end();
[email protected]55e57d42009-02-25 06:10:17725 ++it) {
initial.commitd7cae122008-07-26 21:49:38726 (*it)->WriteHTMLGraph(output);
727 output->append("<br><hr><br>");
728 }
729 output->append("</body></html>");
730}
731
732// static
733void StatisticsRecorder::WriteGraph(const std::string& query,
[email protected]55e57d42009-02-25 06:10:17734 std::string* output) {
initial.commitd7cae122008-07-26 21:49:38735 if (!histograms_)
736 return;
737 if (query.length())
738 StringAppendF(output, "Collections of histograms for %s\n", query.c_str());
739 else
740 output->append("Collections of all histograms\n");
741
742 Histograms snapshot;
743 GetSnapshot(query, &snapshot);
744 for (Histograms::iterator it = snapshot.begin();
745 it != snapshot.end();
[email protected]55e57d42009-02-25 06:10:17746 ++it) {
initial.commitd7cae122008-07-26 21:49:38747 (*it)->WriteAscii(true, "\n", output);
748 output->append("\n");
749 }
750}
751
752// static
753void StatisticsRecorder::GetHistograms(Histograms* output) {
754 if (!histograms_)
755 return;
756 AutoLock auto_lock(*lock_);
757 for (HistogramMap::iterator it = histograms_->begin();
758 histograms_->end() != it;
[email protected]55e57d42009-02-25 06:10:17759 ++it) {
initial.commitd7cae122008-07-26 21:49:38760 output->push_back(it->second);
761 }
762}
763
[email protected]55e57d42009-02-25 06:10:17764Histogram* StatisticsRecorder::GetHistogram(const std::string& query) {
765 if (!histograms_)
766 return NULL;
767 AutoLock auto_lock(*lock_);
768 for (HistogramMap::iterator it = histograms_->begin();
769 histograms_->end() != it;
770 ++it) {
771 if (it->first.find(query) != std::string::npos)
772 return it->second;
773 }
774 return NULL;
775}
776
initial.commitd7cae122008-07-26 21:49:38777// private static
778void StatisticsRecorder::GetSnapshot(const std::string& query,
779 Histograms* snapshot) {
780 AutoLock auto_lock(*lock_);
781 for (HistogramMap::iterator it = histograms_->begin();
782 histograms_->end() != it;
[email protected]55e57d42009-02-25 06:10:17783 ++it) {
initial.commitd7cae122008-07-26 21:49:38784 if (it->first.find(query) != std::string::npos)
785 snapshot->push_back(it->second);
786 }
787}
788
789// static
790StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL;
791// static
792Lock* StatisticsRecorder::lock_ = NULL;
793// static
794bool StatisticsRecorder::dump_on_exit_ = false;