| 6 |
| 7 | #include <utility> |
| 8 |
| 9 | #include "base/memory/ptr_util.h" |
| 10 | #include "base/metrics/dummy_histogram.h" |
| 11 | #include "base/metrics/metrics_hashes.h" |
| 12 | #include "base/metrics/persistent_histogram_allocator.h" |
| 13 | #include "base/metrics/persistent_sample_map.h" |
| 14 | #include "base/metrics/sample_map.h" |
| 15 | #include "base/metrics/statistics_recorder.h" |
| 16 | #include "base/pickle.h" |
| 17 | #include "base/strings/stringprintf.h" |
| 18 | #include "base/synchronization/lock.h" |
| 19 |
| 20 | namespace { |
| 21 | constexpr char kHtmlNewLine[] = "<br>"; |
| 22 | constexpr char kAsciiNewLine[] = "\n"; |
| 23 | } // namespace |
| 24 |
| 25 | namespace base { |
| 26 |
| 27 | typedef HistogramBase::Count Count; |
| 28 | typedef HistogramBase::Sample Sample; |
| 29 |
| 30 | // static |
| 31 | HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
| 32 | int32_t flags) { |
| 33 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 34 | if (!histogram) { |
| 35 | // TODO(gayane): |HashMetricName| is called again in Histogram constructor. |
| 36 | // Refactor code to avoid the additional call. |
| 37 | bool should_record = |
| 38 | StatisticsRecorder::ShouldRecordHistogram(HashMetricName(name)); |
| 39 | if (!should_record) |
| 40 | return DummyHistogram::GetInstance(); |
| 41 | // Try to create the histogram using a "persistent" allocator. As of |
| 42 | // 2016-02-25, the availability of such is controlled by a base::Feature |
| 43 | // that is off by default. If the allocator doesn't exist or if |
| 44 | // allocating from it fails, code below will allocate the histogram from |
| 45 | // the process heap. |
| 46 | PersistentMemoryAllocator::Reference histogram_ref = 0; |
| 47 | std::unique_ptr<HistogramBase> tentative_histogram; |
| 48 | PersistentHistogramAllocator* allocator = GlobalHistogramAllocator::Get(); |
| 49 | if (allocator) { |
| 50 | tentative_histogram = allocator->AllocateHistogram( |
| 51 | SPARSE_HISTOGRAM, name, 0, 0, nullptr, flags, &histogram_ref); |
| 52 | } |
| 53 |
| 54 | // Handle the case where no persistent allocator is present or the |
| 55 | // persistent allocation fails (perhaps because it is full). |
| 56 | if (!tentative_histogram) { |
| 57 | DCHECK(!histogram_ref); // Should never have been set. |
| 58 | DCHECK(!allocator); // Shouldn't have failed. |
| 59 | flags &= ~HistogramBase::kIsPersistent; |
| 60 | tentative_histogram.reset(new SparseHistogram(GetPermanentName(name))); |
| 61 | tentative_histogram->SetFlags(flags); |
| 62 | } |
| 63 |
| 64 | // Register this histogram with the StatisticsRecorder. Keep a copy of |
| 65 | // the pointer value to tell later whether the locally created histogram |
| 66 | // was registered or deleted. The type is "void" because it could point |
| 67 | // to released memory after the following line. |
| 68 | const void* tentative_histogram_ptr = tentative_histogram.get(); |
| 69 | histogram = StatisticsRecorder::RegisterOrDeleteDuplicate( |
| 70 | tentative_histogram.release()); |
| 71 |
| 72 | // Persistent histograms need some follow-up processing. |
| 73 | if (histogram_ref) { |
| 74 | allocator->FinalizeHistogram(histogram_ref, |
| 75 | histogram == tentative_histogram_ptr); |
| 76 | } |
| 77 | } |
| 78 |
| 79 | CHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); |
| 80 | return histogram; |
| 81 | } |
| 82 |
| 83 | // static |
| 84 | std::unique_ptr<HistogramBase> SparseHistogram::PersistentCreate( |
| 85 | PersistentHistogramAllocator* allocator, |
| 86 | const char* name, |
| 87 | HistogramSamples::Metadata* meta, |
| 88 | HistogramSamples::Metadata* logged_meta) { |
| 89 | return WrapUnique( |
| 90 | new SparseHistogram(allocator, name, meta, logged_meta)); |
| 91 | } |
| 92 |
| 93 | SparseHistogram::~SparseHistogram() = default; |
| 94 |
| 95 | uint64_t SparseHistogram::name_hash() const { |
| 96 | return unlogged_samples_->id(); |
| 97 | } |
| 98 |
| 99 | HistogramType SparseHistogram::GetHistogramType() const { |
| 100 | return SPARSE_HISTOGRAM; |
| 101 | } |
| 102 |
| 103 | bool SparseHistogram::HasConstructionArguments( |
| 104 | Sample expected_minimum, |
| 105 | Sample expected_maximum, |
| 106 | uint32_t expected_bucket_count) const { |
| 107 | // SparseHistogram never has min/max/bucket_count limit. |
| 108 | return false; |
| 109 | } |
| 110 |
| 111 | void SparseHistogram::Add(Sample value) { |
| 112 | AddCount(value, 1); |
| 113 | } |
| 114 |
| 115 | void SparseHistogram::AddCount(Sample value, int count) { |
| 116 | if (count <= 0) { |
| 117 | NOTREACHED(); |
| 118 | return; |
| 119 | } |
| 120 | { |
| 121 | base::AutoLock auto_lock(lock_); |
| 122 | unlogged_samples_->Accumulate(value, count); |
| 123 | } |
| 124 |
| 125 | if (UNLIKELY(StatisticsRecorder::have_active_callbacks())) |
| 126 | FindAndRunCallback(value); |
| 127 | } |
| 128 |
| 129 | std::unique_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
| 130 | std::unique_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 131 |
| 132 | base::AutoLock auto_lock(lock_); |
| 133 | snapshot->Add(*unlogged_samples_); |
| 134 | snapshot->Add(*logged_samples_); |
| 135 | return std::move(snapshot); |
| 136 | } |
| 137 |
| 138 | std::unique_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() { |
| 139 | DCHECK(!final_delta_created_); |
| 140 |
| 141 | std::unique_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 142 | base::AutoLock auto_lock(lock_); |
| 143 | snapshot->Add(*unlogged_samples_); |
| 144 |
| 145 | unlogged_samples_->Subtract(*snapshot); |
| 146 | logged_samples_->Add(*snapshot); |
| 147 | return std::move(snapshot); |
| 148 | } |
| 149 |
| 150 | std::unique_ptr<HistogramSamples> SparseHistogram::SnapshotFinalDelta() const { |
| 151 | DCHECK(!final_delta_created_); |
| 152 | final_delta_created_ = true; |
| 153 |
| 154 | std::unique_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
| 155 | base::AutoLock auto_lock(lock_); |
| 156 | snapshot->Add(*unlogged_samples_); |
| 157 |
| 158 | return std::move(snapshot); |
| 159 | } |
| 160 |
| 161 | void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 162 | base::AutoLock auto_lock(lock_); |
| 163 | unlogged_samples_->Add(samples); |
| 164 | } |
| 165 |
| 166 | bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 167 | base::AutoLock auto_lock(lock_); |
| 168 | return unlogged_samples_->AddFromPickle(iter); |
| 169 | } |
| 170 |
| 171 | void SparseHistogram::WriteHTMLGraph(std::string* output) const { |
| 172 | // Get a local copy of the data so we are consistent. |
| 173 | std::unique_ptr<HistogramSamples> snapshot = SnapshotSamples(); |
| 174 |
| 175 | output->append("<PRE>"); |
| 176 | output->append("<h4>"); |
| 177 | WriteAsciiHeader(*snapshot, output); |
| 178 | output->append("</h4>"); |
| 179 | WriteAsciiBody(*snapshot, true, kHtmlNewLine, output); |
| 180 | output->append("</PRE>"); |
| 181 | } |
| 182 |
| 183 | void SparseHistogram::WriteAscii(std::string* output) const { |
| 184 | // Get a local copy of the data so we are consistent. |
| 185 | std::unique_ptr<HistogramSamples> snapshot = SnapshotSamples(); |
| 186 |
| 187 | WriteAsciiHeader(*snapshot, output); |
| 188 | output->append(kAsciiNewLine); |
| 189 | WriteAsciiBody(*snapshot, true, kAsciiNewLine, output); |
| 190 | } |
| 191 |
| 192 | void SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 193 | pickle->WriteString(histogram_name()); |
| 194 | pickle->WriteInt(flags()); |
| 195 | } |
| 196 |
| 197 | SparseHistogram::SparseHistogram(const char* name) |
| 198 | : HistogramBase(name), |
| 199 | unlogged_samples_(new SampleMap(HashMetricName(name))), |
| 200 | logged_samples_(new SampleMap(unlogged_samples_->id())) {} |
| 201 |
| 202 | SparseHistogram::SparseHistogram(PersistentHistogramAllocator* allocator, |
| 203 | const char* name, |
| 204 | HistogramSamples::Metadata* meta, |
| 205 | HistogramSamples::Metadata* logged_meta) |
| 206 | : HistogramBase(name), |
| 207 | // While other histogram types maintain a static vector of values with |
| 208 | // sufficient space for both "active" and "logged" samples, with each |
| 209 | // SampleVector being given the appropriate half, sparse histograms |
| 210 | // have no such initial allocation. Each sample has its own record |
| 211 | // attached to a single PersistentSampleMap by a common 64-bit identifier. |
| 212 | // Since a sparse histogram has two sample maps (active and logged), |
| 213 | // there must be two sets of sample records with diffent IDs. The |
| 214 | // "active" samples use, for convenience purposes, an ID matching |
| 215 | // that of the histogram while the "logged" samples use that number |
| 216 | // plus 1. |
| 217 | unlogged_samples_( |
| 218 | new PersistentSampleMap(HashMetricName(name), allocator, meta)), |
| 219 | logged_samples_(new PersistentSampleMap(unlogged_samples_->id() + 1, |
| 220 | allocator, |
| 221 | logged_meta)) {} |
| 222 |
| 223 | HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
| 224 | std::string histogram_name; |
| 225 | int flags; |
| 226 | if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { |
| 227 | DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
| 228 | return nullptr; |
| 229 | } |
| 230 |
| 231 | flags &= ~HistogramBase::kIPCSerializationSourceFlag; |
| 232 |
| 233 | return SparseHistogram::FactoryGet(histogram_name, flags); |
| 234 | } |
| 235 |
| 236 | void SparseHistogram::GetParameters(DictionaryValue* params) const { |
| 237 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 238 | } |
| 239 |
| 240 | void SparseHistogram::GetCountAndBucketData(Count* count, |
| 241 | int64_t* sum, |
| 242 | ListValue* buckets) const { |
| 243 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 244 | } |
| 245 |
| 246 | void SparseHistogram::WriteAsciiBody(const HistogramSamples& snapshot, |
| 247 | bool graph_it, |
| 248 | const std::string& newline, |
| 249 | std::string* output) const { |
| 250 | Count total_count = snapshot.TotalCount(); |
| 251 | double scaled_total_count = total_count / 100.0; |
| 252 |
| 253 | // Determine how wide the largest bucket range is (how many digits to print), |
| 254 | // so that we'll be able to right-align starts for the graphical bars. |
| 255 | // Determine which bucket has the largest sample count so that we can |
| 256 | // normalize the graphical bar-width relative to that sample count. |
| 257 | Count largest_count = 0; |
| 258 | Sample largest_sample = 0; |
| 259 | std::unique_ptr<SampleCountIterator> it = snapshot.Iterator(); |
| 260 | while (!it->Done()) { |
| 261 | Sample min; |
| 262 | int64_t max; |
| 263 | Count count; |
| 264 | it->Get(&min, &max, &count); |
| 265 | if (min > largest_sample) |
| 266 | largest_sample = min; |
| 267 | if (count > largest_count) |
| 268 | largest_count = count; |
| 269 | it->Next(); |
| 270 | } |
| 271 | size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1; |
| 272 |
| 273 | // iterate over each item and display them |
| 274 | it = snapshot.Iterator(); |
| 275 | while (!it->Done()) { |
| 276 | Sample min; |
| 277 | int64_t max; |
| 278 | Count count; |
| 279 | it->Get(&min, &max, &count); |
| 280 |
| 281 | // value is min, so display it |
| 282 | std::string range = GetSimpleAsciiBucketRange(min); |
| 283 | output->append(range); |
| 284 | for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 285 | output->push_back(' '); |
| 286 |
| 287 | if (graph_it) |
| 288 | WriteAsciiBucketGraph(count, largest_count, output); |
| 289 | WriteAsciiBucketValue(count, scaled_total_count, output); |
| 290 | output->append(newline); |
| 291 | it->Next(); |
| 292 | } |
| 293 | } |
| 294 |
| 295 | void SparseHistogram::WriteAsciiHeader(const HistogramSamples& snapshot, |
| 296 | std::string* output) const { |
| 297 | StringAppendF(output, "Histogram: %s recorded %d samples", histogram_name(), |
| 298 | snapshot.TotalCount()); |
| 299 | if (flags()) |
| 300 | StringAppendF(output, " (flags = 0x%x)", flags()); |
| 301 | } |
| 302 |
| 303 | } // namespace base |
| 304 |