OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" | 5 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/files/file_path.h" | |
12 #include "base/json/string_escape.h" | 13 #include "base/json/string_escape.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 #include "base/trace_event/trace_event_memory_overhead.h" | 15 #include "base/trace_event/trace_event_memory_overhead.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 namespace trace_event { | 18 namespace trace_event { |
18 | 19 |
20 namespace { | |
21 | |
22 // Extract category name from the file name. | |
23 std::string ExtractTypeInfo(const std::string& type_name) { | |
Primiano Tucci (use gerrit)
2016/03/25 01:56:31
I think the arg of this should be const char*, if
ssid
2016/03/28 18:14:49
Done.
| |
24 std::vector<FilePath::StringType> folder_names; | |
25 FilePath(FILE_PATH_LITERAL(type_name)).DirName().GetComponents(&folder_names); | |
26 | |
27 // If |type_name| is a not a file path the directory will be empty, then | |
28 // return the type name. | |
29 if (folder_names.empty()) | |
30 return type_name; | |
31 | |
32 // Remove the parent directory references - '..'. | |
33 while (folder_names.begin()->find(FilePath::kParentDirectory) != | |
34 std::string::npos) { | |
35 folder_names.erase(folder_names.begin()); | |
36 } | |
37 std::string type_info; | |
38 for (std::string folder : folder_names) | |
Primiano Tucci (use gerrit)
2016/03/25 01:56:31
tip for next times, const std::string& would have
ssid
2016/03/28 18:14:49
Thanks.
| |
39 type_info += folder + "/"; | |
40 return type_info; | |
41 } | |
42 | |
43 } // namespace | |
44 | |
19 TypeNameDeduplicator::TypeNameDeduplicator() { | 45 TypeNameDeduplicator::TypeNameDeduplicator() { |
20 // A null pointer has type ID 0 ("unknown type"); | 46 // A null pointer has type ID 0 ("unknown type"); |
21 type_ids_.insert(std::make_pair(nullptr, 0)); | 47 type_ids_.insert(std::make_pair(nullptr, 0)); |
22 } | 48 } |
23 | 49 |
24 TypeNameDeduplicator::~TypeNameDeduplicator() {} | 50 TypeNameDeduplicator::~TypeNameDeduplicator() {} |
25 | 51 |
26 int TypeNameDeduplicator::Insert(const char* type_name) { | 52 int TypeNameDeduplicator::Insert(const char* type_name) { |
27 auto result = type_ids_.insert(std::make_pair(type_name, 0)); | 53 auto result = type_ids_.insert(std::make_pair(type_name, 0)); |
28 auto& elem = result.first; | 54 auto& elem = result.first; |
(...skipping 17 matching lines...) Expand all Loading... | |
46 // Write the first entry manually; the null pointer must not be dereferenced. | 72 // Write the first entry manually; the null pointer must not be dereferenced. |
47 // (The first entry is the null pointer because a |std::map| is ordered.) | 73 // (The first entry is the null pointer because a |std::map| is ordered.) |
48 it++; | 74 it++; |
49 out->append("\"0\":\"[unknown]\""); | 75 out->append("\"0\":\"[unknown]\""); |
50 | 76 |
51 for (; it != type_ids_.end(); it++) { | 77 for (; it != type_ids_.end(); it++) { |
52 // Type IDs in the trace are strings, write them as stringified keys of | 78 // Type IDs in the trace are strings, write them as stringified keys of |
53 // a dictionary. | 79 // a dictionary. |
54 SStringPrintf(&buffer, ",\"%d\":", it->second); | 80 SStringPrintf(&buffer, ",\"%d\":", it->second); |
55 | 81 |
82 // TODO(ssid): crbug.com/594803 the type name is misused for category name. | |
83 std::string type_info = ExtractTypeInfo(it->first); | |
84 | |
56 // |EscapeJSONString| appends, it does not overwrite |buffer|. | 85 // |EscapeJSONString| appends, it does not overwrite |buffer|. |
57 bool put_in_quotes = true; | 86 bool put_in_quotes = true; |
58 EscapeJSONString(it->first, put_in_quotes, &buffer); | 87 EscapeJSONString(type_info, put_in_quotes, &buffer); |
59 out->append(buffer); | 88 out->append(buffer); |
60 } | 89 } |
61 | 90 |
62 out->append("}"); // End the type names dictionary. | 91 out->append("}"); // End the type names dictionary. |
63 } | 92 } |
64 | 93 |
65 void TypeNameDeduplicator::EstimateTraceMemoryOverhead( | 94 void TypeNameDeduplicator::EstimateTraceMemoryOverhead( |
66 TraceEventMemoryOverhead* overhead) { | 95 TraceEventMemoryOverhead* overhead) { |
67 // The size here is only an estimate; it fails to take into account the size | 96 // The size here is only an estimate; it fails to take into account the size |
68 // of the tree nodes for the map, but as an estimate this should be fine. | 97 // of the tree nodes for the map, but as an estimate this should be fine. |
69 size_t map_size = type_ids_.size() * sizeof(std::pair<const char*, int>); | 98 size_t map_size = type_ids_.size() * sizeof(std::pair<const char*, int>); |
70 | 99 |
71 overhead->Add("TypeNameDeduplicator", | 100 overhead->Add("TypeNameDeduplicator", |
72 sizeof(TypeNameDeduplicator) + map_size); | 101 sizeof(TypeNameDeduplicator) + map_size); |
73 } | 102 } |
74 | 103 |
75 } // namespace trace_event | 104 } // namespace trace_event |
76 } // namespace base | 105 } // namespace base |
OLD | NEW |