OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "base/trace_event/heap_profiler_string_deduplicator.h" |
| 6 |
| 7 #include "base/trace_event/memory_usage_estimator.h" |
| 8 #include "base/trace_event/trace_event.h" |
| 9 #include "base/trace_event/trace_event_argument.h" |
| 10 #include "base/trace_event/trace_event_memory_overhead.h" |
| 11 |
| 12 namespace base { |
| 13 namespace trace_event { |
| 14 |
| 15 StringDeduplicator::StringDeduplicator() : last_serialized_index_(0) { |
| 16 // Add implicit entry for id 0 (NULL strings). |
| 17 strings_.push_back("[null]"); |
| 18 } |
| 19 |
| 20 StringDeduplicator::~StringDeduplicator() {} |
| 21 |
| 22 int StringDeduplicator::Insert(StringPiece string) { |
| 23 if (!string.data()) { |
| 24 // NULL strings are mapped to id 0. |
| 25 return 0; |
| 26 } |
| 27 auto it = string_ids_.find(string); |
| 28 if (it != string_ids_.end()) |
| 29 return it->second; |
| 30 |
| 31 // Insert new mapping. Note that |string_ids_| keys reference values |
| 32 // from |strings_|. |
| 33 int string_id = static_cast<int>(strings_.size()); |
| 34 strings_.push_back(string.as_string()); |
| 35 auto iter_and_flag = string_ids_.insert({strings_.back(), string_id}); |
| 36 DCHECK(iter_and_flag.second); // insert() must succeed |
| 37 return string_id; |
| 38 } |
| 39 |
| 40 void StringDeduplicator::SerializeIncrementally(TracedValue* traced_value) { |
| 41 for (; last_serialized_index_ != strings_.size(); ++last_serialized_index_) { |
| 42 traced_value->BeginDictionary(); |
| 43 traced_value->SetInteger("id", last_serialized_index_); |
| 44 traced_value->SetString("string", strings_[last_serialized_index_]); |
| 45 traced_value->EndDictionary(); |
| 46 } |
| 47 } |
| 48 |
| 49 void StringDeduplicator::EstimateTraceMemoryOverhead( |
| 50 TraceEventMemoryOverhead* overhead) { |
| 51 size_t memory_usage = |
| 52 EstimateMemoryUsage(string_ids_) + EstimateMemoryUsage(strings_); |
| 53 overhead->Add(TraceEventMemoryOverhead::kHeapProfilerStringDeduplicator, |
| 54 sizeof(StringDeduplicator) + memory_usage); |
| 55 } |
| 56 |
| 57 } // namespace trace_event |
| 58 } // namespace base |
OLD | NEW |