File: callgraphview.h

package info (click to toggle)
kcachegrind 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,844 kB
  • ctags: 3,250
  • sloc: cpp: 28,541; perl: 325; python: 235; makefile: 7; sh: 5
file content (686 lines) | stat: -rw-r--r-- 14,058 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/* This file is part of KCachegrind.
   Copyright (c) 2003-2015 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * Callgraph View
 */

#ifndef CALLGRAPHVIEW_H
#define CALLGRAPHVIEW_H

#include <qwidget.h>
#include <qmap.h>
#include <qtimer.h>

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsPolygonItem>
#include <QGraphicsPathItem>
#include <QPixmap>
#include <QFocusEvent>
#include <QPolygon>
#include <QList>
#include <QKeyEvent>
#include <QResizeEvent>
#include <QContextMenuEvent>
#include <QMouseEvent>

#include "treemap.h" // for DrawParams
#include "tracedata.h"
#include "traceitemview.h"

class QProcess;
class QTemporaryFile;
class QIODevice;

class CanvasNode;
class CanvasEdge;
class GraphEdge;
class CallGraphView;


// temporary parts of call graph to be shown
class GraphNode
{
public:
	GraphNode();

	TraceFunction* function() const
	{
		return _f;
	}

	void setFunction(TraceFunction* f)
	{
		_f = f;
	}

	CanvasNode* canvasNode() const
	{
		return _cn;
	}

	void setCanvasNode(CanvasNode* cn)
	{
		_cn = cn;
	}

	bool isVisible() const
	{
		return _visible;
	}

	void setVisible(bool v)
	{
		_visible = v;
	}

	void clearEdges();
	void sortEdges();
	void addCallee(GraphEdge*);
	void addCaller(GraphEdge*);
	void addUniqueCallee(GraphEdge*);
	void addUniqueCaller(GraphEdge*);
	void removeEdge(GraphEdge*);
	double calleeCostSum();
	double calleeCountSum();
	double callerCostSum();
	double callerCountSum();

	// keyboard navigation
	TraceCall* visibleCaller();
	TraceCall* visibleCallee();
	void setCallee(GraphEdge*);
	void setCaller(GraphEdge*);
	TraceFunction* nextVisible();
	TraceFunction* priorVisible();
	TraceCall* nextVisibleCaller(GraphEdge* = 0);
	TraceCall* nextVisibleCallee(GraphEdge* = 0);
	TraceCall* priorVisibleCaller(GraphEdge* = 0);
	TraceCall* priorVisibleCallee(GraphEdge* = 0);

	double self, incl;

private:
	TraceFunction* _f;
	CanvasNode* _cn;
	bool _visible;

	QList<GraphEdge*> callers, callees;

	// for keyboard navigation
	int _lastCallerIndex, _lastCalleeIndex;
	bool _lastFromCaller;
};


class GraphEdge
{
public:
	GraphEdge();

	CanvasEdge* canvasEdge() const
	{
		return _ce;
	}

	void setCanvasEdge(CanvasEdge* ce)
	{
		_ce = ce;
	}

	TraceCall* call() const
	{
		return _c;
	}

	void setCall(TraceCall* c)
	{
		_c = c;
	}

	bool isVisible() const
	{
		return _visible;
	}

	void setVisible(bool v)
	{
		_visible = v;
	}

	GraphNode* fromNode() const
	{
		return _fromNode;
	}

	GraphNode* toNode() const
	{
		return _toNode;
	}

	TraceFunction* from() const
	{
		return _from;
	}

	TraceFunction* to() const
	{
		return _to;
	}

	// has special cases for collapsed edges
	QString prettyName();

	void setCaller(TraceFunction* f)
	{
		_from = f;
	}

	void setCallee(TraceFunction* f)
	{
		_to = f;
	}

	void setCallerNode(GraphNode* n)
	{
		_fromNode = n;
	}

	void setCalleeNode(GraphNode* n)
	{
		_toNode = n;
	}

	// keyboard navigation
	TraceFunction* visibleCaller();
	TraceFunction* visibleCallee();
	TraceCall* nextVisible();
	TraceCall* priorVisible();

	double cost, count;

private:
	// we have a _c *and* _from/_to because for collapsed edges,
	// only _to or _from will be unequal NULL
	TraceCall* _c;
	TraceFunction * _from, * _to;
	GraphNode *_fromNode, *_toNode;
	CanvasEdge* _ce;
	bool _visible;
	// for keyboard navigation: have we last reached this edge via a caller?
	bool _lastFromCaller;
};


typedef QMap<TraceFunction*, GraphNode> GraphNodeMap;
typedef QMap<QPair<TraceFunction*, TraceFunction*>, GraphEdge> GraphEdgeMap;


/* Abstract Interface for graph options */
class GraphOptions
{
public:
	enum Layout {TopDown, LeftRight, Circular};
	virtual ~GraphOptions() {}
	virtual double funcLimit() = 0;
	virtual double callLimit() = 0;
	virtual int maxCallerDepth() = 0;
	virtual int maxCalleeDepth() = 0;
	virtual bool showSkipped() = 0;
	virtual bool expandCycles() = 0;
	virtual bool clusterGroups() = 0;
	virtual int detailLevel() = 0;
	virtual Layout layout() = 0;

	static QString layoutString(Layout);
	static Layout layout(QString);
};

/* Graph Options Storage */
class StorableGraphOptions: public GraphOptions
{
 public:
    StorableGraphOptions();
	virtual ~StorableGraphOptions(){}
    // implementation of getters
    virtual double funcLimit() { return _funcLimit; }
    virtual double callLimit() { return _callLimit; }
    virtual int maxCallerDepth() { return _maxCallerDepth; }
    virtual int maxCalleeDepth() { return _maxCalleeDepth; }
    virtual bool showSkipped() { return _showSkipped; }
    virtual bool expandCycles() { return _expandCycles; }
    virtual bool clusterGroups() { return _clusterGroups; }
    virtual int detailLevel() { return _detailLevel; }
    virtual Layout layout() { return _layout; }

    // setters
    void setMaxCallerDepth(int d) { _maxCallerDepth = d; }
    void setMaxCalleeDepth(int d) { _maxCalleeDepth = d; }
    void setFuncLimit(double l) { _funcLimit = l; }
    void setCallLimit(double l) { _callLimit = l; }
    void setShowSkipped(bool b) { _showSkipped = b; }
    void setExpandCycles(bool b) { _expandCycles = b; }
    void setClusterGroups(bool b) { _clusterGroups = b; }
    void setDetailLevel(int l) { _detailLevel = l; }
    void setLayout(Layout l) { _layout = l; }

 protected:
    double _funcLimit, _callLimit;
    int _maxCallerDepth, _maxCalleeDepth;
    bool _showSkipped, _expandCycles, _clusterGroups;
    int _detailLevel;
    Layout _layout;
};

/**
 * GraphExporter
 *
 * Generates a graph file for "dot"
 * Create an instance and
 */
class GraphExporter : public StorableGraphOptions
{
public:
	GraphExporter();
	GraphExporter(TraceData*, TraceFunction*, EventType*,
	              ProfileContext::Type,
		      QString filename = QString());
	virtual ~GraphExporter();

	void reset(TraceData*, CostItem*, EventType*,
		   ProfileContext::Type,
		   QString filename = QString());

	QString filename()
	{
		return _dotName;
	}

	int edgeCount()
	{
		return _edgeMap.count();
	}

	int nodeCount()
	{
		return _nodeMap.count();
	}

	// Set the object from which to get graph options for creation.
	// Default is this object itself (supply 0 for default)
	void setGraphOptions(GraphOptions* go = 0);

	// Create a subgraph with given limits/maxDepths
	void createGraph();

	// calls createGraph before dumping of not already created
	void writeDot(QIODevice* = 0);

	// to map back to structures when parsing a layouted graph

	/* <toFunc> is a helper for node() and edge().
	 * Do not use the returned pointer directly, but only with
	 * node() or edge(), because it could be a dangling pointer.
	 */
	TraceFunction* toFunc(QString);
	GraphNode* node(TraceFunction*);
	GraphEdge* edge(TraceFunction*, TraceFunction*);

	/* After CanvasEdges are attached to GraphEdges, we can
	 * sort the incoming and outgoing edges of all nodes
	 * regarding start/end points for keyboard navigation
	 */
	void sortEdges();

private:
	void buildGraph(TraceFunction*, int, bool, double);

	QString _dotName;
	CostItem* _item;
	EventType* _eventType;
	ProfileContext::Type _groupType;
	QTemporaryFile* _tmpFile;
	double _realFuncLimit, _realCallLimit;
	int _maxDepth;
	bool _graphCreated;

	GraphOptions* _go;

	// optional graph attributes
	bool _useBox;

	// graph parts written to file
	GraphNodeMap _nodeMap;
	GraphEdgeMap _edgeMap;
};


/**
 * A panner laid over a QGraphicsScene
 */
class PanningView : public QGraphicsView
{
	Q_OBJECT

public:
        explicit PanningView(QWidget * parent = 0);

	void setZoomRect(const QRectF& r);

signals:
	void zoomRectMoved(qreal dx, qreal dy);
	void zoomRectMoveFinished();

protected:
	void mousePressEvent(QMouseEvent*);
	void mouseMoveEvent(QMouseEvent*);
	void mouseReleaseEvent(QMouseEvent*);
	void drawForeground(QPainter * p, const QRectF&);

	QRectF _zoomRect;
	bool _movingZoomRect;
	QPointF _lastPos;
};


/*
 * Canvas Items:
 * - CanvasNode       (Rectangular Area)
 * - CanvasEdge       (Spline curve)
 * - CanvasEdgeLabel  (Label for edges)
 * - CanvasEdgeArrow  (Arrows at the end of the edge spline)
 * - CanvasFrame      (Grey background blending to show active node)
 */

enum {
	CANVAS_NODE = 1122,
	CANVAS_EDGE, CANVAS_EDGELABEL, CANVAS_EDGEARROW,
	CANVAS_FRAME
};

class CanvasNode : public QGraphicsRectItem, public StoredDrawParams
{
public:
	CanvasNode(CallGraphView*, GraphNode*, int, int, int, int);

	void updateGroup();
	void setSelected(bool);
	void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

	GraphNode* node()
	{
		return _node;
	}

	int type() const
	{
		return CANVAS_NODE;
	}

private:
	GraphNode* _node;
	CallGraphView* _view;
};


class CanvasEdgeLabel : public QGraphicsRectItem, public StoredDrawParams
{
public:
	CanvasEdgeLabel(CallGraphView*, CanvasEdge*, int, int, int, int);

	void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

	CanvasEdge* canvasEdge()
	{
		return _ce;
	}

	int type() const
	{
		return CANVAS_EDGELABEL;
	}

	double percentage() const
	{
		return _percentage;
	}

private:
	CanvasEdge* _ce;
	CallGraphView* _view;

	double _percentage;
};


class CanvasEdgeArrow : public QGraphicsPolygonItem
{
public:
        explicit CanvasEdgeArrow(CanvasEdge*);

	void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

	CanvasEdge* canvasEdge()
	{
		return _ce;
	}

	int type() const
	{
		return CANVAS_EDGEARROW;
	}

private:
	CanvasEdge* _ce;
};


class CanvasEdge : public QGraphicsPathItem
{
public:
        explicit CanvasEdge(GraphEdge*);

	void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

	void setSelected(bool);

	CanvasEdgeLabel* label()
	{
		return _label;
	}

	void setLabel(CanvasEdgeLabel* l);

	CanvasEdgeArrow* arrow()
	{
		return _arrow;
	}

	void setArrow(CanvasEdgeArrow* a);

	const QPolygon& controlPoints() const
	{
		return _points;
	}

	void setControlPoints(const QPolygon& a);

	GraphEdge* edge()
	{
		return _edge;
	}

	int type() const
	{
		return CANVAS_EDGE;
	}

private:
	GraphEdge* _edge;
	CanvasEdgeLabel* _label;
	CanvasEdgeArrow* _arrow;
	QPolygon _points;

	double _thickness;
};


class CanvasFrame : public QGraphicsRectItem
{
public:
        explicit CanvasFrame(CanvasNode*);

	int type() const
	{
		return CANVAS_FRAME;
	}

	bool hit(const QPoint&) const
	{
		return false;
	}

	void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);

private:
	static QPixmap* _p;
};


class CallGraphTip;

/**
 * A QGraphicsView showing a part of the call graph
 * and another zoomed out CanvasView in a border acting as
 * a panner to select to visible part (only if needed)
 */
class CallGraphView : public QGraphicsView, public TraceItemView,
    public StorableGraphOptions
{
	Q_OBJECT

public:
	enum ZoomPosition {TopLeft, TopRight, BottomLeft, BottomRight, Auto, Hide};

	explicit CallGraphView(TraceItemView* parentView, QWidget* parent=0,
			       const char* name = 0);
	~CallGraphView();

	void restoreOptions(const QString& prefix, const QString& postfix);
	void saveOptions(const QString& prefix, const QString& postfix);

	QWidget* widget()
	{
		return this;
	}

	QString whatsThis() const;

	ZoomPosition zoomPos() const
	{
		return _zoomPosition;
	}

	static ZoomPosition zoomPos(QString);
	static QString zoomPosString(ZoomPosition);

public slots:
	void zoomRectMoved(qreal, qreal);
	void zoomRectMoveFinished();

	void showRenderWarning();
	void showRenderError(QString);
	void stopRendering();
	void readDotOutput();
	void dotError();
	void dotExited();

	// context menu trigger handlers
	void callerDepthTriggered(QAction*);
	void calleeDepthTriggered(QAction*);
	void nodeLimitTriggered(QAction*);
	void callLimitTriggered(QAction*);
	void zoomPosTriggered(QAction*);
	void layoutTriggered(QAction*);

protected:
	void resizeEvent(QResizeEvent*);
	void mousePressEvent(QMouseEvent*);
	void mouseMoveEvent(QMouseEvent*);
	void mouseReleaseEvent(QMouseEvent*);
	void mouseDoubleClickEvent(QMouseEvent*);
	void contextMenuEvent(QContextMenuEvent*);
	void keyPressEvent(QKeyEvent*);
	void focusInEvent(QFocusEvent*);
	void focusOutEvent(QFocusEvent*);
	void scrollContentsBy(int dx, int dy);

private:
	void updateSizes(QSize s = QSize(0,0));
	CostItem* canShow(CostItem*);
	void doUpdate(int, bool);
	void refresh();
	void makeFrame(CanvasNode*, bool active);
	void clear();
	void showText(QString);

	// context menu builders
	QAction* addCallerDepthAction(QMenu*,QString,int);
	QMenu* addCallerDepthMenu(QMenu*);
	QAction* addCalleeDepthAction(QMenu*,QString,int);
	QMenu* addCalleeDepthMenu(QMenu*);
	QAction* addNodeLimitAction(QMenu*,QString,double);
	QMenu* addNodeLimitMenu(QMenu*);
	QAction* addCallLimitAction(QMenu*,QString,double);
	QMenu* addCallLimitMenu(QMenu*);
	QAction* addZoomPosAction(QMenu*,QString,ZoomPosition);
	QMenu* addZoomPosMenu(QMenu*);
	QAction* addLayoutAction(QMenu*,QString,Layout);
	QMenu* addLayoutMenu(QMenu*);

	QGraphicsScene *_scene;
	int _xMargin, _yMargin;
	PanningView *_panningView;
	double _panningZoom;

	CallGraphTip* _tip;

	bool _isMoving;
	QPoint _lastPos;

	GraphExporter _exporter;

	GraphNode* _selectedNode;
	GraphEdge* _selectedEdge;

	// widget options
	ZoomPosition _zoomPosition, _lastAutoPosition;

	// background rendering
	QProcess* _renderProcess;
	QString _renderProcessCmdLine;
	QTimer _renderTimer;
	GraphNode* _prevSelectedNode;
	QPoint _prevSelectedPos;
	QString _unparsedOutput;
};


#endif