1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef memory_profiler_CompactTraceTable_h
#define memory_profiler_CompactTraceTable_h
#include "mozilla/HashFunctions.h"
#include "nsDataHashtable.h"
#include "nsTArray.h"
namespace mozilla {
struct TrieNode final
{
uint32_t parentIdx;
uint32_t nameIdx;
bool operator==(const TrieNode t) const
{
return parentIdx == t.parentIdx && nameIdx == t.nameIdx;
}
uint32_t Hash() const
{
return HashGeneric(parentIdx, nameIdx);
}
};
// This class maps a Node of type T to its parent's index in the
// map. When serializing, the map is traversed and put into an ordered
// array of Nodes.
template<typename KeyClass, typename T>
class NodeIndexMap final
{
public:
uint32_t Insert(const T& e)
{
uint32_t index = mMap.Count();
if (!mMap.Get(e, &index)) {
mMap.Put(e, index);
}
return index;
}
nsTArray<T> Serialize() const
{
nsTArray<T> v;
v.SetLength(mMap.Count());
for (auto iter = mMap.ConstIter(); !iter.Done(); iter.Next()) {
v[iter.Data()] = iter.Key();
}
return v;
}
uint32_t Size() const
{
return mMap.Count();
}
void Clear()
{
mMap.Clear();
}
private:
nsDataHashtable<KeyClass, uint32_t> mMap;
};
// Backtraces are stored in a trie to save spaces.
// Function names are stored in an unique table and TrieNodes contain indexes
// into that table.
// The trie is implemented with a hash table; children are stored in
// traces[TrieNode{parent node index, branch/function name index}].
class CompactTraceTable final
{
public:
CompactTraceTable()
{
mNames.Insert(nsAutoCString("(unknown)"));
mTraces.Insert(TrieNode{0, 0});
}
nsTArray<nsCString> GetNames() const
{
return mNames.Serialize();
}
nsTArray<TrieNode> GetTraces() const
{
return mTraces.Serialize();
}
// Returns an ID to a stacktrace.
uint32_t Insert(const nsTArray<nsCString>& aRawStacktrace)
{
uint32_t parent = 0;
for (auto& frame: aRawStacktrace) {
parent = mTraces.Insert(TrieNode{parent, mNames.Insert(frame)});
}
return parent;
}
void Reset()
{
mNames.Clear();
mTraces.Clear();
}
private:
NodeIndexMap<nsCStringHashKey, nsCString> mNames;
NodeIndexMap<nsGenericHashKey<TrieNode>, TrieNode> mTraces;
};
} // namespace mozilla
#endif // memory_profiler_CompactTraceTable_h
|