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

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: reorganized persistence around PersistentGet methods to fix visibility problems Created 4 years, 11 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 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 #include "base/metrics/histogram_macros.h" 85 #include "base/metrics/histogram_macros.h"
86 #include "base/metrics/histogram_samples.h" 86 #include "base/metrics/histogram_samples.h"
87 #include "base/time/time.h" 87 #include "base/time/time.h"
88 88
89 namespace base { 89 namespace base {
90 90
91 class BooleanHistogram; 91 class BooleanHistogram;
92 class CustomHistogram; 92 class CustomHistogram;
93 class Histogram; 93 class Histogram;
94 class LinearHistogram; 94 class LinearHistogram;
95 class PersistentMemoryAllocator;
95 class Pickle; 96 class Pickle;
96 class PickleIterator; 97 class PickleIterator;
97 class SampleVector; 98 class SampleVector;
98 99
99 class BASE_EXPORT Histogram : public HistogramBase { 100 class BASE_EXPORT Histogram : public HistogramBase {
100 public: 101 public:
101 // Initialize maximum number of buckets in histograms as 16,384. 102 // Initialize maximum number of buckets in histograms as 16,384.
102 static const size_t kBucketCount_MAX; 103 static const size_t kBucketCount_MAX;
103 104
104 typedef std::vector<Count> Counts; 105 typedef std::vector<Count> Counts;
(...skipping 26 matching lines...) Expand all
131 Sample minimum, 132 Sample minimum,
132 Sample maximum, 133 Sample maximum,
133 size_t bucket_count, 134 size_t bucket_count,
134 int32_t flags); 135 int32_t flags);
135 static HistogramBase* FactoryTimeGet(const char* name, 136 static HistogramBase* FactoryTimeGet(const char* name,
136 base::TimeDelta minimum, 137 base::TimeDelta minimum,
137 base::TimeDelta maximum, 138 base::TimeDelta maximum,
138 size_t bucket_count, 139 size_t bucket_count,
139 int32_t flags); 140 int32_t flags);
140 141
142 static HistogramBase* PersistentGet(const char* name,
Alexei Svitkine (slow) 2016/01/12 21:54:44 Add a comment, please.
bcwhite 2016/01/13 13:38:29 Done.
143 Sample minimum,
144 Sample maximum,
145 const BucketRanges* ranges,
146 HistogramBase::AtomicCount* counts,
147 size_t counts_size,
148 HistogramSamples::Metadata* meta);
149
141 static void InitializeBucketRanges(Sample minimum, 150 static void InitializeBucketRanges(Sample minimum,
142 Sample maximum, 151 Sample maximum,
143 BucketRanges* ranges); 152 BucketRanges* ranges);
144 153
145 // This constant if for FindCorruption. Since snapshots of histograms are 154 // This constant if for FindCorruption. Since snapshots of histograms are
146 // taken asynchronously relative to sampling, and our counting code currently 155 // taken asynchronously relative to sampling, and our counting code currently
147 // does not prevent race conditions, it is pretty likely that we'll catch a 156 // does not prevent race conditions, it is pretty likely that we'll catch a
148 // redundant count that doesn't match the sample count. We allow for a 157 // redundant count that doesn't match the sample count. We allow for a
149 // certain amount of slop before flagging this as an inconsistency. Even with 158 // certain amount of slop before flagging this as an inconsistency. Even with
150 // an inconsistency, we'll snapshot it again (for UMA in about a half hour), 159 // an inconsistency, we'll snapshot it again (for UMA in about a half hour),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 size_t expected_bucket_count) const override; 195 size_t expected_bucket_count) const override;
187 void Add(Sample value) override; 196 void Add(Sample value) override;
188 void AddCount(Sample value, int count) override; 197 void AddCount(Sample value, int count) override;
189 scoped_ptr<HistogramSamples> SnapshotSamples() const override; 198 scoped_ptr<HistogramSamples> SnapshotSamples() const override;
190 void AddSamples(const HistogramSamples& samples) override; 199 void AddSamples(const HistogramSamples& samples) override;
191 bool AddSamplesFromPickle(base::PickleIterator* iter) override; 200 bool AddSamplesFromPickle(base::PickleIterator* iter) override;
192 void WriteHTMLGraph(std::string* output) const override; 201 void WriteHTMLGraph(std::string* output) const override;
193 void WriteAscii(std::string* output) const override; 202 void WriteAscii(std::string* output) const override;
194 203
195 protected: 204 protected:
205 // This class, defined entirely within the .cc file, contains all the
206 // common logic for building a Histogram and can be overridden by more
207 // specific types to alter details of how the creation is done. It is
208 // defined as an embedded class (rather than an anonymous one) so it
209 // can access the protected constructors.
210 class Factory;
211
196 // |ranges| should contain the underflow and overflow buckets. See top 212 // |ranges| should contain the underflow and overflow buckets. See top
197 // comments for example. 213 // comments for example.
198 Histogram(const std::string& name, 214 Histogram(const std::string& name,
199 Sample minimum, 215 Sample minimum,
200 Sample maximum, 216 Sample maximum,
201 const BucketRanges* ranges); 217 const BucketRanges* ranges);
202 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
203 ~Histogram() override; 233 ~Histogram() override;
204 234
205 // HistogramBase implementation: 235 // HistogramBase implementation:
206 bool SerializeInfoImpl(base::Pickle* pickle) const override; 236 bool SerializeInfoImpl(base::Pickle* pickle) const override;
207 237
208 // 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.
209 virtual bool PrintEmptyBucket(size_t index) const; 239 virtual bool PrintEmptyBucket(size_t index) const;
210 240
211 // Get normalized size, relative to the ranges(i). 241 // Get normalized size, relative to the ranges(i).
212 virtual double GetBucketSize(Count current, size_t i) const; 242 virtual double GetBucketSize(Count current, size_t i) const;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 Sample minimum, 336 Sample minimum,
307 Sample maximum, 337 Sample maximum,
308 size_t bucket_count, 338 size_t bucket_count,
309 int32_t flags); 339 int32_t flags);
310 static HistogramBase* FactoryTimeGet(const char* name, 340 static HistogramBase* FactoryTimeGet(const char* name,
311 TimeDelta minimum, 341 TimeDelta minimum,
312 TimeDelta maximum, 342 TimeDelta maximum,
313 size_t bucket_count, 343 size_t bucket_count,
314 int32_t flags); 344 int32_t flags);
315 345
346 static HistogramBase* PersistentGet(const char* name,
347 Sample minimum,
348 Sample maximum,
349 const BucketRanges* ranges,
350 HistogramBase::AtomicCount* counts,
351 size_t counts_size,
352 HistogramSamples::Metadata* meta);
353
316 struct DescriptionPair { 354 struct DescriptionPair {
317 Sample sample; 355 Sample sample;
318 const char* description; // Null means end of a list of pairs. 356 const char* description; // Null means end of a list of pairs.
319 }; 357 };
320 358
321 // Create a LinearHistogram and store a list of number/text values for use in 359 // Create a LinearHistogram and store a list of number/text values for use in
322 // writing the histogram graph. 360 // writing the histogram graph.
323 // |descriptions| can be NULL, which means no special descriptions to set. If 361 // |descriptions| can be NULL, which means no special descriptions to set. If
324 // it's not NULL, the last element in the array must has a NULL in its 362 // it's not NULL, the last element in the array must has a NULL in its
325 // "description" field. 363 // "description" field.
326 static HistogramBase* FactoryGetWithRangeDescription( 364 static HistogramBase* FactoryGetWithRangeDescription(
327 const std::string& name, 365 const std::string& name,
328 Sample minimum, 366 Sample minimum,
329 Sample maximum, 367 Sample maximum,
330 size_t bucket_count, 368 size_t bucket_count,
331 int32_t flags, 369 int32_t flags,
332 const DescriptionPair descriptions[]); 370 const DescriptionPair descriptions[]);
333 371
334 static void InitializeBucketRanges(Sample minimum, 372 static void InitializeBucketRanges(Sample minimum,
335 Sample maximum, 373 Sample maximum,
336 BucketRanges* ranges); 374 BucketRanges* ranges);
337 375
338 // Overridden from Histogram: 376 // Overridden from Histogram:
339 HistogramType GetHistogramType() const override; 377 HistogramType GetHistogramType() const override;
340 378
341 protected: 379 protected:
380 class Factory;
381
342 LinearHistogram(const std::string& name, 382 LinearHistogram(const std::string& name,
343 Sample minimum, 383 Sample minimum,
344 Sample maximum, 384 Sample maximum,
345 const BucketRanges* ranges); 385 const BucketRanges* ranges);
346 386
387 LinearHistogram(const std::string& name,
Alexei Svitkine (slow) 2016/01/12 21:54:44 Please add a comment to these extra constructors.
bcwhite 2016/01/13 13:38:29 There's one big descriptive comment for the one in
388 Sample minimum,
389 Sample maximum,
390 const BucketRanges* ranges,
391 HistogramBase::AtomicCount* counts,
392 size_t counts_size,
393 HistogramSamples::Metadata* meta);
394
347 double GetBucketSize(Count current, size_t i) const override; 395 double GetBucketSize(Count current, size_t i) const override;
348 396
349 // If we have a description for a bucket, then return that. Otherwise 397 // If we have a description for a bucket, then return that. Otherwise
350 // let parent class provide a (numeric) description. 398 // let parent class provide a (numeric) description.
351 const std::string GetAsciiBucketRange(size_t i) const override; 399 const std::string GetAsciiBucketRange(size_t i) const override;
352 400
353 // Skip printing of name for numeric range if we have a name (and if this is 401 // Skip printing of name for numeric range if we have a name (and if this is
354 // an empty bucket). 402 // an empty bucket).
355 bool PrintEmptyBucket(size_t index) const override; 403 bool PrintEmptyBucket(size_t index) const override;
356 404
(...skipping 16 matching lines...) Expand all
373 // BooleanHistogram is a histogram for booleans. 421 // BooleanHistogram is a histogram for booleans.
374 class BASE_EXPORT BooleanHistogram : public LinearHistogram { 422 class BASE_EXPORT BooleanHistogram : public LinearHistogram {
375 public: 423 public:
376 static HistogramBase* FactoryGet(const std::string& name, int32_t flags); 424 static HistogramBase* FactoryGet(const std::string& name, int32_t flags);
377 425
378 // Overload of the above function that takes a const char* |name| param, 426 // Overload of the above function that takes a const char* |name| param,
379 // to avoid code bloat from the std::string constructor being inlined into 427 // to avoid code bloat from the std::string constructor being inlined into
380 // call sites. 428 // call sites.
381 static HistogramBase* FactoryGet(const char* name, int32_t flags); 429 static HistogramBase* FactoryGet(const char* name, int32_t flags);
382 430
431 static HistogramBase* PersistentGet(const char* name,
432 const BucketRanges* ranges,
433 HistogramBase::AtomicCount* counts,
434 HistogramSamples::Metadata* meta);
435
383 HistogramType GetHistogramType() const override; 436 HistogramType GetHistogramType() const override;
384 437
438 protected:
439 class Factory;
440
385 private: 441 private:
386 BooleanHistogram(const std::string& name, const BucketRanges* ranges); 442 BooleanHistogram(const std::string& name, const BucketRanges* ranges);
443 BooleanHistogram(const std::string& name,
444 const BucketRanges* ranges,
445 HistogramBase::AtomicCount* counts,
446 HistogramSamples::Metadata* meta);
387 447
388 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 448 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
389 base::PickleIterator* iter); 449 base::PickleIterator* iter);
390 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 450 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
391 451
392 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); 452 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram);
393 }; 453 };
394 454
395 //------------------------------------------------------------------------------ 455 //------------------------------------------------------------------------------
396 456
397 // CustomHistogram is a histogram for a set of custom integers. 457 // CustomHistogram is a histogram for a set of custom integers.
398 class BASE_EXPORT CustomHistogram : public Histogram { 458 class BASE_EXPORT CustomHistogram : public Histogram {
399 public: 459 public:
400 // |custom_ranges| contains a vector of limits on ranges. Each limit should be 460 // |custom_ranges| contains a vector of limits on ranges. Each limit should be
401 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward 461 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward
402 // compatibility). The limits can be unordered or contain duplication, but 462 // compatibility). The limits can be unordered or contain duplication, but
403 // client should not depend on this. 463 // client should not depend on this.
404 static HistogramBase* FactoryGet(const std::string& name, 464 static HistogramBase* FactoryGet(const std::string& name,
405 const std::vector<Sample>& custom_ranges, 465 const std::vector<Sample>& custom_ranges,
406 int32_t flags); 466 int32_t flags);
407 467
408 // Overload of the above function that takes a const char* |name| param, 468 // Overload of the above function that takes a const char* |name| param,
409 // to avoid code bloat from the std::string constructor being inlined into 469 // to avoid code bloat from the std::string constructor being inlined into
410 // call sites. 470 // call sites.
411 static HistogramBase* FactoryGet(const char* name, 471 static HistogramBase* FactoryGet(const char* name,
412 const std::vector<Sample>& custom_ranges, 472 const std::vector<Sample>& custom_ranges,
413 int32_t flags); 473 int32_t flags);
414 474
475 static HistogramBase* PersistentGet(const char* name,
476 const BucketRanges* ranges,
477 HistogramBase::AtomicCount* counts,
478 size_t counts_size,
479 HistogramSamples::Metadata* meta);
480
415 // Overridden from Histogram: 481 // Overridden from Histogram:
416 HistogramType GetHistogramType() const override; 482 HistogramType GetHistogramType() const override;
417 483
418 // Helper method for transforming an array of valid enumeration values 484 // Helper method for transforming an array of valid enumeration values
419 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. 485 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION.
420 // This function ensures that a guard bucket exists right after any 486 // This function ensures that a guard bucket exists right after any
421 // valid sample value (unless the next higher sample is also a valid value), 487 // valid sample value (unless the next higher sample is also a valid value),
422 // so that invalid samples never fall into the same bucket as valid samples. 488 // so that invalid samples never fall into the same bucket as valid samples.
423 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. 489 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges.
424 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, 490 static std::vector<Sample> ArrayToCustomRanges(const Sample* values,
425 size_t num_values); 491 size_t num_values);
426 protected: 492 protected:
493 class Factory;
494
427 CustomHistogram(const std::string& name, 495 CustomHistogram(const std::string& name,
428 const BucketRanges* ranges); 496 const BucketRanges* ranges);
429 497
498 CustomHistogram(const std::string& name,
499 const BucketRanges* ranges,
500 HistogramBase::AtomicCount* counts,
501 size_t counts_size,
502 HistogramSamples::Metadata* meta);
503
430 // HistogramBase implementation: 504 // HistogramBase implementation:
431 bool SerializeInfoImpl(base::Pickle* pickle) const override; 505 bool SerializeInfoImpl(base::Pickle* pickle) const override;
432 506
433 double GetBucketSize(Count current, size_t i) const override; 507 double GetBucketSize(Count current, size_t i) const override;
434 508
435 private: 509 private:
436 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 510 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
437 base::PickleIterator* iter); 511 base::PickleIterator* iter);
438 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 512 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
439 513
440 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); 514 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges);
441 static BucketRanges* CreateBucketRangesFromCustomRanges(
442 const std::vector<Sample>& custom_ranges);
443 515
444 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); 516 DISALLOW_COPY_AND_ASSIGN(CustomHistogram);
445 }; 517 };
446 518
447 } // namespace base 519 } // namespace base
448 520
449 #endif // BASE_METRICS_HISTOGRAM_H_ 521 #endif // BASE_METRICS_HISTOGRAM_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/metrics/histogram.cc » ('j') | base/metrics/histogram.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698