File: coverageitem.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 (330 lines) | stat: -rw-r--r-- 9,093 bytes parent folder | download | duplicates (2)
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
/*
    This file is part of KCachegrind.

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

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

/*
 * Items of coverage view.
 */

#include "coverageitem.h"


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


// CallerCoverageItem


CallerCoverageItem::CallerCoverageItem(QTreeWidget* parent, Coverage* c,
                                       TraceFunction* base,
                                       EventType* ct,
                                       ProfileContext::Type gt)
    : QTreeWidgetItem(parent)
{
    _skipped = 0;
    _coverage = c;
    _function = c->function();
    _base = base;
    _groupType = ProfileContext::InvalidType;

    setText(3, _function->prettyNameWithLocation());
    setTextAlignment(0, Qt::AlignRight);
    setTextAlignment(1, Qt::AlignRight);
    setTextAlignment(2, Qt::AlignRight);
    setCostType(ct);
    setGroupType(gt);
}

CallerCoverageItem::CallerCoverageItem(QTreeWidget* parent, int skipped, Coverage* c,
                                       TraceFunction* base,
                                       EventType* ct,
                                       ProfileContext::Type gt)
    : QTreeWidgetItem(parent)
{
    _skipped = skipped;
    _coverage = c;
    _function = c->function();
    _base = base;
    _groupType = ProfileContext::InvalidType;

    //~ singular (%n function skipped)
    //~ plural (%n functions skipped)
    setText(3, QObject::tr("(%n function(s) skipped)", "", _skipped));
    setTextAlignment(0, Qt::AlignRight);
    setTextAlignment(1, Qt::AlignRight);
    setTextAlignment(2, Qt::AlignRight);
    setCostType(ct);
    setGroupType(gt);
}

void CallerCoverageItem::setGroupType(ProfileContext::Type gt)
{
    if (_skipped) return;
    if (_groupType == gt) return;
    _groupType = gt;

    QColor c = GlobalGUIConfig::functionColor(_groupType, _function);
    setIcon(3, colorPixmap(10, 10, c));
}

void CallerCoverageItem::setCostType(EventType* ct)
{
    _costType = ct;
    update();
}

void CallerCoverageItem::update()
{
    if (!_coverage) {
        setText(0, QString());
        setText(1, QString());
        return;
    }

    _pSum = 100.0 * _coverage->inclusive();
    SubCost realSum = _base->inclusive()->subCost(_costType);
    _sum = SubCost(realSum * _coverage->inclusive());
    QString str;
    if (GlobalConfig::showPercentage())
        str = QStringLiteral("%1").arg(_pSum, 0, 'f', GlobalConfig::percentPrecision());
    else
        str = _sum.pretty();

    if (_skipped) {
        setText(0, QStringLiteral("< %1").arg(str));
        return;
    }

    setText(0, str);
    setIcon(0, partitionPixmap(25, 10, _coverage->inclusiveHistogram(), nullptr,
                               Coverage::maxHistogramDepth, false));

    // call count
    _cc  = SubCost(_coverage->callCount());
    setText(2, _cc ? _cc.pretty() : QStringLiteral("(0)"));

    // distance (min/max/median)
    _distance = _coverage->inclusiveMedian();
    QString distString;
    if (_coverage->minDistance() == _coverage->maxDistance())
        distString = QString::number(_distance);
    else
        distString = QStringLiteral("%1-%2 (%3)")
                     .arg(_coverage->minDistance())
                     .arg(_coverage->maxDistance())
                     .arg(_distance);
    setText(1, distString);
}


bool CallerCoverageItem::operator<( const QTreeWidgetItem & other ) const
{
    const CallerCoverageItem* ci1 = this;
    const CallerCoverageItem* ci2 = (CallerCoverageItem*) &other;
    int col = treeWidget()->sortColumn();

    // a skip entry is always sorted last
    if (ci1->_skipped) return true;
    if (ci2->_skipped) return false;

    if (col==0) {
        if (ci1->_pSum < ci2->_pSum) return true;
        if (ci1->_pSum > ci2->_pSum) return false;

        // for same percentage (e.g. all 100%), use distance info
        return ci1->_distance < ci2->_distance;
    }

    if (col==1) {
        return ci1->_distance < ci2->_distance;
    }

    if (col==2) {
        return ci1->_cc < ci2->_cc;
    }

    return QTreeWidgetItem::operator <(other);
}



// CalleeCoverageItem

CalleeCoverageItem::CalleeCoverageItem(QTreeWidget* parent, Coverage* c,
                                       TraceFunction* base,
                                       EventType* ct,
                                       ProfileContext::Type gt)
    : QTreeWidgetItem(parent)
{
    _skipped = 0;
    _coverage = c;
    _function = c ? c->function() : nullptr;
    _base = base;
    _groupType = ProfileContext::InvalidType;

    if ( _function )
        setText(4, _function->prettyNameWithLocation());

    setTextAlignment(0, Qt::AlignRight);
    setTextAlignment(1, Qt::AlignRight);
    setTextAlignment(2, Qt::AlignRight);
    setTextAlignment(3, Qt::AlignRight);

    setCostType(ct);
    setGroupType(gt);
}

CalleeCoverageItem::CalleeCoverageItem(QTreeWidget* parent, int skipped, Coverage* c,
                                       TraceFunction* base,
                                       EventType* ct,
                                       ProfileContext::Type gt)
    : QTreeWidgetItem(parent)
{
    _skipped = skipped;
    _coverage = c;
    _function = c ? c->function() : nullptr;
    _base = base;
    _groupType = ProfileContext::InvalidType;

    setText(4, QObject::tr("(%n function(s) skipped)", "", _skipped));

    setTextAlignment(0, Qt::AlignRight);
    setTextAlignment(1, Qt::AlignRight);
    setTextAlignment(2, Qt::AlignRight);
    setTextAlignment(3, Qt::AlignRight);

    setCostType(ct);
    setGroupType(gt);
}

void CalleeCoverageItem::setGroupType(ProfileContext::Type gt)
{
    if (_skipped) return;
    if (_groupType == gt) return;
    _groupType = gt;

    QColor c = GlobalGUIConfig::functionColor(_groupType, _function);
    setIcon(4, colorPixmap(10, 10, c));
}

void CalleeCoverageItem::setCostType(EventType* ct)
{
    _costType = ct;
    update();
}

void CalleeCoverageItem::update()
{
    if (!_coverage) {
        setText(0, QString());
        setText(1, QString());
        setText(2, QString());
        return;
    }

    _pSum = 100.0 * _coverage->inclusive();

    // pSum/pSelf are percentages of inclusive cost of base
    SubCost realSum = _base->inclusive()->subCost(_costType);
    _sum = SubCost(realSum * _coverage->inclusive());


    QString str;
    if (GlobalConfig::showPercentage())
        str = QStringLiteral("%1").arg(_pSum, 0, 'f', GlobalConfig::percentPrecision());
    else
        str = _sum.pretty();

    if (_skipped) {
        str = QStringLiteral("< %1").arg(str);
        setText(0, str);
        setText(1, str);
        return;
    }
    setText(0, str);

    _pSelf = 100.0 * _coverage->self();
    _self = SubCost(realSum * _coverage->self());

    if (GlobalConfig::showPercentage()) {
        setText(1, QStringLiteral("%1")
                .arg(_pSelf, 0, 'f', GlobalConfig::percentPrecision()));
    }
    else {
        setText(1, _self.pretty());
    }

    setIcon(0, partitionPixmap(25, 10, _coverage->inclusiveHistogram(), nullptr,
                               Coverage::maxHistogramDepth, false));
    setIcon(1, partitionPixmap(25, 10, _coverage->selfHistogram(), nullptr,
                               Coverage::maxHistogramDepth, false));


    _cc  = SubCost(_coverage->callCount());
    setText(3, _cc ? _cc.pretty() : QStringLiteral("(0)"));

    // for comparisons
    _distance = _coverage->inclusiveMedian();
    QString distString;
    if (_coverage->minDistance() == _coverage->maxDistance())
        distString = QString::number(_distance);
    else {
        int sMed = _coverage->selfMedian();
        QString med;
        if (_distance == sMed)
            med = QString::number(_distance);
        else
            med = QStringLiteral("%1/%2").arg(_distance).arg(sMed);

        distString = QStringLiteral("%1-%2 (%3)")
                     .arg(_coverage->minDistance())
                     .arg(_coverage->maxDistance())
                     .arg(med);
    }
    setText(2, distString);
}


bool CalleeCoverageItem::operator<( const QTreeWidgetItem & other ) const
{
    CalleeCoverageItem* ci = (CalleeCoverageItem*) &other;
    int col = treeWidget()->sortColumn();
    // a skip entry is always sorted last
    if (_skipped) return true;
    if (ci->_skipped) return false;

    if (col==0) {
        if (_pSum < ci->_pSum) return true;
        if (_pSum > ci->_pSum) return false;

        // for same percentage (e.g. all 100%), use distance info
        return _distance < ci->_distance;

    }

    if (col==1) {
        if (_pSelf < ci->_pSelf) return true;
        if (_pSelf > ci->_pSelf) return false;

        // for same percentage (e.g. all 100%), use distance info
        return _distance < ci->_distance;
    }

    if (col==2) {
        // we want to sort the distance in contra direction to costs
        return _distance < ci->_distance;
    }

    if (col==3) {
        return _cc < ci->_cc;
    }
    return QTreeWidgetItem::operator <(other);
}