summaryrefslogtreecommitdiffstats
path: root/base/metrics/sparse_histogram.cc
blob: 169037dcca9c741bc40737b4c7d9e919a1340c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2012 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.

#include "base/metrics/sparse_histogram.h"

#include "base/metrics/sample_map.h"
#include "base/metrics/statistics_recorder.h"
#include "base/synchronization/lock.h"

using std::map;
using std::string;

namespace base {

typedef HistogramBase::Count Count;
typedef HistogramBase::Sample Sample;

// static
HistogramBase* SparseHistogram::FactoryGet(const string& name, int32 flags) {
  // TODO(kaiwang): Register and get SparseHistogram with StatisticsRecorder.
  HistogramBase* histogram = new SparseHistogram(name);
  histogram->SetFlags(flags);
  return histogram;
}

SparseHistogram::~SparseHistogram() {}

HistogramType SparseHistogram::GetHistogramType() const {
  return SPARSE_HISTOGRAM;
}

bool SparseHistogram::HasConstructionArguments(Sample minimum,
                                               Sample maximum,
                                               size_t bucket_count) const {
  // SparseHistogram never has min/max/bucket_count limit.
  return false;
}

void SparseHistogram::Add(Sample value) {
  base::AutoLock auto_lock(lock_);
  sample_counts_[value]++;
  redundant_count_ += 1;
}

scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
  scoped_ptr<SampleMap> snapshot(new SampleMap());

  base::AutoLock auto_lock(lock_);
  for(map<Sample, Count>::const_iterator it = sample_counts_.begin();
      it != sample_counts_.end();
      ++it) {
    snapshot->Accumulate(it->first, it->second);
  }
  snapshot->ResetRedundantCount(redundant_count_);
  return snapshot.PassAs<HistogramSamples>();
}

void SparseHistogram::WriteHTMLGraph(string* output) const {
  // TODO(kaiwang): Implement.
}

void SparseHistogram::WriteAscii(string* output) const {
  // TODO(kaiwang): Implement.
}

SparseHistogram::SparseHistogram(const string& name)
    : HistogramBase(name),
      redundant_count_(0) {}

void SparseHistogram::GetParameters(DictionaryValue* params) const {
  // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
}

void SparseHistogram::GetCountAndBucketData(Count* count,
                                            ListValue* buckets) const {
  // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
}

}  // namespace base