summaryrefslogtreecommitdiff
path: root/src/analyze/gui/callercalleemodel.cpp
blob: 429f77578114b078fa3b616009c5c3e7a3941a72 (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
/*
 * Copyright 2016-2017 Milian Wolff <mail@milianw.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "callercalleemodel.h"

#ifdef NO_K_LIB
#include "noklib.h"
#else
#include <KLocalizedString>
#endif

#include "util.h"

#include <QTextStream>

#include <cmath>

namespace {
/// TODO: share code
QString basename(const QString& path)
{
    int idx = path.lastIndexOf(QLatin1Char('/'));
    return path.mid(idx + 1);
}
}

CallerCalleeModel::CallerCalleeModel(QObject* parent)
    : QAbstractTableModel(parent)
{
    qRegisterMetaType<CallerCalleeRows>();
}

CallerCalleeModel::~CallerCalleeModel() = default;

QVariant CallerCalleeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation != Qt::Horizontal || section < 0 || section >= NUM_COLUMNS) {
        return {};
    }
    if (role == Qt::InitialSortOrderRole) {
        if (section == SelfAllocatedColumn || section == SelfAllocationsColumn || section == SelfPeakColumn
            || section == SelfPeakInstancesColumn || section == SelfLeakedColumn || section == SelfTemporaryColumn || section == InclusiveAllocatedColumn
            || section == InclusivePeakInstancesColumn || section == InclusiveAllocationsColumn || section == InclusivePeakColumn
            || section == InclusiveLeakedColumn || section == InclusiveTemporaryColumn) {
            return Qt::DescendingOrder;
        }
    }
    if (role == Qt::DisplayRole) {
        switch (static_cast<Columns>(section)) {
        case FileColumn:
            return i18n("File");
        case LineColumn:
            return i18n("Line");
        case FunctionColumn:
            return i18n("Function");
        case ModuleColumn:
            return i18n("Module");
        case SelfAllocationsColumn:
            return i18n("Allocations (Self)");
        case SelfTemporaryColumn:
            return i18n("Temporary (Self)");
        case SelfPeakColumn:
            return i18n("Peak (Self)");
        case SelfPeakInstancesColumn:
            return i18n("Peak instances (Self.)");
        case SelfLeakedColumn:
            return i18n("Leaked (Self)");
        case SelfAllocatedColumn:
            return i18n("Allocated (Self)");
        case InclusiveAllocationsColumn:
            return i18n("Allocations (Incl.)");
        case InclusiveTemporaryColumn:
            return i18n("Temporary (Incl.)");
        case InclusivePeakColumn:
            return i18n("Peak (Incl.)");
        case InclusivePeakInstancesColumn:
            return i18n("Peak instances (Incl.)");
        case InclusiveLeakedColumn:
            return i18n("Leaked (Incl.)");
        case InclusiveAllocatedColumn:
            return i18n("Allocated (Incl.)");
        case LocationColumn:
            return i18n("Location");
        case NUM_COLUMNS:
            break;
        }
    } else if (role == Qt::ToolTipRole) {
        switch (static_cast<Columns>(section)) {
        case FileColumn:
            return i18n("<qt>The file where the allocation function was called from. "
                        "May be empty when debug information is missing.</qt>");
        case LineColumn:
            return i18n("<qt>The line number where the allocation function was called from. "
                        "May be empty when debug information is missing.</qt>");
        case FunctionColumn:
            return i18n("<qt>The parent function that called an allocation function. "
                        "May be unknown when debug information is missing.</qt>");
        case ModuleColumn:
            return i18n("<qt>The module, i.e. executable or shared library, from "
                        "which an allocation function was "
                        "called.</qt>");
        case SelfAllocationsColumn:
            return i18n("<qt>The number of times an allocation function was directly "
                        "called from this location.</qt>");
        case SelfTemporaryColumn:
            return i18n("<qt>The number of direct temporary allocations. These "
                        "allocations are directly followed by a "
                        "free without any other allocations in-between.</qt>");
        case SelfPeakColumn:
            return i18n("<qt>The maximum heap memory in bytes consumed from "
                        "allocations originating directly at "
                        "this location. "
                        "This takes deallocations into account.</qt>");
        case SelfPeakInstancesColumn:
            return i18n("<qt>The maximum number of instances in created"
                        "from allocations originating at this "
                        "location or from functions called from here. "
                        "This takes deallocations into account.</qt>");
        case SelfLeakedColumn:
            return i18n("<qt>The bytes allocated directly at this location that have "
                        "not been deallocated.</qt>");
        case SelfAllocatedColumn:
            return i18n("<qt>The sum of all bytes directly allocated from this "
                        "location, ignoring deallocations.</qt>");
        case InclusiveAllocationsColumn:
            return i18n("<qt>The inclusive number of times an allocation function "
                        "was called from this location or any "
                        "functions called from here.</qt>");
        case InclusiveTemporaryColumn:
            return i18n("<qt>The number of inclusive temporary allocations. These "
                        "allocations are directly followed by "
                        "a free without any other allocations in-between.</qt>");
        case InclusivePeakColumn:
            return i18n("<qt>The inclusive maximum heap memory in bytes consumed "
                        "from allocations originating at this "
                        "location or from functions called from here. "
                        "This takes deallocations into account.</qt>");
        case InclusivePeakInstancesColumn:
            return i18n("<qt>The inclusive maximum number of instances in created"
                        "from allocations originating at this "
                        "location or from functions called from here. "
                        "This takes deallocations into account.</qt>");
        case InclusiveLeakedColumn:
            return i18n("<qt>The bytes allocated at this location that have not been "
                        "deallocated.</qt>");
        case InclusiveAllocatedColumn:
            return i18n("<qt>The inclusive sum of all bytes allocated from this "
                        "location or functions called from "
                        "here, ignoring deallocations.</qt>");
        case LocationColumn:
            return i18n("<qt>The location from which an allocation function was "
                        "called. Function symbol and file "
                        "information "
                        "may be unknown when debug information was missing when "
                        "the memory profiler was run.</qt>");
        case NUM_COLUMNS:
            break;
        }
    }
    return {};
}

QVariant CallerCalleeModel::data(const QModelIndex& index, int role) const
{
    if (!hasIndex(index.row(), index.column(), index.parent())) {
        return {};
    }

    const auto& row = (role == MaxCostRole) ? m_maxCost : m_rows.at(index.row());

    if (role == Qt::DisplayRole || role == SortRole || role == MaxCostRole) {
        switch (static_cast<Columns>(index.column())) {
        case SelfAllocatedColumn:
            if (role == SortRole || role == MaxCostRole) {
                return static_cast<qint64>(row.selfCost.allocated);
            } else {
                return Util::formatByteSize(row.selfCost.allocated, 1);
            }
        case SelfAllocationsColumn:
            return static_cast<qint64>(row.selfCost.allocations);
        case SelfTemporaryColumn:
            return static_cast<qint64>(row.selfCost.temporary);
        case SelfPeakColumn:
            if (role == SortRole || role == MaxCostRole) {
                return static_cast<qint64>(row.selfCost.peak);
            } else {
                return Util::formatByteSize(row.selfCost.peak, 1);
            }
        case SelfPeakInstancesColumn:
            return static_cast<qint64>(row.selfCost.peak_instances);
        case SelfLeakedColumn:
            if (role == SortRole || role == MaxCostRole) {
                return static_cast<qint64>(row.selfCost.leaked);
            } else {
                return Util::formatByteSize(row.selfCost.leaked, 1);
            }
        case InclusiveAllocatedColumn:
            if (role == SortRole || role == MaxCostRole) {
                return static_cast<qint64>(row.inclusiveCost.allocated);
            } else {
                return Util::formatByteSize(row.inclusiveCost.allocated, 1);
            }
        case InclusiveAllocationsColumn:
            return static_cast<qint64>(row.inclusiveCost.allocations);
        case InclusiveTemporaryColumn:
            return static_cast<qint64>(row.inclusiveCost.temporary);
        case InclusivePeakColumn:
            if (role == SortRole || role == MaxCostRole) {
                return static_cast<qint64>(row.inclusiveCost.peak);
            } else {
                return Util::formatByteSize(row.inclusiveCost.peak, 1);
            }
        case InclusivePeakInstancesColumn:
            return static_cast<qint64>(row.inclusiveCost.peak_instances);
        case InclusiveLeakedColumn:
            if (role == SortRole || role == MaxCostRole) {
                return static_cast<qint64>(row.inclusiveCost.leaked);
            } else {
                return Util::formatByteSize(row.inclusiveCost.leaked, 1);
            }
        case FunctionColumn:
            return row.location->function;
        case ModuleColumn:
            return row.location->module;
        case FileColumn:
            return row.location->file;
        case LineColumn:
            return row.location->line;
        case LocationColumn:
            if (row.location->file.isEmpty()) {
                return i18n("%1 in ?? (%2)", basename(row.location->function), basename(row.location->module));
            } else {
                return i18n("%1 in %2:%3 (%4)", row.location->function, basename(row.location->file),
                            row.location->line, basename(row.location->module));
            }
        case NUM_COLUMNS:
            break;
        }
    } else if (role == Qt::ToolTipRole) {
        QString tooltip;
        QTextStream stream(&tooltip);
        stream << "<qt><pre style='font-family:monospace;'>";
        if (row.location->line > 0) {
            stream << i18nc("1: function, 2: file, 3: line, 4: module", "%1\n  at %2:%3\n  in %4",
                            row.location->function.toHtmlEscaped(), row.location->file.toHtmlEscaped(),
                            row.location->line, row.location->module.toHtmlEscaped());
        } else {
            stream << i18nc("1: function, 2: module", "%1\n  in %2", row.location->function.toHtmlEscaped(),
                            row.location->module.toHtmlEscaped());
        }
        stream << '\n';
        stream << i18n("inclusive: allocated %1 over %2 calls (%3 temporary, i.e. "
                       "%4%), peak at %5, leaked %6",
                       Util::formatByteSize(row.inclusiveCost.allocated, 1),
                       row.inclusiveCost.allocations,
                       row.inclusiveCost.temporary, round(float(row.inclusiveCost.temporary) * 100.f * 100.f
                                                          / std::max(int64_t(1), row.inclusiveCost.allocations))
                           / 100.f,
                       Util::formatByteSize(row.inclusiveCost.peak, 1),
                       Util::formatByteSize(row.inclusiveCost.leaked, 1));
        stream << '\n';
        stream << i18n(
            "self: allocated %1 over %2 calls (%3 temporary, i.e. %4%), "
            "peak at %5, leaked %6",
            Util::formatByteSize(row.selfCost.allocated, 1),
            row.selfCost.allocations, row.selfCost.temporary,
            round(float(row.selfCost.temporary) * 100.f * 100.f / std::max(int64_t(1), row.selfCost.allocations))
                / 100.f,
            Util::formatByteSize(row.selfCost.peak, 1),
            Util::formatByteSize(row.selfCost.leaked, 1));
        stream << '\n';
        stream << "</pre></qt>";
        return tooltip;
    } else if (role == LocationRole) {
        return QVariant::fromValue(row.location);
    }
    return {};
}

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

int CallerCalleeModel::rowCount(const QModelIndex& parent) const
{
    return parent.isValid() ? 0 : m_rows.size();
}

void CallerCalleeModel::resetData(const QVector<CallerCalleeData>& rows)
{
    beginResetModel();
    m_rows = rows;
    endResetModel();
}

void CallerCalleeModel::setSummary(const SummaryData& data)
{
    beginResetModel();
    m_maxCost.inclusiveCost = data.cost;
    m_maxCost.selfCost = data.cost;
    endResetModel();
}

void CallerCalleeModel::clearData()
{
    beginResetModel();
    m_rows = {};
    m_maxCost = {};
    endResetModel();
}