memory tracing - store parents array instead of children table

This commit is contained in:
Matthias Hochsteger 2020-11-24 15:47:25 +01:00
parent 916eb09f1e
commit efdc57885a
3 changed files with 16 additions and 27 deletions

View File

@ -942,7 +942,7 @@ namespace ngcore
size_t imax_mem_allocated = 0; size_t imax_mem_allocated = 0;
const auto & names = MemoryTracer::GetNames(); const auto & names = MemoryTracer::GetNames();
const auto & tree = MemoryTracer::GetTree(); const auto & parents = MemoryTracer::GetParents();
size_t N = names.size(); size_t N = names.size();
Array<size_t> mem_allocated_id; Array<size_t> mem_allocated_id;
@ -1006,39 +1006,27 @@ namespace ngcore
Array<TreeNode*> nodes; Array<TreeNode*> nodes;
nodes.SetSize(N); nodes.SetSize(N);
nodes = nullptr; nodes = nullptr;
Array<Array<int>> children(N);
Array<size_t> sorting; // topological sorting (parents before children) Array<size_t> sorting; // topological sorting (parents before children)
sorting.SetAllocSize(N); sorting.SetAllocSize(N);
ArrayMem<size_t, 100> stack;
// find root nodes in memory tracer tree, i.e. they have no parents
Array<int> parents;
parents.SetSize(N);
parents = -1;
for( const auto & [iparent, children] : tree )
for (auto child_id : children)
{
if(parents[child_id] != -1)
std::cerr << "Error in memory tracer: multiple parents found for " << names[child_id] << std::endl;
parents[child_id] = iparent;
}
for(auto i : IntRange(1, N)) for(auto i : IntRange(1, N))
if(parents[i]==-1) children[parents[i]].Append(i);
{
sorting.Append(i); ArrayMem<size_t, 100> stack;
if(tree.count(i)) sorting.Append(0);
stack.Append(i); stack.Append(0);
}
while(stack.Size()) while(stack.Size())
{ {
auto current = stack.Last(); auto current = stack.Last();
stack.DeleteLast(); stack.DeleteLast();
for(const auto child : tree.at(current)) for(const auto child : children[current])
{ {
sorting.Append(child); sorting.Append(child);
if(tree.count(child)) if(children[child].Size())
stack.Append(child); stack.Append(child);
} }
} }

View File

@ -114,8 +114,8 @@ namespace ngcore
NgProfiler prof; // NOLINT NgProfiler prof; // NOLINT
#ifdef NETGEN_TRACE_MEMORY #ifdef NETGEN_TRACE_MEMORY
std::vector<std::string> MemoryTracer::names{"root"}; std::vector<std::string> MemoryTracer::names{"all"};
std::map< int, std::vector<int> > MemoryTracer::tree; std::vector<int> MemoryTracer::parents{-1};
#endif // NETGEN_TRACE_MEMORY #endif // NETGEN_TRACE_MEMORY
} // namespace ngcore } // namespace ngcore

View File

@ -324,12 +324,13 @@ namespace ngcore
{ {
#ifdef NETGEN_TRACE_MEMORY #ifdef NETGEN_TRACE_MEMORY
NGCORE_API static std::vector<std::string> names; NGCORE_API static std::vector<std::string> names;
NGCORE_API static std::map< int, std::vector<int> > tree; NGCORE_API static std::vector<int> parents;
static int CreateId(const std::string& name) static int CreateId(const std::string& name)
{ {
int id = names.size(); int id = names.size();
names.push_back(name); names.push_back(name);
parents.push_back(0);
if(id==10*NgProfiler::SIZE) if(id==10*NgProfiler::SIZE)
std::cerr << "Allocated " << id << " MemoryTracer objects" << std::endl; std::cerr << "Allocated " << id << " MemoryTracer objects" << std::endl;
return id; return id;
@ -400,7 +401,7 @@ namespace ngcore
void Track( T & obj, const std::string& name ) const void Track( T & obj, const std::string& name ) const
{ {
obj.GetMemoryTracer().Activate(obj, name); obj.GetMemoryTracer().Activate(obj, name);
tree[id].push_back(obj.GetMemoryTracer().GetId()); parents[obj.GetMemoryTracer().GetId()] = id;
} }
static std::string GetName(int id) static std::string GetName(int id)
@ -433,7 +434,7 @@ namespace ngcore
static const std::vector<std::string> & GetNames() { return names; } static const std::vector<std::string> & GetNames() { return names; }
static const std::map<int, std::vector<int>> & GetTree() { return tree; } static const std::vector<int> & GetParents() { return parents; }
#else // NETGEN_TRACE_MEMORY #else // NETGEN_TRACE_MEMORY
public: public:
MemoryTracer() {} MemoryTracer() {}