aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/tests/test2/testgraph.cpp
blob: b213a517f0eed67e3fc7db81733a2d1f01da45ee (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
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespamalloc/util/index.h>
#include <vespamalloc/util/callgraph.h>
#include <vespamalloc/util/callstack.h>
#include <vespamalloc/util/traceutil.h>
#include <string>

using namespace vespamalloc;

//typedef StackEntry<StackFrameReturnEntry> StackElem;
typedef CallGraph<int, 0x1000, Index> CallGraphIntT;
typedef CallGraph<StackElem, 0x1000, Index> CallGraphStackEntryT;

namespace vespalibtest {

template <typename T>
class DumpGraph
{
public:
    DumpGraph(const char * s="") : _string(s) { }
    void handle(const T & node)
    {
        asciistream os;
        os << ' ' << node;
        _string += os.c_str();
        if (node.callers() == nullptr) {
            printf("%s\n", _string.c_str());
        }
    }
    const std::string & str() const { return _string; }
private:
    std::string _string;
};

}
void testint() {
    CallGraphIntT callGraph;
    vespalibtest::DumpGraph<CallGraphIntT::Node> dump("int: ");
    int s1[3] = { 1, 2, 3 };
    int s2[3] = { 1, 2, 4 };
    int s3[1] = { 1 };
    int s4[3] = { 1, 3, 4 };
    callGraph.addStack(s1, 3);
    callGraph.addStack(s2, 3);
    callGraph.addStack(s3, 1);
    callGraph.addStack(s4, 3);
    callGraph.traverseDepth(dump);
    printf("%s\n", dump.str().c_str());
}

void teststackentry() {
    CallGraphStackEntryT callGraph;
    vespalibtest::DumpGraph<CallGraphStackEntryT::Node> dump("callstack: ");
    StackElem s1[3] = { StackElem((void *)1), StackElem((void *)2), StackElem((void *)3) };
    StackElem s2[3] = { StackElem((void *)1), StackElem((void *)2), StackElem((void *)4) };
    StackElem s3[1] = { StackElem((void *)1) };
    StackElem s4[3] = { StackElem((void *)1), StackElem((void *)3), StackElem((void *)4) };
    callGraph.addStack(s1, 3);
    callGraph.addStack(s2, 3);
    callGraph.addStack(s3, 1);
    callGraph.addStack(s4, 3);
    callGraph.traverseDepth(dump);
    printf("%s\n", dump.str().c_str());
}

void testaggregator() {
    CallGraphStackEntryT callGraph;
    StackElem s1[3] = { StackElem((void *)1), StackElem((void *)2), StackElem((void *)3) };
    StackElem s2[3] = { StackElem((void *)1), StackElem((void *)2), StackElem((void *)4) };
    StackElem s3[1] = { StackElem((void *)1) };
    StackElem s4[3] = { StackElem((void *)1), StackElem((void *)3), StackElem((void *)4) };
    callGraph.addStack(s1, 3);
    callGraph.addStack(s2, 3);
    callGraph.addStack(s3, 1);
    callGraph.addStack(s4, 3);
    Aggregator agg;
    DumpGraph<CallGraphT::Node> dump(&agg, "{ ", " }");
    callGraph.traverseDepth(dump);
    asciistream ost;
    ost << agg;
    printf("%s\n", ost.c_str());
}
int main (int argc, char *argv[])
{
    (void) argc;
    (void) argv;
    testint();
    teststackentry();
    testaggregator();
    return 0;
}