Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a4fac7e491be14da336d94ebcaaae0ca6c7e8a0 (plain) (blame)
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*******************************************************************************
 * Copyright (c) 2009, 2018 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.internal.callgraph;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.parts.ScrollableThumbnail;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.linuxtools.internal.callgraph.core.PluginConstants;
import org.eclipse.linuxtools.internal.callgraph.core.SystemTapParser;
import org.eclipse.linuxtools.internal.callgraph.core.SystemTapUIErrorMessages;
import org.eclipse.linuxtools.internal.callgraph.core.SystemTapView;
import org.eclipse.linuxtools.internal.callgraph.graphlisteners.AutoScrollSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 *    The SystemTap View for displaying output of the 'stap' command, and acts
 *    as a container for any graph to be rendered. Any buttons/controls/actions
 *    necessary to the smooth running of SystemTap could be placed here.
 */
public class CallgraphView extends SystemTapView {

    private StapGraphParser parser;

    private Action viewTreeview;
    private Action viewRadialview;
    private Action viewAggregateview;
    private Action viewLevelview;
    private Action viewRefresh;
    private Action animationSlow;
    private Action animationFast;
    private Action modeCollapsedNodes;
    private Action markersNext;
    private Action markersPrevious;
    private Action limits;
    private Action gotoNext;
    private Action gotoPrevious;
    private Action gotoLast;
    private Action play;
    private Action saveDot;
    private Action saveColDot;
    private Action saveCurDot;
    private Action saveText;
    private ImageDescriptor playImage = getImageDescriptor("icons/perform.png"); //$NON-NLS-1$
    private ImageDescriptor pauseImage = getImageDescriptor("icons/pause.gif"); //$NON-NLS-1$

    private Composite graphComp;
    private Composite treeComp;

    private StapGraph g;
    private static final int TREE_SIZE = 200;

    /**
     * Initializes the view by creating composites (if necessary) and canvases
     * Calls loadData(), and calls finishLoad() if not in realTime mode (otherwise
     * it is up to the user-defined update methods to finish loading).
     *
     * @return status
     *
     */
    @Override
    public IStatus initializeView(Display targetDisplay, IProgressMonitor monitor) {

        if (targetDisplay == null && Display.getCurrent() == null) {
            Display.getDefault();
        }

        makeTreeComp();
        makeGraphComp();
        graphComp.setBackgroundMode(SWT.INHERIT_FORCE);

        //Create papa canvas
        Canvas papaCanvas = new Canvas(graphComp, SWT.BORDER);
        GridLayout papaLayout = new GridLayout(1, true);
        papaLayout.horizontalSpacing=0;
        papaLayout.verticalSpacing=0;
        papaLayout.marginHeight=0;
        papaLayout.marginWidth=0;
        papaCanvas.setLayout(papaLayout);
        GridData papaGD = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
        papaGD.widthHint=160;
        papaCanvas.setLayoutData(papaGD);

        //Add first button
        Image image = getImageDescriptor("icons/up.gif").createImage(); //$NON-NLS-1$
        Button up = new Button(papaCanvas, SWT.PUSH);
        GridData buttonData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
        buttonData.widthHint = 150;
        buttonData.heightHint = 20;
        up.setData(buttonData);
        up.setImage(image);
        up.setToolTipText(Messages.getString("CallgraphView.ThumbNailUp")); //$NON-NLS-1$

        //Add thumb canvas
        Canvas thumbCanvas = new Canvas(papaCanvas, SWT.NONE);

        //Add second button
        image = getImageDescriptor("icons/down.gif").createImage(); //$NON-NLS-1$
        Button down = new Button(papaCanvas, SWT.PUSH);
        buttonData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
        buttonData.widthHint = 150;
        buttonData.heightHint = 0;
        down.setData(buttonData);
        down.setImage(image);
        down.setToolTipText(Messages.getString("CallgraphView.ThumbNailDown")); //$NON-NLS-1$

        //Initialize graph
        g = new StapGraph(graphComp, SWT.BORDER, treeComp, papaCanvas, this);
        g.setLayoutData(new GridData(masterComposite.getBounds().width,Display.getCurrent().getBounds().height - TREE_SIZE));

        up.addSelectionListener(new AutoScrollSelectionListener(
                AutoScrollSelectionListener.AUTO_SCROLL_UP, g));
        down.addSelectionListener(new AutoScrollSelectionListener(
                AutoScrollSelectionListener.AUTO_SCROLL_DOWN, g));

        //Initialize thumbnail
        GridData thumbGD = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
        thumbGD.widthHint=160;
        thumbCanvas.setLayoutData(thumbGD);
        LightweightSystem lws = new LightweightSystem(thumbCanvas);
        ScrollableThumbnail thumb = new ScrollableThumbnail(g.getViewport());
        thumb.setSource(g.getContents());
        lws.setContents(thumb);

        loadData(monitor);
        return finishLoad(monitor);
    }

    /**
     * Load data.
     * @param mon -- Progress monitor.
     * @return
     */
    private IStatus loadData(IProgressMonitor mon) {
        IProgressMonitor monitor = mon;
        //Dummy node, set start time
        if (g.getNodeData(0) == null) {
            g.loadData(SWT.NONE, 0, StapGraph.CONSTANT_TOP_NODE_NAME,
                    1, 1, -1, false, ""); //$NON-NLS-1$
        }
        g.setStartTime(parser.startTime);
        g.setEndTime(parser.endingTimeInNS);


        /*
         * Load graph data
         */
        for (int id_parent : parser.serialMap.keySet()) {
            if (id_parent < 0) {
                continue;
            }
            boolean marked = false;
            String msg = ""; //$NON-NLS-1$
            if (g.getNodeData(id_parent) == null) {
                if (parser.markedMap.get(id_parent) != null) {
                    marked = true;
                    msg = parser.markedMap.remove(id_parent);
                }
                g.loadData(SWT.NONE, id_parent, parser.serialMap.get(id_parent), parser.timeMap.get(id_parent),
                        1, 0, marked, msg);
            }

            for (int key :parser.neighbourMaps.keySet()) {
                HashMap<Integer, ArrayList<Integer>> outNeighbours = parser.neighbourMaps.get(key);
                if (outNeighbours == null || outNeighbours.get(id_parent) == null) {
                    continue;
                }
                for (int id_child : outNeighbours.get(id_parent)) {
                    if (g.getNodeData(id_child) != null && id_child < 0) {
                        //Assume this is an additional call of the same node
                        //Should only happen in dot-files!!
                        g.addCalled(id_child);
                        continue;
                    } else if (g.getNodeData(id_child) != null) {
                        continue;
                    }
                    if (monitor.isCanceled()) {
                        return Status.CANCEL_STATUS;
                    }

                    marked = false;
                    msg = ""; //$NON-NLS-1$
                    if (parser.markedMap.get(id_child) != null) {
                        marked = true;
                        msg = parser.markedMap.remove(id_child);
                    }
                    if (id_child != -1) {
                        if (parser.timeMap.get(id_child) == null){
                            g.loadData(SWT.NONE, id_child, parser.serialMap
                                    .get(id_child), parser.timeMap.get(0),
                                    1, id_parent, marked,msg);
                        }else{
                            g.loadData(SWT.NONE, id_child, parser.serialMap
                                    .get(id_child), parser.timeMap.get(id_child),
                                    1, id_parent, marked,msg);
                        }
                    }
                }
            }

            if (parser.neighbourMaps.size() > 1) {
                g.setThreaded();
            }
        }

        monitor.worked(1);
        if (parser.markedMap.size() > 0) {
            //Still some markers left
            for (int key : parser.markedMap.keySet()) {
                g.insertMessage(key, parser.markedMap.get(key));
            }

            //Erase the remaining nodes, just in case
            parser.markedMap.clear();
        }


        if (g.aggregateTime == null) {
            g.aggregateTime = new HashMap<>();
        }
        if (g.aggregateCount == null) {
            g.aggregateCount = new HashMap<>();
        }

        g.aggregateCount.putAll(parser.countMap);
        g.aggregateTime.putAll(parser.aggregateTimeMap);
        //TODO: Do not set to 0.
        g.setLastFunctionCalled(0);


        //Finish off by collapsing nodes, initializing the tree and setting options
        g.recursivelyCollapseAllChildrenOfNode(g.getTopNode());
        monitor.worked(1);
        setGraphOptions(true);
        g.initializeTree();
        g.setProject(parser.project);


        return Status.OK_STATUS;
    }

    /**
     * Completes the loading process by calculating aggregate data.
     *
     * @param monitor
     * @return
     */
    private IStatus finishLoad(IProgressMonitor monitor) {

        if (g.aggregateCount == null) {
            g.aggregateCount = new HashMap<>();
        }

        g.aggregateCount.putAll(parser.countMap);

        if (g.aggregateTime == null) {
            g.aggregateTime = new HashMap<>();
        }
        g.aggregateTime.putAll(parser.aggregateTimeMap);

        //Set total time
        if (parser.totalTime != -1) {
            g.setTotalTime(parser.totalTime);
        }

        //-------------Finish initializations
        //Generate data for collapsed nodes
        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        }
        g.initializeTree();


        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        }
        g.setCallOrderList(parser.callOrderList);
        g.setProject(parser.project);


        this.initializePartControl();
        return Status.OK_STATUS;
    }


    /**
     * Enable or Disable the graph options
     * @param visible
     */
    private void setGraphOptions (boolean visible){
        play.setEnabled(visible);
        saveFile.setEnabled(visible);
        saveDot.setEnabled(visible);
        saveColDot.setEnabled(visible);
        saveCurDot.setEnabled(visible);
        saveText.setEnabled(visible);

        viewTreeview.setEnabled(visible);
        viewRadialview.setEnabled(visible);
        viewAggregateview.setEnabled(visible);
        viewLevelview.setEnabled(visible);
        viewRefresh.setEnabled(visible);
        limits.setEnabled(visible);

        markersNext.setEnabled(visible);
        markersPrevious.setEnabled(visible);

        animationSlow.setEnabled(visible);
        animationFast.setEnabled(visible);
        modeCollapsedNodes.setEnabled(visible);

        gotoNext.setEnabled(visible);
        gotoPrevious.setEnabled(visible);
        gotoLast.setEnabled(visible);
    }



    private void makeTreeComp() {
        if (treeComp != null && !treeComp.isDisposed()) {
            treeComp.dispose();
        }

        treeComp = new Composite(this.masterComposite, SWT.NONE);
        GridData treegd = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
        treegd.widthHint = TREE_SIZE;
        treeComp.setLayout(new FillLayout());
        treeComp.setLayoutData(treegd);
    }

    private void makeGraphComp() {
        if (graphComp != null && !graphComp.isDisposed()) {
            graphComp.dispose();
        }
        graphComp = new Composite(this.masterComposite, SWT.NONE);
        GridData graphgd = new GridData(SWT.FILL, SWT.FILL, true, true);
        GridLayout gl = new GridLayout(2, false);
        gl.horizontalSpacing=0;
        gl.verticalSpacing=0;

        graphComp.setLayout(gl);
        graphComp.setLayoutData(graphgd);
    }


    /**
     * This must be executed before a Graph is displayed
     */
    private void initializePartControl(){
        setGraphOptions(true);
        if (graphComp == null) {
            return;
        }
        graphComp.setParent(masterComposite);

        if (treeComp != null) {
            treeComp.setParent(masterComposite);
        }

        graphComp.setSize(masterComposite.getSize().x ,masterComposite.getSize().y);
    }

    /**
     * The action performed by saveText.
     */
    private void saveTextAction() {
        //Prints an 80 char table
        Shell sh = new Shell();
        FileDialog dialog = new FileDialog(sh, SWT.SAVE);
        String filePath = dialog.open();

        if (filePath == null) {
            return;
        }
        File f = new File(filePath);
        f.delete();
        try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
            f.createNewFile();
            StringBuilder builder = new StringBuilder();
            builder.append("                           Function                           | Called |  Time\n"); //$NON-NLS-1$

            for (StapData k : g.nodeDataMap.values()) {
                if ( (!k.isCollapsed ) && !k.isOnlyChildWithThisName()) {
                    continue;
                }
                if (k.isCollapsed) {
                    StringBuilder name = new StringBuilder(k.name);
                    name = fixString(name, 60);
                    builder.append(" " + name + " | "); //$NON-NLS-1$ //$NON-NLS-2$

                    StringBuilder called = new StringBuilder("" + k.timesCalled); //$NON-NLS-1$
                    called = fixString(called, 6);

                    StringBuilder time = new StringBuilder("" + //$NON-NLS-1$
                            StapNode.numberFormat.format((float) k.getTime()/g.getTotalTime() * 100)
                            + "%"); //$NON-NLS-1$
                    time = fixString(time, 6);

                    builder.append(called + " | " + time + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
                }

                if (builder.length() > 0) {
                    out.append(builder.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * This is a callback that will allow us to create the viewer and
     * initialize it.
     */
    @Override
    public void createPartControl(Composite parent) {
        if (masterComposite != null) {
            masterComposite.dispose();
        }
        masterComposite = parent;
        GridLayout layout = new GridLayout(2, false);
        layout.horizontalSpacing=0;
        GridData gd = new GridData(100, 100);

        parent.setLayout(layout);
        parent.setLayoutData(gd);

        // LOAD ALL ACTIONS
        createActions();

        //MENU FOR SYSTEMTAP BUTTONS
        IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();


        //MENU FOR SYSTEMTAP GRAPH OPTIONS
        IMenuManager menu = getViewSite().getActionBars().getMenuManager();

        // ADD OPTIONS TO THE GRAPH MENU
        addFileMenu();

        saveCurDot = new Action(Messages.getString("CallgraphView.SaveViewAsDot")) { //$NON-NLS-1$
            @Override
            public void run(){
                writeToDot(g.getCollapseMode(), g.nodeMap.keySet());
            }

        };
        saveDot = new Action(Messages.getString("CallgraphView.SaveAllUncollapsedAsDot")) { //$NON-NLS-1$
            @Override
            public void run(){
              writeToDot(false, g.nodeDataMap.keySet());
            }
        };

        saveColDot = new Action (Messages.getString("CallgraphView.SaveAllCollapsedAsDot")) { //$NON-NLS-1$
             @Override
            public void run(){
                    writeToDot(true, g.nodeDataMap.keySet());
                }

        };

        saveText = new Action (Messages.getString("CallgraphView.SaveCollapsedAsASCII")) { //$NON-NLS-1$
            @Override
            public void run() {
                saveTextAction();
            }
        };
        IMenuManager saveMenu = new MenuManager(Messages.getString("CallgraphView.SaveMenu")); //$NON-NLS-1$
        file.add(saveMenu);
        saveMenu.add(saveCurDot);
        saveMenu.add(saveColDot);
        saveMenu.add(saveText);
        saveMenu.add(saveDot);
        IMenuManager view = new MenuManager(Messages.getString("CallgraphView.ViewMenu")); //$NON-NLS-1$
        IMenuManager animation = new MenuManager(Messages.getString("CallgraphView.AnimationMenu")); //$NON-NLS-1$
        IMenuManager markers = new MenuManager(Messages.getString("CallgraphView.Markers")); //$NON-NLS-1$
        IMenuManager gotoMenu = new MenuManager(Messages.getString("CallgraphView.GoTo")); //$NON-NLS-1$
        menu.add(view);
        menu.add(gotoMenu);
        addHelpMenu();

        view.add(viewTreeview);
        view.add(viewRadialview);
        view.add(viewAggregateview);
        view.add(viewLevelview);
        view.add(getViewRefresh());
        view.add(modeCollapsedNodes);
        view.add(limits);
        view.add(animation);


        gotoMenu.add(play);
        gotoMenu.add(gotoPrevious);
        gotoMenu.add(gotoNext);
        gotoMenu.add(gotoLast);
        gotoMenu.add(markers);

        addKillButton();
        mgr.add(play);
        mgr.add(viewRadialview);
        mgr.add(viewTreeview);
        mgr.add(viewLevelview);
        mgr.add(viewAggregateview);
        mgr.add(modeCollapsedNodes);

        markers.add(markersNext);
        markers.add(markersPrevious);

        animation.add(animationSlow);
        animation.add(animationFast);

        setGraphOptions(false);
    }


    private static StringBuilder fixString(StringBuilder name, int length) {
        if (name.length() > length) {
            name = new StringBuilder(name.substring(0, length - 1));
        } else {
            int diff = length - name.length();
            boolean left = true;
            while (diff > 0) {
                if (left) {
                    name.insert(0, " "); //$NON-NLS-1$
                    left = false;
                } else {
                    name.append(" "); //$NON-NLS-1$
                    left = true;
                }
                diff--;
            }
        }
        return name;
    }


    private void createViewActions() {
        viewTreeview = new Action(Messages.getString("CallgraphView.TreeView")){ //$NON-NLS-1$
            @Override
            public void run() {
                g.draw(StapGraph.CONSTANT_DRAWMODE_TREE,
                        g.getAnimationMode(), g.getRootVisibleNodeNumber());
                g.scrollTo(g.getNode(g.getRootVisibleNodeNumber())
                        .getLocation().x - g.getBounds().width / 2, g
                        .getNode(g.getRootVisibleNodeNumber())
                        .getLocation().y);
                if (play != null) {
                    play.setEnabled(true);
                }
            }
        };
        ImageDescriptor treeImage = getImageDescriptor("icons/tree_view.gif"); //$NON-NLS-1$
        viewTreeview.setImageDescriptor(treeImage);


        //Set drawmode to radial view
        viewRadialview = new Action(Messages.getString("CallgraphView.RadialView")){ //$NON-NLS-1$
            @Override
            public void run(){
                g.draw(StapGraph.CONSTANT_DRAWMODE_RADIAL,
                        g.getAnimationMode(), g.getRootVisibleNodeNumber());
                if (play != null) {
                    play.setEnabled(true);
                }
            }
        };
        ImageDescriptor d = getImageDescriptor("/icons/radial_view.gif"); //$NON-NLS-1$
        viewRadialview.setImageDescriptor(d);

        //Set drawmode to aggregate view
        viewAggregateview = new Action(Messages.getString("CallgraphView.AggregateView")){ //$NON-NLS-1$
            @Override
            public void run(){
                g.draw(StapGraph.CONSTANT_DRAWMODE_AGGREGATE,
                        g.getAnimationMode(), g.getRootVisibleNodeNumber());
                if (play != null) {
                    play.setEnabled(false);
                }
            }
        };
        ImageDescriptor aggregateImage = getImageDescriptor("/icons/view_aggregateview.gif"); //$NON-NLS-1$
        viewAggregateview.setImageDescriptor(aggregateImage);


        //Set drawmode to level view
        viewLevelview = new Action(Messages.getString("CallgraphView.LevelView")){ //$NON-NLS-1$
            @Override
            public void run(){
                g.draw(StapGraph.CONSTANT_DRAWMODE_LEVEL,
                        g.getAnimationMode(), g.getRootVisibleNodeNumber());
                if (play != null) {
                    play.setEnabled(true);
                }
            }
        };
        ImageDescriptor levelImage = getImageDescriptor("/icons/showchild_mode.gif"); //$NON-NLS-1$
        viewLevelview.setImageDescriptor(levelImage);


        this.viewRefresh = new Action(Messages.getString("CallgraphView.Reset")){ //$NON-NLS-1$
            @Override
            public void run(){
                g.reset();
            }
        };
        ImageDescriptor refreshImage = getImageDescriptor("/icons/nav_refresh.gif"); //$NON-NLS-1$
        getViewRefresh().setImageDescriptor(refreshImage);

    }

    /**
     * Populates Animate menu.
     */
    private void createAnimateActions() {
        //Set animation mode to slow
        animationSlow = new Action(Messages.getString("CallgraphView.AnimationSlow"), IAction.AS_RADIO_BUTTON){ //$NON-NLS-1$
            @Override
            public void run(){
                g.setAnimationMode(StapGraph.CONSTANT_ANIMATION_SLOW);
                this.setChecked(true);
                animationSlow.setChecked(true);
                animationFast.setChecked(false);
            }
        };

        animationSlow.setChecked(true);

        //Set animation mode to fast
        animationFast = new Action(Messages.getString("CallgraphView.AnimationFast"), IAction.AS_RADIO_BUTTON){ //$NON-NLS-1$
            @Override
            public void run(){
                g.setAnimationMode(StapGraph.CONSTANT_ANIMATION_FASTEST);
                animationSlow.setChecked(false);
                animationFast.setChecked(true);
            }
        };

        //Toggle collapse mode
        modeCollapsedNodes = new Action(Messages.getString("CallgraphView.CollapsedMode"), IAction.AS_CHECK_BOX){ //$NON-NLS-1$
            @Override
            public void run(){

                if (g.isCollapseMode()) {
                    g.setCollapseMode(false);
                    g.draw(g.getRootVisibleNodeNumber());
                } else {
                    g.setCollapseMode(true);
                    g.draw(g.getRootVisibleNodeNumber());
                }
            }
        };

        ImageDescriptor newImage = getImageDescriptor("icons/mode_collapsednodes.gif"); //$NON-NLS-1$
        modeCollapsedNodes.setImageDescriptor(newImage);

        limits = new Action(Messages.getString("CallgraphView.SetLimits"), IAction.AS_PUSH_BUTTON) { //$NON-NLS-1$
            private Spinner limit;
            private Spinner buffer;
            private Shell sh;
            @Override
            public void run() {
                sh = new Shell();
                sh.setLayout(new GridLayout());
                sh.setSize(150, 200);
                Label limitLabel = new Label(sh, SWT.NONE);
                limitLabel.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT, true, false));
                limitLabel.setText(Messages.getString("CallgraphView.MaxNodes")); //$NON-NLS-1$
                limit = new Spinner(sh, SWT.BORDER);
                limit.setMaximum(5000);
                limit.setSelection(g.getMaxNodes());
                limit.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT, true, false));

                Label bufferLabel = new Label(sh, SWT.NONE);
                bufferLabel.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT, true, false));
                bufferLabel.setText(Messages.getString("CallgraphView.MaxDepth")); //$NON-NLS-1$
                buffer = new Spinner(sh, SWT.BORDER);
                buffer.setMaximum(5000);
                buffer.setSelection(g.getLevelBuffer());
                buffer.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT, true, false));

                Button setLimit = new Button(sh, SWT.PUSH);
                setLimit.setText(Messages.getString("CallgraphView.SetValues")); //$NON-NLS-1$
                setLimit.setLayoutData(new GridData(SWT.CENTER, SWT.DEFAULT, true, false));
				setLimit.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
					boolean redraw = false;
					if (limit.getSelection() >= 0 && buffer.getSelection() >= 0) {
						g.setMaxNodes(limit.getSelection());
						g.setLevelBuffer(buffer.getSelection());

						if (g.changeLevelLimits(g.getLevelOfNode(g.getRootVisibleNodeNumber()))) {
							SystemTapUIErrorMessages mess = new SystemTapUIErrorMessages(
									Messages.getString("CallgraphView.BufferTooHigh"), //$NON-NLS-1$
									Messages.getString("CallgraphView.BufferTooHigh"), //$NON-NLS-1$
									Messages.getString("CallgraphView.BufferMessage1") + //$NON-NLS-1$
							Messages.getString("CallgraphView.BufferMessage2") + //$NON-NLS-1$
							Messages.getString("CallgraphView.BufferMessage3") + //$NON-NLS-1$
							Messages.getString("CallgraphView.BufferMessage4") + g.getLevelBuffer() + //$NON-NLS-1$
							Messages.getString("CallgraphView.BufferMessage5") + PluginConstants.NEW_LINE //$NON-NLS-1$
											+ PluginConstants.NEW_LINE + Messages.getString("CallgraphView.BufferMessage6") + //$NON-NLS-1$
							Messages.getString("CallgraphView.BufferMessage7")); //$NON-NLS-1$
							mess.schedule();
						}

						redraw = true;
					}
					sh.dispose();

					if (redraw) {
						g.draw();
					}
				}));


                sh.open();            }
        };

    }

/**
 * Convenience method for creating all the various actions
 */
    private void createActions() {
        createViewActions();
        createAnimateActions();
        createMarkerActions();
        createMovementActions();

        modeCollapsedNodes.setChecked(true);

    }

    private void createMovementActions() {
        gotoNext = new Action(Messages.getString("CallgraphView.Next")) { //$NON-NLS-1$
            @Override
            public void run() {
                g.drawNextNode();
            }
        };

        gotoPrevious = new Action(Messages.getString("CallgraphView.Previous")) { //$NON-NLS-1$
            @Override
            public void run() {
                if (g.isCollapseMode()) {
                    g.setCollapseMode(false);
                }
                int toDraw = g.getPreviousCalledNode(g.getRootVisibleNodeNumber());
                if (toDraw != -1)
                    g.draw(toDraw);
            }
        };

        gotoLast = new Action(Messages.getString("CallgraphView.Last")) { //$NON-NLS-1$
            @Override
            public void run() {
                if (g.isCollapseMode())
                    g.setCollapseMode(false);
                g.draw(g.getLastFunctionCalled());
            }
        };

        play = new Action(Messages.getString("CallgraphView.Play")) { //$NON-NLS-1$
            @Override
            public void run() {
                if (g.getDrawMode() != StapGraph.CONSTANT_DRAWMODE_AGGREGATE) {
                    g.play();
                    togglePlayImage();
                }
            }
        };
        play.setImageDescriptor(playImage);
    }

    /**
     * Toggles the play/pause image
     * @param play
     */
    private void togglePlayImage() {
        if (play.getToolTipText() == Messages.getString("CallgraphView.Pause")) { //$NON-NLS-1$
            play.setImageDescriptor(playImage);
            play.setToolTipText(Messages.getString("CallgraphView.Play")); //$NON-NLS-1$
        }
        else {
            play.setImageDescriptor(pauseImage);
            play.setToolTipText(""); //$NON-NLS-1$
        }
    }

    private void createMarkerActions() {
        markersNext = new Action(Messages.getString("CallgraphView.nextMarker")) { //$NON-NLS-1$
            @Override
            public void run() {
                g.draw(g.getNextMarkedNode());
            }
        };

        markersPrevious = new Action(Messages.getString("CallgraphView.previousMarker")) { //$NON-NLS-1$
            @Override
            public void run() {
                g.draw(g.getPreviousMarkedNode());
            }
        };
    }

    @Override
    protected boolean createOpenAction() {
        //Opens from specified location
        openFile = new Action(Messages.getString("CallgraphView.Open")){ //$NON-NLS-1$
            @Override
            public void run(){
                FileDialog dialog = new FileDialog(new Shell(), SWT.DEFAULT);
                String filePath =  dialog.open();
                if (filePath != null){
                    StapGraphParser new_parser = new StapGraphParser();
                    new_parser.setSourcePath(filePath);
                        new_parser.setViewID(CallGraphConstants.VIEW_ID);
                    new_parser.schedule();
                }
            }
        };
        return true;
    }


    @Override
    protected boolean createOpenDefaultAction() {
        //Opens from the default location
        openDefault = new Action(Messages.getString("CallgraphView.OpenLastRun")){ //$NON-NLS-1$
            @Override
            public void run(){
                StapGraphParser new_parser = new StapGraphParser();
                new_parser.setViewID(CallGraphConstants.VIEW_ID);
                new_parser.schedule();
            }
        };

        return true;
    }

    @Override
    public boolean setParser(SystemTapParser newParser) {
        if (newParser instanceof StapGraphParser) {
            parser = (StapGraphParser) newParser;
            return true;
        }
        return false;

    }

    @Override
    public void setViewID() {
        viewID = "org.eclipse.linuxtools.callgraph.callgraphview";         //$NON-NLS-1$
    }

    public  Action getAnimationSlow() {
        return animationSlow;
    }

    public  Action getAnimationFast() {
        return animationFast;
    }

    public  Action getModeCollapsednodes() {
        return modeCollapsedNodes;
    }

    public  Action getViewRefresh() {
        return viewRefresh;
    }

    public  Action getGotoNext() {
        return gotoNext;
    }

    public  Action getGotoPrevious() {
        return gotoPrevious;
    }

    public  Action getGotoLast() {
        return gotoLast;
    }

    public  Action getViewTreeview() {
        return viewTreeview;
    }

    public  Action getViewRadialview() {
        return viewRadialview;
    }

    public  Action getViewAggregateview() {
        return viewAggregateview;
    }

    public  Action getViewLevelview() {
        return viewLevelview;
    }

    public Action getPlay() {
        return play;
    }

    public StapGraph getGraph() {
        return g;
    }

    @Override
    public void setFocus() {
        if(masterComposite != null){
            masterComposite.setFocus();
        }
    }


    @Override
    public void updateMethod() {
        IProgressMonitor m = new NullProgressMonitor();
        m.beginTask("Updating callgraph", 4); //$NON-NLS-1$

        loadData(m);
        m.worked(1);
        if (parser.totalTime > 0) {
            finishLoad(m);
        }
        m.worked(1);

        g.draw(StapGraph.CONSTANT_DRAWMODE_RADIAL, StapGraph.CONSTANT_ANIMATION_SLOW, g.getFirstUsefulNode());
    }

    @Override
    public SystemTapParser getParser() {
        return parser;
    }

    private void writeToDot(boolean mode, Set<Integer> keySet) {
        Shell sh = new Shell();
        FileDialog dialog = new FileDialog(sh, SWT.SAVE);

        String filePath = dialog.open();

        if (filePath != null) {
            File f = new File(filePath);
            f.delete();
            try {
                f.createNewFile();
            } catch (IOException e) {
                return;
            }

            try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
                StringBuilder build = new StringBuilder(""); //$NON-NLS-1$

                out.write("digraph stapgraph {\n"); //$NON-NLS-1$
                for (int i : keySet) {
                    if (i == 0) {
                        continue;
                    }
                    StapData d = g.getNodeData(i);
                    if ( (d.isCollapsed != mode) && !d.isOnlyChildWithThisName()) {
                        continue;
                    }
                    build.append(i + " [label=\"" + d.name + " " ); //$NON-NLS-1$ //$NON-NLS-2$
                    build.append(StapNode.numberFormat.format((float) d.getTime()/g.getTotalTime() * 100) + "%\"]\n"); //$NON-NLS-1$
                    int j = d.parent;
                    if (mode) {
                        j = d.collapsedParent;
                    }

                    if (!keySet.contains(j) || j == 0) {
                        continue;
                    }

                    String called = mode ? " [label=\"" + g.getNodeData(i).timesCalled + "\"]\n" : "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    build.append( j + "->" + i ); //$NON-NLS-1$
                    build.append( called );
                    out.write(build.toString());
                    build.setLength(0);
                }
                out.write("}"); //$NON-NLS-1$
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static ImageDescriptor getImageDescriptor(String path) {
        return AbstractUIPlugin.imageDescriptorFromPlugin(CallGraphConstants.PLUGIN_ID, path);
    }


}

Back to the top