2015-05-08 06:23:20 +03:00
|
|
|
/* -*- 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"
|
|
|
|
|
2015-08-26 13:55:55 +03:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2015-05-08 06:23:20 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
struct TrieNode final
|
|
|
|
{
|
|
|
|
uint32_t parentIdx;
|
|
|
|
uint32_t nameIdx;
|
|
|
|
bool operator==(const TrieNode t) const
|
|
|
|
{
|
|
|
|
return parentIdx == t.parentIdx && nameIdx == t.nameIdx;
|
|
|
|
}
|
2015-08-26 13:55:55 +03:00
|
|
|
uint32_t Hash() const
|
2015-05-08 06:23:20 +03:00
|
|
|
{
|
2015-08-26 13:55:55 +03:00
|
|
|
return HashGeneric(parentIdx, nameIdx);
|
2015-05-08 06:23:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2015-08-26 13:55:55 +03:00
|
|
|
// array of Nodes.
|
|
|
|
template<typename KeyClass, typename T>
|
2015-05-08 06:23:20 +03:00
|
|
|
class NodeIndexMap final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint32_t Insert(const T& e)
|
|
|
|
{
|
2015-08-26 13:55:55 +03:00
|
|
|
uint32_t index = mMap.Count();
|
|
|
|
if (!mMap.Get(e, &index)) {
|
|
|
|
mMap.Put(e, index);
|
|
|
|
}
|
|
|
|
return index;
|
2015-05-08 06:23:20 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 13:55:55 +03:00
|
|
|
nsTArray<T> Serialize() const
|
2015-05-08 06:23:20 +03:00
|
|
|
{
|
2015-08-26 13:55:55 +03:00
|
|
|
nsTArray<T> v;
|
|
|
|
v.SetLength(mMap.Count());
|
|
|
|
for (auto iter = mMap.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
v[iter.Data()] = iter.Key();
|
2015-05-08 06:23:20 +03:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Size() const
|
|
|
|
{
|
2015-08-26 13:55:55 +03:00
|
|
|
return mMap.Count();
|
2015-05-08 06:23:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2015-08-26 13:55:55 +03:00
|
|
|
mMap.Clear();
|
2015-05-08 06:23:20 +03:00
|
|
|
}
|
|
|
|
private:
|
2015-08-26 13:55:55 +03:00
|
|
|
nsDataHashtable<KeyClass, uint32_t> mMap;
|
2015-05-08 06:23:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
2015-08-26 13:55:55 +03:00
|
|
|
mNames.Insert(nsAutoCString("(unknown)"));
|
2015-05-08 06:23:20 +03:00
|
|
|
mTraces.Insert(TrieNode{0, 0});
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:55:55 +03:00
|
|
|
nsTArray<nsCString> GetNames() const
|
2015-05-08 06:23:20 +03:00
|
|
|
{
|
|
|
|
return mNames.Serialize();
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:55:55 +03:00
|
|
|
nsTArray<TrieNode> GetTraces() const
|
2015-05-08 06:23:20 +03:00
|
|
|
{
|
|
|
|
return mTraces.Serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an ID to a stacktrace.
|
2015-08-26 13:55:55 +03:00
|
|
|
uint32_t Insert(const nsTArray<nsCString>& aRawStacktrace)
|
2015-05-08 06:23:20 +03:00
|
|
|
{
|
|
|
|
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:
|
2015-08-26 13:55:55 +03:00
|
|
|
NodeIndexMap<nsCStringHashKey, nsCString> mNames;
|
|
|
|
NodeIndexMap<nsGenericHashKey<TrieNode>, TrieNode> mTraces;
|
2015-05-08 06:23:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // memory_profiler_CompactTraceTable_h
|