Tracer.output.cc 8.23 KB
Newer Older
1
2
#include "Tracer.hh"

Philip Trettner's avatar
Philip Trettner committed
3
#include <algorithm>
4
#include <iostream>
5
#include <map>
6
#include <unordered_map>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>

#include <fstream>

namespace aion
{
namespace tracing
{
void write_dir(const std::string &path)
{
    write_json(path + "/trace.json");
    write_summary_csv(path + "/trace.csv");
}

Philip Trettner's avatar
Philip Trettner committed
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
static std::string beautify_function_name(std::string const &name)
{
    auto p = name.rfind(')');
    if (p == std::string::npos) // no (..)
    {
        p = name.rfind(' ');
        if (p == std::string::npos) // no space
            return name;
        return name.substr(p + 1); // "void foo" -> "foo"
    }

    int i = p;
    int db = 0;
    int da = 0;
    int cc = 0;
    while (i >= 0)
    {
        if (name[i] == ')')
            ++db;
        if (name[i] == '>')
            ++da;
        if (name[i] == '<')
            --da;
        if (name[i] == '(')
            --db;

        if (name[i] == ':' && da == 0 && db == 0)
        {
            ++cc;
            if (cc > 2)
                break;
        }

        if (name[i] == ' ' && da == 0 && db == 0)
            break;

        --i;
    }

    if (i < 0)
        return name;

    return name.substr(i + 1);
}

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
void write_speedscope_json(std::string const &filename)
{
    std::ofstream out(filename);
    if (!out.good())
        return;

    struct event
    {
        char type;
        int frame;
        uint64_t at;
    };
    struct stack_entry
    {
        int frame;
    };

    struct visitor : tracing::visitor
    {
        uint64_t min_cycles = std::numeric_limits<uint64_t>::max();
        uint64_t max_cycles = 0;
        uint32_t last_cpu = 0;
        std::unordered_map<location *, int> frames;
        std::vector<location *> locations;
        std::vector<stack_entry> stack;
        std::vector<event> events;
        std::thread::id thread;

        int frame_of(location *loc)
        {
            auto it = frames.find(loc);
            if (it != frames.end())
                return it->second;

            auto f = frames.size();
            frames[loc] = f;
            locations.push_back(loc);
            return f;
        }

        virtual void on_thread(std::thread::id thread) override
        {
            close_pending_actions();

            this->thread = thread;
            stack.clear();
        }
        virtual void on_trace_start(tracing::location *loc, uint64_t cycles, uint32_t cpu) override
        {
            last_cpu = cpu;
            min_cycles = std::min(min_cycles, cycles);
            max_cycles = std::max(max_cycles, cycles);

            auto f = frame_of(loc);
            events.push_back({'O', f, cycles});

            stack.push_back({f});
        }
        virtual void on_trace_end(uint64_t cycles, uint32_t cpu) override
        {
            last_cpu = cpu;
            min_cycles = std::min(min_cycles, cycles);
            max_cycles = std::max(max_cycles, cycles);

            auto se = stack.back();
            stack.pop_back();

            events.push_back({'C', se.frame, cycles});
        }

        void close_pending_actions()
        {
            while (!stack.empty())
                on_trace_end(max_cycles, last_cpu);
        }
    };
    visitor v;
    tracing::visit_thread(v);
    v.close_pending_actions();

    out << "{";
    out << "\"version\":\"0.0.1\",";
    out << "\"$schema\": \"https://www.speedscope.app/file-format-schema.json\",";
    out << "\"shared\":{";
    out << "\"frames\":[";
    for (auto i = 0u; i < v.locations.size(); ++i)
    {
        auto loc = v.locations[i];
        if (i > 0)
            out << ",";
        out << "{";
Philip Trettner's avatar
Philip Trettner committed
157
        out << "\"name\":\"" << (std::string(loc->name).empty() ? beautify_function_name(loc->function) : loc->name) << "\",";
158
159
160
        std::string escapedFilePath = loc->file;
        std::replace(escapedFilePath.begin(), escapedFilePath.end(), '\\', '/');
        out << "\"file\":\"" << escapedFilePath << "\",";
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        out << "\"line\":" << loc->line << "";
        out << "}";
    }
    out << "]";
    out << "},";
    out << "\"profiles\":[{";
    out << "\"type\":\"evented\",";
    out << "\"name\":\"Aion Trace\",";
    out << "\"unit\":\"none\",";
    out << "\"startValue\":0,";
    out << "\"endValue\":" << v.max_cycles - v.min_cycles << ",";
    out << "\"events\":[";
    auto first = true;
    for (auto const &e : v.events)
    {
        if (!first)
            out << ",";
        first = false;
        out << "{";
        out << "\"type\":\"" << e.type << "\",";
        out << "\"frame\":" << e.frame << ",";
        out << "\"at\":" << e.at - v.min_cycles;
        out << "}";
    }
    out << "]";
    out << "}]";
    out << "}";
}

void write_collapsed_stack(std::string const &filename)
{
    std::ofstream out(filename);
    if (!out.good())
        return;

    std::cerr << "Not implemented" << std::endl;
    /*
        struct stack_entry
        {
            location *loc;
            uint64_t cycles;
            std::string prefix;
        };

        struct visitor : tracing::visitor
        {
            std::vector<stack_entry> stack;
            std::thread::id thread;

            virtual void on_thread(std::thread::id thread) override
            {
                this->thread = thread;
                stack.clear();
                // TODO: close pending actions
            }
            virtual void on_trace_start(tracing::location *loc, uint64_t cycles, uint32_t cpu) override
            {
                std::string name = loc->name;
                std::string function = loc->function;
                auto prefix = loc->name loc->function
                //
                stack.push_back({loc, cycles});
            }
            virtual void on_trace_end(uint64_t cycles, uint32_t cpu) override
            {
                auto se = stack.back();
                stack.pop_back();
                auto dt = cycles - se.cycles;


            }
        };
        visitor v;
        tracing::visit_thread(v);*/
}

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
void write_json(const std::string &filename)
{
    std::ofstream out(filename);
    if (!out.good())
        return;

    // TODO:
    out << "implement me";
}

void write_summary_csv(const std::string &filename)
{
    std::ofstream out(filename);
    if (!out.good())
        return;

    struct entry
    {
        int count = 0;
        uint64_t cycles_total = 0;
        uint64_t cycles_children = 0;
        uint64_t cycles_min = std::numeric_limits<uint64_t>::max();
        uint64_t cycles_max = 0;
    };

    struct stack_entry
    {
        location *loc;
        uint64_t cycles;
        uint64_t cycles_children;
    };

    struct visitor : tracing::visitor
    {
        std::map<location *, entry> entries;
        std::vector<stack_entry> stack;
        int depth = 1;
        std::thread::id thread;
        virtual void on_thread(std::thread::id thread) override
        {
            this->thread = thread;
            stack.clear();
            // TODO: close pending actions
        }
        virtual void on_trace_start(tracing::location *loc, uint64_t cycles, uint32_t cpu) override { stack.push_back({loc, cycles, 0}); }
        virtual void on_trace_end(uint64_t cycles, uint32_t cpu) override
        {
            auto se = stack.back();
            stack.pop_back();
            auto dt = cycles - se.cycles;

            auto &e = entries[se.loc];
            e.count++;
            e.cycles_total += dt;
            e.cycles_children += se.cycles_children;
            e.cycles_min = std::min(e.cycles_min, dt);
            e.cycles_max = std::max(e.cycles_max, dt);

            if (!stack.empty())
                stack.back().cycles_children += dt;
        }
    };
    visitor v;
    tracing::visit_thread(v);

    out << "name,file,function,count,total,avg,min,max,total_body,avg_body\n";
    for (auto const &kvp : v.entries)
    {
        auto l = kvp.first;
        auto e = kvp.second;
        out << '"' << l->name << '"' << ",";
        out << '"' << l->file << ":" << l->line << '"' << ",";
        out << '"' << l->function << '"' << ",";
        out << e.count << ",";
        out << e.cycles_total << ",";
312
        out << e.cycles_total / e.count << ",";
313
314
315
        out << e.cycles_min << ",";
        out << e.cycles_max << ",";
        out << e.cycles_total - e.cycles_children << ",";
316
        out << (e.cycles_total - e.cycles_children) / e.count;
317
318
319
320
321
        out << "\n";
    }
}
}
}