Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1068)

Side by Side Diff: base/metrics/histogram.cc

Issue 2867303004: [histogram] Make histograms more resistant to overflows. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Histogram is an object that aggregates statistics, and can summarize them in 5 // Histogram is an object that aggregates statistics, and can summarize them in
6 // various forms, including ASCII graphical, HTML, and numerically (as a 6 // various forms, including ASCII graphical, HTML, and numerically (as a
7 // vector of numbers corresponding to each of the aggregating buckets). 7 // vector of numbers corresponding to each of the aggregating buckets).
8 // See header file for details and examples. 8 // See header file for details and examples.
9 9
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 407
408 if (!check_okay) { 408 if (!check_okay) {
409 UMA_HISTOGRAM_SPARSE_SLOWLY("Histogram.BadConstructionArguments", 409 UMA_HISTOGRAM_SPARSE_SLOWLY("Histogram.BadConstructionArguments",
410 static_cast<Sample>(HashMetricName(name))); 410 static_cast<Sample>(HashMetricName(name)));
411 } 411 }
412 412
413 return check_okay; 413 return check_okay;
414 } 414 }
415 415
416 uint64_t Histogram::name_hash() const { 416 uint64_t Histogram::name_hash() const {
417 return samples_->id(); 417 return unlogged_samples_->id();
418 } 418 }
419 419
420 HistogramType Histogram::GetHistogramType() const { 420 HistogramType Histogram::GetHistogramType() const {
421 return HISTOGRAM; 421 return HISTOGRAM;
422 } 422 }
423 423
424 bool Histogram::HasConstructionArguments(Sample expected_minimum, 424 bool Histogram::HasConstructionArguments(Sample expected_minimum,
425 Sample expected_maximum, 425 Sample expected_maximum,
426 uint32_t expected_bucket_count) const { 426 uint32_t expected_bucket_count) const {
427 return ((expected_minimum == declared_min_) && 427 return ((expected_minimum == declared_min_) &&
(...skipping 10 matching lines...) Expand all
438 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count())); 438 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count()));
439 439
440 if (value > kSampleType_MAX - 1) 440 if (value > kSampleType_MAX - 1)
441 value = kSampleType_MAX - 1; 441 value = kSampleType_MAX - 1;
442 if (value < 0) 442 if (value < 0)
443 value = 0; 443 value = 0;
444 if (count <= 0) { 444 if (count <= 0) {
445 NOTREACHED(); 445 NOTREACHED();
446 return; 446 return;
447 } 447 }
448 samples_->Accumulate(value, count); 448 unlogged_samples_->Accumulate(value, count);
449 449
450 FindAndRunCallback(value); 450 FindAndRunCallback(value);
451 } 451 }
452 452
453 std::unique_ptr<HistogramSamples> Histogram::SnapshotSamples() const { 453 std::unique_ptr<HistogramSamples> Histogram::SnapshotSamples() const {
454 return SnapshotSampleVector(); 454 return SnapshotSampleVector();
455 } 455 }
456 456
457 std::unique_ptr<HistogramSamples> Histogram::SnapshotDelta() { 457 std::unique_ptr<HistogramSamples> Histogram::SnapshotDelta() {
458 DCHECK(!final_delta_created_); 458 DCHECK(!final_delta_created_);
459 459
460 std::unique_ptr<HistogramSamples> snapshot = SnapshotSampleVector(); 460 std::unique_ptr<HistogramSamples> snapshot =
461 if (!logged_samples_) { 461 CloneSampleVector(*unlogged_samples_);
462 // If nothing has been previously logged, save this one as 462 unlogged_samples_->Subtract(*snapshot);
463 // |logged_samples_| and gather another snapshot to return. 463 logged_samples_->Add(*snapshot);
Ilya Sherman 2017/05/09 22:15:32 I'm noticing that the samples in this vector could
altimin 2017/05/10 11:50:38 I can see three call sites to Histogram::SnapshotS
464 logged_samples_.swap(snapshot);
465 return SnapshotSampleVector();
466 }
467 464
468 // Subtract what was previously logged and update that information.
469 snapshot->Subtract(*logged_samples_);
470 logged_samples_->Add(*snapshot);
471 return snapshot; 465 return snapshot;
472 } 466 }
473 467
474 std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const { 468 std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const {
475 DCHECK(!final_delta_created_); 469 DCHECK(!final_delta_created_);
476 final_delta_created_ = true; 470 final_delta_created_ = true;
477 471
478 std::unique_ptr<HistogramSamples> snapshot = SnapshotSampleVector(); 472 return CloneSampleVector(*unlogged_samples_);
479
480 // Subtract what was previously logged and then return.
481 if (logged_samples_)
482 snapshot->Subtract(*logged_samples_);
483 return snapshot;
484 } 473 }
485 474
486 void Histogram::AddSamples(const HistogramSamples& samples) { 475 void Histogram::AddSamples(const HistogramSamples& samples) {
487 samples_->Add(samples); 476 unlogged_samples_->Add(samples);
488 } 477 }
489 478
490 bool Histogram::AddSamplesFromPickle(PickleIterator* iter) { 479 bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {
491 return samples_->AddFromPickle(iter); 480 return unlogged_samples_->AddFromPickle(iter);
492 } 481 }
493 482
494 // The following methods provide a graphical histogram display. 483 // The following methods provide a graphical histogram display.
495 void Histogram::WriteHTMLGraph(std::string* output) const { 484 void Histogram::WriteHTMLGraph(std::string* output) const {
496 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. 485 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
497 output->append("<PRE>"); 486 output->append("<PRE>");
498 WriteAsciiImpl(true, "<br>", output); 487 WriteAsciiImpl(true, "<br>", output);
499 output->append("</PRE>"); 488 output->append("</PRE>");
500 } 489 }
501 490
(...skipping 12 matching lines...) Expand all
514 } 503 }
515 504
516 Histogram::Histogram(const std::string& name, 505 Histogram::Histogram(const std::string& name,
517 Sample minimum, 506 Sample minimum,
518 Sample maximum, 507 Sample maximum,
519 const BucketRanges* ranges) 508 const BucketRanges* ranges)
520 : HistogramBase(name), 509 : HistogramBase(name),
521 bucket_ranges_(ranges), 510 bucket_ranges_(ranges),
522 declared_min_(minimum), 511 declared_min_(minimum),
523 declared_max_(maximum) { 512 declared_max_(maximum) {
524 if (ranges) 513 if (ranges) {
525 samples_.reset(new SampleVector(HashMetricName(name), ranges)); 514 unlogged_samples_.reset(new SampleVector(HashMetricName(name), ranges));
515 logged_samples_.reset(new SampleVector(HashMetricName(name), ranges));
Ilya Sherman 2017/05/09 22:15:32 nit: Could you avoid the second HashMetricName cal
altimin 2017/05/10 11:50:38 Done.
516 }
526 } 517 }
527 518
528 Histogram::Histogram(const std::string& name, 519 Histogram::Histogram(const std::string& name,
529 Sample minimum, 520 Sample minimum,
530 Sample maximum, 521 Sample maximum,
531 const BucketRanges* ranges, 522 const BucketRanges* ranges,
532 const DelayedPersistentAllocation& counts, 523 const DelayedPersistentAllocation& counts,
533 const DelayedPersistentAllocation& logged_counts, 524 const DelayedPersistentAllocation& logged_counts,
534 HistogramSamples::Metadata* meta, 525 HistogramSamples::Metadata* meta,
535 HistogramSamples::Metadata* logged_meta) 526 HistogramSamples::Metadata* logged_meta)
536 : HistogramBase(name), 527 : HistogramBase(name),
537 bucket_ranges_(ranges), 528 bucket_ranges_(ranges),
538 declared_min_(minimum), 529 declared_min_(minimum),
539 declared_max_(maximum) { 530 declared_max_(maximum) {
540 if (ranges) { 531 if (ranges) {
541 samples_.reset( 532 unlogged_samples_.reset(
542 new PersistentSampleVector(HashMetricName(name), ranges, meta, counts)); 533 new PersistentSampleVector(HashMetricName(name), ranges, meta, counts));
543 logged_samples_.reset(new PersistentSampleVector( 534 logged_samples_.reset(new PersistentSampleVector(
544 samples_->id(), ranges, logged_meta, logged_counts)); 535 unlogged_samples_->id(), ranges, logged_meta, logged_counts));
545 } 536 }
546 } 537 }
547 538
548 Histogram::~Histogram() { 539 Histogram::~Histogram() {
549 } 540 }
550 541
551 bool Histogram::PrintEmptyBucket(uint32_t index) const { 542 bool Histogram::PrintEmptyBucket(uint32_t index) const {
552 return true; 543 return true;
553 } 544 }
554 545
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 histogram_name, declared_min, declared_max, bucket_count, flags); 583 histogram_name, declared_min, declared_max, bucket_count, flags);
593 584
594 if (!ValidateRangeChecksum(*histogram, range_checksum)) { 585 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
595 // The serialized histogram might be corrupted. 586 // The serialized histogram might be corrupted.
596 return NULL; 587 return NULL;
597 } 588 }
598 return histogram; 589 return histogram;
599 } 590 }
600 591
601 std::unique_ptr<SampleVector> Histogram::SnapshotSampleVector() const { 592 std::unique_ptr<SampleVector> Histogram::SnapshotSampleVector() const {
602 std::unique_ptr<SampleVector> samples( 593 std::unique_ptr<SampleVector> samples = CloneSampleVector(*unlogged_samples_);
603 new SampleVector(samples_->id(), bucket_ranges())); 594 samples->Add(*logged_samples_);
604 samples->Add(*samples_);
605 return samples; 595 return samples;
606 } 596 }
607 597
598 std::unique_ptr<SampleVector> Histogram::CloneSampleVector(
Ilya Sherman 2017/05/09 22:15:32 nit: I'd name this SnapshotUnloggedSamples, and dr
altimin 2017/05/10 11:50:38 Nice, thanks! Done.
599 const HistogramSamples& original) const {
600 std::unique_ptr<SampleVector> samples =
601 base::MakeUnique<SampleVector>(original.id(), bucket_ranges());
602 samples->Add(original);
603 return samples;
604 }
605
608 void Histogram::WriteAsciiImpl(bool graph_it, 606 void Histogram::WriteAsciiImpl(bool graph_it,
609 const std::string& newline, 607 const std::string& newline,
610 std::string* output) const { 608 std::string* output) const {
611 // Get local (stack) copies of all effectively volatile class data so that we 609 // Get local (stack) copies of all effectively volatile class data so that we
612 // are consistent across our output activities. 610 // are consistent across our output activities.
613 std::unique_ptr<SampleVector> snapshot = SnapshotSampleVector(); 611 std::unique_ptr<SampleVector> snapshot = SnapshotSampleVector();
614 Count sample_count = snapshot->TotalCount(); 612 Count sample_count = snapshot->TotalCount();
615 613
616 WriteAsciiHeader(*snapshot, sample_count, output); 614 WriteAsciiHeader(*snapshot, sample_count, output);
617 output->append(newline); 615 output->append(newline);
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 Sample sample = custom_ranges[i]; 1201 Sample sample = custom_ranges[i];
1204 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) 1202 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
1205 return false; 1203 return false;
1206 if (sample != 0) 1204 if (sample != 0)
1207 has_valid_range = true; 1205 has_valid_range = true;
1208 } 1206 }
1209 return has_valid_range; 1207 return has_valid_range;
1210 } 1208 }
1211 1209
1212 } // namespace base 1210 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698