File: tst_perfparser.cpp

package info (click to toggle)
hotspot 1.6.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,756 kB
  • sloc: cpp: 27,071; ansic: 281; sh: 261; python: 75; xml: 48; makefile: 16
file content (1023 lines) | stat: -rw-r--r-- 42,121 bytes parent folder | download
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
/*
    SPDX-FileCopyrightText: Nate Rogers <nate.rogers@kdab.com>
    SPDX-FileCopyrightText: Milian Wolff <milian.wolff@kdab.com>
    SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <QDebug>
#include <QObject>
#include <QProcess>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTemporaryFile>
#include <QTest>
#include <QTextStream>

#include "data.h"
#include "perfparser.h"
#include "perfrecord.h"
#include "recordhost.h"
#include "util.h"

#include "../testutils.h"
#include <hotspot-config.h>

#if QT_VERSION < QT_VERSION_CHECK(6, 3, 0)
// workaround issues with string literals in QTest that we cannot workaround locally
// this was fixed upstream, see: https://codereview.qt-project.org/c/qt/qtbase/+/354227
// clazy:excludeall=qstring-allocations
#endif

namespace {
template<typename T>
bool searchForChildSymbol(const T& root, const QString& searchString, bool exact = true)
{
    if (exact && root.symbol.symbol == searchString) {
        return true;
    } else if (!exact && root.symbol.symbol.contains(searchString)) {
        return true;
    } else {
        for (const auto& entry : root.children) {
            if (searchForChildSymbol(entry, searchString, exact)) {
                return true;
            }
        }
    }
    return false;
}

bool compareCosts(const Data::TopDown& lhs, const Data::TopDown& rhs, const Data::TopDownResults& results,
                  int costIndex)
{
    return results.inclusiveCosts.cost(costIndex, lhs.id) < results.inclusiveCosts.cost(costIndex, rhs.id);
}

bool compareCosts(const Data::BottomUp& lhs, const Data::BottomUp& rhs, const Data::BottomUpResults& results,
                  int costIndex)
{
    return results.costs.cost(costIndex, lhs.id) < results.costs.cost(costIndex, rhs.id);
}

template<typename Results>
int maxElementTopIndex(const Results& collection, int costIndex = 0)
{
    using DataType = decltype(*collection.root.children.begin());
    auto topResult = std::max_element(collection.root.children.constBegin(), collection.root.children.constEnd(),
                                      [collection, costIndex](const DataType& lhs, const DataType& rhs) {
                                          return compareCosts(lhs, rhs, collection, costIndex);
                                      });
    return std::distance(collection.root.children.begin(), topResult);
}
}

struct ComparableSymbol
{
    ComparableSymbol() = default;

    ComparableSymbol(Data::Symbol symbol)
        : symbol(std::move(symbol))
        , isPattern(false)
    {
    }

    ComparableSymbol(QString symbol, QString binary)
        : pattern({{std::move(symbol), std::move(binary)}})
        , isPattern(true)
    {
    }

    struct Pattern
    {
        QString symbol;
        QString binary;
    };

    ComparableSymbol(QVector<Pattern> pattern)
        : pattern(std::move(pattern))
        , isPattern(true)
    {
    }

    bool isValid() const
    {
        if (isPattern)
            return !pattern.isEmpty();
        else
            return symbol.isValid();
    }

    bool operator==(const ComparableSymbol& rhs) const
    {
        VERIFY_OR_THROW(isPattern != rhs.isPattern);
        auto cmp = [](const Data::Symbol& symbol, const QVector<Pattern>& pattern) {
            return std::any_of(pattern.begin(), pattern.end(), [&symbol](const Pattern& pattern) {
                return symbol.symbol.contains(pattern.symbol) && symbol.binary.contains(pattern.binary);
            });
        };
        return isPattern ? cmp(rhs.symbol, pattern) : cmp(symbol, rhs.pattern);
    }

    QVector<Pattern> pattern;
    Data::Symbol symbol;
    bool isPattern = false;
};

char* toString(const ComparableSymbol& symbol)
{
    if (symbol.isPattern) {
        QStringList patterns;
        for (const auto& pattern : symbol.pattern)
            patterns.append(QLatin1Char('{') + pattern.symbol + QLatin1String(", ") + pattern.binary
                            + QLatin1Char('}'));
        return QTest::toString(
            QString(QLatin1String("ComparableSymbol{[") + patterns.join(QLatin1String(", ")) + QLatin1String("]}")));
    } else {
        return QTest::toString(QString(QLatin1String("ComparableSymbol{") + symbol.symbol.symbol + QLatin1String(", ")
                                       + symbol.symbol.binary + QLatin1Char('}')));
    }
}

ComparableSymbol cppInliningTopSymbol(const QString& binary = QStringLiteral("cpp-inlining"))
{
    // depending on libstdc++ version, we either get the slow libm
    // or it's fully inlined
    return ComparableSymbol(
        QVector<ComparableSymbol::Pattern> {{QStringLiteral("hypot"), QStringLiteral("libm")},
                                            {QStringLiteral("std::__detail::_Mod<unsigned long,"), binary},
                                            {QStringLiteral("std::generate_canonical"), binary}});
}

ComparableSymbol cppRecursionTopSymbol(const QString& binary = QStringLiteral("cpp-recursion"))
{
    // recursion is notoriously hard to handle, we currently often fail
    return ComparableSymbol(QVector<ComparableSymbol::Pattern> {{QStringLiteral("fibonacci"), binary}, {{}, binary}});
}

void dump(const Data::BottomUp& bottomUp, QTextStream& stream, const QByteArray& prefix)
{
    stream << prefix << bottomUp.symbol.symbol << '\n';

    for (const auto& child : bottomUp.children) {
        dump(child, stream, prefix + '\t');
    }
}

class TestPerfParser : public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;

private slots:
    void initTestCase()
    {
        qputenv("DEBUGINFOD_URLS", {});
        RecordHost host;
        QSignalSpy capabilitiesSpy(&host, &RecordHost::perfCapabilitiesChanged);
        QSignalSpy installedSpy(&host, &RecordHost::isPerfInstalledChanged);
        QVERIFY(installedSpy.wait());
        if (!host.isPerfInstalled()) {
            QSKIP("perf is not available, cannot run integration tests.");
        }

        if (capabilitiesSpy.count() == 0) {
            QVERIFY(capabilitiesSpy.wait());
        }
        m_capabilities = host.perfCapabilities();
    }

    void init()
    {
        m_bottomUpData = {};
        m_topDownData = {};
        m_callerCalleeData = {};
        m_summaryData = {};
        m_perfCommand.clear();
        m_cpuArchitecture = QSysInfo::currentCpuArchitecture();
        m_linuxKernelVersion = QSysInfo::kernelVersion();
        m_machineHostName = QSysInfo::machineHostName();
    }

    void testFileErrorHandling_data()
    {
        QTest::addColumn<QString>("perfFile");
        QTest::addColumn<QString>("errorMessagePart");

        QTest::addRow("missing file") << QStringLiteral("not_here") << QStringLiteral("does not exist");
        QTest::addRow("not a file") << QStringLiteral("../..") << QStringLiteral("is not a file");

        QTest::addRow("permissions") << QString() << QStringLiteral("not readable");
    }

    void testFileErrorHandling()
    {

        PerfParser parser(this);
        QSignalSpy parsingFailedSpy(&parser, &PerfParser::parsingFailed);

        QFETCH(QString, perfFile);
        QFETCH(QString, errorMessagePart);

        QTemporaryFile tempFile;
        if (perfFile.isEmpty()) {
            tempFile.open();
            tempFile.write("test content");
            tempFile.close();
            tempFile.setPermissions({}); // drop all permissons
            perfFile = tempFile.fileName();
        }

        parser.initParserArgs(perfFile);
        QCOMPARE(parsingFailedSpy.count(), 1);
        auto message = parsingFailedSpy.takeFirst().at(0).toString();
        QVERIFY(message.contains(perfFile));
        QVERIFY(message.contains(errorMessagePart));
    }

    void testFileContent_data()
    {
        QTest::addColumn<QString>("perfFile");
        QTest::addColumn<QString>("errorMessagePart");

        const auto perfData = QFINDTESTDATA("file_content/true.perfparser");
        QTest::addRow("pre-exported perfparser") << perfData << QString();
#if KFArchive_FOUND
        QTest::addRow("perfparser, xzipped") << QFINDTESTDATA("file_content/true.perfparser.xz") << QString();
#endif
        const auto perfDataSomeName = QStringLiteral("fruitper");
        QFile::copy(perfData, perfDataSomeName); // we can ignore errors (file exist) here
        QTest::addRow("pre-exported perfparser \"bad extension\"") << perfDataSomeName << QString();
        QTest::addRow("no expected magic header")
            << QFINDTESTDATA("tst_perfparser.cpp") << QStringLiteral("File format unknown");
        QTest::addRow("PERF v1") << QFINDTESTDATA("file_content/perf.data.true.v1") << QStringLiteral("V1 perf data");

        QTest::addRow("PERF v2") << QFINDTESTDATA("file_content/perf.data.true.v2") << QString();
#if KFArchive_FOUND
        QTest::addRow("PERF v2, gzipped") << QFINDTESTDATA("file_content/perf.data.true.v2.gz") << QString();
        QTest::addRow("no expected magic header, gzipped")
            << QFINDTESTDATA("file_content/perf.data.broken.gz") << QStringLiteral("File format unknown");
#endif
    }

    void testFileContent()
    {
        // setting the application path as the checked perf files recorded a `true` binary which commonly
        // is not available in the same place (and we don't get the any reasonable parser output in this case)
        // the same place as the tests are run
        Settings::instance()->setAppPath(
            QFileInfo(QStandardPaths::findExecutable(QStringLiteral("true"))).dir().path());
        // add extra paths to at least allow manually including the matched libc.so/ld.so during a test
        Settings::instance()->setExtraLibPaths(QFINDTESTDATA("file_content"));

        PerfParser parser(this);
        QSignalSpy parsingFailedSpy(&parser, &PerfParser::parsingFailed);
        QSignalSpy parsingFinishedSpy(&parser, &PerfParser::parsingFinished);

        QFETCH(QString, perfFile);
        QFETCH(QString, errorMessagePart);

        QVERIFY(!perfFile.isEmpty() && QFile::exists(perfFile));
        parser.startParseFile(perfFile);

        if (errorMessagePart.isEmpty()) {
            // if we don't expect an error message (Null String created by `QString()`)
            // then expect a finish within the given time frame
            QTRY_COMPARE_WITH_TIMEOUT(parsingFinishedSpy.count(), 1, 2000);
            QCOMPARE(parsingFailedSpy.count(), 0);
        } else {
            // otherwise wait for failed parsing, the check for if the required part is
            // found in the error message (we only check a part to allow adjustments later)
            QTRY_COMPARE_WITH_TIMEOUT(parsingFailedSpy.count(), 1, 2000);
            QCOMPARE(parsingFinishedSpy.count(), 0);
            const auto message = parsingFailedSpy.takeFirst().at(0).toString();
            QVERIFY(message.contains(errorMessagePart));
            QVERIFY(message.contains(perfFile));
        }
    }

    /* tests a perf file that has data with PERF_FORMAT_LOST attribute, see KDAB/hotspot#578 */
    void testPerfFormatLost()
    {
        PerfParser parser(this);
        QSignalSpy parsingFailedSpy(&parser, &PerfParser::parsingFailed);
        QSignalSpy parsingFinishedSpy(&parser, &PerfParser::parsingFinished);

        parser.startParseFile(QFINDTESTDATA("perf.data.PerfFormatLost"));

        QTRY_COMPARE_WITH_TIMEOUT(parsingFinishedSpy.count(), 1, 58000);
        QCOMPARE(parsingFailedSpy.count(), 0);
    }

    void testCppInliningNoOptions()
    {
        const QStringList perfOptions;
        QStringList exeOptions;

        const QString exePath = findExe(QStringLiteral("cpp-inlining"));
        QTemporaryFile tempFile;
        tempFile.open();

        // top-down data is too vague here, don't check it
        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppInliningTopSymbol(), {}, tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());

        // we don't know the on/off CPU time
        QCOMPARE(m_summaryData.onCpuTime, quint64(0));
        QCOMPARE(m_summaryData.offCpuTime, quint64(0));
    }

    void testCppInliningCallGraphDwarf_data()
    {
        QTest::addColumn<QStringList>("otherOptions");

        QTest::addRow("normal") << QStringList();
        if (m_capabilities.canUseAio)
            QTest::addRow("aio") << QStringList(QStringLiteral("--aio"));
        if (m_capabilities.canCompress)
            QTest::addRow("zstd") << QStringList(QStringLiteral("-z"));
    }

    void testCppInliningCallGraphDwarf()
    {
        QFETCH(QStringList, otherOptions);

        QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf")};
        perfOptions + otherOptions;

        QStringList exeOptions;

        const QString exePath = findExe(QStringLiteral("cpp-inlining"));
        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppInliningTopSymbol(), {QStringLiteral("start"), QStringLiteral("cpp-inlining")},
                         tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());

        QVERIFY(searchForChildSymbol(m_bottomUpData.root.children.at(maxElementTopIndex(m_bottomUpData)),
                                     QStringLiteral("main")));
        QVERIFY(searchForChildSymbol(m_topDownData.root.children.at(maxElementTopIndex(m_topDownData)),
                                     QStringLiteral("main")));
    }

    void testCppInliningEventCycles()
    {
        const QStringList perfOptions = {QStringLiteral("--event"), QStringLiteral("cycles")};
        QStringList exeOptions;

        const QString exePath = findExe(QStringLiteral("cpp-inlining"));
        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppInliningTopSymbol(), {}, tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());
    }

    void testCppInliningEventCyclesInstructions()
    {
        QFETCH(QString, eventSpec);
        const QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"),
                                         QStringLiteral("--event"), eventSpec};
        QStringList exeOptions;

        const QString exePath = findExe(QStringLiteral("cpp-inlining"));
        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppInliningTopSymbol(), {QStringLiteral("start"), QStringLiteral("cpp-inlining")},
                         tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());

        QCOMPARE(m_bottomUpData.costs.numTypes(), 2);
        QCOMPARE(m_topDownData.inclusiveCosts.numTypes(), 2);
        QCOMPARE(m_topDownData.selfCosts.numTypes(), 2);
        QVERIFY(m_bottomUpData.costs.typeName(0).startsWith(QStringLiteral("cycles")));
        QVERIFY(m_bottomUpData.costs.typeName(1).startsWith(QStringLiteral("instructions")));

        int bottomUpTopIndex = maxElementTopIndex(m_bottomUpData);
        qint64 bottomUpCycleCost = m_bottomUpData.costs.cost(0, m_bottomUpData.root.children.at(bottomUpTopIndex).id);
        qint64 bottomUpInstructionCost =
            m_bottomUpData.costs.cost(1, m_bottomUpData.root.children.at(bottomUpTopIndex).id);
        QVERIFY2(bottomUpCycleCost != bottomUpInstructionCost,
                 "Bottom-Up Cycle Cost should not be equal to Bottom-Up Instruction Cost");

        int topDownTopIndex = maxElementTopIndex(m_topDownData);
        qint64 topDownCycleCost =
            m_topDownData.inclusiveCosts.cost(0, m_topDownData.root.children.at(topDownTopIndex).id);
        qint64 topDownInstructionCost =
            m_topDownData.inclusiveCosts.cost(1, m_topDownData.root.children.at(topDownTopIndex).id);
        QVERIFY2(topDownCycleCost != topDownInstructionCost,
                 "Top-Down Cycle Cost should not be equal to Top-Down Instruction Cost");
    }

    void testCppInliningEventCyclesInstructions_data()
    {
        QTest::addColumn<QString>("eventSpec");

        QTest::newRow("separate-events") << QStringLiteral("cycles,instructions");
        QTest::newRow("group") << QStringLiteral("{cycles,instructions}");
        QTest::newRow("leader-sampling") << QStringLiteral("{cycles,instructions}:S");
    }

    void testCppRecursionNoOptions()
    {
        const QStringList perfOptions;
        const QStringList exeOptions = {QStringLiteral("40")};

        const QString exePath = findExe(QStringLiteral("cpp-recursion"));
        QTemporaryFile tempFile;
        tempFile.open();
        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppRecursionTopSymbol(), cppRecursionTopSymbol(), tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());
    }

    void testCppRecursionCallGraphDwarf()
    {
        const QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf")};
        const QStringList exeOptions = {QStringLiteral("40")};

        const QString exePath = findExe(QStringLiteral("cpp-recursion"));
        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppRecursionTopSymbol(), {QStringLiteral("start"), QStringLiteral("cpp-recursion")},
                         tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());

        QVERIFY(searchForChildSymbol(m_bottomUpData.root.children.at(maxElementTopIndex(m_bottomUpData)),
                                     QStringLiteral("main")));
        const auto maxTop = m_topDownData.root.children.at(maxElementTopIndex(m_topDownData));
        if (!maxTop.symbol.isValid()) {
            QSKIP("unwinding failed from the fibonacci function, unclear why - increasing the stack dump size doesn't "
                  "help");
        }
        QVERIFY(searchForChildSymbol(maxTop, QStringLiteral("main")));
    }

    void testCppRecursionEventCycles()
    {
        const QStringList perfOptions = {QStringLiteral("--event"), QStringLiteral("cycles")};
        const QStringList exeOptions = {QStringLiteral("40")};

        const QString exePath = findExe(QStringLiteral("cpp-recursion"));
        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppRecursionTopSymbol(), cppRecursionTopSymbol(), tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());
    }

    void testCppRecursionEventCyclesInstructions()
    {
        const QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"),
                                         QStringLiteral("--event"), QStringLiteral("cycles,instructions")};
        const QStringList exeOptions = {QStringLiteral("40")};

        const QString exePath = findExe(QStringLiteral("cpp-recursion"));
        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeOptions, tempFile.fileName());
            testPerfData(cppRecursionTopSymbol(), {QStringLiteral("start"), QStringLiteral("cpp-recursion")},
                         tempFile.fileName());
        } catch (...) {
        }
        QVERIFY(!m_bottomUpData.root.children.isEmpty());
        QVERIFY(!m_topDownData.root.children.isEmpty());

        int bottomUpTopIndex = maxElementTopIndex(m_bottomUpData);
        qint64 bottomUpCycleCost = m_bottomUpData.costs.cost(0, m_bottomUpData.root.children.at(bottomUpTopIndex).id);
        qint64 bottomUpInstructionCost =
            m_bottomUpData.costs.cost(1, m_bottomUpData.root.children.at(bottomUpTopIndex).id);
        QVERIFY2(bottomUpCycleCost != bottomUpInstructionCost,
                 "Bottom-Up Cycle Cost should not be equal to Bottom-Up Instruction Cost");

        int topDownTopIndex = maxElementTopIndex(m_topDownData);
        qint64 topDownCycleCost =
            m_topDownData.inclusiveCosts.cost(0, m_topDownData.root.children.at(topDownTopIndex).id);
        qint64 topDownInstructionCost =
            m_topDownData.inclusiveCosts.cost(1, m_topDownData.root.children.at(topDownTopIndex).id);
        QVERIFY2(topDownCycleCost != topDownInstructionCost,
                 "Top-Down Cycle Cost should not be equal to Top-Down Instruction Cost");
    }

    void testSendStdIn()
    {
        const QStringList exeOptions = {QStringLiteral("40")};

        const QString exePath = findExe(QStringLiteral("cpp-stdin"));

        QTemporaryFile tempFile;
        tempFile.open();

        RecordHost host;
        PerfRecord perf(&host);
        QSignalSpy recordingFinishedSpy(&perf, &PerfRecord::recordingFinished);
        QSignalSpy recordingFailedSpy(&perf, &PerfRecord::recordingFailed);

        perf.record({QStringLiteral("--no-buildid-cache")}, tempFile.fileName(), false, exePath, exeOptions);
        perf.sendInput(QByteArrayLiteral("some input\n"));
        QVERIFY(recordingFinishedSpy.wait());

        QCOMPARE(recordingFailedSpy.count(), 0);
        QCOMPARE(recordingFinishedSpy.count(), 1);
    }

    void testSwitchEvents()
    {
        const QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"),
                                         QStringLiteral("--switch-events")};

        const QString exePath = findExe(QStringLiteral("cpp-sleep"));

        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, {}, tempFile.fileName());
            testPerfData(cppInliningTopSymbol(QStringLiteral("cpp-sleep")),
                         {QStringLiteral("start"), QStringLiteral("cpp-sleep")}, tempFile.fileName(), false);
        } catch (...) {
        }

        QVERIFY(m_summaryData.offCpuTime > 1E9); // it should sleep at least 1s in total
        QVERIFY(m_summaryData.onCpuTime > 0); // there's some CPU time, but not sure how much
        QCOMPARE(m_summaryData.applicationTime.delta(), m_summaryData.offCpuTime + m_summaryData.onCpuTime);
    }

    void testThreadNames()
    {
        const QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"),
                                         QStringLiteral("--switch-events")};

        const QString exePath = findExe(QStringLiteral("cpp-threadnames"));

        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, {}, tempFile.fileName());
            testPerfData({}, {}, tempFile.fileName(), false);
        } catch (...) {
        }

        // in total, there should only be about 1s runtime
        QVERIFY(m_summaryData.applicationTime.delta() > 1E9);
        // and it should be less than the total sleep time
        QVERIFY(m_summaryData.applicationTime.delta() < m_summaryData.offCpuTime);
        // which is about 2s since the main thread sleeps most of the time, and every one of the others, too
        QVERIFY(m_summaryData.offCpuTime > 2E9);
        // there's some CPU time, but not sure how much
        QVERIFY(m_summaryData.onCpuTime > 0 && m_summaryData.onCpuTime < m_summaryData.offCpuTime);

        QCOMPARE(m_eventData.threads.size(), 11);
        quint64 lastTime = 0;
        for (int i = 0; i < 11; ++i) {
            const auto& thread = m_eventData.threads[i];
            QVERIFY(thread.time.start > lastTime);
            lastTime = thread.time.start;
            if (i == 0) {
                QCOMPARE(thread.name, QStringLiteral("cpp-threadnames"));
                QVERIFY(thread.offCpuTime > 1E9); // sleeps about 1s in total
            } else {
                QCOMPARE(thread.name, QStringLiteral("threadname%1").arg(i - 1));
                QVERIFY(thread.offCpuTime > 1E8);
                QVERIFY(thread.offCpuTime < 1E9);
            }
            QVERIFY(thread.time.delta() > thread.offCpuTime);
        }
    }

    void testOffCpu()
    {
        if (!m_capabilities.canProfileOffCpu) {
            QSKIP("cannot access sched_switch trace points. execute the following to run this test:\n"
                  "    sudo mount -o remount,mode=755 /sys/kernel/debug{,/tracing} with mode=755");
        }

        QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"), QStringLiteral("-e"),
                                   QStringLiteral("cycles")};
        perfOptions += PerfRecord::offCpuProfilingOptions();

        const QString exePath = findExe(QStringLiteral("cpp-sleep"));

        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, {}, tempFile.fileName());
            testPerfData(cppInliningTopSymbol(QStringLiteral("cpp-sleep")),
                         {QStringLiteral("start"), QStringLiteral("cpp-sleep")}, tempFile.fileName(), false);
        } catch (...) {
        }

        QCOMPARE(m_bottomUpData.costs.numTypes(), 3);
        QCOMPARE(m_bottomUpData.costs.typeName(0), QStringLiteral("cycles"));
        QCOMPARE(m_bottomUpData.costs.typeName(1), QStringLiteral("sched:sched_switch"));
        QCOMPARE(m_bottomUpData.costs.typeName(2), QStringLiteral("off-CPU Time"));

        // find sched switch hotspot
        int bottomUpTopIndex = maxElementTopIndex(m_bottomUpData, 1);
        QVERIFY(bottomUpTopIndex != -1);

        // should be the same as off-cpu hotspot
        QCOMPARE(bottomUpTopIndex, maxElementTopIndex(m_bottomUpData, 2));

        const auto topBottomUp = m_bottomUpData.root.children[bottomUpTopIndex];
        QCOMPARE(ComparableSymbol(topBottomUp.symbol),
                 ComparableSymbol({{QStringLiteral("schedule"), QStringLiteral("kernel")},
                                   {QStringLiteral("__schedule"), QString()}}));
        QVERIFY(searchForChildSymbol(topBottomUp, QStringLiteral("std::this_thread::sleep_for"), false));

        QVERIFY(m_bottomUpData.costs.cost(1, topBottomUp.id) >= 10); // at least 10 sched switches
        QVERIFY(m_bottomUpData.costs.cost(2, topBottomUp.id) >= 1E9); // at least 1s sleep time
    }

    void testOffCpuSleep()
    {
        const auto sleep = QStandardPaths::findExecutable(QStringLiteral("sleep"));
        if (sleep.isEmpty()) {
            QSKIP("no sleep command available");
        }

        if (!m_capabilities.canProfileOffCpu) {
            QSKIP("cannot access sched_switch trace points. execute the following to run this test:\n"
                  "    sudo mount -o remount,mode=755 /sys/kernel/debug{,/tracing} with mode=755");
        }

        QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"), QStringLiteral("-e"),
                                   QStringLiteral("cycles")};
        perfOptions += PerfRecord::offCpuProfilingOptions();

        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, sleep, {QStringLiteral(".5")}, tempFile.fileName());
            testPerfData({}, {}, tempFile.fileName(), false);
        } catch (...) {
        }

        QCOMPARE(m_bottomUpData.costs.numTypes(), 3);
        QCOMPARE(m_bottomUpData.costs.typeName(0), QStringLiteral("cycles"));
        QCOMPARE(m_bottomUpData.costs.typeName(1), QStringLiteral("sched:sched_switch"));
        QCOMPARE(m_bottomUpData.costs.typeName(2), QStringLiteral("off-CPU Time"));
        QVERIFY(m_bottomUpData.costs.totalCost(1) >= 1); // at least 1 sched switch
        QVERIFY(m_bottomUpData.costs.totalCost(2) >= 5E8); // at least .5s sleep time
    }

    void testSampleCpu()
    {
        QStringList perfOptions = {QStringLiteral("--call-graph"), QStringLiteral("dwarf"),
                                   QStringLiteral("--sample-cpu"), QStringLiteral("-e"), QStringLiteral("cycles")};
        if (m_capabilities.canProfileOffCpu) {
            perfOptions += PerfRecord::offCpuProfilingOptions();
        }

        const QString exePath = findExe(QStringLiteral("cpp-parallel"));
        const int numThreads = QThread::idealThreadCount();
        const QStringList exeArgs = {QString::number(numThreads)};

        QTemporaryFile tempFile;
        tempFile.open();

        try {
            perfRecord(perfOptions, exePath, exeArgs, tempFile.fileName());
            testPerfData({}, {}, tempFile.fileName(), false);
        } catch (...) {
        }

        QCOMPARE(m_eventData.threads.size(), numThreads + 1);
        QCOMPARE(m_eventData.cpus.size(), numThreads);

        if (m_capabilities.canProfileOffCpu) {
            QCOMPARE(m_bottomUpData.costs.numTypes(), 3);
            QCOMPARE(m_bottomUpData.costs.typeName(0), QStringLiteral("cycles"));
            QCOMPARE(m_bottomUpData.costs.typeName(1), QStringLiteral("sched:sched_switch"));
            QCOMPARE(m_bottomUpData.costs.typeName(2), QStringLiteral("off-CPU Time"));

            QSet<quint32> eventCpuIds[3];
            for (const auto& thread : std::as_const(m_eventData.threads)) {
                for (const auto& event : thread.events) {
                    eventCpuIds[event.type].insert(event.cpuId);
                }
            }
            QVERIFY(eventCpuIds[0].size() > 1);
            QVERIFY(eventCpuIds[1].size() > 1);
            QVERIFY(eventCpuIds[2].size() > 1);
        } else {
            qDebug() << "skipping extended off-CPU profiling check";
        }
    }

    void testCustomCostAggregation_data()
    {
        QTest::addColumn<Settings::CostAggregation>("aggregation");
        QTest::addColumn<QString>("filename");

        QTest::addRow("by_symbol") << Settings::CostAggregation::BySymbol << "by_symbol.txt";
        QTest::addRow("by_cpu") << Settings::CostAggregation::ByCPU << "by_cpu.txt";
        QTest::addRow("by_process") << Settings::CostAggregation::ByProcess << "by_process.txt";
        QTest::addRow("by_thread") << Settings::CostAggregation::ByThread << "by_thread.txt";
    }

    void testCustomCostAggregation()
    {
        QFETCH(Settings::CostAggregation, aggregation);
        QFETCH(QString, filename);

        QFile expectedData(QFINDTESTDATA(QLatin1String("custom_cost_aggregation_testfiles/") + filename));
        QVERIFY(expectedData.open(QIODevice::ReadOnly | QIODevice::Text));
        const auto expected = expectedData.readAll();

        Settings::instance()->setCostAggregation(aggregation);
        m_perfCommand = QStringLiteral("perf record --call-graph dwarf --sample-cpu --switch-events --event "
                                       "sched:sched_switch -c 1000000 /tmp/cpp-threadnames");
        m_cpuArchitecture = QStringLiteral("x86_64");
        m_linuxKernelVersion = QStringLiteral("5.17.5-arch1-1");
        m_machineHostName = QStringLiteral("Sparrow");

        const auto perfData = QFINDTESTDATA("custom_cost_aggregation_testfiles/custom_cost_aggregation.perfparser");
        QVERIFY(!perfData.isEmpty() && QFile::exists(perfData));

        try {
            testPerfData({}, {}, perfData, false);
        } catch (...) {
        }

        QByteArray actual;
        {
            QTextStream stream(&actual);
            dump(m_bottomUpData.root, stream, {});
        }

        if (expected != actual) {
            QFile actualData(expectedData.fileName() + QLatin1String(".actual"));
            QVERIFY(actualData.open(QIODevice::WriteOnly | QIODevice::Text));
            actualData.write(actual);

            const auto diff = QStandardPaths::findExecutable(QStringLiteral("diff"));
            if (!diff.isEmpty()) {
                QProcess::execute(diff, {QStringLiteral("-u"), expectedData.fileName(), actualData.fileName()});
            }
        }
        QCOMPARE(actual, expected);
    }

#if KFArchive_FOUND
    void testDecompression_data()
    {
        QTest::addColumn<QString>("filename");

        QTest::newRow("plain") << QFINDTESTDATA("archives/test.txt");
        QTest::newRow("gzip") << QFINDTESTDATA("archives/test.gz");
        QTest::newRow("bzip2") << QFINDTESTDATA("archives/test.bz2");
        QTest::newRow("xz") << QFINDTESTDATA("archives/test.xz");
    }

    void testDecompression()
    {
        QFETCH(QString, filename);

        PerfParser parser;
        QFile decompressed(parser.decompressIfNeeded(filename));
        decompressed.open(QIODevice::ReadOnly);

        QCOMPARE(decompressed.readAll(), QByteArrayLiteral("Hello World\n"));
    }

    void testArchive_data()
    {
        QTest::addColumn<QString>("filename");

        QTest::addRow("uncompressed tar") << QFINDTESTDATA("archives/test.tar");

        QTest::addRow("gzip compressed tar") << QFINDTESTDATA("archives/test.tar.gz");

        QTest::addRow("zip file") << QFINDTESTDATA("archives/test.zip");

        QTest::addRow("tar file with multiple files") << QFINDTESTDATA("archives/test-multi-files.tar");
    }

    void testArchive()
    {
        QFETCH(QString, filename);

        PerfParser parser;
        QFile decompressed(parser.decompressIfNeeded(filename));
        decompressed.open(QIODevice::ReadOnly);

        QCOMPARE(decompressed.readAll(), QByteArrayLiteral("Hello World\n"));
    }
#endif

private:
    Data::Summary m_summaryData;
    Data::BottomUpResults m_bottomUpData;
    Data::TopDownResults m_topDownData;
    Data::CallerCalleeResults m_callerCalleeData;
    Data::EventResults m_eventData;
    QString m_perfCommand;
    QString m_cpuArchitecture;
    QString m_linuxKernelVersion;
    QString m_machineHostName;
    RecordHost::PerfCapabilities m_capabilities;

    void perfRecord(const QStringList& perfOptions, const QString& exePath, const QStringList& exeOptions,
                    const QString& fileName)
    {
        RecordHost host;
        PerfRecord perf(&host);
        QSignalSpy recordingFinishedSpy(&perf, &PerfRecord::recordingFinished);
        QSignalSpy recordingFailedSpy(&perf, &PerfRecord::recordingFailed);

        // always add `-c 1000000`, as perf's frequency mode is too unreliable for testing purposes
        perf.record(
            perfOptions
                + QStringList {QStringLiteral("-c"), QStringLiteral("1000000"), QStringLiteral("--no-buildid-cache")},
            fileName, false, exePath, exeOptions);
        m_perfCommand = perf.perfCommand();

        QVERIFY(recordingFinishedSpy.wait(10000));

        QCOMPARE(recordingFailedSpy.count(), 0);
        QCOMPARE(recordingFinishedSpy.count(), 1);
        QCOMPARE(QFileInfo::exists(fileName), true);
    }

    static void validateCosts(const Data::Costs& costs, const Data::BottomUp& row)
    {
        if (row.parent) {
            bool hasCost = false;
            for (int i = 0; i < costs.numTypes(); ++i) {
                if (costs.cost(i, row.id) > 0) {
                    hasCost = true;
                    break;
                }
            }
            if (!hasCost) {
                qWarning() << "row without cost: " << row.id << row.symbol << row.parent;
                auto* r = &row;
                while (auto p = r->parent) {
                    qWarning() << p->symbol;
                    r = p;
                }
            }
            QVERIFY(hasCost);
        }
        for (const auto& child : row.children) {
            validateCosts(costs, child);
        }
    }

    void testPerfData(const ComparableSymbol& topBottomUpSymbol, const ComparableSymbol& topTopDownSymbol,
                      const QString& fileName, bool checkFrequency = true)
    {
        PerfParser parser(this);

        QSignalSpy parsingFinishedSpy(&parser, &PerfParser::parsingFinished);
        QSignalSpy parsingFailedSpy(&parser, &PerfParser::parsingFailed);
        QSignalSpy summaryDataSpy(&parser, &PerfParser::summaryDataAvailable);
        QSignalSpy bottomUpDataSpy(&parser, &PerfParser::bottomUpDataAvailable);
        QSignalSpy topDownDataSpy(&parser, &PerfParser::topDownDataAvailable);
        QSignalSpy callerCalleeDataSpy(&parser, &PerfParser::callerCalleeDataAvailable);
        QSignalSpy eventsDataSpy(&parser, &PerfParser::eventsAvailable);

        parser.startParseFile(fileName);

        QVERIFY(parsingFinishedSpy.wait(12000));

        // Verify that the test passed
        QCOMPARE(parsingFailedSpy.count(), 0);
        QCOMPARE(parsingFinishedSpy.count(), 1);

        // Verify the summary data isn't empty
        QCOMPARE(summaryDataSpy.count(), 1);
        QList<QVariant> summaryDataArgs = summaryDataSpy.takeFirst();
        m_summaryData = qvariant_cast<Data::Summary>(summaryDataArgs.at(0));
        QCOMPARE(m_perfCommand, m_summaryData.command);
        QVERIFY(m_summaryData.sampleCount > 0);
        QVERIFY(m_summaryData.applicationTime.delta() > 0);
        QVERIFY(m_summaryData.cpusAvailable > 0);
        QCOMPARE(m_summaryData.processCount, quint32(1)); // for now we always have a single process
        QVERIFY(m_summaryData.threadCount > 0); // and at least one thread
        QCOMPARE(m_summaryData.cpuArchitecture, m_cpuArchitecture);
        QCOMPARE(m_summaryData.linuxKernelVersion, m_linuxKernelVersion);
        QCOMPARE(m_summaryData.hostName, m_machineHostName);

        if (checkFrequency) {
            // Verify the sample frequency is acceptable, greater than 500Hz
            double frequency = (1E9 * m_summaryData.sampleCount) / m_summaryData.applicationTime.delta();
            QVERIFY2(frequency > 500, qPrintable(QLatin1String("Low Frequency: ") + QString::number(frequency)));
        }

        // Verify the top Bottom-Up symbol result contains the expected data
        QCOMPARE(bottomUpDataSpy.count(), 1);
        QList<QVariant> bottomUpDataArgs = bottomUpDataSpy.takeFirst();
        m_bottomUpData = bottomUpDataArgs.at(0).value<Data::BottomUpResults>();
        validateCosts(m_bottomUpData.costs, m_bottomUpData.root);
        QVERIFY(m_bottomUpData.root.children.count() > 0);

        if (topBottomUpSymbol.isValid()) {
            int bottomUpTopIndex = maxElementTopIndex(m_bottomUpData);
            const auto actualTopBottomUpSymbol =
                ComparableSymbol(m_bottomUpData.root.children[bottomUpTopIndex].symbol);
            if (actualTopBottomUpSymbol == ComparableSymbol(QStringLiteral("__FRAME_END__"), {})) {
                QEXPECT_FAIL("", "bad symbol offsets - bug in mmap handling or symbol cache?", Continue);
            }
            QCOMPARE(actualTopBottomUpSymbol, topBottomUpSymbol);
        }

        // Verify the top Top-Down symbol result contains the expected data
        QCOMPARE(topDownDataSpy.count(), 1);
        QList<QVariant> topDownDataArgs = topDownDataSpy.takeFirst();
        m_topDownData = topDownDataArgs.at(0).value<Data::TopDownResults>();
        QVERIFY(m_topDownData.root.children.count() > 0);

        if (topTopDownSymbol.isValid()
            && QLatin1String(QTest::currentTestFunction()) != QLatin1String("testCppRecursionCallGraphDwarf")) {
            int topDownTopIndex = maxElementTopIndex(m_topDownData);
            const auto actualTopTopDownSymbol = ComparableSymbol(m_topDownData.root.children[topDownTopIndex].symbol);

            if (actualTopTopDownSymbol == ComparableSymbol(QStringLiteral("__FRAME_END__"), {})) {
                QEXPECT_FAIL("", "bad symbol offsets - bug in mmap handling or symbol cache?", Continue);
            }
            QCOMPARE(actualTopTopDownSymbol, topTopDownSymbol);
        }

        // Verify the Caller/Callee data isn't empty
        QCOMPARE(callerCalleeDataSpy.count(), 1);
        QList<QVariant> callerCalleeDataArgs = callerCalleeDataSpy.takeFirst();
        m_callerCalleeData = callerCalleeDataArgs.at(0).value<Data::CallerCalleeResults>();
        QVERIFY(m_callerCalleeData.entries.count() > 0);

        // Verify that no individual cost in the Caller/Callee data is greater than the total cost of all samples
        for (const auto& entry : std::as_const(m_callerCalleeData.entries)) {
            QVERIFY(m_callerCalleeData.inclusiveCosts.cost(0, entry.id)
                    <= static_cast<qint64>(m_summaryData.costs[0].totalPeriod));
        }

        // Verify that the events data is not empty and somewhat sane
        QCOMPARE(eventsDataSpy.count(), 1);
        m_eventData = eventsDataSpy.first().first().value<Data::EventResults>();
        QVERIFY(!m_eventData.stacks.isEmpty());
        QVERIFY(!m_eventData.threads.isEmpty());
        QCOMPARE(static_cast<quint32>(m_eventData.threads.size()), m_summaryData.threadCount);
        for (const auto& thread : std::as_const(m_eventData.threads)) {
            QVERIFY(!thread.name.isEmpty());
            QVERIFY(thread.pid != 0);
            QVERIFY(thread.tid != 0);
            QVERIFY(thread.time.isValid());
            QVERIFY(thread.time.end > thread.time.start);
            QVERIFY(thread.offCpuTime == 0 || thread.offCpuTime < thread.time.delta());
        }
        QVERIFY(!m_eventData.totalCosts.isEmpty());
        for (const auto& costs : std::as_const(m_eventData.totalCosts)) {
            QVERIFY(!costs.label.isEmpty());
            QVERIFY(costs.sampleCount > 0);
            QVERIFY(costs.totalPeriod > 0);
        }
    }
};

QTEST_GUILESS_MAIN(TestPerfParser)

#include "tst_perfparser.moc"