aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/execution_profiler.h
blob: a3fd3ae5f75f86ca08ecbc96c5a3a52988a361b0 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "time.h"

#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <functional>

namespace vespalib {

namespace slime { struct Cursor; }

/**
 * A simple single-threaded profiler used to measure where time is
 * spent when executing tasks that may depend on each other (doing one
 * task includes doing another task; like one function calls another
 * function). Each task is identified by a unique name. Data is
 * collected in real-time using signals about when a task is started
 * and when it completes. Any sub-task must complete before any parent
 * task. Any task may be executed any number of times and may depend
 * on any other task.
 **/
class ExecutionProfiler {
public:
    using TaskId = uint32_t;
    struct ReportContext;
    struct Impl {
        virtual ~Impl() = default;
        virtual void track_start(TaskId task) = 0;
        virtual void track_complete() = 0;
        virtual void report(slime::Cursor &obj, ReportContext &ctx) const = 0;
    };
    using NameMapper = std::function<vespalib::string(const vespalib::string &)>;

private:
    size_t _level;
    size_t _max_depth;
    std::vector<vespalib::string> _names;
    vespalib::hash_map<vespalib::string,size_t> _name_map;
    std::unique_ptr<Impl> _impl;

public:
    ExecutionProfiler(int32_t profile_depth);
    ~ExecutionProfiler();
    TaskId resolve(const vespalib::string &name);
    const vespalib::string &name_of(TaskId task) const { return _names[task]; }
    void start(TaskId task) {
        if (++_level <= _max_depth) {
            _impl->track_start(task);
        }
    }
    void complete() {
        if (--_level < _max_depth) {
            _impl->track_complete();
        }
    }
    void report(slime::Cursor &obj, const NameMapper &name_mapper =
                [](const vespalib::string &name) noexcept { return name; }) const;
};

}