summaryrefslogtreecommitdiffstats
path: root/tests/auto/perfdata/tst_perfdata.cpp
blob: 98551724d11cb69de7b52f5515327057e522fca9 (plain)
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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Enterprise Perf Profiler Add-on.
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in
** the file LICENSE.GPLv3 included in the packaging of this file. Please
** review the following information to ensure the GNU General Public License
** requirements will be met: https://www.gnu.org/licenses/gpl.html.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include "perfdata.h"
#include "perfparsertestclient.h"
#include "perfunwind.h"

#include <QBuffer>
#include <QDebug>
#include <QObject>
#include <QSignalSpy>
#include <QTest>
#include <QtEndian>
#include <QProcess>
#include <QRegularExpression>
#include <QStandardPaths>

class TestPerfData : public QObject
{
    Q_OBJECT
private slots:
    void testTracingData_data();
    void testTracingData();
    void testContentSize();
    void testFiles_data();
    void testFiles();
};

static void setupUnwind(PerfUnwind *unwind, PerfHeader *header, QIODevice *input,
                        PerfAttributes *attributes, PerfData *data, const QByteArray &expectedVersion)
{
    if (!header->isPipe()) {
        const qint64 filePos = input->pos();

        attributes->read(input, header);

        PerfFeatures features;
        features.read(input, header);

        if (header->hasFeature(PerfHeader::COMPRESSED))
            data->setCompressed(features.compressed());

        PerfTracingData tracingData = features.tracingData();
        if (tracingData.size() > 0)
            QCOMPARE(tracingData.version(), expectedVersion);

        unwind->features(features);
        const auto& attrs = attributes->attributes();
        for (auto it = attrs.begin(), end = attrs.end(); it != end; ++it)
            unwind->attr(PerfRecordAttr(it.value(), {it.key()}));
        const QByteArray &featureArch = features.architecture();
        unwind->setArchitecture(PerfRegisterInfo::archByName(featureArch));
        input->seek(filePos);
    }
}

static void process(PerfUnwind *unwind, QIODevice *input, const QByteArray &expectedVersion)
{
    PerfHeader header(input);
    PerfAttributes attributes;
    PerfData data(unwind, &header, &attributes);
    data.setSource(input);

    QSignalSpy spy(&data, &PerfData::finished);
    QObject::connect(&header, &PerfHeader::finished, &data, [&](){
        setupUnwind(unwind, &header, input, &attributes, &data, expectedVersion);
        data.read();
    });

    QObject::connect(&data, &PerfData::error, []() {
        QFAIL("PerfData reported an error");
    });

    header.read();
    QCOMPARE(spy.count(), 1);

    unwind->finalize();
}

void TestPerfData::testTracingData_data()
{
    QTest::addColumn<QString>("file");
    QTest::addColumn<uint>("flushes");
    QTest::addColumn<quint64>("maxTime");
    QTest::addColumn<bool>("stats");
    QTest::newRow("stream stats") << ":/probe.data.stream" << 1u << 13780586573357ull << true;
    QTest::newRow("file stats") << ":/probe.data.file" << 1u << 13732862274100ull << true;
    QTest::newRow("stream data") << ":/probe.data.stream" << 2u << 13780586522722ull << false;
    QTest::newRow("file data") << ":/probe.data.file" << 3u << 13732862219876ull << false;
}

void TestPerfData::testTracingData()
{
    QFETCH(QString, file);
    QFETCH(uint, flushes);
    QFETCH(quint64, maxTime);
    QFETCH(bool, stats);

    QBuffer output;
    QFile input(file);

    QVERIFY(input.open(QIODevice::ReadOnly));
    QVERIFY(output.open(QIODevice::WriteOnly));

    // Don't try to load any system files. They are not the same as the ones we use to report.
    PerfUnwind unwind(&output, QStringLiteral(":/"), QString(), QString(), QString(), stats);
    if (!stats) {
        QTest::ignoreMessage(QtWarningMsg,
                             QRegularExpression(QRegularExpression::escape(
                                 QStringLiteral("Could not find ELF file for "
                                                "/home/ulf/dev/untitled1-Qt_5_9_1_gcc_64-Profile/untitled1. "
                                                "This can break stack unwinding and lead to missing symbols."))));
        QTest::ignoreMessage(QtWarningMsg,
                             QRegularExpression(QRegularExpression::escape(
                                 QStringLiteral("Could not find ELF file for "
                                                "/lib/x86_64-linux-gnu/ld-2.24.so. "
                                                "This can break stack unwinding and lead to missing symbols."))));
        QTest::ignoreMessage(QtWarningMsg,
                             QRegularExpression(QRegularExpression::escape(
                                 QStringLiteral("Could not find ELF file for "
                                                "/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22. "
                                                "This can break stack unwinding and lead to missing symbols."))));
        QTest::ignoreMessage(QtWarningMsg,
                             QRegularExpression(QRegularExpression::escape(
                                 QStringLiteral("Could not find ELF file for "
                                                "/lib/x86_64-linux-gnu/libm-2.24.so. "
                                                "This can break stack unwinding and lead to missing symbols."))));
        QTest::ignoreMessage(QtWarningMsg,
                             QRegularExpression(QRegularExpression::escape(
                                 QStringLiteral("Could not find ELF file for "
                                                "/lib/x86_64-linux-gnu/libgcc_s.so.1. "
                                                "This can break stack unwinding and lead to missing symbols."))));
        QTest::ignoreMessage(QtWarningMsg,
                             QRegularExpression(QRegularExpression::escape(
                                 QStringLiteral("Could not find ELF file for "
                                                "/lib/x86_64-linux-gnu/libc-2.24.so. "
                                                "This can break stack unwinding and lead to missing symbols."))));
    }
    process(&unwind, &input, QByteArray("0.5"));

    if (stats) {
        const PerfUnwind::Stats stats = unwind.stats();
        QCOMPARE(stats.numSamples, 1u);
        QCOMPARE(stats.numMmaps, 120u);
        QCOMPARE(stats.numRounds, 2u);
        QCOMPARE(stats.numBufferFlushes, flushes);
        QCOMPARE(stats.numTimeViolatingSamples, 0u);
        QCOMPARE(stats.numTimeViolatingMmaps, 0u);
        QCOMPARE(stats.maxBufferSize, 15608u);
        QCOMPARE(stats.maxTime, maxTime);
        return;
    }

    output.close();
    output.open(QIODevice::ReadOnly);

    PerfParserTestClient client;
    client.extractTrace(&output);

    const QList<PerfParserTestClient::SampleEvent> samples = client.samples();
    QVERIFY(samples.length() > 0);
    for (const PerfParserTestClient::SampleEvent &sample : samples) {
        QCOMPARE(sample.values.size(), 1);
        const PerfParserTestClient::AttributeEvent attribute
                = client.attribute(sample.values[0].first);
        QCOMPARE(attribute.type, 2u);
        QVERIFY(attribute.config <= quint64(std::numeric_limits<qint32>::max()));
        const PerfParserTestClient::TracePointFormatEvent format
                = client.tracePointFormat(static_cast<qint32>(attribute.config));
        QCOMPARE(client.string(format.system), QByteArray("probe_untitled1"));
        QCOMPARE(client.string(format.name), QByteArray("main"));
        QCOMPARE(format.flags, 0u);

        QCOMPARE(sample.tracePointData.size(), 1);
        auto it = sample.tracePointData.constBegin();
        QCOMPARE(client.string(it.key()), QByteArray("__probe_ip"));
        QCOMPARE(it.value().typeId(), QMetaType::ULongLong);
    }
}

void TestPerfData::testContentSize()
{
    QString file(QStringLiteral(":/contentsize.data"));

    QBuffer output;
    QFile input(file);

    QVERIFY(input.open(QIODevice::ReadOnly));
    QVERIFY(output.open(QIODevice::WriteOnly));

    // Don't try to load any system files. They are not the same as the ones we use to report.
    PerfUnwind unwind(&output, QStringLiteral(":/"), QString(), QString(), QString(), true);
    process(&unwind, &input, QByteArray("0.5"));

    QCOMPARE(unwind.stats().numSamples, 69u);
}

Q_DECL_UNUSED static void compressFile(const QString& input, const QString& output = QString())
{
    QVERIFY(!input.isEmpty() && QFileInfo::exists(input));

    if (output.isEmpty()) {
        compressFile(input, input + QLatin1String(".zlib"));
        return;
    }

    QFile raw(input);
    QVERIFY(raw.open(QIODevice::ReadOnly));

    QFile compressed(output);
    QVERIFY(compressed.open(QIODevice::WriteOnly));

    compressed.write(qCompress(raw.readAll()));
}

static void uncompressFile(const QString& input, const QString& output = QString())
{
    QVERIFY(!input.isEmpty() && QFileInfo::exists(input));

    if (output.isEmpty()) {
        auto suffix = QLatin1String(".zlib");
        QVERIFY(input.endsWith(suffix));
        uncompressFile(input, input.chopped(suffix.size()));
        return;
    }

    QFile compressed(input);
    QVERIFY(compressed.open(QIODevice::ReadOnly));

    QFile raw(output);
    QVERIFY(raw.open(QIODevice::WriteOnly));

    raw.write(qUncompress(compressed.readAll()));
}

void TestPerfData::testFiles_data()
{
    QTest::addColumn<QString>("dataFile");

    // to add a new compressed binary, you'd run this test once with a line like the following:
    // compressFile(QFINDTESTDATA("vector_static_clang/vector_static_clang_v8.0.1"));

    // uncompress binaries to let unwinding work
    uncompressFile(QFINDTESTDATA("vector_static_clang/vector_static_clang_v8.0.1.zlib"));
    uncompressFile(QFINDTESTDATA("vector_static_gcc/vector_static_gcc_v9.1.0.zlib"));
    uncompressFile(QFINDTESTDATA("fork_static_gcc/fork.zlib"));
    uncompressFile(QFINDTESTDATA("parallel_static_gcc/parallel_static_gcc.zlib"));

    const auto files = {
        "vector_static_clang/perf.data",
        "vector_static_gcc/perf.data",
        "vector_static_gcc/perf.lbr.data",
        "vector_static_gcc/perf.data.zstd",
        "fork_static_gcc/perf.data.zstd",
        "parallel_static_gcc/perf.data.zstd",
    };
    for (auto file : files)
        QTest::addRow("%s", file) << file;
}

void TestPerfData::testFiles()
{
    QFETCH(QString, dataFile);
#ifndef HAVE_ZSTD
    if (dataFile.contains(QStringLiteral("zstd")))
        QSKIP("zstd support disabled, skipping test");
#endif

    const auto perfDataFileCompressed = QFINDTESTDATA(dataFile + QLatin1String(".zlib"));
    QVERIFY(!perfDataFileCompressed.isEmpty() && QFileInfo::exists(perfDataFileCompressed));
    uncompressFile(perfDataFileCompressed);

    const auto perfDataFile = QFINDTESTDATA(dataFile);
    QVERIFY(!perfDataFile.isEmpty() && QFileInfo::exists(perfDataFile));
    const auto expectedOutputFileCompressed = QString(perfDataFile + QLatin1String(".expected.txt.zlib"));
    const auto expectedOutputFileUncompressed = QString(perfDataFile + QLatin1String(".expected.txt"));
    const auto actualOutputFile = QString(perfDataFile + QLatin1String(".actual.txt"));

    QBuffer output;
    QVERIFY(output.open(QIODevice::WriteOnly));

    // Don't try to load any system files. They are not the same as the ones we use to report.
    PerfUnwind unwind(&output, QStringLiteral(":/"), QString(), QString(), QFileInfo(perfDataFile).absolutePath());
    {
        QFile input(perfDataFile);
        QVERIFY(input.open(QIODevice::ReadOnly));
        // don't try to parse kallsyms here, it's not the main point and it wouldn't be portable without the mapping file
        // from where we recorded the data. these files are usually large, and we don't want to bloat the repo too much
        if (QLatin1String(QTest::currentDataTag()) != QLatin1String("fork_static_gcc/perf.data.zstd")) {
            QTest::ignoreMessage(QtWarningMsg,
                                 QRegularExpression(QStringLiteral(
                                     "Failed to parse kernel symbol mapping file \".+\": Mapping is empty")));
        }
        unwind.setKallsymsPath(QProcess::nullDevice());

        auto version = QByteArray("0.5");
        if (dataFile == QLatin1String("parallel_static_gcc/perf.data.zstd"))
            version = "0.6";
        process(&unwind, &input, version);
    }

    output.close();
    output.open(QIODevice::ReadOnly);

    QString actualText;
    {
        QTextStream stream(&actualText);
        PerfParserTestClient client;
        client.extractTrace(&output);
        client.convertToText(stream);
        stream.flush();

        // some older platforms produce strange type names for complex doubles...
        // similarly, older systems failed to demangle some symbols
        // always use the new form as the canonical form in our expected files
        // and replace the actual text in case we run on an older system
        const std::pair<const char*, const char*> replacements[] = {
            {"doublecomplex ", "double _Complex"},
            {"_ZSt3cosIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_",
             "__gnu_cxx::__enable_if<__is_integer<int>::__value, double>::__type std::cos<int>(int)"},
            {"_ZSt3sinIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_",
             "__gnu_cxx::__enable_if<__is_integer<int>::__value, double>::__type std::sin<int>(int)"},
            {"_ZSt14__relocate_a_1IddENSt9enable_ifIXsr3std24__is_bitwise_relocatableIT_EE5valueEPS1_E4typeES2_S2_S2_"
             "RSaIT0_E",
             "std::enable_if<std::__is_bitwise_relocatable<double>::value, double*>::type std::__relocate_a_1<double, "
             "double>(double*, double*, double*, std::allocator<double>&)"}};
        for (const auto& replacement : replacements) {
            actualText.replace(QLatin1String(replacement.first), QLatin1String(replacement.second));
        }

        QFile actual(actualOutputFile);
        QVERIFY(actual.open(QIODevice::WriteOnly | QIODevice::Text));
        actual.write(actualText.toUtf8());
    }

    QString expectedText;
    {
        QFile expected(expectedOutputFileCompressed);
        QVERIFY(expected.open(QIODevice::ReadOnly));
        expectedText = QString::fromUtf8(qUncompress(expected.readAll()));
    }

    if (actualText != expectedText) {
        compressFile(actualOutputFile);

        const auto diff = QStandardPaths::findExecutable(QStringLiteral("diff"));
        if (!diff.isEmpty()) {
            {
                QFile expectedUncompressed(expectedOutputFileUncompressed);
                QVERIFY(expectedUncompressed.open(QIODevice::WriteOnly | QIODevice::Text));
                expectedUncompressed.write(expectedText.toUtf8());
            }
            QProcess::execute(diff, {QStringLiteral("-u"), expectedOutputFileUncompressed, actualOutputFile});
        }
    }
    QCOMPARE(actualText, expectedText);
}

QTEST_GUILESS_MAIN(TestPerfData)

#include "tst_perfdata.moc"