aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/perfprofiler/perfconfigeventsmodel.cpp
blob: ee36ce33f932d106b10954fbd970ab24687df505 (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
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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
****************************************************************************/

#include "perfconfigeventsmodel.h"

#include <utils/algorithm.h>

#include <QMetaEnum>

namespace PerfProfiler {
namespace Internal {

PerfConfigEventsModel::PerfConfigEventsModel(PerfSettings *settings, QObject *parent) :
    QAbstractTableModel(parent), m_settings(settings)
{
    connect(m_settings, &PerfSettings::changed, this, &PerfConfigEventsModel::reset);
}

int PerfConfigEventsModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_settings->events().length();
}

int PerfConfigEventsModel::columnCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : ColumnInvalid;
}

QVariant PerfConfigEventsModel::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
        break; // Retrieve the actual value
    default:
        return QVariant(); // ignore
    }

    QString event = m_settings->events().value(index.row());
    const EventDescription description = parseEvent(event);
    switch (index.column()) {
    case ColumnEventType: {
        if (role == Qt::DisplayRole) {
            if (description.eventType == EventTypeInvalid)
                return QVariant();
            auto meta = QMetaEnum::fromType<EventType>();
            return QString::fromLatin1(meta.valueToKey(description.eventType))
                    .mid(static_cast<int>(strlen("EventType"))).toLower();
        }
        return description.eventType;
    }
    case ColumnSubType: {
        switch (description.eventType) {
        case EventTypeHardware:
        case EventTypeSoftware:
        case EventTypeCache:
            if (role == Qt::DisplayRole)
                return subTypeString(description.eventType, description.subType);
            return description.subType;
        case EventTypeRaw:
            if (role == Qt::DisplayRole)
                return QString("r%1").arg(description.numericEvent, 3, 16, QLatin1Char('0'));
            else
                return description.numericEvent;
        case EventTypeBreakpoint:
            if (role == Qt::DisplayRole)
                return QString("0x%1").arg(description.numericEvent, 16, 16, QLatin1Char('0'));
            else
                return description.numericEvent;
        case EventTypeCustom:
            return description.customEvent;
        default:
            return QVariant();
        }
    }
    case ColumnOperation:
        if (role == Qt::DisplayRole) {
            if (description.eventType == EventTypeBreakpoint) {
                QString result;
                if (description.operation & OperationLoad)
                    result += 'r';
                if (description.operation & OperationStore)
                    result += 'w';
                if (description.operation & OperationExecute)
                    result += 'x';
                return result;
            }

            if (description.eventType == EventTypeCache) {
                if (description.operation == OperationInvalid)
                    return QVariant();
                auto meta = QMetaEnum::fromType<Operation>();
                return QString::fromLatin1(meta.valueToKey(description.operation)).mid(
                            static_cast<int>(strlen("Operation"))).toLower();
            }

            return QVariant();
        }

        return description.operation;
    case ColumnResult:
        if (role == Qt::DisplayRole) {
            if (description.result == ResultInvalid)
                return QVariant();
            auto meta = QMetaEnum::fromType<Result>();
            return QString::fromLatin1(meta.valueToKey(description.result)).mid(
                        static_cast<int>(strlen("Result"))).toLower();
        }
        return description.result;
    default:
        return QVariant();
    }
}

bool PerfConfigEventsModel::setData(const QModelIndex &dataIndex, const QVariant &value, int role)
{
    if (role != Qt::EditRole)
        return false;

    const int row = dataIndex.row();
    const int column = dataIndex.column();

    QStringList events = m_settings->events();
    EventDescription description = parseEvent(events[row]);
    switch (column) {
    case ColumnEventType:
        description.eventType = qvariant_cast<EventType>(value);
        break;
    case ColumnSubType:
        switch (description.eventType) {
        case EventTypeHardware:
        case EventTypeSoftware:
        case EventTypeCache:
            description.subType = qvariant_cast<SubType>(value);
            break;
        case EventTypeRaw:
            description.numericEvent = value.toULongLong();
            break;
        case EventTypeBreakpoint:
            description.numericEvent = value.toULongLong();
            break;
        case EventTypeCustom:
            description.customEvent = value.toString();
            break;
        default:
            break;
        }
        break;
    case ColumnOperation:
        description.operation = value.toInt();
        break;
    case ColumnResult:
        description.result = qvariant_cast<Result>(value);
        break;
    }
    events[row] = generateEvent(description);
    m_settings->setEvents(events);
    emit dataChanged(index(row, ColumnEventType), index(row, ColumnResult));
    return true;
}

QVariant PerfConfigEventsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Vertical || role != Qt::DisplayRole)
        return QVariant();

    switch (section) {
    case ColumnEventType: return tr("Event Type");
    case ColumnSubType:   return tr("Counter");
    case ColumnOperation: return tr("Operation");
    case ColumnResult:    return tr("Result");
    default:              return QVariant();
    }

}

bool PerfConfigEventsModel::insertRows(int row, int count, const QModelIndex &parent)
{
    if (parent.isValid())
        return false;

    QStringList events = m_settings->events();
    for (int i = 0; i < count; ++i)
        events.insert(row, "dummy");
    beginInsertRows(parent, row, row + count - 1);
    m_settings->setEvents(events);
    endInsertRows();
    return true;
}

bool PerfConfigEventsModel::removeRows(int row, int count, const QModelIndex &parent)
{
    if (parent.isValid())
        return false;

    QStringList events = m_settings->events();
    for (int i = 0; i < count; ++i)
        events.removeAt(row);
    beginRemoveRows(parent, row, row + count - 1);
    m_settings->setEvents(events);
    endRemoveRows();

    if (events.isEmpty()) {
        beginInsertRows(parent, 0, 0);
        events.append("dummy");
        m_settings->setEvents(events);
        endInsertRows();
    }

    return true;
}

Qt::ItemFlags PerfConfigEventsModel::flags(const QModelIndex &index) const
{
    return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}

QString PerfConfigEventsModel::subTypeString(EventType eventType, SubType subType)
{
    switch (eventType) {
    case EventTypeHardware:
        switch (subType) {
        case SubTypeCpuCycles:             return QLatin1String("cpu-cycles");
        case SubTypeInstructions:          return QLatin1String("instructions");
        case SubTypeCacheReferences:       return QLatin1String("cache-references");
        case SubTypeCacheMisses:           return QLatin1String("cache-misses");
        case SubTypeBranchInstructions:    return QLatin1String("branch-instructions");
        case SubTypeBranchMisses:          return QLatin1String("branch-misses");
        case SubTypeBusCycles:             return QLatin1String("bus-cycles");
        case SubTypeStalledCyclesFrontend: return QLatin1String("stalled-cycles-frontend");
        case SubTypeStalledCyclesBackend:  return QLatin1String("stalled-cycles-backend");
        case SubTypeRefCycles:             return QLatin1String("ref-cycles");
        default:                           return QLatin1String("cpu-cycles");
        }
    case EventTypeSoftware:
        switch (subType) {
        case SubTypeCpuClock:              return QLatin1String("cpu-clock");
        case SubTypeTaskClock:             return QLatin1String("task-clock");
        case SubTypePageFaults:            return QLatin1String("page-faults");
        case SubTypeContextSwitches:       return QLatin1String("context-switches");
        case SubTypeCpuMigrations:         return QLatin1String("cpu-migrations");
        case SubTypeMinorFaults:           return QLatin1String("minor-faults");
        case SubTypeMajorFaults:           return QLatin1String("major-faults");
        case SubTypeAlignmentFaults:       return QLatin1String("alignment-faults");
        case SubTypeEmulationFaults:       return QLatin1String("emulation-faults");
        case SubTypeDummy:                 return QLatin1String("dummy");
        default:                           return QLatin1String("cpu-clock");
        }
    case EventTypeCache:
        switch (subType) {
        case SubTypeL1Dcache:              return QLatin1String("L1-dcache");
        case SubTypeL1Icache:              return QLatin1String("L1-icache");
        case SubTypeLLC:                   return QLatin1String("LLC");
        case SubTypeDTLB:                  return QLatin1String("dTLB");
        case SubTypeITLB:                  return QLatin1String("iTLB");
        case SubTypeBranch:                return QLatin1String("branch");
        case SubTypeNode:                  return QLatin1String("node");
        default:                           return QLatin1String("L1-dcache");
        }
    default: return QString();
    }
}

QString PerfConfigEventsModel::generateEvent(const EventDescription &description) const
{
    switch (description.eventType) {
    case EventTypeHardware:
    case EventTypeSoftware:
        return subTypeString(description.eventType, description.subType);
    case EventTypeCache: {
        QString result = subTypeString(description.eventType, description.subType);
        switch (description.operation) {
        case OperationLoad:     result += "-load";     break;
        case OperationStore:    result += "-store";    break;
        case OperationPrefetch: result += "-prefetch"; break;
        default:                result += "-load";     break;
        }
        switch (description.result) {
        case ResultRefs:   return result + "-refs";
        case ResultMisses: return result + "-misses";
        default:           return result + "-misses";
        };
    }
    case EventTypeRaw:
        return QString::fromLatin1("r%1").arg(description.numericEvent, 3, 16, QLatin1Char('0'));
    case EventTypeBreakpoint: {
        QString rwx;
        if (description.operation & OperationLoad)
            rwx += 'r';
        if (description.operation & OperationStore)
            rwx += 'w';
        if (description.operation & OperationExecute)
            rwx += 'x';
        return QString::fromLatin1("mem:%1:%2")
                .arg(description.numericEvent, 16, 16, QLatin1Char('0'))
                .arg(rwx.isEmpty() ? "r" : rwx);
    }
    case EventTypeCustom:
        return description.customEvent;
    default:
        return QLatin1String("cpu-cycles");
    }
}

void PerfConfigEventsModel::reset()
{
    beginResetModel();
    endResetModel();
}

PerfConfigEventsModel::EventDescription PerfConfigEventsModel::parseEvent(
        const QString &event) const
{
    EventDescription description;

    if (event.startsWith("mem:")) {
        description.eventType = EventTypeBreakpoint;
        QStringList parts = event.split(':');
        if (parts.length() > 1) {
            description.numericEvent = parts[1].toULongLong(nullptr, 16);
            if (parts.length() > 2) {
                QString rwx = parts[2];
                if (rwx.contains('r'))
                    description.operation = description.operation | OperationLoad;
                if (rwx.contains('w'))
                    description.operation = description.operation | OperationStore;
                if (rwx.contains('x'))
                    description.operation = description.operation | OperationExecute;
            }
        }
        return description;
    }

    if (event.startsWith('r') && event.length() == 4) {
        bool ok = false;
        const uint eventNumber = event.mid(1).toUInt(&ok, 16);
        if (ok) {
            description.eventType = EventTypeRaw;
            description.numericEvent = eventNumber;
            return description;
        }
    }

    QStringList parts = Utils::transform(event.split("-"), [](const QString &part) {
        if (part.isEmpty())
            return part;
        QString ret = part;
        ret[0] = part[0].toUpper();
        return ret;
    });

    QMetaEnum subTypeMeta = QMetaEnum::fromType<SubType>();
    QStringList extras;

    int subtype = -1;
    while (!parts.isEmpty()) {
        QString key = QString("SubType") + parts.join(QString());
        subtype = subTypeMeta.keyToValue(key.toLatin1());
        if (subtype == -1)
            extras.prepend(parts.takeLast());
        else
            break;
    }

    if (subtype != -1) {
        description.subType = SubType(subtype);
        if (subtype < int(SubTypeEventTypeSoftware))
            description.eventType = EventTypeHardware;
        else if (subtype < int(SubTypeEventTypeCache))
            description.eventType = EventTypeSoftware;
        else
            description.eventType = EventTypeCache;

        if (!extras.isEmpty()) {
            QMetaEnum operationMeta = QMetaEnum::fromType<Operation>();
            int operation = operationMeta.keyToValue(QByteArray("Operation")
                                                     + extras.takeFirst().toLatin1());
            if (operation != -1)
                description.operation = operation;
        }

        if (!extras.isEmpty()) {
            QMetaEnum resultMeta = QMetaEnum::fromType<Result>();
            int result = resultMeta.keyToValue(QByteArray("Result") + extras.takeFirst().toLatin1());
            if (result != -1)
                description.result = Result(result);
        }

        if (extras.isEmpty())
            return description;
    }

    description.eventType = EventTypeCustom;
    description.subType = SubTypeInvalid;
    description.operation = OperationInvalid;
    description.result = ResultInvalid;
    description.customEvent = event;
    return description;

}

} // namespace Internal
} // namespace PerfProfiler