summaryrefslogtreecommitdiffstats
path: root/tests/auto/shared/perfparsertestclient.cpp
blob: 9c103d12bfe1c2b111d798fbbf45d6dc61dbc7b5 (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
/****************************************************************************
**
** 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 "perffeatures.h"
#include "perfparsertestclient.h"

#include <QtEndian>
#include <QtTest>

PerfParserTestClient::PerfParserTestClient(QObject *parent) : QObject(parent)
{
}

void PerfParserTestClient::extractTrace(QIODevice *device)
{
    QVERIFY(device->bytesAvailable() > 0);
    const char streamMagic[] = "QPERFSTREAM";
    const int magicSize = sizeof(streamMagic);

    QVarLengthArray<char> magic(magicSize);
    device->read(magic.data(), magicSize);
    QCOMPARE(QByteArray(magic.data(), magic.size()), QByteArray(streamMagic, magicSize));

    qint32 version;
    device->read(reinterpret_cast<char *>(&version), sizeof(qint32));
    version = qFromLittleEndian(version);

    QVERIFY(version == QDataStream::Qt_DefaultCompiledVersion);

    float progress = -1;

    auto checkString = [this](qint32 id) {
        QVERIFY(id < m_strings.length());
        QVERIFY(!m_strings[id].isEmpty());
    };

    auto checkLocation = [this](qint32 id) {
        QVERIFY(id < m_locations.length());
        QVERIFY(m_locations[id].pid != 0);
    };

    auto checkAttribute = [this, &checkString](qint32 id) {
        QVERIFY(id < m_attributes.length());
        checkString(m_attributes[id].name);
    };

    while (device->bytesAvailable() >= static_cast<qint64>(sizeof(quint32))) {
        qint32 size;
        device->read(reinterpret_cast<char *>(&size), sizeof(quint32));
        size = qFromLittleEndian(size);

        QVERIFY(device->bytesAvailable() >= size);
        QDataStream stream(device->read(size));

        quint8 eventType;
        stream >> eventType;

        switch (eventType) {
        case ThreadEnd: {
            ThreadEndEvent threadEnd;
            stream >> threadEnd.pid >> threadEnd.tid >> threadEnd.time;
            m_threadEnds.append(threadEnd);
            break;
        }
        case Command: {
            CommandEvent command;
            stream >> command.pid >> command.tid >> command.time >> command.name;
            checkString(command.name);
            m_commands.append(command);
            break;
        }
        case LocationDefinition: {
            qint32 id;
            LocationEvent location;
            stream >> id >> location.address >> location.file >> location.pid >> location.line
                   >> location.column >> location.parentLocationId;
            if (location.file != -1)
                checkString(location.file);
            if (location.parentLocationId != -1)
                checkLocation(location.parentLocationId);
            QCOMPARE(id, m_locations.length());
            m_locations.append(location);
            break;
        }
        case SymbolDefinition: {
            qint32 id;
            SymbolEvent symbol;
            stream >> id >> symbol.name >> symbol.binary >> symbol.isKernel;
            if (symbol.name != -1)
                checkString(symbol.name);
            if (symbol.binary != -1)
                checkString(symbol.binary);
            QCOMPARE(id, m_symbols.length());
            m_symbols.append(symbol);
            break;
        }
        case AttributesDefinition: {
            qint32 id;
            AttributeEvent attribute;
            stream >> id >> attribute.type >> attribute.config >> attribute.name;
            checkString(attribute.name);
            QCOMPARE(id, m_attributes.length());
            m_attributes.append(attribute);
            break;
        }
        case StringDefinition: {
            qint32 id;
            QByteArray string;
            stream >> id >> string;
            QCOMPARE(id, m_strings.length());
            m_strings.append(string);
            break;
        }
        case Error: {
            qint32 errorCode;
            QString message;
            stream >> errorCode >> message;
            // Ignore this: We cannot find the elfs of course.
            break;
        }
        case Sample:
        case TracePointSample: {
            SampleEvent sample;
            stream >> sample.pid >> sample.tid >> sample.time >> sample.frames
                   >> sample.numGuessedFrames >> sample.attributeId >> sample.period
                   >> sample.weight;
            for (qint32 locationId : qAsConst(sample.frames))
                checkLocation(locationId);
            checkAttribute(sample.attributeId);

            if (eventType == TracePointSample) {
                stream >> sample.tracePointData;
                for (auto it = sample.tracePointData.constBegin(),
                     end = sample.tracePointData.constEnd();
                     it != end; ++it) {
                    checkString(it.key());
                }
            }

            m_samples.append(sample);
            break;
        }
        case Progress: {
            const float oldProgress = progress;
            stream >> progress;
            QVERIFY(progress > oldProgress);
            break;
        }
        case TracePointFormat: {
            qint32 id;
            TracePointFormatEvent tracePointFormat;
            stream >> id >> tracePointFormat.system >> tracePointFormat.name
                   >> tracePointFormat.flags;
            checkString(tracePointFormat.system);
            checkString(tracePointFormat.name);
            QVERIFY(!m_tracePointFormats.contains(id));
            m_tracePointFormats.insert(id, tracePointFormat);
            break;
        }
        default:
            stream.skipRawData(size);
            break;
        }
        QVERIFY(stream.atEnd());
    }
}