summaryrefslogtreecommitdiffstats
path: root/vespalib/src/apps/vespa-tsan-digest/tsan_digest.cpp
blob: bebb32ac1ec8c07cace1db361dbdacca00e66053 (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
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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/size_literals.h>
#include <xxhash.h>
#include <cassert>
#include <vector>
#include <map>
#include <memory>
#include <algorithm>
#include <unistd.h>
#include <string.h>

using vespalib::make_string_short::fmt;

constexpr auto npos = vespalib::string::npos;

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

size_t trace_limit = 7;

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

class Hasher {
private:
    XXH3_state_t *_state;
public:
    Hasher() : _state(XXH3_createState()) { assert(_state != nullptr && "Out of memory!"); }
    ~Hasher() { XXH3_freeState(_state); }
    void reset() { XXH3_64bits_reset(_state); }
    void update(const char *buf, size_t len) { XXH3_64bits_update(_state, buf, len); }
    uint64_t get() const { return XXH3_64bits_digest(_state); }
};

uint64_t get_hash(const std::vector<vespalib::string> &list) {
    static Hasher hasher;
    hasher.reset();
    for (const auto &item: list) {
        hasher.update(item.data(), item.size());
    }
    return hasher.get();
}

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

class StackTrace {
private:
    vespalib::string _heading;
    std::vector<vespalib::string> _frames;
    uint64_t _hash;
public:
    StackTrace(const vespalib::string &heading) noexcept
    : _heading(heading), _frames(), _hash() {}
    void add_frame(const vespalib::string &frame) {
        _frames.push_back(frame);
    }
    void done() { _hash = get_hash(_frames); }
    uint64_t hash() const { return _hash; }
    void dump(FILE *dst) const {
        fprintf(dst, "%s\n", _heading.c_str());
        for (const auto &frame: _frames) {
            fprintf(dst, "%s\n", frame.c_str());            
        }
        fprintf(dst, "\n");
    }
};

vespalib::string make_trace_heading(const vespalib::string &line) {
    auto pos = line.find(" at 0x");
    if ((pos != npos) && (line.find("Location") == npos)) {
        return line.substr(0, pos);
    }
    return line;
}

std::vector<StackTrace> extract_traces(const std::vector<vespalib::string> &lines, size_t cutoff) {
    std::vector<StackTrace> result;
    for (size_t i = 1; (i < lines.size()) && (result.size() < cutoff); ++i) {
        auto pos = lines[i].find("#0 ");
        if (pos != npos) {
            size_t start = i;
            result.emplace_back(make_trace_heading(lines[i - 1]));
            result.back().add_frame(lines[i]);
            for (i = i + 1; i < lines.size(); ++i) {
                if (((i - start) > trace_limit) ||
                    (lines[i].find("#") == npos))
                {
                    break;
                }
                result.back().add_frame(lines[i]);
            }
            result.back().done();
        }
    }
    return result;
};

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

enum class ReportType { UNKNOWN, RACE };

ReportType detect_report_type(const std::vector<vespalib::string> &lines) {
    for (const auto &line: lines) {
        if (starts_with(line, "WARNING: ThreadSanitizer: data race")) {
            return ReportType::RACE;
        }
    }
    return ReportType::UNKNOWN;
}

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

bool is_delimiter(const vespalib::string &line) {
    // TSAN delimiter is 18=, look for at least 16=
    return (line.find("================") < line.size());
}

void dump_delimiter(FILE *dst) {
    fprintf(dst, "==================\n");
}

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

struct Report {
    using UP = std::unique_ptr<Report>;
    virtual vespalib::string make_key() const = 0;
    virtual void add(Report::UP report) = 0;
    virtual size_t count() const = 0;
    virtual void dump(FILE *dst) const = 0;
    virtual ~Report() {}
};

class RawReport : public Report {
private:
    std::vector<vespalib::string> _lines;
public:
    RawReport(const std::vector<vespalib::string> &lines)
      : _lines(lines) {}
    vespalib::string make_key() const override {
        return fmt("raw:%zu", get_hash(_lines));
    }
    void add(Report::UP) override {
        fprintf(stderr, "WARNING: hash collision for raw report\n");
    }
    size_t count() const override { return 1; }
    void dump(FILE *dst) const override {
        for (const auto &line: _lines) {
            fprintf(dst, "%s\n", line.c_str());            
        }
    }
};

class RaceReport : public Report {
private:
    StackTrace _trace1;
    StackTrace _trace2;
    size_t _total;
    size_t _inverted;

public:
    RaceReport(const StackTrace &trace1, const StackTrace &trace2)
      : _trace1(trace1), _trace2(trace2), _total(1), _inverted(0) {}

    vespalib::string make_key() const override {
        if (_trace2.hash() < _trace1.hash()) {
            return fmt("race:%zu,%zu", _trace2.hash(), _trace1.hash());
        }
        return fmt("race:%zu,%zu", _trace1.hash(), _trace2.hash());
    }
    void add(Report::UP report) override {
        // should have correct type due to key prefix
        const RaceReport &rhs = dynamic_cast<RaceReport&>(*report);
        ++_total;
        if (_trace1.hash() != rhs._trace1.hash()) {
            ++_inverted;
        }
    }
    size_t count() const override { return _total; }
    void dump(FILE *dst) const override {
        fprintf(dst, "WARNING: ThreadSanitizer: data race\n");
        _trace1.dump(dst);
        _trace2.dump(dst);
        fprintf(dst, "INFO: total: %zu (inverted: %zu)\n", _total, _inverted);
    }
};

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

size_t total_reports = 0;
std::map<vespalib::string,Report::UP> reports;

void handle_report(std::unique_ptr<Report> report) {
    ++total_reports;
    auto [pos, first] = reports.try_emplace(report->make_key(), std::move(report));
    if (!first) {
        assert(report && "should still be valid");
        pos->second->add(std::move(report));
    }
}

void make_report(const std::vector<vespalib::string> &lines) {
    auto type = detect_report_type(lines);
    if (type == ReportType::RACE) {
        auto traces = extract_traces(lines, 2);
        if (traces.size() == 2) {
            return handle_report(std::make_unique<RaceReport>(traces[0], traces[1]));
        }
    }
    return handle_report(std::make_unique<RawReport>(lines));
}

void handle_line(const vespalib::string &line) {
    static bool inside = false;
    static std::vector<vespalib::string> lines;
    if (is_delimiter(line)) {
        inside = !inside;
        if (!inside && !lines.empty()) {
            make_report(lines);
            lines.clear();
        }
    } else if (inside) {
        lines.push_back(line);
    }
}

void read_input() {
    char buf[64_Ki];
    bool eof = false;
    vespalib::string line;
    while (!eof) {
        ssize_t res = read(STDIN_FILENO, buf, sizeof(buf));
        if (res < 0) {
            throw fmt("error reading stdin: %s", strerror(errno));
        }
        eof = (res == 0);
        for (int i = 0; i < res; ++i) {
            if (buf[i] == '\n') {
                handle_line(line);
                line.clear();
            } else {
                line.push_back(buf[i]);
            }
        }
    }
    if (!line.empty()) {
        handle_line(line);
    }
}

void write_output() {
    std::vector<Report*> list;
    list.reserve(reports.size());
    for (const auto &[key, value]: reports) {
        list.push_back(value.get());
    }
    std::sort(list.begin(), list.end(),
              [](const auto &a, const auto &b) {
                  return (a->count() > b->count());
              });
    for (const auto *report: list) {
        dump_delimiter(stdout);
        report->dump(stdout);
        dump_delimiter(stdout);
    }
    fprintf(stderr, "%zu reports in, %zu reports out\n", total_reports, reports.size());
}

int main(int, char **) {
    try {
        read_input();
        write_output();
    } catch (vespalib::string &err) {
        fprintf(stderr, "%s\n", err.c_str());
        return 1;
    }
    return 0;
}