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

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

Issue 2867303004: [histogram] Make histograms more resistant to overflows. (Closed)
Patch Set: addressed comments from bcwhite@ 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 = SnapshotUnloggedSamples();
461 if (!logged_samples_) { 461 unlogged_samples_->Subtract(*snapshot);
Alexei Svitkine (slow) 2017/05/10 16:55:38 So logic difference here was that before it was:
altimin 2017/05/11 12:38:33 Please take a look.
bcwhite 2017/05/11 17:34:52 It's okay in that everything is still "eventually
462 // If nothing has been previously logged, save this one as 462 logged_samples_->Add(*snapshot);
463 // |logged_samples_| and gather another snapshot to return.
464 logged_samples_.swap(snapshot);
465 return SnapshotSampleVector();
466 }
467 463
468 // Subtract what was previously logged and update that information.
469 snapshot->Subtract(*logged_samples_);
470 logged_samples_->Add(*snapshot);
471 return snapshot; 464 return snapshot;
472 } 465 }
473 466
474 std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const { 467 std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const {
475 DCHECK(!final_delta_created_); 468 DCHECK(!final_delta_created_);
476 final_delta_created_ = true; 469 final_delta_created_ = true;
477 470
478 std::unique_ptr<HistogramSamples> snapshot = SnapshotSampleVector(); 471 return SnapshotUnloggedSamples();
479
480 // Subtract what was previously logged and then return.
481 if (logged_samples_)
482 snapshot->Subtract(*logged_samples_);
483 return snapshot;
484 } 472 }
485 473
486 void Histogram::AddSamples(const HistogramSamples& samples) { 474 void Histogram::AddSamples(const HistogramSamples& samples) {
487 samples_->Add(samples); 475 unlogged_samples_->Add(samples);
488 } 476 }
489 477
490 bool Histogram::AddSamplesFromPickle(PickleIterator* iter) { 478 bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {
491 return samples_->AddFromPickle(iter); 479 return unlogged_samples_->AddFromPickle(iter);
492 } 480 }
493 481
494 // The following methods provide a graphical histogram display. 482 // The following methods provide a graphical histogram display.
495 void Histogram::WriteHTMLGraph(std::string* output) const { 483 void Histogram::WriteHTMLGraph(std::string* output) const {
496 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. 484 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
497 output->append("<PRE>"); 485 output->append("<PRE>");
498 WriteAsciiImpl(true, "<br>", output); 486 WriteAsciiImpl(true, "<br>", output);
499 output->append("</PRE>"); 487 output->append("</PRE>");
500 } 488 }
501 489
(...skipping 12 matching lines...) Expand all
514 } 502 }
515 503
516 Histogram::Histogram(const std::string& name, 504 Histogram::Histogram(const std::string& name,
517 Sample minimum, 505 Sample minimum,
518 Sample maximum, 506 Sample maximum,
519 const BucketRanges* ranges) 507 const BucketRanges* ranges)
520 : HistogramBase(name), 508 : HistogramBase(name),
521 bucket_ranges_(ranges), 509 bucket_ranges_(ranges),
522 declared_min_(minimum), 510 declared_min_(minimum),
523 declared_max_(maximum) { 511 declared_max_(maximum) {
524 if (ranges) 512 if (ranges) {
525 samples_.reset(new SampleVector(HashMetricName(name), ranges)); 513 unlogged_samples_.reset(new SampleVector(HashMetricName(name), ranges));
514 logged_samples_.reset(new SampleVector(unlogged_samples_->id(), ranges));
515 }
526 } 516 }
527 517
528 Histogram::Histogram(const std::string& name, 518 Histogram::Histogram(const std::string& name,
529 Sample minimum, 519 Sample minimum,
530 Sample maximum, 520 Sample maximum,
531 const BucketRanges* ranges, 521 const BucketRanges* ranges,
532 const DelayedPersistentAllocation& counts, 522 const DelayedPersistentAllocation& counts,
533 const DelayedPersistentAllocation& logged_counts, 523 const DelayedPersistentAllocation& logged_counts,
534 HistogramSamples::Metadata* meta, 524 HistogramSamples::Metadata* meta,
535 HistogramSamples::Metadata* logged_meta) 525 HistogramSamples::Metadata* logged_meta)
536 : HistogramBase(name), 526 : HistogramBase(name),
537 bucket_ranges_(ranges), 527 bucket_ranges_(ranges),
538 declared_min_(minimum), 528 declared_min_(minimum),
539 declared_max_(maximum) { 529 declared_max_(maximum) {
540 if (ranges) { 530 if (ranges) {
541 samples_.reset( 531 unlogged_samples_.reset(
542 new PersistentSampleVector(HashMetricName(name), ranges, meta, counts)); 532 new PersistentSampleVector(HashMetricName(name), ranges, meta, counts));
543 logged_samples_.reset(new PersistentSampleVector( 533 logged_samples_.reset(new PersistentSampleVector(
544 samples_->id(), ranges, logged_meta, logged_counts)); 534 unlogged_samples_->id(), ranges, logged_meta, logged_counts));
545 } 535 }
546 } 536 }
547 537
548 Histogram::~Histogram() { 538 Histogram::~Histogram() {
549 } 539 }
550 540
551 bool Histogram::PrintEmptyBucket(uint32_t index) const { 541 bool Histogram::PrintEmptyBucket(uint32_t index) const {
552 return true; 542 return true;
553 } 543 }
554 544
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 histogram_name, declared_min, declared_max, bucket_count, flags); 582 histogram_name, declared_min, declared_max, bucket_count, flags);
593 583
594 if (!ValidateRangeChecksum(*histogram, range_checksum)) { 584 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
595 // The serialized histogram might be corrupted. 585 // The serialized histogram might be corrupted.
596 return NULL; 586 return NULL;
597 } 587 }
598 return histogram; 588 return histogram;
599 } 589 }
600 590
601 std::unique_ptr<SampleVector> Histogram::SnapshotSampleVector() const { 591 std::unique_ptr<SampleVector> Histogram::SnapshotSampleVector() const {
602 std::unique_ptr<SampleVector> samples( 592 std::unique_ptr<SampleVector> samples = SnapshotUnloggedSamples();
603 new SampleVector(samples_->id(), bucket_ranges())); 593 samples->Add(*logged_samples_);
604 samples->Add(*samples_);
605 return samples; 594 return samples;
606 } 595 }
607 596
597 std::unique_ptr<SampleVector> Histogram::SnapshotUnloggedSamples() const {
598 std::unique_ptr<SampleVector> samples =
599 base::MakeUnique<SampleVector>(unlogged_samples_->id(), bucket_ranges());
Alexei Svitkine (slow) 2017/05/10 16:55:38 Nit: base::MakeUnique doesn't seem to add much her
altimin 2017/05/11 12:38:33 Done.
bcwhite 2017/05/11 17:34:52 I've been told that its desirable to always use Ma
600 samples->Add(*unlogged_samples_);
601 return samples;
602 }
603
608 void Histogram::WriteAsciiImpl(bool graph_it, 604 void Histogram::WriteAsciiImpl(bool graph_it,
609 const std::string& newline, 605 const std::string& newline,
610 std::string* output) const { 606 std::string* output) const {
611 // Get local (stack) copies of all effectively volatile class data so that we 607 // Get local (stack) copies of all effectively volatile class data so that we
612 // are consistent across our output activities. 608 // are consistent across our output activities.
613 std::unique_ptr<SampleVector> snapshot = SnapshotSampleVector(); 609 std::unique_ptr<SampleVector> snapshot = SnapshotSampleVector();
614 Count sample_count = snapshot->TotalCount(); 610 Count sample_count = snapshot->TotalCount();
615 611
616 WriteAsciiHeader(*snapshot, sample_count, output); 612 WriteAsciiHeader(*snapshot, sample_count, output);
617 output->append(newline); 613 output->append(newline);
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 Sample sample = custom_ranges[i]; 1199 Sample sample = custom_ranges[i];
1204 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) 1200 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
1205 return false; 1201 return false;
1206 if (sample != 0) 1202 if (sample != 0)
1207 has_valid_range = true; 1203 has_valid_range = true;
1208 } 1204 }
1209 return has_valid_range; 1205 return has_valid_range;
1210 } 1206 }
1211 1207
1212 } // namespace base 1208 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698