histogram_delta_serialization.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "base/metrics/histogram_delta_serialization.h"
  5. #include "base/logging.h"
  6. #include "base/metrics/histogram_base.h"
  7. #include "base/metrics/histogram_snapshot_manager.h"
  8. #include "base/metrics/statistics_recorder.h"
  9. #include "base/numerics/safe_conversions.h"
  10. #include "base/pickle.h"
  11. #include "base/values.h"
  12. namespace base {
  13. namespace {
  14. // Create or find existing histogram and add the samples from pickle.
  15. // Silently returns when seeing any data problem in the pickle.
  16. void DeserializeHistogramAndAddSamples(PickleIterator* iter) {
  17. HistogramBase* histogram = DeserializeHistogramInfo(iter);
  18. if (!histogram)
  19. return;
  20. if (histogram->flags() & HistogramBase::kIPCSerializationSourceFlag) {
  21. DVLOG(1) << "Single process mode, histogram observed and not copied: "
  22. << histogram->histogram_name();
  23. return;
  24. }
  25. histogram->AddSamplesFromPickle(iter);
  26. }
  27. } // namespace
  28. HistogramDeltaSerialization::HistogramDeltaSerialization(
  29. const std::string& caller_name)
  30. : histogram_snapshot_manager_(this), serialized_deltas_(nullptr) {}
  31. HistogramDeltaSerialization::~HistogramDeltaSerialization() = default;
  32. void HistogramDeltaSerialization::PrepareAndSerializeDeltas(
  33. std::vector<std::string>* serialized_deltas,
  34. bool include_persistent) {
  35. DCHECK(thread_checker_.CalledOnValidThread());
  36. serialized_deltas_ = serialized_deltas;
  37. // Note: Before serializing, we set the kIPCSerializationSourceFlag for all
  38. // the histograms, so that the receiving process can distinguish them from the
  39. // local histograms.
  40. StatisticsRecorder::PrepareDeltas(
  41. include_persistent, Histogram::kIPCSerializationSourceFlag,
  42. Histogram::kNoFlags, &histogram_snapshot_manager_);
  43. serialized_deltas_ = nullptr;
  44. }
  45. // static
  46. void HistogramDeltaSerialization::DeserializeAndAddSamples(
  47. const std::vector<std::string>& serialized_deltas) {
  48. for (auto it = serialized_deltas.begin(); it != serialized_deltas.end();
  49. ++it) {
  50. Pickle pickle(it->data(), it->size());
  51. PickleIterator iter(pickle);
  52. DeserializeHistogramAndAddSamples(&iter);
  53. }
  54. }
  55. void HistogramDeltaSerialization::RecordDelta(
  56. const HistogramBase& histogram,
  57. const HistogramSamples& snapshot) {
  58. DCHECK(thread_checker_.CalledOnValidThread());
  59. DCHECK_NE(0, snapshot.TotalCount());
  60. Pickle pickle;
  61. histogram.SerializeInfo(&pickle);
  62. snapshot.Serialize(&pickle);
  63. serialized_deltas_->push_back(
  64. std::string(static_cast<const char*>(pickle.data()), pickle.size()));
  65. }
  66. } // namespace base