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 directory name from task context if |type_name| was task context. | |
23 // Otherwise, return |type_name|. | |
24 StringPiece ExtractDirNameFromTaskContext(const char* type_name) { | |
25 StringPiece result = type_name; | |
Primiano Tucci (use gerrit)
2016/03/31 15:37:16
StringPiece result(type_name);
no need to disturb
ssid
2016/04/01 04:34:54
Done.
| |
26 size_t last_seperator = result.find_last_of(FilePath::kSeparators); | |
27 | |
28 // If |type_name| was a not a file path, the seperator will not be found, so | |
29 // the whole type name is returned. | |
30 if (last_seperator == StringPiece::npos) | |
31 return result; | |
32 | |
33 // Remove the file name from the path. | |
34 result.remove_suffix(result.length() - last_seperator); | |
35 | |
36 // Remove the parent directory references - '..'. | |
37 while (result.starts_with(FilePath::kParentDirectory)) { | |
38 size_t parent_seperator = result.find_first_of(FilePath::kSeparators); | |
39 DCHECK_NE(StringPiece::npos, parent_seperator); | |
40 result.remove_prefix(parent_seperator + 1); | |
Primiano Tucci (use gerrit)
2016/03/31 15:37:16
I think here is fine to assume that .. is always f
ssid
2016/04/01 04:34:54
Done.
| |
41 } | |
42 return result; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
19 TypeNameDeduplicator::TypeNameDeduplicator() { | 47 TypeNameDeduplicator::TypeNameDeduplicator() { |
20 // A null pointer has type ID 0 ("unknown type"); | 48 // A null pointer has type ID 0 ("unknown type"); |
21 type_ids_.insert(std::make_pair(nullptr, 0)); | 49 type_ids_.insert(std::make_pair(nullptr, 0)); |
22 } | 50 } |
23 | 51 |
24 TypeNameDeduplicator::~TypeNameDeduplicator() {} | 52 TypeNameDeduplicator::~TypeNameDeduplicator() {} |
25 | 53 |
26 int TypeNameDeduplicator::Insert(const char* type_name) { | 54 int TypeNameDeduplicator::Insert(const char* type_name) { |
27 auto result = type_ids_.insert(std::make_pair(type_name, 0)); | 55 auto result = type_ids_.insert(std::make_pair(type_name, 0)); |
28 auto& elem = result.first; | 56 auto& elem = result.first; |
(...skipping 17 matching lines...) Expand all Loading... | |
46 // Write the first entry manually; the null pointer must not be dereferenced. | 74 // 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.) | 75 // (The first entry is the null pointer because a |std::map| is ordered.) |
48 it++; | 76 it++; |
49 out->append("\"0\":\"[unknown]\""); | 77 out->append("\"0\":\"[unknown]\""); |
50 | 78 |
51 for (; it != type_ids_.end(); it++) { | 79 for (; it != type_ids_.end(); it++) { |
52 // Type IDs in the trace are strings, write them as stringified keys of | 80 // Type IDs in the trace are strings, write them as stringified keys of |
53 // a dictionary. | 81 // a dictionary. |
54 SStringPrintf(&buffer, ",\"%d\":", it->second); | 82 SStringPrintf(&buffer, ",\"%d\":", it->second); |
55 | 83 |
84 // TODO(ssid): crbug.com/594803 the type name is misused for category name. | |
85 StringPiece type_info = ExtractDirNameFromTaskContext(it->first); | |
86 | |
56 // |EscapeJSONString| appends, it does not overwrite |buffer|. | 87 // |EscapeJSONString| appends, it does not overwrite |buffer|. |
57 bool put_in_quotes = true; | 88 bool put_in_quotes = true; |
58 EscapeJSONString(it->first, put_in_quotes, &buffer); | 89 EscapeJSONString(type_info, put_in_quotes, &buffer); |
59 out->append(buffer); | 90 out->append(buffer); |
60 } | 91 } |
61 | 92 |
62 out->append("}"); // End the type names dictionary. | 93 out->append("}"); // End the type names dictionary. |
63 } | 94 } |
64 | 95 |
65 void TypeNameDeduplicator::EstimateTraceMemoryOverhead( | 96 void TypeNameDeduplicator::EstimateTraceMemoryOverhead( |
66 TraceEventMemoryOverhead* overhead) { | 97 TraceEventMemoryOverhead* overhead) { |
67 // The size here is only an estimate; it fails to take into account the size | 98 // 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. | 99 // 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>); | 100 size_t map_size = type_ids_.size() * sizeof(std::pair<const char*, int>); |
70 | 101 |
71 overhead->Add("TypeNameDeduplicator", | 102 overhead->Add("TypeNameDeduplicator", |
72 sizeof(TypeNameDeduplicator) + map_size); | 103 sizeof(TypeNameDeduplicator) + map_size); |
73 } | 104 } |
74 | 105 |
75 } // namespace trace_event | 106 } // namespace trace_event |
76 } // namespace base | 107 } // namespace base |
OLD | NEW |