Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Side by Side Diff: base/trace_event/heap_profiler_type_name_deduplicator.cc

Issue 1467453003: [Tracing] Make heap profiler type info a string (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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_type_name_deduplicator.h"
6
7 #include <stdlib.h>
8 #include <string>
9 #include <utility>
10
11 #include "base/json/string_escape.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/trace_event/trace_event_memory_overhead.h"
14
15 namespace base {
16 namespace trace_event {
17
18 TypeNameDeduplicator::TypeNameDeduplicator() {
19 // A null pointer has type ID 0 ("unknown type");
20 type_ids_.insert(std::make_pair(nullptr, 0));
21 }
22
23 TypeNameDeduplicator::~TypeNameDeduplicator() {}
24
25 int TypeNameDeduplicator::Insert(const char* type_name) {
26 auto result = type_ids_.insert(std::make_pair(type_name, 0));
27 auto& elem = result.first;
28 bool did_not_exist_before = result.second;
29
30 if (did_not_exist_before) {
31 // The type IDs are assigned sequentially and they are zero-based, so
32 // |size() - 1| is the ID of the new element.
33 elem->second = static_cast<int>(type_ids_.size() - 1);
34 }
35
36 return elem->second;
37 }
38
39 void TypeNameDeduplicator::AppendAsTraceFormat(std::string* out) const {
40 out->append("{"); // Begin the type names dictionary.
41
42 auto it = type_ids_.begin();
43 std::string buffer;
44
45 // Write the first entry manually; the null pointer must not be dereferenced.
46 // (The first entry is the null pointer because a |std::map| is ordered.)
47 it++;
48 out->append("\"0\":\"[unknown]\"");
49
50 for (; it != type_ids_.end(); it++) {
51 // Type IDs in the trace are strings, write them as stringified keys of
52 // a dictionary.
53 SStringPrintf(&buffer, ",\"%d\":", it->second);
54
55 // |EscapeJSONString| appends, it does not overwrite |buffer|.
56 bool put_in_quotes = true;
57 EscapeJSONString(it->first, put_in_quotes, &buffer);
58 out->append(buffer);
59 }
60
61 out->append("}"); // End the type names dictionary.
62 }
63
64 void TypeNameDeduplicator::EstimateTraceMemoryOverhead(
65 TraceEventMemoryOverhead* overhead) {
66 // The size here is only an estimate; it fails to take into account the size
67 // of the tree nodes for the map, but as an estimate this should be fine.
68 size_t map_size = type_ids_.size() * sizeof(std::pair<const char*, int>);
69
70 overhead->Add("TypeNameDeduplicator",
71 sizeof(TypeNameDeduplicator) + map_size);
72 }
73
74 } // namespace trace_event
75 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698