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

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

Issue 1425533011: Support "shared" histograms between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: extract common histogram FactoryGet code; extract histogram persistence into seperate files Created 5 years 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 8
9 // It supports calls to accumulate either time intervals (which are processed 9 // It supports calls to accumulate either time intervals (which are processed
10 // as integral number of milliseconds), or arbitrary integral units. 10 // as integral number of milliseconds), or arbitrary integral units.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 #include "base/metrics/histogram_macros.h" 82 #include "base/metrics/histogram_macros.h"
83 #include "base/metrics/histogram_samples.h" 83 #include "base/metrics/histogram_samples.h"
84 #include "base/time/time.h" 84 #include "base/time/time.h"
85 85
86 namespace base { 86 namespace base {
87 87
88 class BooleanHistogram; 88 class BooleanHistogram;
89 class CustomHistogram; 89 class CustomHistogram;
90 class Histogram; 90 class Histogram;
91 class LinearHistogram; 91 class LinearHistogram;
92 class PersistentMemoryAllocator;
92 class Pickle; 93 class Pickle;
93 class PickleIterator; 94 class PickleIterator;
94 class SampleVector; 95 class SampleVector;
95 96
97 struct PersistentHistogramData;
98
96 class BASE_EXPORT Histogram : public HistogramBase { 99 class BASE_EXPORT Histogram : public HistogramBase {
97 public: 100 public:
98 // Initialize maximum number of buckets in histograms as 16,384. 101 // Initialize maximum number of buckets in histograms as 16,384.
99 static const size_t kBucketCount_MAX; 102 static const size_t kBucketCount_MAX;
100 103
101 typedef std::vector<Count> Counts; 104 typedef std::vector<Count> Counts;
102 105
103 //---------------------------------------------------------------------------- 106 //----------------------------------------------------------------------------
104 // For a valid histogram, input should follow these restrictions: 107 // For a valid histogram, input should follow these restrictions:
105 // minimum > 0 (if a minimum below 1 is specified, it will implicitly be 108 // minimum > 0 (if a minimum below 1 is specified, it will implicitly be
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 size_t expected_bucket_count) const override; 186 size_t expected_bucket_count) const override;
184 void Add(Sample value) override; 187 void Add(Sample value) override;
185 void AddCount(Sample value, int count) override; 188 void AddCount(Sample value, int count) override;
186 scoped_ptr<HistogramSamples> SnapshotSamples() const override; 189 scoped_ptr<HistogramSamples> SnapshotSamples() const override;
187 void AddSamples(const HistogramSamples& samples) override; 190 void AddSamples(const HistogramSamples& samples) override;
188 bool AddSamplesFromPickle(base::PickleIterator* iter) override; 191 bool AddSamplesFromPickle(base::PickleIterator* iter) override;
189 void WriteHTMLGraph(std::string* output) const override; 192 void WriteHTMLGraph(std::string* output) const override;
190 void WriteAscii(std::string* output) const override; 193 void WriteAscii(std::string* output) const override;
191 194
192 protected: 195 protected:
196 // This is the basis of all FactoryGet methods, parameterized with three
197 // histogram-type-specific blocks of code to create the ranges, perform
198 // allocation from the heap, and do any post-construction datafill. If
199 // the min/max/count is not known in advance (i.e. it's determined
200 // dynamically in |create_ranges|), pass zero (0) for all three.
201 static HistogramBase* Histogram::FactoryGet(
202 std::function<BucketRanges*()> create_ranges,
203 std::function<HistogramBase*(const BucketRanges*)> heap_alloc,
204 std::function<void(HistogramBase*)> fill_histogram,
205 HistogramType histogram_type,
206 const std::string& name,
207 Sample minimum,
208 Sample maximum,
209 size_t bucket_count,
210 int32 flags);
211
193 // |ranges| should contain the underflow and overflow buckets. See top 212 // |ranges| should contain the underflow and overflow buckets. See top
194 // comments for example. 213 // comments for example.
195 Histogram(const std::string& name, 214 Histogram(const std::string& name,
196 Sample minimum, 215 Sample minimum,
197 Sample maximum, 216 Sample maximum,
198 const BucketRanges* ranges); 217 const BucketRanges* ranges);
199 218
219 // Traditionally, histograms allocate their own memory for the bucket
220 // vector but "shared" histograms use memory regions allocated from a
221 // special memory segment that is passed in here. It is assumed that
222 // the life of this memory is managed externally and exceeds the lifetime
223 // of this object. Practically, this memory is never released until the
224 // process exits and the OS cleans it up.
225 Histogram(const std::string& name,
226 Sample minimum,
227 Sample maximum,
228 const BucketRanges* ranges,
229 HistogramBase::AtomicCount* counts,
230 size_t counts_size,
231 HistogramSamples::Metadata* meta);
232
200 ~Histogram() override; 233 ~Histogram() override;
201 234
202 // HistogramBase implementation: 235 // HistogramBase implementation:
203 bool SerializeInfoImpl(base::Pickle* pickle) const override; 236 bool SerializeInfoImpl(base::Pickle* pickle) const override;
204 237
205 // Method to override to skip the display of the i'th bucket if it's empty. 238 // Method to override to skip the display of the i'th bucket if it's empty.
206 virtual bool PrintEmptyBucket(size_t index) const; 239 virtual bool PrintEmptyBucket(size_t index) const;
207 240
208 // Get normalized size, relative to the ranges(i). 241 // Get normalized size, relative to the ranges(i).
209 virtual double GetBucketSize(Count current, size_t i) const; 242 virtual double GetBucketSize(Count current, size_t i) const;
210 243
211 // Return a string description of what goes in a given bucket. 244 // Return a string description of what goes in a given bucket.
212 // Most commonly this is the numeric value, but in derived classes it may 245 // Most commonly this is the numeric value, but in derived classes it may
213 // be a name (or string description) given to the bucket. 246 // be a name (or string description) given to the bucket.
214 virtual const std::string GetAsciiBucketRange(size_t it) const; 247 virtual const std::string GetAsciiBucketRange(size_t it) const;
215 248
216 private: 249 private:
217 // Allow tests to corrupt our innards for testing purposes. 250 // Allow tests to corrupt our innards for testing purposes.
218 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest); 251 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest);
219 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest); 252 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest);
220 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds); 253 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds);
221 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); 254 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts);
222 FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest); 255 FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest);
223 FRIEND_TEST_ALL_PREFIXES(HistogramTest, AddCountTest); 256 FRIEND_TEST_ALL_PREFIXES(HistogramTest, AddCountTest);
224 257
225 friend class StatisticsRecorder; // To allow it to delete duplicates. 258 friend class StatisticsRecorder; // To allow it to delete duplicates.
226 friend class StatisticsRecorderTest; 259 friend class StatisticsRecorderTest;
227 260
261 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram(
262 PersistentMemoryAllocator* allocator,
263 PersistentHistogramData* histogram_data);
228 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 264 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
229 base::PickleIterator* iter); 265 base::PickleIterator* iter);
230 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 266 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
231 267
232 // Implementation of SnapshotSamples function. 268 // Implementation of SnapshotSamples function.
233 scoped_ptr<SampleVector> SnapshotSampleVector() const; 269 scoped_ptr<SampleVector> SnapshotSampleVector() const;
234 270
235 //---------------------------------------------------------------------------- 271 //----------------------------------------------------------------------------
236 // Helpers for emitting Ascii graphic. Each method appends data to output. 272 // Helpers for emitting Ascii graphic. Each method appends data to output.
237 273
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 368
333 // Overridden from Histogram: 369 // Overridden from Histogram:
334 HistogramType GetHistogramType() const override; 370 HistogramType GetHistogramType() const override;
335 371
336 protected: 372 protected:
337 LinearHistogram(const std::string& name, 373 LinearHistogram(const std::string& name,
338 Sample minimum, 374 Sample minimum,
339 Sample maximum, 375 Sample maximum,
340 const BucketRanges* ranges); 376 const BucketRanges* ranges);
341 377
378 LinearHistogram(const std::string& name,
379 Sample minimum,
380 Sample maximum,
381 const BucketRanges* ranges,
382 HistogramBase::AtomicCount* counts,
383 size_t counts_size,
384 HistogramSamples::Metadata* meta);
385
342 double GetBucketSize(Count current, size_t i) const override; 386 double GetBucketSize(Count current, size_t i) const override;
343 387
344 // If we have a description for a bucket, then return that. Otherwise 388 // If we have a description for a bucket, then return that. Otherwise
345 // let parent class provide a (numeric) description. 389 // let parent class provide a (numeric) description.
346 const std::string GetAsciiBucketRange(size_t i) const override; 390 const std::string GetAsciiBucketRange(size_t i) const override;
347 391
348 // Skip printing of name for numeric range if we have a name (and if this is 392 // Skip printing of name for numeric range if we have a name (and if this is
349 // an empty bucket). 393 // an empty bucket).
350 bool PrintEmptyBucket(size_t index) const override; 394 bool PrintEmptyBucket(size_t index) const override;
351 395
352 private: 396 private:
397 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram(
398 PersistentMemoryAllocator* allocator,
399 PersistentHistogramData* histogram_data);
353 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 400 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
354 base::PickleIterator* iter); 401 base::PickleIterator* iter);
355 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 402 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
356 403
357 // For some ranges, we store a printable description of a bucket range. 404 // For some ranges, we store a printable description of a bucket range.
358 // If there is no description, then GetAsciiBucketRange() uses parent class 405 // If there is no description, then GetAsciiBucketRange() uses parent class
359 // to provide a description. 406 // to provide a description.
360 typedef std::map<Sample, std::string> BucketDescriptionMap; 407 typedef std::map<Sample, std::string> BucketDescriptionMap;
361 BucketDescriptionMap bucket_description_; 408 BucketDescriptionMap bucket_description_;
362 409
363 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); 410 DISALLOW_COPY_AND_ASSIGN(LinearHistogram);
364 }; 411 };
365 412
366 //------------------------------------------------------------------------------ 413 //------------------------------------------------------------------------------
367 414
368 // BooleanHistogram is a histogram for booleans. 415 // BooleanHistogram is a histogram for booleans.
369 class BASE_EXPORT BooleanHistogram : public LinearHistogram { 416 class BASE_EXPORT BooleanHistogram : public LinearHistogram {
370 public: 417 public:
371 static HistogramBase* FactoryGet(const std::string& name, int32 flags); 418 static HistogramBase* FactoryGet(const std::string& name, int32 flags);
372 419
373 // Overload of the above function that takes a const char* |name| param, 420 // Overload of the above function that takes a const char* |name| param,
374 // to avoid code bloat from the std::string constructor being inlined into 421 // to avoid code bloat from the std::string constructor being inlined into
375 // call sites. 422 // call sites.
376 static HistogramBase* FactoryGet(const char* name, int32 flags); 423 static HistogramBase* FactoryGet(const char* name, int32 flags);
377 424
378 HistogramType GetHistogramType() const override; 425 HistogramType GetHistogramType() const override;
379 426
380 private: 427 private:
381 BooleanHistogram(const std::string& name, const BucketRanges* ranges); 428 BooleanHistogram(const std::string& name, const BucketRanges* ranges);
429 BooleanHistogram(const std::string& name,
430 const BucketRanges* ranges,
431 HistogramBase::AtomicCount* counts,
432 HistogramSamples::Metadata* meta);
382 433
434 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram(
435 PersistentMemoryAllocator* allocator,
436 PersistentHistogramData* histogram_data);
383 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 437 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
384 base::PickleIterator* iter); 438 base::PickleIterator* iter);
385 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 439 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
386 440
387 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); 441 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram);
388 }; 442 };
389 443
390 //------------------------------------------------------------------------------ 444 //------------------------------------------------------------------------------
391 445
392 // CustomHistogram is a histogram for a set of custom integers. 446 // CustomHistogram is a histogram for a set of custom integers.
(...skipping 22 matching lines...) Expand all
415 // This function ensures that a guard bucket exists right after any 469 // This function ensures that a guard bucket exists right after any
416 // valid sample value (unless the next higher sample is also a valid value), 470 // valid sample value (unless the next higher sample is also a valid value),
417 // so that invalid samples never fall into the same bucket as valid samples. 471 // so that invalid samples never fall into the same bucket as valid samples.
418 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. 472 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges.
419 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, 473 static std::vector<Sample> ArrayToCustomRanges(const Sample* values,
420 size_t num_values); 474 size_t num_values);
421 protected: 475 protected:
422 CustomHistogram(const std::string& name, 476 CustomHistogram(const std::string& name,
423 const BucketRanges* ranges); 477 const BucketRanges* ranges);
424 478
479 CustomHistogram(const std::string& name,
480 const BucketRanges* ranges,
481 HistogramBase::AtomicCount* counts,
482 size_t counts_size,
483 HistogramSamples::Metadata* meta);
484
425 // HistogramBase implementation: 485 // HistogramBase implementation:
426 bool SerializeInfoImpl(base::Pickle* pickle) const override; 486 bool SerializeInfoImpl(base::Pickle* pickle) const override;
427 487
428 double GetBucketSize(Count current, size_t i) const override; 488 double GetBucketSize(Count current, size_t i) const override;
429 489
430 private: 490 private:
491 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram(
492 PersistentMemoryAllocator* allocator,
493 PersistentHistogramData* histogram_data);
431 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 494 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
432 base::PickleIterator* iter); 495 base::PickleIterator* iter);
433 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 496 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
434 497
435 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); 498 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges);
436 static BucketRanges* CreateBucketRangesFromCustomRanges(
437 const std::vector<Sample>& custom_ranges);
438 499
439 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); 500 DISALLOW_COPY_AND_ASSIGN(CustomHistogram);
440 }; 501 };
441 502
442 } // namespace base 503 } // namespace base
443 504
444 #endif // BASE_METRICS_HISTOGRAM_H_ 505 #endif // BASE_METRICS_HISTOGRAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698