aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/test/time_tracer.cpp
blob: 6bcd0ac80f74f7fd119e014d9d87eefca4732aab (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
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
157
158
159
160
161
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "time_tracer.h"

namespace vespalib::test {

//-----------------------------------------------------------------------------

TimeTracer &
TimeTracer::master()
{
    static TimeTracer instance;
    return instance;
}

//-----------------------------------------------------------------------------

thread_local TimeTracer::ThreadState *TimeTracer::_thread_state = nullptr;

//-----------------------------------------------------------------------------

TimeTracer::Tag::Tag(const vespalib::string &name)
    : _id(master().get_tag_id(name))
{
}

TimeTracer::Tag::~Tag() = default;

//-----------------------------------------------------------------------------

double
TimeTracer::Record::ms_duration() const
{
    return std::chrono::duration<double, std::milli>(stop - start).count();
}

vespalib::string
TimeTracer::Record::tag_name() const
{
    return master().get_tag_name(tag_id);
}

//-----------------------------------------------------------------------------

bool
TimeTracer::Extractor::keep(const Record &entry) const
{
    return ((!_by_thread || (entry.thread_id == _thread_id)) &&
            (!_by_tag    || (entry.tag_id == _tag_id))       &&
            (!_by_time   || (entry.stop > _a))               &&
            (!_by_time   || (entry.start < _b)));
}

TimeTracer::Extractor &&
TimeTracer::Extractor::by_thread(uint32_t thread_id) &&
{
    _by_thread = true;
    _thread_id = thread_id;
    return std::move(*this);
}

TimeTracer::Extractor &&
TimeTracer::Extractor::by_tag(uint32_t tag_id) &&
{
    _by_tag = true;
    _tag_id = tag_id;
    return std::move(*this);
}

TimeTracer::Extractor &&
TimeTracer::Extractor::by_time(time_point a, time_point b) &&
{
    _by_time = true;
    _a = a;
    _b = b;
    return std::move(*this);
}

std::vector<TimeTracer::Record>
TimeTracer::Extractor::get() const
{
    return master().extract_impl(*this);
}

TimeTracer::Extractor::~Extractor() = default;

//-----------------------------------------------------------------------------

void
TimeTracer::init_thread_state() noexcept
{
    _thread_state = master().create_thread_state();
}

//-----------------------------------------------------------------------------

TimeTracer::TimeTracer()
    : _lock(),
      _state_list(),
      _tags(),
      _tag_names()
{
}

uint32_t
TimeTracer::get_tag_id(const vespalib::string &tag_name)
{
    std::lock_guard guard(_lock);
    auto pos = _tags.find(tag_name);
    if (pos != _tags.end()) {
        return pos->second;
    }
    uint32_t id = _tags.size();
    _tags[tag_name] = id;
    _tag_names.push_back(tag_name);
    return id;
}

vespalib::string
TimeTracer::get_tag_name(uint32_t tag_id)
{
    std::lock_guard guard(_lock);
    if (tag_id < _tag_names.size()) {
        return _tag_names[tag_id];
    } else {
        return "<undef>";
    }
}

TimeTracer::ThreadState *
TimeTracer::create_thread_state()
{
    std::lock_guard guard(_lock);
    uint32_t thread_id = _state_list.size();
    _state_list.push_back(std::make_unique<ThreadState>(thread_id));
    return _state_list.back().get();
}

std::vector<TimeTracer::Record>
TimeTracer::extract_impl(const Extractor &extractor)
{
    std::lock_guard guard(_lock);
    std::vector<Record> list;
    for (const ThreadState::UP &state: _state_list) {
        const LogEntry *entry = state->get_log_entries();
        while (entry != nullptr) {
            Record record(state->thread_id(), entry->tag_id, entry->start, entry->stop);
            if (extractor.keep(record)) {
                list.push_back(record);
            }
            entry = entry->next;
        }
    }
    return list;
}

TimeTracer::~TimeTracer() = default;

//-----------------------------------------------------------------------------

} // namespace vespalib::test