• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

kcachegrind

  • sources
  • kde-4.14
  • kdesdk
  • kcachegrind
  • libviews
functionlistmodel.cpp
Go to the documentation of this file.
1 /* This file is part of KCachegrind.
2  Copyright (C) 2010 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
3 
4  KCachegrind is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public
6  License as published by the Free Software Foundation, version 2.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "functionlistmodel.h"
20 
21 #include "globalguiconfig.h"
22 #include "listutils.h"
23 
24 FunctionListModel::FunctionListModel()
25  : QAbstractItemModel(0)
26 {
27  _maxCount = 300;
28  _sortColumn = 0;
29  _sortOrder = Qt::DescendingOrder;
30 
31  _headerData
32  << tr("Incl.")
33  << tr("Self")
34  << tr("Called")
35  << tr("Function")
36  << tr("Location");
37 
38  _max0 = _max1 = _max2 = 0;
39 }
40 
41 FunctionListModel::~FunctionListModel()
42 {}
43 
44 int FunctionListModel::columnCount(const QModelIndex& parent) const
45 {
46  return (parent.isValid()) ? 0 : 5;
47 }
48 
49 int FunctionListModel::rowCount(const QModelIndex& parent ) const
50 {
51  if (parent.isValid()) return 0;
52 
53  int rowCount = _topList.count();
54  // add one more row if functions are skipped
55  if (_topList.count() < _filteredList.count()) rowCount++;
56  return rowCount;
57 }
58 
59 TraceFunction* FunctionListModel::function(const QModelIndex& index)
60 {
61  if (!index.isValid()) return 0;
62 
63  return (TraceFunction*) index.internalPointer();
64 }
65 
66 QVariant FunctionListModel::data(const QModelIndex &index, int role) const
67 {
68  if (!index.isValid()) return QVariant();
69 
70  // the skipped items entry
71  if ( (_topList.count() < _filteredList.count()) &&
72  (index.row() == _topList.count()) ) {
73  if( (role != Qt::DisplayRole) || (index.column() != 3))
74  return QVariant();
75 
76  return tr("(%1 function(s) skipped)").arg(_filteredList.count() - _topList.count());
77  }
78 
79  TraceFunction *f = (TraceFunction*) index.internalPointer();
80  Q_ASSERT(f != 0);
81  switch(role) {
82  case Qt::TextAlignmentRole:
83  return (index.column()<3) ? Qt::AlignRight : Qt::AlignLeft;
84 
85  case Qt::DecorationRole:
86  switch (index.column()) {
87  case 0:
88  return getInclPixmap(f);
89  case 1:
90  return getSelfPixmap(f);
91  case 3:
92  return getNamePixmap(f);
93  default:
94  break;
95  }
96  break;
97 
98  case Qt::DisplayRole:
99  switch (index.column()) {
100  case 0:
101  return getInclCost(f);
102  case 1:
103  return getSelfCost(f);
104  case 2:
105  return getCallCount(f);
106  case 3:
107  return getName(f);
108  case 4:
109  return getLocation(f);
110  default:
111  break;
112  }
113 
114  default:
115  break;
116  }
117  return QVariant();
118 }
119 
120 Qt::ItemFlags FunctionListModel::flags(const QModelIndex &index) const
121 {
122  if (!index.isValid())
123  return 0;
124 
125  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
126 }
127 
128 QVariant FunctionListModel::headerData(int section, Qt::Orientation orientation,
129  int role) const
130 {
131  if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole))
132  return QVariant();
133 
134  return _headerData.value(section);
135 }
136 
137 QModelIndex FunctionListModel::index(int row, int column,
138  const QModelIndex &parent) const
139 {
140  if (!hasIndex(row, column, parent)) return QModelIndex();
141 
142  //the skipped items entry
143  if ( (_topList.count() < _list.count()) && (row == _topList.count()) )
144  return createIndex(row, column);
145 
146  return createIndex(row, column, (void*)_topList[row]);
147 }
148 
149 QModelIndex FunctionListModel::indexForFunction(TraceFunction *f, bool add)
150 {
151  if (!f) return QModelIndex();
152 
153  int row = _topList.indexOf(f);
154  if (row<0) {
155  // we only add a function from _list matching the filter
156  if ( !add ||
157  !_filteredList.contains(f) ) return QModelIndex();
158 
159  // find insertion point with current list order
160  FunctionLessThan lessThan(_sortColumn, _sortOrder, _eventType);
161  QList<TraceFunction*>::iterator insertPos;
162  insertPos = qLowerBound(_topList.begin(), _topList.end(),
163  f, lessThan);
164  row = insertPos - _topList.begin();
165  beginInsertRows(QModelIndex(), row, row);
166  _topList.insert(row, f);
167  endInsertRows();
168  }
169 
170  return createIndex(row, 0, (void*)f);
171 }
172 
173 QModelIndex FunctionListModel::parent(const QModelIndex& /*index*/ ) const
174 {
175  /* only toplevel items */
176  return QModelIndex();
177 }
178 
179 void FunctionListModel::sort(int column, Qt::SortOrder order)
180 {
181  _sortColumn = column;
182  _sortOrder = order;
183  computeTopList();
184 }
185 
186 void FunctionListModel::setFilter(QString filterString)
187 {
188  if (_filterString == filterString) return;
189  _filterString = filterString;
190 
191  _filter = QRegExp(_filterString, Qt::CaseInsensitive, QRegExp::Wildcard);
192  computeFilteredList();
193  computeTopList();
194 }
195 
196 void FunctionListModel::setEventType(EventType* et)
197 {
198  _eventType = et;
199  // needed to recalculate max value entries
200  computeFilteredList();
201  computeTopList();
202 }
203 
204 void FunctionListModel::setMaxCount(int c)
205 {
206  if (_maxCount == c) return;
207  _maxCount = c;
208  computeTopList();
209 }
210 
211 void FunctionListModel::resetModelData(TraceData *data,
212  TraceCostItem *group, QString filterString,
213  EventType * eventType)
214 {
215  _eventType = eventType;
216 
217  if (!group) {
218  _list.clear();
219  _groupType = ProfileContext::Function;
220  if (data) {
221  TraceFunctionMap::iterator i = data->functionMap().begin();
222  while (i != data->functionMap().end()) {
223  _list.append(&(i.value()));
224  ++i;
225  }
226  foreach(TraceFunction* f, data->functionCycles())
227  _list.append(f);
228  }
229  }
230  else {
231  _groupType = group->type();
232  switch(_groupType) {
233  case ProfileContext::Object:
234  {
235  TraceObject* o = dynamic_cast<TraceObject*>(group);
236  Q_ASSERT(o != 0);
237  _list = o->functions();
238  }
239  break;
240 
241  case ProfileContext::Class:
242  {
243  TraceClass* c = dynamic_cast<TraceClass*>(group);
244  Q_ASSERT(c != 0);
245  _list = c->functions();
246  }
247  break;
248 
249  case ProfileContext::File:
250  {
251  TraceFile* f = dynamic_cast<TraceFile*>(group);
252  Q_ASSERT(f != 0);
253  _list = f->functions();
254  }
255  break;
256 
257  case ProfileContext::FunctionCycle:
258  {
259  TraceFunctionCycle* c = dynamic_cast<TraceFunctionCycle*>(group);
260  Q_ASSERT(c != 0);
261  _list = c->members();
262  }
263  break;
264 
265  default:
266  _list.clear();
267  break;
268  }
269  }
270 
271  _filterString = filterString;
272  _filter = QRegExp(_filterString, Qt::CaseInsensitive, QRegExp::Wildcard);
273 
274  computeFilteredList();
275  computeTopList();
276 }
277 
278 void FunctionListModel::computeFilteredList()
279 {
280  FunctionLessThan lessThan0(0, Qt::AscendingOrder, _eventType);
281  FunctionLessThan lessThan1(1, Qt::AscendingOrder, _eventType);
282  FunctionLessThan lessThan2(2, Qt::AscendingOrder, _eventType);
283 
284  // reset max functions
285  _max0 = 0;
286  _max1 = 0;
287  _max2 = 0;
288 
289  _filteredList.clear();
290  int index = 0;
291  foreach(TraceFunction* f, _list) {
292  if (!_filterString.isEmpty())
293  if (_filter.indexIn(f->name()) == -1) continue;
294 
295  _filteredList.append(f);
296  if (!_max0 || lessThan0(_max0, f)) { _max0 = f; }
297  if (!_max1 || lessThan1(_max1, f)) { _max1 = f; }
298  if (!_max2 || lessThan2(_max2, f)) { _max2 = f; }
299  index++;
300  }
301 }
302 
303 void FunctionListModel::computeTopList()
304 {
305  beginResetModel();
306  _topList.clear();
307  if (_filteredList.isEmpty()) {
308  endResetModel();
309  return;
310  }
311 
312  FunctionLessThan lessThan(_sortColumn, _sortOrder, _eventType);
313  qStableSort(_filteredList.begin(), _filteredList.end(), lessThan);
314 
315  foreach(TraceFunction* f, _filteredList) {
316  _topList.append(f);
317  if (_topList.count() >= _maxCount) break;
318  }
319 
320  // append max entries
321  QList<TraceFunction*> maxList;
322  if (_max0 && !_topList.contains(_max0)) maxList.append(_max0);
323  if (_max1 && !_topList.contains(_max1)) maxList.append(_max1);
324  if (_max2 && !_topList.contains(_max2)) maxList.append(_max2);
325  qSort(maxList.begin(), maxList.end(), lessThan);
326  _topList.append(maxList);
327 
328  endResetModel();
329 }
330 
331 QString FunctionListModel::getName(TraceFunction *f) const
332 {
333  return f->prettyName();
334 }
335 
336 QPixmap FunctionListModel::getNamePixmap(TraceFunction *f) const
337 {
338  QColor c = GlobalGUIConfig::functionColor(_groupType, f);
339  return colorPixmap(10, 10, c);
340 }
341 
342 QString FunctionListModel::getLocation(TraceFunction *f) const
343 {
344  return f->prettyLocation();
345 }
346 
347 QString FunctionListModel::getSelfCost(TraceFunction *f) const
348 {
349  ProfileCostArray* selfCost = f->data();
350  if (GlobalConfig::showExpanded()) {
351  switch(_groupType) {
352  case ProfileContext::Object: selfCost = f->object(); break;
353  case ProfileContext::Class: selfCost = f->cls(); break;
354  case ProfileContext::File: selfCost = f->file(); break;
355  default: break;
356  }
357  }
358  double selfTotal = selfCost->subCost(_eventType);
359  if (selfTotal == 0.0)
360  return "-";
361 
362  // self
363  SubCost pure = f->subCost(_eventType);
364  double self = 100.0 * pure / selfTotal;
365  if (GlobalConfig::showPercentage())
366  return QString("%1")
367  .arg(self, 0, 'f', GlobalConfig::percentPrecision());
368  else
369  return f->prettySubCost(_eventType);
370 }
371 
372 QPixmap FunctionListModel::getSelfPixmap(TraceFunction *f) const
373 {
374  ProfileCostArray* selfCost = f->data();
375  if (GlobalConfig::showExpanded()) {
376  switch(_groupType) {
377  case ProfileContext::Object: selfCost = f->object(); break;
378  case ProfileContext::Class: selfCost = f->cls(); break;
379  case ProfileContext::File: selfCost = f->file(); break;
380  default: break;
381  }
382  }
383  double selfTotal = selfCost->subCost(_eventType);
384  if (selfTotal == 0.0)
385  return QPixmap();
386 
387  return costPixmap(_eventType, f, selfTotal, false);
388 }
389 
390 QString FunctionListModel::getInclCost(TraceFunction *f) const
391 {
392  double inclTotal = f->data()->subCost(_eventType);
393  if (inclTotal == 0.0)
394  return "-";
395 
396  SubCost sum = f->inclusive()->subCost(_eventType);
397  double incl = 100.0 * sum / inclTotal;
398  if (GlobalConfig::showPercentage())
399  return QString("%1")
400  .arg(incl, 0, 'f', GlobalConfig::percentPrecision());
401  else
402  return f->inclusive()->prettySubCost(_eventType);
403 }
404 
405 QPixmap FunctionListModel::getInclPixmap(TraceFunction *f) const
406 {
407  double inclTotal = f->data()->subCost(_eventType);
408  if (inclTotal == 0.0)
409  return QPixmap();
410 
411  return costPixmap(_eventType, f->inclusive(), inclTotal, false);
412 }
413 
414 
415 QString FunctionListModel::getCallCount(TraceFunction *f) const
416 {
417  QString str;
418  if (f->calledCount() > 0)
419  str = f->prettyCalledCount();
420  else {
421  if (f == f->cycle())
422  str = QString("-");
423  else
424  str = QString("(0)");
425  }
426  return str;
427 }
428 
429 //
430 // FunctionListModel::FunctionLessThan
431 //
432 bool FunctionListModel::FunctionLessThan::operator()(TraceFunction *left,
433  TraceFunction *right)
434 {
435  TraceFunction* f1 = left;
436  TraceFunction* f2 = right;
437 
438  // descending: swap arguments
439  if (_order == Qt::DescendingOrder) {
440  TraceFunction* temp = f1;
441  f1 = f2;
442  f2 = temp;
443  }
444 
445  switch(_column) {
446  case 0:
447  {
448  SubCost sum1 = f1->inclusive()->subCost(_eventType);
449  SubCost sum2 = f2->inclusive()->subCost(_eventType);
450  return sum1 < sum2;
451  }
452 
453  case 1:
454  {
455  SubCost pure1 = f1->subCost(_eventType);
456  SubCost pure2 = f2->subCost(_eventType);
457  return pure1 < pure2;
458  }
459 
460  case 2:
461  return f1->calledCount() < f2->calledCount();
462 
463  case 3:
464  return f1->name() < f2->name();
465 
466  case 4:
467  return f1->object()->name() < f2->object()->name();
468  }
469 
470  return false;
471 }
472 
473 
474 #include "functionlistmodel.moc"
QAbstractItemModel::hasIndex
bool hasIndex(int row, int column, const QModelIndex &parent) const
FunctionListModel::setFilter
void setFilter(QString filter)
Definition: functionlistmodel.cpp:186
FunctionListModel::flags
Qt::ItemFlags flags(const QModelIndex &) const
Definition: functionlistmodel.cpp:120
QList::clear
void clear()
QModelIndex
TraceFile
A source file containing function definitions.
Definition: tracedata.h:1295
FunctionListModel::setMaxCount
void setMaxCount(int)
Definition: functionlistmodel.cpp:204
GlobalConfig::showPercentage
static bool showPercentage()
Definition: globalconfig.cpp:328
ProfileCostArray::subCost
SubCost subCost(EventType *)
Returns a sub cost.
Definition: costitem.cpp:591
TraceObject::functions
const TraceFunctionList & functions() const
Definition: tracedata.h:1346
ProfileContext::FunctionCycle
Definition: context.h:46
FunctionListModel::~FunctionListModel
~FunctionListModel()
Definition: functionlistmodel.cpp:41
FunctionListModel::FunctionLessThan
Definition: functionlistmodel.h:66
FunctionListModel::function
TraceFunction * function(const QModelIndex &index)
Definition: functionlistmodel.cpp:59
TraceClass::functions
const TraceFunctionList & functions() const
Definition: tracedata.h:1281
ProfileContext::File
Definition: context.h:48
TraceFunctionCycle
A cycle of recursive calling functions.
Definition: tracedata.h:1241
CostItem::type
ProfileContext::Type type() const
Definition: costitem.h:45
GlobalGUIConfig::functionColor
static QColor functionColor(ProfileContext::Type gt, TraceFunction *)
Definition: globalguiconfig.cpp:205
FunctionListModel::index
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const
Definition: functionlistmodel.cpp:137
ProfileCostArray::prettySubCost
QString prettySubCost(EventType *)
Returns a cost attribute converted to a string (with space after every 3 digits)
Definition: costitem.cpp:601
TraceFunction
A traced function.
Definition: tracedata.h:1122
TraceCostItem
Definition: tracedata.h:980
TraceFunction::object
TraceObject * object() const
Definition: tracedata.h:1164
FunctionListModel::setEventType
void setEventType(EventType *)
Definition: functionlistmodel.cpp:196
TraceFunction::cycle
TraceFunctionCycle * cycle()
Definition: tracedata.h:1200
functionlistmodel.h
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
EventType
A cost type, e.g.
Definition: eventtype.h:43
ProfileContext::Class
Definition: context.h:47
TraceObject
A object containing a text segment (shared lib/executable) with defined functions.
Definition: tracedata.h:1331
QList::value
T value(int i) const
QList::indexOf
int indexOf(const T &value, int from) const
TraceFunction::cls
TraceClass * cls() const
Definition: tracedata.h:1162
QAbstractItemModel::beginResetModel
void beginResetModel()
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
QModelIndex::isValid
bool isValid() const
ProfileCostArray
An array of basic cost metrics for a trace item.
Definition: costitem.h:144
QList::count
int count(const T &value) const
FunctionListModel::sort
void sort(int column, Qt::SortOrder order)
Definition: functionlistmodel.cpp:179
QList::append
void append(const T &value)
QAbstractItemModel::endInsertRows
void endInsertRows()
FunctionListModel::indexForFunction
QModelIndex indexForFunction(TraceFunction *f, bool add=false)
Definition: functionlistmodel.cpp:149
listutils.h
FunctionListModel::FunctionLessThan::operator()
bool operator()(TraceFunction *left, TraceFunction *right)
Definition: functionlistmodel.cpp:432
TraceData::functionCycles
const TraceFunctionCycleList & functionCycles()
Definition: tracedata.h:1443
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
TraceFunctionCycle::members
const TraceFunctionList & members() const
Definition: tracedata.h:1254
QModelIndex::row
int row() const
TraceCostItem::name
virtual QString name() const
Returns dynamic name info (without type)
Definition: tracedata.h:986
QModelIndex::internalPointer
void * internalPointer() const
TraceFunction::prettyName
QString prettyName() const
Similar to name, but prettyfied = more descriptive to humans.
Definition: tracedata.cpp:1889
CostItem::data
virtual TraceData * data()
Definition: costitem.cpp:111
FunctionListModel::FunctionListModel
FunctionListModel()
Definition: functionlistmodel.cpp:24
QString
QList
QMap::end
iterator end()
QColor
globalguiconfig.h
QMap::begin
iterator begin()
TraceFunction::file
TraceFile * file() const
Definition: tracedata.h:1163
QPixmap
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
QList::end
iterator end()
QList::contains
bool contains(const T &value) const
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
FunctionListModel::data
QVariant data(const QModelIndex &, int) const
Definition: functionlistmodel.cpp:66
GlobalConfig::showExpanded
static bool showExpanded()
Definition: globalconfig.cpp:333
TraceFile::functions
const TraceFunctionList & functions() const
Definition: tracedata.h:1313
GlobalConfig::percentPrecision
static int percentPrecision()
Definition: globalconfig.cpp:385
QList::insert
void insert(int i, const T &value)
FunctionListModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: functionlistmodel.cpp:44
SubCost
Cost event counter, simple wrapper around a 64bit entity.
Definition: subcost.h:32
TraceFunction::calledCount
SubCost calledCount()
Definition: tracedata.cpp:2239
FunctionListModel::resetModelData
void resetModelData(TraceData *data, TraceCostItem *group, QString filter, EventType *eventType)
Definition: functionlistmodel.cpp:211
QModelIndex::column
int column() const
QAbstractItemModel
TraceClass
A C++ Class / Namespace.
Definition: tracedata.h:1271
TraceData::functionMap
TraceFunctionMap & functionMap()
Definition: tracedata.h:1441
TraceFunction::prettyLocation
QString prettyLocation(int maxFiles=0) const
Definition: tracedata.cpp:2047
ProfileContext::Object
Definition: context.h:49
TraceData
This class holds profiling data of multiple tracefiles generated with cachegrind on one command...
Definition: tracedata.h:1363
QAbstractItemModel::endResetModel
void endResetModel()
QObject::parent
QObject * parent() const
TraceFunction::prettyCalledCount
QString prettyCalledCount()
Definition: tracedata.cpp:2267
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
costPixmap
QPixmap costPixmap(EventType *ct, ProfileCostArray *cost, double total, bool framed)
Definition: listutils.cpp:217
colorPixmap
QPixmap colorPixmap(int w, int h, QColor c)
Definition: listutils.cpp:32
QList::begin
iterator begin()
FunctionListModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: functionlistmodel.cpp:128
FunctionListModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: functionlistmodel.cpp:49
TraceInclusiveCost::inclusive
ProfileCostArray * inclusive()
Definition: tracedata.cpp:163
ProfileContext::Function
Definition: context.h:46
QVariant
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:39:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcachegrind

Skip menu "kcachegrind"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal