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;
const auto & names = MemoryTracer::GetNames();
const auto & tree = MemoryTracer::GetTree();
const auto & parents = MemoryTracer::GetParents();
size_t N = names.size();
Array<size_t> mem_allocated_id;
@ -1006,39 +1006,27 @@ namespace ngcore
Array<TreeNode*> nodes;
nodes.SetSize(N);
nodes = nullptr;
Array<Array<int>> children(N);
Array<size_t> sorting; // topological sorting (parents before children)
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))
if(parents[i]==-1)
{
sorting.Append(i);
if(tree.count(i))
stack.Append(i);
}
children[parents[i]].Append(i);
ArrayMem<size_t, 100> stack;
sorting.Append(0);
stack.Append(0);
while(stack.Size())
{
auto current = stack.Last();
stack.DeleteLast();
for(const auto child : tree.at(current))
for(const auto child : children[current])
{
sorting.Append(child);
if(tree.count(child))
if(children[child].Size())
stack.Append(child);
}
}

View File

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

View File

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