File: functionlistmodel.cpp

package info (click to toggle)
kcachegrind 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,964 kB
  • sloc: cpp: 32,149; xml: 403; perl: 325; python: 235; sh: 8; makefile: 6
file content (488 lines) | stat: -rw-r--r-- 13,217 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
/*
    This file is part of KCachegrind.

    SPDX-FileCopyrightText: 2010-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

    SPDX-License-Identifier: GPL-2.0-only
*/

#include "functionlistmodel.h"

#include "globalguiconfig.h"
#include "listutils.h"

/* helper for setting function filter: we want it to work similar to globbing:
 * - escape most special characters in regexps: ( ) [ ] | . \
 * - change * to .*
 */
QString glob2Regex(QString pattern)
{
    pattern.replace(QChar('\\'),QLatin1String("\\\\"));
    pattern.replace(QChar('('),QLatin1String("\\("));
    pattern.replace(QChar(')'),QLatin1String("\\)"));
    pattern.replace(QChar('['),QLatin1String("\\["));
    pattern.replace(QChar(']'),QLatin1String("\\]"));
    pattern.replace(QChar('|'),QLatin1String("\\|"));
    pattern.replace(QChar('.'),QLatin1String("\\."));
    pattern.replace(QChar('*'),QLatin1String(".*"));

    return pattern;
}


//
// FunctionListModel
//

FunctionListModel::FunctionListModel()
    : QAbstractItemModel(nullptr)
{
    _maxCount = 300;
    _sortColumn = 0;
    _sortOrder = Qt::DescendingOrder;

    _headerData
            << tr("Incl.")
            << tr("Self")
            << tr("Called")
            << tr("Function")
            << tr("Location");

    _max0 = _max1 = _max2 = nullptr;
}

FunctionListModel::~FunctionListModel()
{}

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

int FunctionListModel::rowCount(const QModelIndex& parent ) const
{
    if (parent.isValid()) return 0;

    int rowCount = _topList.count();
    // add one more row if functions are skipped
    if (_topList.count() < _filteredList.count()) rowCount++;
    return rowCount;
}

TraceFunction* FunctionListModel::function(const QModelIndex& index)
{
    if (!index.isValid()) return nullptr;

    return (TraceFunction*) index.internalPointer();
}

QVariant FunctionListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();

    // the skipped items entry
    if ( (_topList.count() < _filteredList.count()) &&
         (index.row() == _topList.count()) ) {
        if( (role != Qt::DisplayRole) || (index.column() != 3))
            return QVariant();

        return tr("(%1 function(s) skipped)").arg(_filteredList.count() - _topList.count());
    }

    TraceFunction *f = (TraceFunction*) index.internalPointer();
    Q_ASSERT(f != nullptr);
    switch(role) {
    case Qt::TextAlignmentRole:
        return (index.column()<3) ? Qt::AlignRight : Qt::AlignLeft;

    case Qt::DecorationRole:
        switch (index.column()) {
        case 0:
            return getInclPixmap(f);
        case 1:
            return getSelfPixmap(f);
        case 3:
            return getNamePixmap(f);
        default:
            break;
        }
        break;

    case Qt::DisplayRole:
        switch (index.column()) {
        case 0:
            return getInclCost(f);
        case 1:
            return getSelfCost(f);
        case 2:
            return getCallCount(f);
        case 3:
            return getName(f);
        case 4:
            return getLocation(f);
        default:
            break;
        }

    default:
        break;
    }
    return QVariant();
}

Qt::ItemFlags FunctionListModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

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

    return _headerData.value(section);
}

QModelIndex FunctionListModel::index(int row, int column,
                                     const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent)) return QModelIndex();

    //the skipped items entry
    if ( (_topList.count() < _list.count()) && (row == _topList.count()) )
        return createIndex(row, column);

    return createIndex(row, column, (void*)_topList[row]);
}

QModelIndex FunctionListModel::indexForFunction(TraceFunction *f, bool add)
{
    if (!f) return QModelIndex();

    int row = _topList.indexOf(f);
    if (row<0) {
        // we only add a function from _list matching the filter
        if ( !add ||
             !_filteredList.contains(f) ) return QModelIndex();

        // find insertion point with current list order
        FunctionLessThan lessThan(_sortColumn, _sortOrder, _eventType);
        QList<TraceFunction*>::iterator insertPos;
        insertPos = std::lower_bound(_topList.begin(), _topList.end(),
                                     f, lessThan);
        row = insertPos - _topList.begin();
        beginInsertRows(QModelIndex(), row, row);
        _topList.insert(row, f);
        endInsertRows();
    }

    return createIndex(row, 0, (void*)f);
}

QModelIndex FunctionListModel::parent(const QModelIndex& /*index*/ ) const
{
    /* only toplevel items */
    return QModelIndex();
}

void FunctionListModel::sort(int column, Qt::SortOrder order)
{
    _sortColumn = column;
    _sortOrder = order;
    computeTopList();
}

void FunctionListModel::setFilter(QString filterString)
{
    if (_filterString == filterString) return;
    _filterString = filterString;

    _filter = QRegularExpression(glob2Regex(_filterString),
                                 QRegularExpression::CaseInsensitiveOption);
    computeFilteredList();
    computeTopList();
}

void FunctionListModel::setEventType(EventType* et)
{
    _eventType = et;
    // needed to recalculate max value entries
    computeFilteredList();
    computeTopList();
}

void FunctionListModel::setMaxCount(int c)
{
    if (_maxCount == c) return;
    _maxCount = c;
    computeTopList();
}

void FunctionListModel::resetModelData(TraceData *data,
                                       TraceCostItem *group, QString filterString,
                                       EventType * eventType)
{
    _eventType = eventType;

    if (!group) {
        _list.clear();
        _groupType = ProfileContext::Function;
        if (data) {
            TraceFunctionMap::iterator i = data->functionMap().begin();
            while (i != data->functionMap().end()) {
                _list.append(&(i.value()));
                ++i;
            }
            foreach(TraceFunction* f, data->functionCycles())
                _list.append(f);
        }
    }
    else {
        _groupType = group->type();
        switch(_groupType) {
        case ProfileContext::Object:
        {
            TraceObject* o = dynamic_cast<TraceObject*>(group);
            Q_ASSERT(o != nullptr);
            _list = o->functions();
        }
            break;

        case ProfileContext::Class:
        {
            TraceClass* c = dynamic_cast<TraceClass*>(group);
            Q_ASSERT(c != nullptr);
            _list = c->functions();
        }
            break;

        case ProfileContext::File:
        {
            TraceFile* f = dynamic_cast<TraceFile*>(group);
            Q_ASSERT(f != nullptr);
            _list = f->functions();
        }
            break;

        case ProfileContext::FunctionCycle:
        {
            TraceFunctionCycle* c = dynamic_cast<TraceFunctionCycle*>(group);
            Q_ASSERT(c != nullptr);
            _list = c->members();
        }
            break;

        default:
            _list.clear();
            break;
        }
    }

    _filterString = filterString;
    _filter = QRegularExpression(glob2Regex(_filterString),
                                 QRegularExpression::CaseInsensitiveOption);

    computeFilteredList();
    computeTopList();
}

void FunctionListModel::computeFilteredList()
{
    FunctionLessThan lessThan0(0, Qt::AscendingOrder, _eventType);
    FunctionLessThan lessThan1(1, Qt::AscendingOrder, _eventType);
    FunctionLessThan lessThan2(2, Qt::AscendingOrder, _eventType);

    // reset max functions
    _max0 = nullptr;
    _max1 = nullptr;
    _max2 = nullptr;

    _filteredList.clear();
    int index = 0;
    foreach(TraceFunction* f, _list) {
        if (!_filterString.isEmpty() && _filter.isValid())
            if (!f->name().contains(_filter)) continue;

        _filteredList.append(f);
        if (!_max0 || lessThan0(_max0, f)) { _max0 = f; }
        if (!_max1 || lessThan1(_max1, f)) { _max1 = f; }
        if (!_max2 || lessThan2(_max2, f)) { _max2 = f; }
        index++;
    }
}

void FunctionListModel::computeTopList()
{
    beginResetModel();
    _topList.clear();
    if (_filteredList.isEmpty()) {
        endResetModel();
        return;
    }

    FunctionLessThan lessThan(_sortColumn, _sortOrder, _eventType);
    std::stable_sort(_filteredList.begin(), _filteredList.end(), lessThan);

    foreach(TraceFunction* f, _filteredList) {
        _topList.append(f);
        if (_topList.count() >= _maxCount) break;
    }

    // append max entries
    QList<TraceFunction*> maxList;
    if (_max0 && !_topList.contains(_max0)) maxList.append(_max0);
    if (_max1 && !_topList.contains(_max1)) maxList.append(_max1);
    if (_max2 && !_topList.contains(_max2)) maxList.append(_max2);
    std::stable_sort(maxList.begin(), maxList.end(), lessThan);
    _topList.append(maxList);

    endResetModel();
}

QString FunctionListModel::getName(TraceFunction *f) const
{
    return f->prettyName();
}

QPixmap FunctionListModel::getNamePixmap(TraceFunction *f) const
{
    QColor c = GlobalGUIConfig::functionColor(_groupType, f);
    return colorPixmap(10, 10, c);
}

QString FunctionListModel::getLocation(TraceFunction *f) const
{
    return f->prettyLocation();
}

QString FunctionListModel::getSelfCost(TraceFunction *f) const
{
    ProfileCostArray* selfCost = f->data();
    if (GlobalConfig::showExpanded()) {
        switch(_groupType) {
        case ProfileContext::Object: selfCost = f->object(); break;
        case ProfileContext::Class:  selfCost = f->cls(); break;
        case ProfileContext::File:   selfCost = f->file(); break;
        default: break;
        }
    }
    double selfTotal = selfCost->subCost(_eventType);
    if (selfTotal == 0.0)
        return QStringLiteral("-");

    // self
    SubCost pure = f->subCost(_eventType);
    double self  = 100.0 * pure / selfTotal;
    if (GlobalConfig::showPercentage())
        return QStringLiteral("%1")
                .arg(self, 0, 'f', GlobalConfig::percentPrecision());
    else
        return f->prettySubCost(_eventType);
}

QPixmap FunctionListModel::getSelfPixmap(TraceFunction *f) const
{
    ProfileCostArray* selfCost = f->data();
    if (GlobalConfig::showExpanded()) {
        switch(_groupType) {
        case ProfileContext::Object: selfCost = f->object(); break;
        case ProfileContext::Class:  selfCost = f->cls(); break;
        case ProfileContext::File:   selfCost = f->file(); break;
        default: break;
        }
    }
    double selfTotal = selfCost->subCost(_eventType);
    if (selfTotal == 0.0)
        return QPixmap();

    return costPixmap(_eventType, f, selfTotal, false);
}

QString FunctionListModel::getInclCost(TraceFunction *f) const
{
    double inclTotal = f->data()->subCost(_eventType);
    if (inclTotal == 0.0)
        return QStringLiteral("-");

    SubCost sum  = f->inclusive()->subCost(_eventType);
    double incl  = 100.0 * sum / inclTotal;
    if (GlobalConfig::showPercentage())
        return QStringLiteral("%1")
                .arg(incl, 0, 'f', GlobalConfig::percentPrecision());
    else
        return f->inclusive()->prettySubCost(_eventType);
}

QPixmap FunctionListModel::getInclPixmap(TraceFunction *f) const
{
    double inclTotal = f->data()->subCost(_eventType);
    if (inclTotal == 0.0)
        return QPixmap();

    return costPixmap(_eventType, f->inclusive(), inclTotal, false);
}


QString FunctionListModel::getCallCount(TraceFunction *f) const
{
    QString str;
    if (f->calledCount() > 0)
        str = f->prettyCalledCount();
    else {
        if (f == f->cycle())
            str = QStringLiteral("-");
        else
            str = QStringLiteral("(0)");
    }
    return str;
}

//
// FunctionListModel::FunctionLessThan
//
bool FunctionListModel::FunctionLessThan::operator()(TraceFunction *left,
                                                     TraceFunction *right)
{
    TraceFunction* f1 = left;
    TraceFunction* f2 = right;

    // descending: swap arguments
    if (_order == Qt::DescendingOrder) {
        TraceFunction* temp = f1;
        f1 = f2;
        f2 = temp;
    }

    switch(_column) {
    case 0:
    {
        SubCost sum1 = f1->inclusive()->subCost(_eventType);
        SubCost sum2 = f2->inclusive()->subCost(_eventType);
        return sum1 < sum2;
    }

    case 1:
    {
        SubCost pure1 = f1->subCost(_eventType);
        SubCost pure2 = f2->subCost(_eventType);
        return pure1 < pure2;
    }

    case 2:
        return f1->calledCount() < f2->calledCount();

    case 3:
        return f1->name() < f2->name();

    case 4:
        return f1->object()->name() < f2->object()->name();
    }

    return false;
}

#include "moc_functionlistmodel.cpp"