/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ // Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.
// Histogram is an object that aggregates statistics, and can summarize them in // various forms, including ASCII graphical, HTML, and numerically (as a // vector of numbers corresponding to each of the aggregating buckets).
// It supports calls to accumulate either time intervals (which are processed // as integral number of milliseconds), or arbitrary integral units.
// The default layout of buckets is exponential. For example, buckets might // contain (sequentially) the count of values in the following intervals: // [0,1), [1,2), [2,4), [4,8), [8,16), [16,32), [32,64), [64,infinity) // That bucket allocation would actually result from construction of a histogram // for values between 1 and 64, with 8 buckets, such as: // Histogram count(L"some name", 1, 64, 8); // Note that the underflow bucket [0,1) and the overflow bucket [64,infinity) // are not counted by the constructor in the user supplied "bucket_count" // argument. // The above example has an exponential ratio of 2 (doubling the bucket width // in each consecutive bucket. The Histogram class automatically calculates // the smallest ratio that it can use to construct the number of buckets // selected in the constructor. An another example, if you had 50 buckets, // and millisecond time values from 1 to 10000, then the ratio between // consecutive bucket widths will be approximately somewhere around the 50th // root of 10000. This approach provides very fine grain (narrow) buckets // at the low end of the histogram scale, but allows the histogram to cover a // gigantic range with the addition of very few buckets.
// Histograms use a pattern involving a function static variable, that is a // pointer to a histogram. This static is explicitly initialized on any thread // that detects a uninitialized (NULL) pointer. The potentially racy // initialization is not a problem as it is always set to point to the same // value (i.e., the FactoryGet always returns the same value). FactoryGet // is also completely thread safe, which results in a completely thread safe, // and relatively fast, set of counters. To avoid races at shutdown, the static // pointer is NOT deleted, and we leak the histograms at process termination.
#ifndef BASE_METRICS_HISTOGRAM_H_ #define BASE_METRICS_HISTOGRAM_H_ #pragma once
class BooleanHistogram; class CustomHistogram; class Histogram; class LinearHistogram;
class Histogram { public: typedefint Sample; // Used for samples (and ranges of samples). typedefint Count; // Used to count samples in a bucket. staticconst Sample kSampleType_MAX = INT_MAX; // Initialize maximum number of buckets in histograms as 16,384. staticconst size_t kBucketCount_MAX;
// These enums are used to facilitate deserialization of renderer histograms // into the browser. enum ClassType {
HISTOGRAM,
LINEAR_HISTOGRAM,
BOOLEAN_HISTOGRAM,
FLAG_HISTOGRAM,
COUNT_HISTOGRAM,
CUSTOM_HISTOGRAM,
NOT_VALID_IN_RENDERER
};
protected: // Actual histogram data is stored in buckets, showing the count of values // that fit into each bucket.
Counts counts_;
// Save simple stats locally. Note that this MIGHT get done in base class // without shared memory at some point.
int64_t sum_; // sum of samples.
// To help identify memory corruption, we reduntantly save the number of // samples we've accumulated into all of our buckets. We can compare this // count to the sum of the counts in all buckets, and detect problems. Note // that due to races in histogram accumulation (if a histogram is indeed // updated on several threads simultaneously), the tallies might mismatch, // and also the snapshotting code may asynchronously get a mismatch (though // generally either race based mismatch cause is VERY rare).
int64_t redundant_count_;
};
//---------------------------------------------------------------------------- // minimum should start from 1. 0 is invalid as a minimum. 0 is an implicit // default underflow bucket. static Histogram* FactoryGet(Sample minimum, Sample maximum,
size_t bucket_count, Flags flags, constint* buckets);
virtual ~Histogram();
void Add(int value); void Subtract(int value);
// This method is an interface, used only by BooleanHistogram. virtualvoid AddBoolean(bool value);
// Accept a TimeDelta to increment. void AddTime(TimeDelta time) { Add(static_cast<int>(time.InMilliseconds())); }
// This method is an interface, used only by LinearHistogram. virtualvoid SetRangeDescriptions(const DescriptionPair descriptions[]);
// Support generic flagging of Histograms. // 0x1 Currently used to mark this histogram to be recorded by UMA.. // 0x8000 means print ranges in hex. void SetFlags(Flags flags) { flags_ = static_cast<Flags>(flags_ | flags); } void ClearFlags(Flags flags) { flags_ = static_cast<Flags>(flags_ & ~flags); } int flags() const { return flags_; }
// Check to see if bucket ranges, counts and tallies in the snapshot are // consistent with the bucket ranges and checksums in our histogram. This can // produce a false-alarm if a race occurred in the reading of the data during // a SnapShot process, but should otherwise be false at all times (unless we // have memory over-writes, or DRAM failures). virtual Inconsistencies FindCorruption(const SampleSet& snapshot) const;
// Initialize ranges_ mapping from raw data. void InitializeBucketRangeFromData(constint* buckets);
// Method to override to skip the display of the i'th bucket if it's empty. virtualbool PrintEmptyBucket(size_t index) const;
//---------------------------------------------------------------------------- // Methods to override to create histogram with different bucket widths. //---------------------------------------------------------------------------- // Find bucket to increment for sample value. virtual size_t BucketIndex(Sample value) const; // Get normalized size, relative to the ranges_[i]. virtualdouble GetBucketSize(Count current, size_t i) const;
// Return a string description of what goes in a given bucket. // Most commonly this is the numeric value, but in derived classes it may // be a name (or string description) given to the bucket. virtualconst std::string GetAsciiBucketRange(size_t it) const;
//---------------------------------------------------------------------------- // Methods to override to create thread safe histogram. //---------------------------------------------------------------------------- // Update all our internal data, including histogram virtualvoid Accumulate(Sample value, Count count, size_t index);
// Validate that ranges_ was created sensibly (top and bottom range // values relate properly to the declared_min_ and declared_max_).. bool ValidateBucketRanges() const;
virtual uint32_t CalculateRangeChecksum() const;
// Finally, provide the state that changes with the addition of each new // sample.
SampleSet sample_;
private: // Post constructor initialization. void Initialize();
// Checksum function for accumulating range values into a checksum. static uint32_t Crc32(uint32_t sum, Sample range);
//---------------------------------------------------------------------------- // Helpers for emitting Ascii graphic. Each method appends data to output.
// Find out how large the (graphically) the largest bucket will appear to be. double GetPeakBucketSize(const SampleSet& snapshot) const;
//---------------------------------------------------------------------------- // Table for generating Crc32 values. staticconst uint32_t kCrcTable[256]; //---------------------------------------------------------------------------- // Invariant values set at/near construction time
Sample declared_min_; // Less than this goes into counts_[0]
Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1].
size_t bucket_count_; // Dimension of counts_[].
// Flag the histogram for recording by UMA via metric_services.h.
Flags flags_;
// For each index, show the least value that can be stored in the // corresponding bucket. We also append one extra element in this array, // containing kSampleType_MAX, to make calculations easy. // The dimension of ranges_ is bucket_count + 1.
Ranges ranges_;
// For redundancy, we store a checksum of all the sample ranges when ranges // are generated. If ever there is ever a difference, then the histogram must // have been corrupted.
uint32_t range_checksum_;
// LinearHistogram is a more traditional histogram, with evenly spaced // buckets. class LinearHistogram : public Histogram { public: virtual ~LinearHistogram();
/* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit
default underflow bucket. */ static Histogram* FactoryGet(Sample minimum, Sample maximum,
size_t bucket_count, Flags flags, constint* buckets);
// Overridden from Histogram: virtual ClassType histogram_type() const override;
// Store a list of number/text values for use in rendering the histogram. // The last element in the array has a null in its "description" slot. virtualvoid SetRangeDescriptions( const DescriptionPair descriptions[]) override;
// If we have a description for a bucket, then return that. Otherwise // let parent class provide a (numeric) description. virtualconst std::string GetAsciiBucketRange(size_t i) const override;
// Skip printing of name for numeric range if we have a name (and if this is // an empty bucket). virtualbool PrintEmptyBucket(size_t index) const override;
private: // For some ranges, we store a printable description of a bucket range. // If there is no desciption, then GetAsciiBucketRange() uses parent class // to provide a description. typedef std::map<Sample, std::string> BucketDescriptionMap;
BucketDescriptionMap bucket_description_;
// BooleanHistogram is a histogram for booleans. class BooleanHistogram : public LinearHistogram { public: static Histogram* FactoryGet(Flags flags, constint* buckets);
// FlagHistogram is like boolean histogram, but only allows a single off/on // value. class FlagHistogram : public BooleanHistogram { public: static Histogram* FactoryGet(Flags flags, constint* buckets);
// CountHistogram only allows a single monotic counter value. class CountHistogram : public LinearHistogram { public: static Histogram* FactoryGet(Flags flags, constint* buckets);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.