aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/util/traceutil.h
blob: fe424d7259ebc16f26c1330835c5345a3fc89227 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <dlfcn.h>
#include <csignal>
#include <stdlib.h>
#include <cassert>
#include <vector>
#include <vespamalloc/util/index.h>
#include <vespamalloc/util/callstack.h>
#include <vespamalloc/util/callgraph.h>


namespace vespamalloc {

using StackElem = StackEntry;
typedef CallGraph<StackElem, 0x10000, Index> CallGraphT;

class Aggregator
{
public:
    Aggregator();
    ~Aggregator();
    void push_back(size_t num, const string & s) { _map.emplace_back(num, s); }
    friend asciistream & operator << (asciistream & os, const Aggregator & v);
private:
    typedef std::vector< std::pair<size_t, string> > Map;
    Map _map;
};


template<typename N>
class DumpGraph
{
public:
    DumpGraph(Aggregator * aggregator, const char * s="{ ", const char * end=" }") __attribute__ ((noinline));
    ~DumpGraph() __attribute__ ((noinline));
    void handle(const N & node) __attribute__ ((noinline));
private:
    string       _string;
    string       _endString;
    size_t       _sum;
    size_t       _min;
    Aggregator * _aggregator;
};

asciistream & operator << (asciistream & os, const Aggregator & v);

template<typename N>
DumpGraph<N>::DumpGraph(Aggregator * aggregator, const char * start, const char * end) :
    _string(start),
    _endString(end),
    _sum(0),
    _min(-1),
    _aggregator(aggregator)
{
}

template<typename N>
DumpGraph<N>::~DumpGraph() = default;

template<typename N>
void DumpGraph<N>::handle(const N & node)
{
    _sum += node.count();
    if (node.count() < _min) {
        _min = node.count();
    }
    asciistream os;
    os << ' ' << node;
    _string += os.c_str();
    if (node.callers() == nullptr) {
        _string += _endString;
        _aggregator->push_back(_min, _string);
    }
}

}