histogram_delta_serialization.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
  5. #define BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/base_export.h"
  10. #include "base/macros.h"
  11. #include "base/metrics/histogram_flattener.h"
  12. #include "base/metrics/histogram_snapshot_manager.h"
  13. #include "base/threading/thread_checker.h"
  14. namespace base {
  15. class HistogramBase;
  16. // Serializes and restores histograms deltas.
  17. class BASE_EXPORT HistogramDeltaSerialization : public HistogramFlattener {
  18. public:
  19. // |caller_name| is string used in histograms for counting inconsistencies.
  20. explicit HistogramDeltaSerialization(const std::string& caller_name);
  21. ~HistogramDeltaSerialization() override;
  22. // Computes deltas in histogram bucket counts relative to the previous call to
  23. // this method. Stores the deltas in serialized form into |serialized_deltas|.
  24. // If |serialized_deltas| is null, no data is serialized, though the next call
  25. // will compute the deltas relative to this one. Setting |include_persistent|
  26. // will include histograms held in persistent memory (and thus may be reported
  27. // elsewhere); otherwise only histograms local to this process are serialized.
  28. void PrepareAndSerializeDeltas(std::vector<std::string>* serialized_deltas,
  29. bool include_persistent);
  30. // Deserialize deltas and add samples to corresponding histograms, creating
  31. // them if necessary. Silently ignores errors in |serialized_deltas|.
  32. static void DeserializeAndAddSamples(
  33. const std::vector<std::string>& serialized_deltas);
  34. private:
  35. // HistogramFlattener implementation.
  36. void RecordDelta(const HistogramBase& histogram,
  37. const HistogramSamples& snapshot) override;
  38. ThreadChecker thread_checker_;
  39. // Calculates deltas in histogram counters.
  40. HistogramSnapshotManager histogram_snapshot_manager_;
  41. // Output buffer for serialized deltas.
  42. std::vector<std::string>* serialized_deltas_;
  43. DISALLOW_COPY_AND_ASSIGN(HistogramDeltaSerialization);
  44. };
  45. } // namespace base
  46. #endif // BASE_METRICS_HISTOGRAM_DELTA_SERIALIZATION_H_