VirtualBox

source: vbox/trunk/src/VBox/Debugger/VBoxDbgStatsQt.cpp@ 103460

Last change on this file since 103460 was 103460, checked in by vboxsync, 15 months ago

VBoxDbg: Added some simple sub-tree filtering to the statistics viewer. bugref:10376

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 125.5 KB
Line 
1/* $Id: VBoxDbgStatsQt.cpp 103460 2024-02-19 21:17:15Z vboxsync $ */
2/** @file
3 * VBox Debugger GUI - Statistics.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DBGG
33#include "VBoxDbgStatsQt.h"
34
35#include <QAction>
36#include <QApplication>
37#include <QCheckBox>
38#include <QClipboard>
39#include <QContextMenuEvent>
40#include <QDialog>
41#include <QDialogButtonBox>
42#include <QGroupBox>
43#include <QGridLayout>
44#include <QHBoxLayout>
45#include <QHeaderView>
46#include <QKeySequence>
47#include <QLabel>
48#include <QLineEdit>
49#include <QLocale>
50#include <QMessageBox>
51#include <QPushButton>
52#include <QSortFilterProxyModel>
53#include <QSpinBox>
54#include <QVBoxLayout>
55
56#include <iprt/errcore.h>
57#include <VBox/log.h>
58#include <iprt/string.h>
59#include <iprt/mem.h>
60#include <iprt/assert.h>
61
62#include "VBoxDbgGui.h"
63
64
65/*********************************************************************************************************************************
66* Defined Constants And Macros *
67*********************************************************************************************************************************/
68/** The number of column. */
69#define DBGGUI_STATS_COLUMNS 9
70
71/** Enables the sorting and filtering. */
72#define VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
73
74
75/*********************************************************************************************************************************
76* Structures and Typedefs *
77*********************************************************************************************************************************/
78/**
79 * The state of a statistics sample node.
80 *
81 * This is used for two pass refresh (1. get data, 2. update the view) and
82 * for saving the result of a diff.
83 */
84typedef enum DBGGUISTATSNODESTATE
85{
86 /** The typical invalid zeroth entry. */
87 kDbgGuiStatsNodeState_kInvalid = 0,
88 /** The node is the root node. */
89 kDbgGuiStatsNodeState_kRoot,
90 /** The node is visible. */
91 kDbgGuiStatsNodeState_kVisible,
92 /** The node should be refreshed. */
93 kDbgGuiStatsNodeState_kRefresh,
94#if 0 /// @todo not implemented
95 /** diff: The node equals. */
96 kDbgGuiStatsNodeState_kDiffEqual,
97 /** diff: The node in set 1 is less than the one in set 2. */
98 kDbgGuiStatsNodeState_kDiffSmaller,
99 /** diff: The node in set 1 is greater than the one in set 2. */
100 kDbgGuiStatsNodeState_kDiffGreater,
101 /** diff: The node is only in set 1. */
102 kDbgGuiStatsNodeState_kDiffOnlyIn1,
103 /** diff: The node is only in set 2. */
104 kDbgGuiStatsNodeState_kDiffOnlyIn2,
105#endif
106 /** The end of the valid state values. */
107 kDbgGuiStatsNodeState_kEnd
108} DBGGUISTATENODESTATE;
109
110
111/**
112 * Filtering data.
113 */
114typedef struct VBoxGuiStatsFilterData
115{
116 /** Number of instances. */
117 static uint32_t volatile s_cInstances;
118 uint64_t uMinValue;
119 uint64_t uMaxValue;
120 QRegularExpression *pRegexName;
121
122 VBoxGuiStatsFilterData()
123 : uMinValue(0)
124 , uMaxValue(UINT64_MAX)
125 , pRegexName(NULL)
126 {
127 s_cInstances += 1;
128 }
129
130 ~VBoxGuiStatsFilterData()
131 {
132 if (pRegexName)
133 {
134 delete pRegexName;
135 pRegexName = NULL;
136 }
137 s_cInstances -= 1;
138 }
139
140 bool isAllDefaults(void) const
141 {
142 return (uMinValue == 0 || uMinValue == UINT64_MAX)
143 && (uMaxValue == 0 || uMaxValue == UINT64_MAX)
144 && pRegexName == NULL;
145 }
146} VBoxGuiStatsFilterData;
147
148
149/**
150 * A tree node representing a statistic sample.
151 *
152 * The nodes carry a reference to the parent and to its position among its
153 * siblings. Both of these need updating when the grand parent or parent adds a
154 * new child. This will hopefully not be too expensive but rather pay off when
155 * we need to create a parent index.
156 */
157typedef struct DBGGUISTATSNODE
158{
159 /** Pointer to the parent. */
160 PDBGGUISTATSNODE pParent;
161 /** Array of pointers to the child nodes. */
162 PDBGGUISTATSNODE *papChildren;
163 /** The number of children. */
164 uint32_t cChildren;
165 /** Our index among the parent's children. */
166 uint32_t iSelf;
167 /** Sub-tree filtering config (typically NULL). */
168 VBoxGuiStatsFilterData *pFilter;
169 /** The unit string. (not allocated) */
170 const char *pszUnit;
171 /** The delta. */
172 int64_t i64Delta;
173 /** The name. */
174 char *pszName;
175 /** The length of the name. */
176 size_t cchName;
177 /** The description string. */
178 QString *pDescStr;
179 /** The node state. */
180 DBGGUISTATENODESTATE enmState;
181 /** The data type.
182 * For filler nodes not containing data, this will be set to STAMTYPE_INVALID. */
183 STAMTYPE enmType;
184 /** The data at last update. */
185 union
186 {
187 /** STAMTYPE_COUNTER. */
188 STAMCOUNTER Counter;
189 /** STAMTYPE_PROFILE. */
190 STAMPROFILE Profile;
191 /** STAMTYPE_PROFILE_ADV. */
192 STAMPROFILEADV ProfileAdv;
193 /** STAMTYPE_RATIO_U32. */
194 STAMRATIOU32 RatioU32;
195 /** STAMTYPE_U8 & STAMTYPE_U8_RESET. */
196 uint8_t u8;
197 /** STAMTYPE_U16 & STAMTYPE_U16_RESET. */
198 uint16_t u16;
199 /** STAMTYPE_U32 & STAMTYPE_U32_RESET. */
200 uint32_t u32;
201 /** STAMTYPE_U64 & STAMTYPE_U64_RESET. */
202 uint64_t u64;
203 /** STAMTYPE_BOOL and STAMTYPE_BOOL_RESET. */
204 bool f;
205 /** STAMTYPE_CALLBACK. */
206 QString *pStr;
207 } Data;
208} DBGGUISTATSNODE;
209
210
211/**
212 * Recursion stack.
213 */
214typedef struct DBGGUISTATSSTACK
215{
216 /** The top stack entry. */
217 int32_t iTop;
218 /** The stack array. */
219 struct DBGGUISTATSSTACKENTRY
220 {
221 /** The node. */
222 PDBGGUISTATSNODE pNode;
223 /** The current child. */
224 int32_t iChild;
225 /** Name string offset (if used). */
226 uint16_t cchName;
227 } a[32];
228} DBGGUISTATSSTACK;
229
230
231
232
233/**
234 * The item model for the statistics tree view.
235 *
236 * This manages the DBGGUISTATSNODE trees.
237 */
238class VBoxDbgStatsModel : public QAbstractItemModel
239{
240protected:
241 /** The root of the sample tree. */
242 PDBGGUISTATSNODE m_pRoot;
243
244private:
245 /** Next update child. This is UINT32_MAX when invalid. */
246 uint32_t m_iUpdateChild;
247 /** Pointer to the node m_szUpdateParent represent and m_iUpdateChild refers to. */
248 PDBGGUISTATSNODE m_pUpdateParent;
249 /** The length of the path. */
250 size_t m_cchUpdateParent;
251 /** The path to the current update parent, including a trailing slash. */
252 char m_szUpdateParent[1024];
253 /** Inserted or/and removed nodes during the update. */
254 bool m_fUpdateInsertRemove;
255
256
257public:
258 /**
259 * Constructor.
260 *
261 * @param a_pParent The parent object. See QAbstractItemModel in the Qt
262 * docs for details.
263 */
264 VBoxDbgStatsModel(QObject *a_pParent);
265
266 /**
267 * Destructor.
268 *
269 * This will free all the data the model holds.
270 */
271 virtual ~VBoxDbgStatsModel();
272
273 /**
274 * Updates the data matching the specified pattern, normally for the whole tree
275 * but optionally a sub-tree if @a a_pSubTree is given.
276 *
277 * This will should invoke updatePrep, updateCallback and updateDone.
278 *
279 * It is vitally important that updateCallback is fed the data in the right
280 * order. The code make very definite ASSUMPTIONS about the ordering being
281 * strictly sorted and taking the slash into account when doing so.
282 *
283 * @returns true if we reset the model and it's necessary to set the root index.
284 * @param a_rPatStr The selection pattern.
285 * @param a_pSubTree The node / sub-tree to update if this is partial update.
286 * This is NULL for a full tree update.
287 *
288 * @remarks The default implementation is an empty stub.
289 */
290 virtual bool updateStatsByPattern(const QString &a_rPatStr, PDBGGUISTATSNODE a_pSubTree = NULL);
291
292 /**
293 * Similar to updateStatsByPattern, except that it only works on a sub-tree and
294 * will not remove anything that's outside that tree.
295 *
296 * The default implementation will call redirect to updateStatsByPattern().
297 *
298 * @param a_rIndex The sub-tree root. Invalid index means root.
299 */
300 virtual void updateStatsByIndex(QModelIndex const &a_rIndex);
301
302 /**
303 * Reset the stats matching the specified pattern.
304 *
305 * @param a_rPatStr The selection pattern.
306 *
307 * @remarks The default implementation is an empty stub.
308 */
309 virtual void resetStatsByPattern(QString const &a_rPatStr);
310
311 /**
312 * Reset the stats of a sub-tree.
313 *
314 * @param a_rIndex The sub-tree root. Invalid index means root.
315 * @param a_fSubTree Whether to reset the sub-tree as well. Default is true.
316 *
317 * @remarks The default implementation makes use of resetStatsByPattern
318 */
319 virtual void resetStatsByIndex(QModelIndex const &a_rIndex, bool a_fSubTree = true);
320
321 /**
322 * Iterator callback function.
323 * @returns true to continue, false to stop.
324 */
325 typedef bool FNITERATOR(PDBGGUISTATSNODE pNode, QModelIndex const &a_rIndex, const char *pszFullName, void *pvUser);
326
327 /**
328 * Callback iterator.
329 *
330 * @param a_rPatStr The selection pattern.
331 * @param a_pfnCallback Callback function.
332 * @param a_pvUser Callback argument.
333 * @param a_fMatchChildren How to handle children of matching nodes:
334 * - @c true: continue with the children,
335 * - @c false: skip children.
336 */
337 virtual void iterateStatsByPattern(QString const &a_rPatStr, FNITERATOR *a_pfnCallback, void *a_pvUser,
338 bool a_fMatchChildren = true);
339
340 /**
341 * Gets the model index of the root node.
342 *
343 * @returns root index.
344 */
345 QModelIndex getRootIndex(void) const;
346
347
348protected:
349 /**
350 * Set the root node.
351 *
352 * This will free all the current data before taking the ownership of the new
353 * root node and its children.
354 *
355 * @param a_pRoot The new root node.
356 */
357 void setRootNode(PDBGGUISTATSNODE a_pRoot);
358
359 /** Creates the root node. */
360 static PDBGGUISTATSNODE createRootNode(void);
361
362 /** Creates and insert a node under the given parent. */
363 static PDBGGUISTATSNODE createAndInsertNode(PDBGGUISTATSNODE pParent, const char *pszName, size_t cchName, uint32_t iPosition);
364
365 /** Creates and insert a node under the given parent with correct Qt
366 * signalling. */
367 PDBGGUISTATSNODE createAndInsert(PDBGGUISTATSNODE pParent, const char *pszName, size_t cchName, uint32_t iPosition);
368
369 /**
370 * Resets the node to a pristine state.
371 *
372 * @param pNode The node.
373 */
374 static void resetNode(PDBGGUISTATSNODE pNode);
375
376 /**
377 * Initializes a pristine node.
378 */
379 static int initNode(PDBGGUISTATSNODE pNode, STAMTYPE enmType, void *pvSample, const char *pszUnit, const char *pszDesc);
380
381 /**
382 * Updates (or reinitializes if you like) a node.
383 */
384 static void updateNode(PDBGGUISTATSNODE pNode, STAMTYPE enmType, void *pvSample, const char *pszUnit, const char *pszDesc);
385
386 /**
387 * Called by updateStatsByPattern(), makes the necessary preparations.
388 *
389 * @returns Success indicator.
390 * @param a_pSubTree The node / sub-tree to update if this is partial update.
391 * This is NULL for a full tree update.
392 */
393 bool updatePrepare(PDBGGUISTATSNODE a_pSubTree = NULL);
394
395 /**
396 * Called by updateStatsByPattern(), finalizes the update.
397 *
398 * @returns See updateStatsByPattern().
399 *
400 * @param a_fSuccess Whether the update was successful or not.
401 * @param a_pSubTree The node / sub-tree to update if this is partial update.
402 * This is NULL for a full tree update.
403 */
404 bool updateDone(bool a_fSuccess, PDBGGUISTATSNODE a_pSubTree = NULL);
405
406 /**
407 * updateCallback() worker taking care of in-tree inserts and removals.
408 *
409 * @returns The current node.
410 * @param pszName The name of the tree element to update.
411 */
412 PDBGGUISTATSNODE updateCallbackHandleOutOfOrder(const char *pszName);
413
414 /**
415 * updateCallback() worker taking care of tail insertions.
416 *
417 * @returns The current node.
418 * @param pszName The name of the tree element to update.
419 */
420 PDBGGUISTATSNODE updateCallbackHandleTail(const char *pszName);
421
422 /**
423 * updateCallback() worker that advances the update state to the next data node
424 * in anticipation of the next updateCallback call.
425 *
426 * @param pNode The current node.
427 */
428 void updateCallbackAdvance(PDBGGUISTATSNODE pNode);
429
430 /** Callback used by updateStatsByPattern() and updateStatsByIndex() to feed
431 * changes.
432 * @copydoc FNSTAMR3ENUM */
433 static DECLCALLBACK(int) updateCallback(const char *pszName, STAMTYPE enmType, void *pvSample, STAMUNIT enmUnit,
434 const char *pszUnit, STAMVISIBILITY enmVisibility, const char *pszDesc, void *pvUser);
435
436public:
437 /**
438 * Calculates the full path of a node.
439 *
440 * @returns Number of bytes returned, negative value on buffer overflow
441 *
442 * @param pNode The node.
443 * @param psz The output buffer.
444 * @param cch The size of the buffer.
445 */
446 static ssize_t getNodePath(PCDBGGUISTATSNODE pNode, char *psz, ssize_t cch);
447
448protected:
449 /**
450 * Calculates the full path of a node, returning the string pointer.
451 *
452 * @returns @a psz. On failure, NULL.
453 *
454 * @param pNode The node.
455 * @param psz The output buffer.
456 * @param cch The size of the buffer.
457 */
458 static char *getNodePath2(PCDBGGUISTATSNODE pNode, char *psz, ssize_t cch);
459
460 /**
461 * Returns the pattern for the node, optionally including the entire sub-tree
462 * under it.
463 *
464 * @returns Pattern.
465 * @param pNode The node.
466 * @param fSubTree Whether to include the sub-tree in the pattern.
467 */
468 static QString getNodePattern(PCDBGGUISTATSNODE pNode, bool fSubTree = true);
469
470 /**
471 * Check if the first node is an ancestor to the second one.
472 *
473 * @returns true/false.
474 * @param pAncestor The first node, the alleged ancestor.
475 * @param pDescendant The second node, the alleged descendant.
476 */
477 static bool isNodeAncestorOf(PCDBGGUISTATSNODE pAncestor, PCDBGGUISTATSNODE pDescendant);
478
479 /**
480 * Advance to the next node in the tree.
481 *
482 * @returns Pointer to the next node, NULL if we've reached the end or
483 * was handed a NULL node.
484 * @param pNode The current node.
485 */
486 static PDBGGUISTATSNODE nextNode(PDBGGUISTATSNODE pNode);
487
488 /**
489 * Advance to the next node in the tree that contains data.
490 *
491 * @returns Pointer to the next data node, NULL if we've reached the end or
492 * was handed a NULL node.
493 * @param pNode The current node.
494 */
495 static PDBGGUISTATSNODE nextDataNode(PDBGGUISTATSNODE pNode);
496
497 /**
498 * Advance to the previous node in the tree.
499 *
500 * @returns Pointer to the previous node, NULL if we've reached the end or
501 * was handed a NULL node.
502 * @param pNode The current node.
503 */
504 static PDBGGUISTATSNODE prevNode(PDBGGUISTATSNODE pNode);
505
506 /**
507 * Advance to the previous node in the tree that contains data.
508 *
509 * @returns Pointer to the previous data node, NULL if we've reached the end or
510 * was handed a NULL node.
511 * @param pNode The current node.
512 */
513 static PDBGGUISTATSNODE prevDataNode(PDBGGUISTATSNODE pNode);
514
515 /**
516 * Removes a node from the tree.
517 *
518 * @returns pNode.
519 * @param pNode The node.
520 */
521 static PDBGGUISTATSNODE removeNode(PDBGGUISTATSNODE pNode);
522
523 /**
524 * Removes a node from the tree and destroys it and all its descendants.
525 *
526 * @param pNode The node.
527 */
528 static void removeAndDestroyNode(PDBGGUISTATSNODE pNode);
529
530 /** Removes a node from the tree and destroys it and all its descendants
531 * performing the required Qt signalling. */
532 void removeAndDestroy(PDBGGUISTATSNODE pNode);
533
534 /**
535 * Destroys a statistics tree.
536 *
537 * @param a_pRoot The root of the tree. NULL is fine.
538 */
539 static void destroyTree(PDBGGUISTATSNODE a_pRoot);
540
541 /**
542 * Stringifies exactly one node, no children.
543 *
544 * This is for logging and clipboard.
545 *
546 * @param a_pNode The node.
547 * @param a_rString The string to append the stringified node to.
548 */
549 static void stringifyNodeNoRecursion(PDBGGUISTATSNODE a_pNode, QString &a_rString);
550
551 /**
552 * Stringifies a node and its children.
553 *
554 * This is for logging and clipboard.
555 *
556 * @param a_pNode The node.
557 * @param a_rString The string to append the stringified node to.
558 */
559 static void stringifyNode(PDBGGUISTATSNODE a_pNode, QString &a_rString);
560
561public:
562 /**
563 * Converts the specified tree to string.
564 *
565 * This is for logging and clipboard.
566 *
567 * @param a_rRoot Where to start. Use QModelIndex() to start at the root.
568 * @param a_rString Where to return to return the string dump.
569 */
570 void stringifyTree(QModelIndex &a_rRoot, QString &a_rString) const;
571
572 /**
573 * Dumps the given (sub-)tree as XML.
574 *
575 * @param a_rRoot Where to start. Use QModelIndex() to start at the root.
576 * @param a_rString Where to return to return the XML dump.
577 */
578 void xmlifyTree(QModelIndex &a_rRoot, QString &a_rString) const;
579
580 /**
581 * Puts the stringified tree on the clipboard.
582 *
583 * @param a_rRoot Where to start. Use QModelIndex() to start at the root.
584 */
585 void copyTreeToClipboard(QModelIndex &a_rRoot) const;
586
587
588protected:
589 /** Worker for logTree. */
590 static void logNode(PDBGGUISTATSNODE a_pNode, bool a_fReleaseLog);
591
592public:
593 /** Logs a (sub-)tree.
594 *
595 * @param a_rRoot Where to start. Use QModelIndex() to start at the root.
596 * @param a_fReleaseLog Whether to use the release log (true) or the debug log (false).
597 */
598 void logTree(QModelIndex &a_rRoot, bool a_fReleaseLog) const;
599
600 /** Gets the unit. */
601 static QString strUnit(PCDBGGUISTATSNODE pNode);
602 /** Gets the value/times. */
603 static QString strValueTimes(PCDBGGUISTATSNODE pNode);
604 /** Gets the value/times. */
605 static uint64_t getValueTimesAsUInt(PCDBGGUISTATSNODE pNode);
606 /** Gets the value/avg. */
607 static uint64_t getValueOrAvgAsUInt(PCDBGGUISTATSNODE pNode);
608 /** Gets the minimum value. */
609 static QString strMinValue(PCDBGGUISTATSNODE pNode);
610 /** Gets the minimum value. */
611 static uint64_t getMinValueAsUInt(PCDBGGUISTATSNODE pNode);
612 /** Gets the average value. */
613 static QString strAvgValue(PCDBGGUISTATSNODE pNode);
614 /** Gets the average value. */
615 static uint64_t getAvgValueAsUInt(PCDBGGUISTATSNODE pNode);
616 /** Gets the maximum value. */
617 static QString strMaxValue(PCDBGGUISTATSNODE pNode);
618 /** Gets the maximum value. */
619 static uint64_t getMaxValueAsUInt(PCDBGGUISTATSNODE pNode);
620 /** Gets the total value. */
621 static QString strTotalValue(PCDBGGUISTATSNODE pNode);
622 /** Gets the total value. */
623 static uint64_t getTotalValueAsUInt(PCDBGGUISTATSNODE pNode);
624 /** Gets the delta value. */
625 static QString strDeltaValue(PCDBGGUISTATSNODE pNode);
626
627
628protected:
629 /**
630 * Destroys a node and all its children.
631 *
632 * @param a_pNode The node to destroy.
633 */
634 static void destroyNode(PDBGGUISTATSNODE a_pNode);
635
636public:
637 /**
638 * Converts an index to a node pointer.
639 *
640 * @returns Pointer to the node, NULL if invalid reference.
641 * @param a_rIndex Reference to the index
642 */
643 inline PDBGGUISTATSNODE nodeFromIndex(const QModelIndex &a_rIndex) const
644 {
645 if (RT_LIKELY(a_rIndex.isValid()))
646 return (PDBGGUISTATSNODE)a_rIndex.internalPointer();
647 return NULL;
648 }
649
650public:
651
652 /** @name Overridden QAbstractItemModel methods
653 * @{ */
654 virtual int columnCount(const QModelIndex &a_rParent) const RT_OVERRIDE;
655 virtual QVariant data(const QModelIndex &a_rIndex, int a_eRole) const RT_OVERRIDE;
656 virtual Qt::ItemFlags flags(const QModelIndex &a_rIndex) const RT_OVERRIDE;
657 virtual bool hasChildren(const QModelIndex &a_rParent) const RT_OVERRIDE;
658 virtual QVariant headerData(int a_iSection, Qt::Orientation a_ePrientation, int a_eRole) const RT_OVERRIDE;
659 virtual QModelIndex index(int a_iRow, int a_iColumn, const QModelIndex &a_rParent) const RT_OVERRIDE;
660 virtual QModelIndex parent(const QModelIndex &a_rChild) const RT_OVERRIDE;
661 virtual int rowCount(const QModelIndex &a_rParent) const RT_OVERRIDE;
662 ///virtual void sort(int a_iColumn, Qt::SortOrder a_eOrder) RT_OVERRIDE;
663 /** @} */
664};
665
666
667/**
668 * Model using the VM / STAM interface as data source.
669 */
670class VBoxDbgStatsModelVM : public VBoxDbgStatsModel, public VBoxDbgBase
671{
672public:
673 /**
674 * Constructor.
675 *
676 * @param a_pDbgGui Pointer to the debugger gui object.
677 * @param a_rPatStr The selection pattern.
678 * @param a_pParent The parent object. NULL is fine.
679 * @param a_pVMM The VMM function table.
680 */
681 VBoxDbgStatsModelVM(VBoxDbgGui *a_pDbgGui, QString &a_rPatStr, QObject *a_pParent, PCVMMR3VTABLE a_pVMM);
682
683 /** Destructor */
684 virtual ~VBoxDbgStatsModelVM();
685
686 virtual bool updateStatsByPattern(const QString &a_rPatStr, PDBGGUISTATSNODE a_pSubTree = NULL);
687 virtual void resetStatsByPattern(const QString &a_rPatStr);
688
689protected:
690 /**
691 * Enumeration callback used by createNewTree.
692 */
693 static DECLCALLBACK(int) createNewTreeCallback(const char *pszName, STAMTYPE enmType, void *pvSample, STAMUNIT enmUnit,
694 const char *pszUnit, STAMVISIBILITY enmVisibility, const char *pszDesc,
695 void *pvUser);
696
697 /**
698 * Constructs a new statistics tree by query data from the VM.
699 *
700 * @returns Pointer to the root of the tree we've constructed. This will be NULL
701 * if the STAM API throws an error or we run out of memory.
702 * @param a_rPatStr The selection pattern.
703 */
704 PDBGGUISTATSNODE createNewTree(QString &a_rPatStr);
705
706 /** The VMM function table. */
707 PCVMMR3VTABLE m_pVMM;
708};
709
710#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
711
712/**
713 * Model using the VM / STAM interface as data source.
714 */
715class VBoxDbgStatsSortFileProxyModel : public QSortFilterProxyModel
716{
717public:
718 /**
719 * Constructor.
720 *
721 * @param a_pParent The parent object.
722 */
723 VBoxDbgStatsSortFileProxyModel(QObject *a_pParent);
724
725 /** Destructor */
726 virtual ~VBoxDbgStatsSortFileProxyModel()
727 {}
728
729 /** Sets whether or not to show unused rows (all zeros). */
730 void setShowUnusedRows(bool a_fHide);
731
732 /**
733 * Notification that a filter has been added, removed or modified.
734 */
735 void notifyFilterChanges();
736
737protected:
738 /**
739 * Converts an index to a node pointer.
740 *
741 * @returns Pointer to the node, NULL if invalid reference.
742 * @param a_rIndex Reference to the index
743 */
744 inline PDBGGUISTATSNODE nodeFromIndex(const QModelIndex &a_rIndex) const
745 {
746 if (RT_LIKELY(a_rIndex.isValid()))
747 return (PDBGGUISTATSNODE)a_rIndex.internalPointer();
748 return NULL;
749 }
750
751 /** Does the row filtering. */
752 bool filterAcceptsRow(int a_iSrcRow, const QModelIndex &a_rSrcParent) const RT_OVERRIDE;
753 /** For implementing the sorting. */
754 bool lessThan(const QModelIndex &a_rSrcLeft, const QModelIndex &a_rSrcRight) const RT_OVERRIDE;
755
756 /** Whether to show unused rows (all zeros) or not. */
757 bool m_fShowUnusedRows;
758};
759
760
761/**
762 * Dialog for sub-tree filtering config.
763 */
764class VBoxDbgStatsFilterDialog : public QDialog
765{
766public:
767 /**
768 * Constructor.
769 *
770 * @param a_pNode The node to configure filtering for.
771 */
772 VBoxDbgStatsFilterDialog(QWidget *a_pParent, PCDBGGUISTATSNODE a_pNode);
773
774 /** Destructor. */
775 virtual ~VBoxDbgStatsFilterDialog();
776
777 /**
778 * Returns a copy of the filter data or NULL if all defaults.
779 */
780 VBoxGuiStatsFilterData *dupFilterData(void) const;
781
782protected slots:
783
784 /** Validates and (maybe) accepts the dialog data. */
785 void validateAndAccept(void);
786
787protected:
788 /**
789 * Validates and converts the content of an uint64_t entry field.s
790 *
791 * @returns The converted value (or default)
792 * @param a_pField The entry field widget.
793 * @param a_uDefault The default return value.
794 * @param a_pszField The field name (for error messages).
795 * @param a_pLstErrors The error list to append validation errors to.
796 */
797 static uint64_t validateUInt64Field(QLineEdit const *a_pField, uint64_t a_uDefault,
798 const char *a_pszField, QStringList *a_pLstErrors);
799
800
801private:
802 /** The filter data. */
803 VBoxGuiStatsFilterData m_Data;
804
805 /** The minium value/average entry field. */
806 QLineEdit *m_pValueAvgMin;
807 /** The maxium value/average entry field. */
808 QLineEdit *m_pValueAvgMax;
809 /** The name filtering regexp entry field. */
810 QLineEdit *m_pNameRegExp;
811
812 /** Regular expression for validating the uint64_t entry fields. */
813 static QRegularExpression const s_UInt64ValidatorRegExp;
814
815 /**
816 * Creates an entry field for a uint64_t value.
817 */
818 static QLineEdit *createUInt64LineEdit(uint64_t uValue);
819};
820
821#endif /* VBOXDBG_WITH_SORTED_AND_FILTERED_STATS */
822
823
824/*********************************************************************************************************************************
825* Global Variables *
826*********************************************************************************************************************************/
827/*static*/ uint32_t volatile VBoxGuiStatsFilterData::s_cInstances = 0;
828
829
830/*********************************************************************************************************************************
831* Internal Functions *
832*********************************************************************************************************************************/
833
834
835/**
836 * Formats a number into a 64-byte buffer.
837 */
838static char *formatNumber(char *psz, uint64_t u64)
839{
840 if (!u64)
841 {
842 psz[0] = '0';
843 psz[1] = '\0';
844 }
845 else
846 {
847 static const char s_szDigits[] = "0123456789";
848 psz += 63;
849 *psz-- = '\0';
850 unsigned cDigits = 0;
851 for (;;)
852 {
853 const unsigned iDigit = u64 % 10;
854 u64 /= 10;
855 *psz = s_szDigits[iDigit];
856 if (!u64)
857 break;
858 psz--;
859 if (!(++cDigits % 3))
860 *psz-- = ',';
861 }
862 }
863 return psz;
864}
865
866
867/**
868 * Formats a number into a 64-byte buffer.
869 * (18 446 744 073 709 551 615)
870 */
871static char *formatNumberSigned(char *psz, int64_t i64, bool fPositivePlus)
872{
873 static const char s_szDigits[] = "0123456789";
874 psz += 63;
875 *psz-- = '\0';
876 const bool fNegative = i64 < 0;
877 uint64_t u64 = fNegative ? -i64 : i64;
878 unsigned cDigits = 0;
879 for (;;)
880 {
881 const unsigned iDigit = u64 % 10;
882 u64 /= 10;
883 *psz = s_szDigits[iDigit];
884 if (!u64)
885 break;
886 psz--;
887 if (!(++cDigits % 3))
888 *psz-- = ',';
889 }
890 if (fNegative)
891 *--psz = '-';
892 else if (fPositivePlus)
893 *--psz = '+';
894 return psz;
895}
896
897
898/**
899 * Formats a unsigned hexadecimal number into a into a 64-byte buffer.
900 */
901static char *formatHexNumber(char *psz, uint64_t u64, unsigned cZeros)
902{
903 static const char s_szDigits[] = "0123456789abcdef";
904 psz += 63;
905 *psz-- = '\0';
906 unsigned cDigits = 0;
907 for (;;)
908 {
909 const unsigned iDigit = u64 % 16;
910 u64 /= 16;
911 *psz = s_szDigits[iDigit];
912 ++cDigits;
913 if (!u64 && cDigits >= cZeros)
914 break;
915 psz--;
916 if (!(cDigits % 8))
917 *psz-- = '\'';
918 }
919 return psz;
920}
921
922
923#if 0/* unused */
924/**
925 * Formats a sort key number.
926 */
927static void formatSortKey(char *psz, uint64_t u64)
928{
929 static const char s_szDigits[] = "0123456789abcdef";
930 /* signed */
931 *psz++ = '+';
932
933 /* 16 hex digits */
934 psz[16] = '\0';
935 unsigned i = 16;
936 while (i-- > 0)
937 {
938 if (u64)
939 {
940 const unsigned iDigit = u64 % 16;
941 u64 /= 16;
942 psz[i] = s_szDigits[iDigit];
943 }
944 else
945 psz[i] = '0';
946 }
947}
948#endif
949
950
951#if 0/* unused */
952/**
953 * Formats a sort key number.
954 */
955static void formatSortKeySigned(char *psz, int64_t i64)
956{
957 static const char s_szDigits[] = "0123456789abcdef";
958
959 /* signed */
960 uint64_t u64;
961 if (i64 >= 0)
962 {
963 *psz++ = '+';
964 u64 = i64;
965 }
966 else
967 {
968 *psz++ = '-';
969 u64 = -i64;
970 }
971
972 /* 16 hex digits */
973 psz[16] = '\0';
974 unsigned i = 16;
975 while (i-- > 0)
976 {
977 if (u64)
978 {
979 const unsigned iDigit = u64 % 16;
980 u64 /= 16;
981 psz[i] = s_szDigits[iDigit];
982 }
983 else
984 psz[i] = '0';
985 }
986}
987#endif
988
989
990
991/*
992 *
993 * V B o x D b g S t a t s M o d e l
994 * V B o x D b g S t a t s M o d e l
995 * V B o x D b g S t a t s M o d e l
996 *
997 *
998 */
999
1000
1001VBoxDbgStatsModel::VBoxDbgStatsModel(QObject *a_pParent)
1002 : QAbstractItemModel(a_pParent),
1003 m_pRoot(NULL), m_iUpdateChild(UINT32_MAX), m_pUpdateParent(NULL), m_cchUpdateParent(0)
1004{
1005}
1006
1007
1008
1009VBoxDbgStatsModel::~VBoxDbgStatsModel()
1010{
1011 destroyTree(m_pRoot);
1012 m_pRoot = NULL;
1013}
1014
1015
1016/*static*/ void
1017VBoxDbgStatsModel::destroyTree(PDBGGUISTATSNODE a_pRoot)
1018{
1019 if (!a_pRoot)
1020 return;
1021 Assert(!a_pRoot->pParent);
1022 Assert(!a_pRoot->iSelf);
1023
1024 destroyNode(a_pRoot);
1025}
1026
1027
1028/* static*/ void
1029VBoxDbgStatsModel::destroyNode(PDBGGUISTATSNODE a_pNode)
1030{
1031 /* destroy all our children */
1032 uint32_t i = a_pNode->cChildren;
1033 while (i-- > 0)
1034 {
1035 destroyNode(a_pNode->papChildren[i]);
1036 a_pNode->papChildren[i] = NULL;
1037 }
1038
1039 /* free the resources we're using */
1040 a_pNode->pParent = NULL;
1041
1042 RTMemFree(a_pNode->papChildren);
1043 a_pNode->papChildren = NULL;
1044
1045 if (a_pNode->enmType == STAMTYPE_CALLBACK)
1046 {
1047 delete a_pNode->Data.pStr;
1048 a_pNode->Data.pStr = NULL;
1049 }
1050
1051 a_pNode->cChildren = 0;
1052 a_pNode->iSelf = UINT32_MAX;
1053 a_pNode->pszUnit = "";
1054 a_pNode->enmType = STAMTYPE_INVALID;
1055
1056 RTMemFree(a_pNode->pszName);
1057 a_pNode->pszName = NULL;
1058
1059 if (a_pNode->pDescStr)
1060 {
1061 delete a_pNode->pDescStr;
1062 a_pNode->pDescStr = NULL;
1063 }
1064
1065 VBoxGuiStatsFilterData const *pFilter = a_pNode->pFilter;
1066 if (!pFilter)
1067 { /* likely */ }
1068 else
1069 {
1070 delete pFilter;
1071 a_pNode->pFilter = NULL;
1072 }
1073
1074#ifdef VBOX_STRICT
1075 /* poison it. */
1076 a_pNode->pParent++;
1077 a_pNode->Data.pStr++;
1078 a_pNode->pDescStr++;
1079 a_pNode->papChildren++;
1080 a_pNode->cChildren = 8442;
1081 a_pNode->pFilter++;
1082#endif
1083
1084 /* Finally ourselves */
1085 a_pNode->enmState = kDbgGuiStatsNodeState_kInvalid;
1086 RTMemFree(a_pNode);
1087}
1088
1089
1090/*static*/ PDBGGUISTATSNODE
1091VBoxDbgStatsModel::createRootNode(void)
1092{
1093 PDBGGUISTATSNODE pRoot = (PDBGGUISTATSNODE)RTMemAllocZ(sizeof(DBGGUISTATSNODE));
1094 if (!pRoot)
1095 return NULL;
1096 pRoot->iSelf = 0;
1097 pRoot->enmType = STAMTYPE_INVALID;
1098 pRoot->pszUnit = "";
1099 pRoot->pszName = (char *)RTMemDup("/", sizeof("/"));
1100 pRoot->cchName = 1;
1101 pRoot->enmState = kDbgGuiStatsNodeState_kRoot;
1102
1103 return pRoot;
1104}
1105
1106
1107/*static*/ PDBGGUISTATSNODE
1108VBoxDbgStatsModel::createAndInsertNode(PDBGGUISTATSNODE pParent, const char *pszName, size_t cchName, uint32_t iPosition)
1109{
1110 /*
1111 * Create it.
1112 */
1113 PDBGGUISTATSNODE pNode = (PDBGGUISTATSNODE)RTMemAllocZ(sizeof(DBGGUISTATSNODE));
1114 if (!pNode)
1115 return NULL;
1116 pNode->iSelf = UINT32_MAX;
1117 pNode->enmType = STAMTYPE_INVALID;
1118 pNode->pszUnit = "";
1119 pNode->pszName = (char *)RTMemDupEx(pszName, cchName, 1);
1120 pNode->cchName = cchName;
1121 pNode->enmState = kDbgGuiStatsNodeState_kVisible;
1122
1123 /*
1124 * Do we need to expand the array?
1125 */
1126 if (!(pParent->cChildren & 31))
1127 {
1128 void *pvNew = RTMemRealloc(pParent->papChildren, sizeof(*pParent->papChildren) * (pParent->cChildren + 32));
1129 if (!pvNew)
1130 {
1131 destroyNode(pNode);
1132 return NULL;
1133 }
1134 pParent->papChildren = (PDBGGUISTATSNODE *)pvNew;
1135 }
1136
1137 /*
1138 * Insert it.
1139 */
1140 pNode->pParent = pParent;
1141 if (iPosition >= pParent->cChildren)
1142 /* Last. */
1143 iPosition = pParent->cChildren;
1144 else
1145 {
1146 /* Shift all the items after ours. */
1147 uint32_t iShift = pParent->cChildren;
1148 while (iShift-- > iPosition)
1149 {
1150 PDBGGUISTATSNODE pChild = pParent->papChildren[iShift];
1151 pParent->papChildren[iShift + 1] = pChild;
1152 pChild->iSelf = iShift + 1;
1153 }
1154 }
1155
1156 /* Insert ours */
1157 pNode->iSelf = iPosition;
1158 pParent->papChildren[iPosition] = pNode;
1159 pParent->cChildren++;
1160
1161 return pNode;
1162}
1163
1164
1165PDBGGUISTATSNODE
1166VBoxDbgStatsModel::createAndInsert(PDBGGUISTATSNODE pParent, const char *pszName, size_t cchName, uint32_t iPosition)
1167{
1168 PDBGGUISTATSNODE pNode;
1169 if (m_fUpdateInsertRemove)
1170 pNode = createAndInsertNode(pParent, pszName, cchName, iPosition);
1171 else
1172 {
1173 beginInsertRows(createIndex(pParent->iSelf, 0, pParent), iPosition, iPosition);
1174 pNode = createAndInsertNode(pParent, pszName, cchName, iPosition);
1175 endInsertRows();
1176 }
1177 return pNode;
1178}
1179
1180/*static*/ PDBGGUISTATSNODE
1181VBoxDbgStatsModel::removeNode(PDBGGUISTATSNODE pNode)
1182{
1183 PDBGGUISTATSNODE pParent = pNode->pParent;
1184 if (pParent)
1185 {
1186 uint32_t iPosition = pNode->iSelf;
1187 Assert(pParent->papChildren[iPosition] == pNode);
1188 uint32_t const cChildren = --pParent->cChildren;
1189 for (; iPosition < cChildren; iPosition++)
1190 {
1191 PDBGGUISTATSNODE pChild = pParent->papChildren[iPosition + 1];
1192 pParent->papChildren[iPosition] = pChild;
1193 pChild->iSelf = iPosition;
1194 }
1195#ifdef VBOX_STRICT /* poison */
1196 pParent->papChildren[iPosition] = (PDBGGUISTATSNODE)0x42;
1197#endif
1198 }
1199 return pNode;
1200}
1201
1202
1203/*static*/ void
1204VBoxDbgStatsModel::removeAndDestroyNode(PDBGGUISTATSNODE pNode)
1205{
1206 removeNode(pNode);
1207 destroyNode(pNode);
1208}
1209
1210
1211void
1212VBoxDbgStatsModel::removeAndDestroy(PDBGGUISTATSNODE pNode)
1213{
1214 if (m_fUpdateInsertRemove)
1215 removeAndDestroyNode(pNode);
1216 else
1217 {
1218 /*
1219 * Removing is fun since the docs are imprecise as to how persistent
1220 * indexes are updated (or aren't). So, let try a few different ideas
1221 * and see which works.
1222 */
1223#if 1
1224 /* destroy the children first with the appropriate begin/endRemoveRows signals. */
1225 DBGGUISTATSSTACK Stack;
1226 Stack.a[0].pNode = pNode;
1227 Stack.a[0].iChild = -1;
1228 Stack.iTop = 0;
1229 while (Stack.iTop >= 0)
1230 {
1231 /* get top element */
1232 PDBGGUISTATSNODE pCurNode = Stack.a[Stack.iTop].pNode;
1233 uint32_t iChild = ++Stack.a[Stack.iTop].iChild;
1234 if (iChild < pCurNode->cChildren)
1235 {
1236 /* push */
1237 Stack.iTop++;
1238 Assert(Stack.iTop < (int32_t)RT_ELEMENTS(Stack.a));
1239 Stack.a[Stack.iTop].pNode = pCurNode->papChildren[iChild];
1240 Stack.a[Stack.iTop].iChild = 0;
1241 }
1242 else
1243 {
1244 /* pop and destroy all the children. */
1245 Stack.iTop--;
1246 uint32_t i = pCurNode->cChildren;
1247 if (i)
1248 {
1249 beginRemoveRows(createIndex(pCurNode->iSelf, 0, pCurNode), 0, i - 1);
1250 while (i-- > 0)
1251 destroyNode(pCurNode->papChildren[i]);
1252 pCurNode->cChildren = 0;
1253 endRemoveRows();
1254 }
1255 }
1256 }
1257 Assert(!pNode->cChildren);
1258
1259 /* finally the node it self. */
1260 PDBGGUISTATSNODE pParent = pNode->pParent;
1261 beginRemoveRows(createIndex(pParent->iSelf, 0, pParent), pNode->iSelf, pNode->iSelf);
1262 removeAndDestroyNode(pNode);
1263 endRemoveRows();
1264
1265#elif 0
1266 /* This ain't working, leaves invalid indexes behind. */
1267 PDBGGUISTATSNODE pParent = pNode->pParent;
1268 beginRemoveRows(createIndex(pParent->iSelf, 0, pParent), pNode->iSelf, pNode->iSelf);
1269 removeAndDestroyNode(pNode);
1270 endRemoveRows();
1271#else
1272 /* Force reset() of the model after the update. */
1273 m_fUpdateInsertRemove = true;
1274 removeAndDestroyNode(pNode);
1275#endif
1276 }
1277}
1278
1279
1280/*static*/ void
1281VBoxDbgStatsModel::resetNode(PDBGGUISTATSNODE pNode)
1282{
1283 /* free and reinit the data. */
1284 if (pNode->enmType == STAMTYPE_CALLBACK)
1285 {
1286 delete pNode->Data.pStr;
1287 pNode->Data.pStr = NULL;
1288 }
1289 pNode->enmType = STAMTYPE_INVALID;
1290
1291 /* free the description. */
1292 if (pNode->pDescStr)
1293 {
1294 delete pNode->pDescStr;
1295 pNode->pDescStr = NULL;
1296 }
1297}
1298
1299
1300/*static*/ int
1301VBoxDbgStatsModel::initNode(PDBGGUISTATSNODE pNode, STAMTYPE enmType, void *pvSample,
1302 const char *pszUnit, const char *pszDesc)
1303{
1304 /*
1305 * Copy the data.
1306 */
1307 pNode->pszUnit = pszUnit;
1308 Assert(pNode->enmType == STAMTYPE_INVALID);
1309 pNode->enmType = enmType;
1310 if (pszDesc)
1311 pNode->pDescStr = new QString(pszDesc); /* ignore allocation failure (well, at least up to the point we can ignore it) */
1312
1313 switch (enmType)
1314 {
1315 case STAMTYPE_COUNTER:
1316 pNode->Data.Counter = *(PSTAMCOUNTER)pvSample;
1317 break;
1318
1319 case STAMTYPE_PROFILE:
1320 case STAMTYPE_PROFILE_ADV:
1321 pNode->Data.Profile = *(PSTAMPROFILE)pvSample;
1322 break;
1323
1324 case STAMTYPE_RATIO_U32:
1325 case STAMTYPE_RATIO_U32_RESET:
1326 pNode->Data.RatioU32 = *(PSTAMRATIOU32)pvSample;
1327 break;
1328
1329 case STAMTYPE_CALLBACK:
1330 {
1331 const char *pszString = (const char *)pvSample;
1332 pNode->Data.pStr = new QString(pszString);
1333 break;
1334 }
1335
1336 case STAMTYPE_U8:
1337 case STAMTYPE_U8_RESET:
1338 case STAMTYPE_X8:
1339 case STAMTYPE_X8_RESET:
1340 pNode->Data.u8 = *(uint8_t *)pvSample;
1341 break;
1342
1343 case STAMTYPE_U16:
1344 case STAMTYPE_U16_RESET:
1345 case STAMTYPE_X16:
1346 case STAMTYPE_X16_RESET:
1347 pNode->Data.u16 = *(uint16_t *)pvSample;
1348 break;
1349
1350 case STAMTYPE_U32:
1351 case STAMTYPE_U32_RESET:
1352 case STAMTYPE_X32:
1353 case STAMTYPE_X32_RESET:
1354 pNode->Data.u32 = *(uint32_t *)pvSample;
1355 break;
1356
1357 case STAMTYPE_U64:
1358 case STAMTYPE_U64_RESET:
1359 case STAMTYPE_X64:
1360 case STAMTYPE_X64_RESET:
1361 pNode->Data.u64 = *(uint64_t *)pvSample;
1362 break;
1363
1364 case STAMTYPE_BOOL:
1365 case STAMTYPE_BOOL_RESET:
1366 pNode->Data.f = *(bool *)pvSample;
1367 break;
1368
1369 default:
1370 AssertMsgFailed(("%d\n", enmType));
1371 break;
1372 }
1373
1374 return VINF_SUCCESS;
1375}
1376
1377
1378
1379
1380/*static*/ void
1381VBoxDbgStatsModel::updateNode(PDBGGUISTATSNODE pNode, STAMTYPE enmType, void *pvSample, const char *pszUnit, const char *pszDesc)
1382{
1383 /*
1384 * Reset and init the node if the type changed.
1385 */
1386 if (enmType != pNode->enmType)
1387 {
1388 if (pNode->enmType != STAMTYPE_INVALID)
1389 resetNode(pNode);
1390 initNode(pNode, enmType, pvSample, pszUnit, pszDesc);
1391 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1392 }
1393 else
1394 {
1395 /*
1396 * ASSUME that only the sample value will change and that the unit, visibility
1397 * and description remains the same.
1398 */
1399
1400 int64_t iDelta;
1401 switch (enmType)
1402 {
1403 case STAMTYPE_COUNTER:
1404 {
1405 uint64_t cPrev = pNode->Data.Counter.c;
1406 pNode->Data.Counter = *(PSTAMCOUNTER)pvSample;
1407 iDelta = pNode->Data.Counter.c - cPrev;
1408 if (iDelta || pNode->i64Delta)
1409 {
1410 pNode->i64Delta = iDelta;
1411 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1412 }
1413 break;
1414 }
1415
1416 case STAMTYPE_PROFILE:
1417 case STAMTYPE_PROFILE_ADV:
1418 {
1419 uint64_t cPrevPeriods = pNode->Data.Profile.cPeriods;
1420 pNode->Data.Profile = *(PSTAMPROFILE)pvSample;
1421 iDelta = pNode->Data.Profile.cPeriods - cPrevPeriods;
1422 if (iDelta || pNode->i64Delta)
1423 {
1424 pNode->i64Delta = iDelta;
1425 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1426 }
1427 break;
1428 }
1429
1430 case STAMTYPE_RATIO_U32:
1431 case STAMTYPE_RATIO_U32_RESET:
1432 {
1433 STAMRATIOU32 Prev = pNode->Data.RatioU32;
1434 pNode->Data.RatioU32 = *(PSTAMRATIOU32)pvSample;
1435 int32_t iDeltaA = pNode->Data.RatioU32.u32A - Prev.u32A;
1436 int32_t iDeltaB = pNode->Data.RatioU32.u32B - Prev.u32B;
1437 if (iDeltaA == 0 && iDeltaB == 0)
1438 {
1439 if (pNode->i64Delta)
1440 {
1441 pNode->i64Delta = 0;
1442 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1443 }
1444 }
1445 else
1446 {
1447 if (iDeltaA >= 0)
1448 pNode->i64Delta = iDeltaA + (iDeltaB >= 0 ? iDeltaB : -iDeltaB);
1449 else
1450 pNode->i64Delta = iDeltaA + (iDeltaB < 0 ? iDeltaB : -iDeltaB);
1451 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1452 }
1453 break;
1454 }
1455
1456 case STAMTYPE_CALLBACK:
1457 {
1458 const char *pszString = (const char *)pvSample;
1459 if (!pNode->Data.pStr)
1460 {
1461 pNode->Data.pStr = new QString(pszString);
1462 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1463 }
1464 else if (*pNode->Data.pStr == pszString)
1465 {
1466 delete pNode->Data.pStr;
1467 pNode->Data.pStr = new QString(pszString);
1468 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1469 }
1470 break;
1471 }
1472
1473 case STAMTYPE_U8:
1474 case STAMTYPE_U8_RESET:
1475 case STAMTYPE_X8:
1476 case STAMTYPE_X8_RESET:
1477 {
1478 uint8_t uPrev = pNode->Data.u8;
1479 pNode->Data.u8 = *(uint8_t *)pvSample;
1480 iDelta = (int32_t)pNode->Data.u8 - (int32_t)uPrev;
1481 if (iDelta || pNode->i64Delta)
1482 {
1483 pNode->i64Delta = iDelta;
1484 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1485 }
1486 break;
1487 }
1488
1489 case STAMTYPE_U16:
1490 case STAMTYPE_U16_RESET:
1491 case STAMTYPE_X16:
1492 case STAMTYPE_X16_RESET:
1493 {
1494 uint16_t uPrev = pNode->Data.u16;
1495 pNode->Data.u16 = *(uint16_t *)pvSample;
1496 iDelta = (int32_t)pNode->Data.u16 - (int32_t)uPrev;
1497 if (iDelta || pNode->i64Delta)
1498 {
1499 pNode->i64Delta = iDelta;
1500 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1501 }
1502 break;
1503 }
1504
1505 case STAMTYPE_U32:
1506 case STAMTYPE_U32_RESET:
1507 case STAMTYPE_X32:
1508 case STAMTYPE_X32_RESET:
1509 {
1510 uint32_t uPrev = pNode->Data.u32;
1511 pNode->Data.u32 = *(uint32_t *)pvSample;
1512 iDelta = (int64_t)pNode->Data.u32 - (int64_t)uPrev;
1513 if (iDelta || pNode->i64Delta)
1514 {
1515 pNode->i64Delta = iDelta;
1516 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1517 }
1518 break;
1519 }
1520
1521 case STAMTYPE_U64:
1522 case STAMTYPE_U64_RESET:
1523 case STAMTYPE_X64:
1524 case STAMTYPE_X64_RESET:
1525 {
1526 uint64_t uPrev = pNode->Data.u64;
1527 pNode->Data.u64 = *(uint64_t *)pvSample;
1528 iDelta = pNode->Data.u64 - uPrev;
1529 if (iDelta || pNode->i64Delta)
1530 {
1531 pNode->i64Delta = iDelta;
1532 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1533 }
1534 break;
1535 }
1536
1537 case STAMTYPE_BOOL:
1538 case STAMTYPE_BOOL_RESET:
1539 {
1540 bool fPrev = pNode->Data.f;
1541 pNode->Data.f = *(bool *)pvSample;
1542 iDelta = pNode->Data.f - fPrev;
1543 if (iDelta || pNode->i64Delta)
1544 {
1545 pNode->i64Delta = iDelta;
1546 pNode->enmState = kDbgGuiStatsNodeState_kRefresh;
1547 }
1548 break;
1549 }
1550
1551 default:
1552 AssertMsgFailed(("%d\n", enmType));
1553 break;
1554 }
1555 }
1556}
1557
1558
1559/*static*/ ssize_t
1560VBoxDbgStatsModel::getNodePath(PCDBGGUISTATSNODE pNode, char *psz, ssize_t cch)
1561{
1562 ssize_t off;
1563 if (!pNode->pParent)
1564 {
1565 /* root - don't add it's slash! */
1566 AssertReturn(cch >= 1, -1);
1567 off = 0;
1568 *psz = '\0';
1569 }
1570 else
1571 {
1572 cch -= pNode->cchName + 1;
1573 AssertReturn(cch > 0, -1);
1574 off = getNodePath(pNode->pParent, psz, cch);
1575 if (off >= 0)
1576 {
1577 psz[off++] = '/';
1578 memcpy(&psz[off], pNode->pszName, pNode->cchName + 1);
1579 off += pNode->cchName;
1580 }
1581 }
1582 return off;
1583}
1584
1585
1586/*static*/ char *
1587VBoxDbgStatsModel::getNodePath2(PCDBGGUISTATSNODE pNode, char *psz, ssize_t cch)
1588{
1589 if (VBoxDbgStatsModel::getNodePath(pNode, psz, cch) < 0)
1590 return NULL;
1591 return psz;
1592}
1593
1594
1595/*static*/ QString
1596VBoxDbgStatsModel::getNodePattern(PCDBGGUISTATSNODE pNode, bool fSubTree /*= true*/)
1597{
1598 /* the node pattern. */
1599 char szPat[1024+1024+4];
1600 ssize_t cch = getNodePath(pNode, szPat, 1024);
1601 AssertReturn(cch >= 0, QString("//////////////////////////////////////////////////////"));
1602
1603 /* the sub-tree pattern. */
1604 if (fSubTree && pNode->cChildren)
1605 {
1606 char *psz = &szPat[cch];
1607 *psz++ = '|';
1608 memcpy(psz, szPat, cch);
1609 psz += cch;
1610 *psz++ = '/';
1611 *psz++ = '*';
1612 *psz++ = '\0';
1613 }
1614 return szPat;
1615}
1616
1617
1618/*static*/ bool
1619VBoxDbgStatsModel::isNodeAncestorOf(PCDBGGUISTATSNODE pAncestor, PCDBGGUISTATSNODE pDescendant)
1620{
1621 while (pDescendant)
1622 {
1623 pDescendant = pDescendant->pParent;
1624 if (pDescendant == pAncestor)
1625 return true;
1626 }
1627 return false;
1628}
1629
1630
1631/*static*/ PDBGGUISTATSNODE
1632VBoxDbgStatsModel::nextNode(PDBGGUISTATSNODE pNode)
1633{
1634 if (!pNode)
1635 return NULL;
1636
1637 /* descend to children. */
1638 if (pNode->cChildren)
1639 return pNode->papChildren[0];
1640
1641 PDBGGUISTATSNODE pParent = pNode->pParent;
1642 if (!pParent)
1643 return NULL;
1644
1645 /* next sibling. */
1646 if (pNode->iSelf + 1 < pNode->pParent->cChildren)
1647 return pParent->papChildren[pNode->iSelf + 1];
1648
1649 /* ascend and advanced to a parent's sibiling. */
1650 for (;;)
1651 {
1652 uint32_t iSelf = pParent->iSelf;
1653 pParent = pParent->pParent;
1654 if (!pParent)
1655 return NULL;
1656 if (iSelf + 1 < pParent->cChildren)
1657 return pParent->papChildren[iSelf + 1];
1658 }
1659}
1660
1661
1662/*static*/ PDBGGUISTATSNODE
1663VBoxDbgStatsModel::nextDataNode(PDBGGUISTATSNODE pNode)
1664{
1665 do
1666 pNode = nextNode(pNode);
1667 while ( pNode
1668 && pNode->enmType == STAMTYPE_INVALID);
1669 return pNode;
1670}
1671
1672
1673/*static*/ PDBGGUISTATSNODE
1674VBoxDbgStatsModel::prevNode(PDBGGUISTATSNODE pNode)
1675{
1676 if (!pNode)
1677 return NULL;
1678 PDBGGUISTATSNODE pParent = pNode->pParent;
1679 if (!pParent)
1680 return NULL;
1681
1682 /* previous sibling's latest descendant (better expression anyone?). */
1683 if (pNode->iSelf > 0)
1684 {
1685 pNode = pParent->papChildren[pNode->iSelf - 1];
1686 while (pNode->cChildren)
1687 pNode = pNode->papChildren[pNode->cChildren - 1];
1688 return pNode;
1689 }
1690
1691 /* ascend to the parent. */
1692 return pParent;
1693}
1694
1695
1696/*static*/ PDBGGUISTATSNODE
1697VBoxDbgStatsModel::prevDataNode(PDBGGUISTATSNODE pNode)
1698{
1699 do
1700 pNode = prevNode(pNode);
1701 while ( pNode
1702 && pNode->enmType == STAMTYPE_INVALID);
1703 return pNode;
1704}
1705
1706
1707#if 0
1708/*static*/ PDBGGUISTATSNODE
1709VBoxDbgStatsModel::createNewTree(IMachineDebugger *a_pIMachineDebugger)
1710{
1711 /** @todo */
1712 return NULL;
1713}
1714#endif
1715
1716
1717#if 0
1718/*static*/ PDBGGUISTATSNODE
1719VBoxDbgStatsModel::createNewTree(const char *pszFilename)
1720{
1721 /** @todo */
1722 return NULL;
1723}
1724#endif
1725
1726
1727#if 0
1728/*static*/ PDBGGUISTATSNODE
1729VBoxDbgStatsModel::createDiffTree(PDBGGUISTATSNODE pTree1, PDBGGUISTATSNODE pTree2)
1730{
1731 /** @todo */
1732 return NULL;
1733}
1734#endif
1735
1736
1737PDBGGUISTATSNODE
1738VBoxDbgStatsModel::updateCallbackHandleOutOfOrder(const char *pszName)
1739{
1740#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
1741 char szStrict[1024];
1742#endif
1743
1744 /*
1745 * We might be inserting a new node between pPrev and pNode
1746 * or we might be removing one or more nodes. Either case is
1747 * handled in the same rough way.
1748 *
1749 * Might consider optimizing insertion at some later point since this
1750 * is a normal occurrence (dynamic statistics in PATM, IOM, MM, ++).
1751 */
1752 Assert(pszName[0] == '/');
1753 Assert(m_szUpdateParent[m_cchUpdateParent - 1] == '/');
1754
1755 /*
1756 * Start with the current parent node and look for a common ancestor
1757 * hoping that this is faster than going from the root (saves lookup).
1758 */
1759 PDBGGUISTATSNODE pNode = m_pUpdateParent->papChildren[m_iUpdateChild];
1760 PDBGGUISTATSNODE const pPrev = prevDataNode(pNode);
1761 AssertMsg(strcmp(pszName, getNodePath2(pNode, szStrict, sizeof(szStrict))), ("%s\n", szStrict));
1762 AssertMsg(!pPrev || strcmp(pszName, getNodePath2(pPrev, szStrict, sizeof(szStrict))), ("%s\n", szStrict));
1763 Log(("updateCallbackHandleOutOfOrder: pszName='%s' m_szUpdateParent='%s' m_cchUpdateParent=%u pNode='%s'\n",
1764 pszName, m_szUpdateParent, m_cchUpdateParent, getNodePath2(pNode, szStrict, sizeof(szStrict))));
1765
1766 pNode = pNode->pParent;
1767 while (pNode != m_pRoot)
1768 {
1769 if (!strncmp(pszName, m_szUpdateParent, m_cchUpdateParent))
1770 break;
1771 Assert(m_cchUpdateParent > pNode->cchName);
1772 m_cchUpdateParent -= pNode->cchName + 1;
1773 m_szUpdateParent[m_cchUpdateParent] = '\0';
1774 Log2(("updateCallbackHandleOutOfOrder: m_szUpdateParent='%s' m_cchUpdateParent=%u, removed '/%s' (%u)\n", m_szUpdateParent, m_cchUpdateParent, pNode->pszName, __LINE__));
1775 pNode = pNode->pParent;
1776 }
1777 Assert(m_szUpdateParent[m_cchUpdateParent - 1] == '/');
1778
1779 /*
1780 * Descend until we've found/created the node pszName indicates,
1781 * modifying m_szUpdateParent as we go along.
1782 */
1783 while (pszName[m_cchUpdateParent - 1] == '/')
1784 {
1785 /* Find the end of this component. */
1786 const char * const pszSubName = &pszName[m_cchUpdateParent];
1787 const char *pszEnd = strchr(pszSubName, '/');
1788 if (!pszEnd)
1789 pszEnd = strchr(pszSubName, '\0');
1790 size_t cchSubName = pszEnd - pszSubName;
1791
1792 /* Add the name to the path. */
1793 memcpy(&m_szUpdateParent[m_cchUpdateParent], pszSubName, cchSubName);
1794 m_cchUpdateParent += cchSubName;
1795 m_szUpdateParent[m_cchUpdateParent++] = '/';
1796 m_szUpdateParent[m_cchUpdateParent] = '\0';
1797 Assert(m_cchUpdateParent < sizeof(m_szUpdateParent));
1798 Log2(("updateCallbackHandleOutOfOrder: m_szUpdateParent='%s' m_cchUpdateParent=%u (%u)\n", m_szUpdateParent, m_cchUpdateParent, __LINE__));
1799
1800 if (!pNode->cChildren)
1801 {
1802 /* first child */
1803 pNode = createAndInsert(pNode, pszSubName, cchSubName, 0);
1804 AssertReturn(pNode, NULL);
1805 }
1806 else
1807 {
1808 /* binary search. */
1809 int32_t iStart = 0;
1810 int32_t iLast = pNode->cChildren - 1;
1811 for (;;)
1812 {
1813 int32_t i = iStart + (iLast + 1 - iStart) / 2;
1814 int iDiff;
1815 size_t const cchCompare = RT_MIN(pNode->papChildren[i]->cchName, cchSubName);
1816 iDiff = memcmp(pszSubName, pNode->papChildren[i]->pszName, cchCompare);
1817 if (!iDiff)
1818 {
1819 iDiff = cchSubName == cchCompare ? 0 : cchSubName > cchCompare ? 1 : -1;
1820 /* For cases when exisiting node name is same as new node name with additional characters. */
1821 if (!iDiff)
1822 iDiff = cchSubName == pNode->papChildren[i]->cchName ? 0 : cchSubName > pNode->papChildren[i]->cchName ? 1 : -1;
1823 }
1824 if (iDiff > 0)
1825 {
1826 iStart = i + 1;
1827 if (iStart > iLast)
1828 {
1829 pNode = createAndInsert(pNode, pszSubName, cchSubName, iStart);
1830 AssertReturn(pNode, NULL);
1831 break;
1832 }
1833 }
1834 else if (iDiff < 0)
1835 {
1836 iLast = i - 1;
1837 if (iLast < iStart)
1838 {
1839 pNode = createAndInsert(pNode, pszSubName, cchSubName, i);
1840 AssertReturn(pNode, NULL);
1841 break;
1842 }
1843 }
1844 else
1845 {
1846 pNode = pNode->papChildren[i];
1847 break;
1848 }
1849 }
1850 }
1851 }
1852 Assert( !memcmp(pszName, m_szUpdateParent, m_cchUpdateParent - 2)
1853 && pszName[m_cchUpdateParent - 1] == '\0');
1854
1855 /*
1856 * Remove all the nodes between pNode and pPrev but keep all
1857 * of pNode's ancestors (or it'll get orphaned).
1858 */
1859 PDBGGUISTATSNODE pCur = prevNode(pNode);
1860 while (pCur != pPrev)
1861 {
1862 PDBGGUISTATSNODE pAdv = prevNode(pCur); Assert(pAdv || !pPrev);
1863 if (!isNodeAncestorOf(pCur, pNode))
1864 {
1865 Assert(pCur != m_pRoot);
1866 removeAndDestroy(pCur);
1867 }
1868 pCur = pAdv;
1869 }
1870
1871 /*
1872 * Remove the data from all ancestors of pNode that it doesn't
1873 * share them pPrev.
1874 */
1875 if (pPrev)
1876 {
1877 pCur = pNode->pParent;
1878 while (!isNodeAncestorOf(pCur, pPrev))
1879 {
1880 resetNode(pNode);
1881 pCur = pCur->pParent;
1882 }
1883 }
1884
1885 /*
1886 * Finally, adjust the globals (szUpdateParent is one level too deep).
1887 */
1888 Assert(m_cchUpdateParent > pNode->cchName + 1);
1889 m_cchUpdateParent -= pNode->cchName + 1;
1890 m_szUpdateParent[m_cchUpdateParent] = '\0';
1891 m_pUpdateParent = pNode->pParent;
1892 m_iUpdateChild = pNode->iSelf;
1893 Log2(("updateCallbackHandleOutOfOrder: m_szUpdateParent='%s' m_cchUpdateParent=%u (%u)\n", m_szUpdateParent, m_cchUpdateParent, __LINE__));
1894
1895 return pNode;
1896}
1897
1898
1899PDBGGUISTATSNODE
1900VBoxDbgStatsModel::updateCallbackHandleTail(const char *pszName)
1901{
1902 /*
1903 * Insert it at the end of the tree.
1904 *
1905 * Do the same as we're doing down in createNewTreeCallback, walk from the
1906 * root and create whatever we need.
1907 */
1908 AssertReturn(*pszName == '/' && pszName[1] != '/', NULL);
1909 PDBGGUISTATSNODE pNode = m_pRoot;
1910 const char *pszCur = pszName + 1;
1911 while (*pszCur)
1912 {
1913 /* Find the end of this component. */
1914 const char *pszNext = strchr(pszCur, '/');
1915 if (!pszNext)
1916 pszNext = strchr(pszCur, '\0');
1917 size_t cchCur = pszNext - pszCur;
1918
1919 /* Create it if it doesn't exist (it will be last if it exists). */
1920 if ( !pNode->cChildren
1921 || strncmp(pNode->papChildren[pNode->cChildren - 1]->pszName, pszCur, cchCur)
1922 || pNode->papChildren[pNode->cChildren - 1]->pszName[cchCur])
1923 {
1924 pNode = createAndInsert(pNode, pszCur, pszNext - pszCur, pNode->cChildren);
1925 AssertReturn(pNode, NULL);
1926 }
1927 else
1928 pNode = pNode->papChildren[pNode->cChildren - 1];
1929
1930 /* Advance */
1931 pszCur = *pszNext ? pszNext + 1 : pszNext;
1932 }
1933
1934 return pNode;
1935}
1936
1937
1938void
1939VBoxDbgStatsModel::updateCallbackAdvance(PDBGGUISTATSNODE pNode)
1940{
1941 /*
1942 * Advance to the next node with data.
1943 *
1944 * ASSUMES a leaf *must* have data and again we're ASSUMING the sorting
1945 * on slash separated sub-strings.
1946 */
1947 if (m_iUpdateChild != UINT32_MAX)
1948 {
1949#ifdef VBOX_STRICT
1950 PDBGGUISTATSNODE const pCorrectNext = nextDataNode(pNode);
1951#endif
1952 PDBGGUISTATSNODE pParent = pNode->pParent;
1953 if (pNode->cChildren)
1954 {
1955 /* descend to the first child. */
1956 Assert(m_cchUpdateParent + pNode->cchName + 2 < sizeof(m_szUpdateParent));
1957 memcpy(&m_szUpdateParent[m_cchUpdateParent], pNode->pszName, pNode->cchName);
1958 m_cchUpdateParent += pNode->cchName;
1959 m_szUpdateParent[m_cchUpdateParent++] = '/';
1960 m_szUpdateParent[m_cchUpdateParent] = '\0';
1961
1962 pNode = pNode->papChildren[0];
1963 }
1964 else if (pNode->iSelf + 1 < pParent->cChildren)
1965 {
1966 /* next sibling or one if its descendants. */
1967 Assert(m_pUpdateParent == pParent);
1968 pNode = pParent->papChildren[pNode->iSelf + 1];
1969 }
1970 else
1971 {
1972 /* move up and down- / on-wards */
1973 for (;;)
1974 {
1975 /* ascend */
1976 pNode = pParent;
1977 pParent = pParent->pParent;
1978 if (!pParent)
1979 {
1980 Assert(pNode == m_pRoot);
1981 m_iUpdateChild = UINT32_MAX;
1982 m_szUpdateParent[0] = '\0';
1983 m_cchUpdateParent = 0;
1984 m_pUpdateParent = NULL;
1985 break;
1986 }
1987 Assert(m_cchUpdateParent > pNode->cchName + 1);
1988 m_cchUpdateParent -= pNode->cchName + 1;
1989
1990 /* try advance */
1991 if (pNode->iSelf + 1 < pParent->cChildren)
1992 {
1993 pNode = pParent->papChildren[pNode->iSelf + 1];
1994 m_szUpdateParent[m_cchUpdateParent] = '\0';
1995 break;
1996 }
1997 }
1998 }
1999
2000 /* descend to a node containing data and finalize the globals. (ASSUMES leaf has data.) */
2001 if (m_iUpdateChild != UINT32_MAX)
2002 {
2003 while ( pNode->enmType == STAMTYPE_INVALID
2004 && pNode->cChildren > 0)
2005 {
2006 Assert(pNode->enmState == kDbgGuiStatsNodeState_kVisible);
2007
2008 Assert(m_cchUpdateParent + pNode->cchName + 2 < sizeof(m_szUpdateParent));
2009 memcpy(&m_szUpdateParent[m_cchUpdateParent], pNode->pszName, pNode->cchName);
2010 m_cchUpdateParent += pNode->cchName;
2011 m_szUpdateParent[m_cchUpdateParent++] = '/';
2012 m_szUpdateParent[m_cchUpdateParent] = '\0';
2013
2014 pNode = pNode->papChildren[0];
2015 }
2016 Assert(pNode->enmType != STAMTYPE_INVALID);
2017 m_iUpdateChild = pNode->iSelf;
2018 m_pUpdateParent = pNode->pParent;
2019 Assert(pNode == pCorrectNext);
2020 }
2021 }
2022 /* else: we're at the end */
2023}
2024
2025
2026/*static*/ DECLCALLBACK(int)
2027VBoxDbgStatsModel::updateCallback(const char *pszName, STAMTYPE enmType, void *pvSample, STAMUNIT enmUnit,
2028 const char *pszUnit, STAMVISIBILITY enmVisibility, const char *pszDesc, void *pvUser)
2029{
2030 VBoxDbgStatsModelVM *pThis = (VBoxDbgStatsModelVM *)pvUser;
2031 Log3(("updateCallback: %s\n", pszName));
2032 RT_NOREF(enmUnit);
2033
2034 /*
2035 * Skip the ones which shouldn't be visible in the GUI.
2036 */
2037 if (enmVisibility == STAMVISIBILITY_NOT_GUI)
2038 return 0;
2039
2040 /*
2041 * The default assumption is that nothing has changed.
2042 * For now we'll reset the model when ever something changes.
2043 */
2044 PDBGGUISTATSNODE pNode;
2045 if (pThis->m_iUpdateChild != UINT32_MAX)
2046 {
2047 pNode = pThis->m_pUpdateParent->papChildren[pThis->m_iUpdateChild];
2048 if ( !strncmp(pszName, pThis->m_szUpdateParent, pThis->m_cchUpdateParent)
2049 && !strcmp(pszName + pThis->m_cchUpdateParent, pNode->pszName))
2050 /* got it! */;
2051 else
2052 {
2053 /* insert/remove */
2054 pNode = pThis->updateCallbackHandleOutOfOrder(pszName);
2055 if (!pNode)
2056 return VERR_NO_MEMORY;
2057 }
2058 }
2059 else
2060 {
2061 /* append */
2062 pNode = pThis->updateCallbackHandleTail(pszName);
2063 if (!pNode)
2064 return VERR_NO_MEMORY;
2065 }
2066
2067 /*
2068 * Perform the update and advance to the next one.
2069 */
2070 updateNode(pNode, enmType, pvSample, pszUnit, pszDesc);
2071 pThis->updateCallbackAdvance(pNode);
2072
2073 return VINF_SUCCESS;
2074}
2075
2076
2077bool
2078VBoxDbgStatsModel::updatePrepare(PDBGGUISTATSNODE a_pSubTree /*= NULL*/)
2079{
2080 /*
2081 * Find the first child with data and set it up as the 'next'
2082 * node to be updated.
2083 */
2084 PDBGGUISTATSNODE pFirst;
2085 Assert(m_pRoot);
2086 Assert(m_pRoot->enmType == STAMTYPE_INVALID);
2087 if (!a_pSubTree)
2088 pFirst = nextDataNode(m_pRoot);
2089 else
2090 pFirst = a_pSubTree->enmType != STAMTYPE_INVALID ? a_pSubTree : nextDataNode(a_pSubTree);
2091 if (pFirst)
2092 {
2093 m_iUpdateChild = pFirst->iSelf;
2094 m_pUpdateParent = pFirst->pParent; Assert(m_pUpdateParent);
2095 m_cchUpdateParent = getNodePath(m_pUpdateParent, m_szUpdateParent, sizeof(m_szUpdateParent) - 1);
2096 AssertReturn(m_cchUpdateParent >= 1, false);
2097 m_szUpdateParent[m_cchUpdateParent++] = '/';
2098 m_szUpdateParent[m_cchUpdateParent] = '\0';
2099 }
2100 else
2101 {
2102 m_iUpdateChild = UINT32_MAX;
2103 m_pUpdateParent = NULL;
2104 m_szUpdateParent[0] = '\0';
2105 m_cchUpdateParent = 0;
2106 }
2107
2108 /*
2109 * Set the flag and signal possible layout change.
2110 */
2111 m_fUpdateInsertRemove = false;
2112 /* emit layoutAboutToBeChanged(); - debug this, it gets stuck... */
2113 return true;
2114}
2115
2116
2117bool
2118VBoxDbgStatsModel::updateDone(bool a_fSuccess, PDBGGUISTATSNODE a_pSubTree /*= NULL*/)
2119{
2120 /*
2121 * Remove any nodes following the last in the update (unless the update failed).
2122 */
2123 if ( a_fSuccess
2124 && m_iUpdateChild != UINT32_MAX
2125 && a_pSubTree == NULL)
2126 {
2127 PDBGGUISTATSNODE const pLast = prevDataNode(m_pUpdateParent->papChildren[m_iUpdateChild]);
2128 if (!pLast)
2129 {
2130 /* nuking the whole tree. */
2131 setRootNode(createRootNode());
2132 m_fUpdateInsertRemove = true;
2133 }
2134 else
2135 {
2136 PDBGGUISTATSNODE pNode;
2137 while ((pNode = nextNode(pLast)))
2138 {
2139 Assert(pNode != m_pRoot);
2140 removeAndDestroy(pNode);
2141 }
2142 }
2143 }
2144
2145 /*
2146 * We're done making layout changes (if I understood it correctly), so,
2147 * signal this and then see what to do next. If we did too many removals
2148 * we'll just reset the whole shebang.
2149 */
2150 if (m_fUpdateInsertRemove)
2151 {
2152#if 0 /* hrmpf, layoutChanged() didn't work reliably at some point so doing this as well... */
2153 beginResetModel();
2154 endResetModel();
2155#else
2156 emit layoutChanged();
2157#endif
2158 }
2159 else
2160 {
2161 /*
2162 * Send dataChanged events.
2163 *
2164 * We do this here instead of from the updateCallback because it reduces
2165 * the clutter in that method and allow us to emit bulk signals in an
2166 * easier way because we can traverse the tree in a different fashion.
2167 */
2168 DBGGUISTATSSTACK Stack;
2169 Stack.a[0].pNode = !a_pSubTree ? m_pRoot : a_pSubTree;
2170 Stack.a[0].iChild = -1;
2171 Stack.iTop = 0;
2172
2173 while (Stack.iTop >= 0)
2174 {
2175 /* get top element */
2176 PDBGGUISTATSNODE pNode = Stack.a[Stack.iTop].pNode;
2177 uint32_t iChild = ++Stack.a[Stack.iTop].iChild;
2178 if (iChild < pNode->cChildren)
2179 {
2180 /* push */
2181 Stack.iTop++;
2182 Assert(Stack.iTop < (int32_t)RT_ELEMENTS(Stack.a));
2183 Stack.a[Stack.iTop].pNode = pNode->papChildren[iChild];
2184 Stack.a[Stack.iTop].iChild = -1;
2185 }
2186 else
2187 {
2188 /* pop */
2189 Stack.iTop--;
2190
2191 /* do the actual work. */
2192 iChild = 0;
2193 while (iChild < pNode->cChildren)
2194 {
2195 /* skip to the first needing updating. */
2196 while ( iChild < pNode->cChildren
2197 && pNode->papChildren[iChild]->enmState != kDbgGuiStatsNodeState_kRefresh)
2198 iChild++;
2199 if (iChild >= pNode->cChildren)
2200 break;
2201 PDBGGUISTATSNODE pChild = pNode->papChildren[iChild];
2202 QModelIndex const TopLeft = createIndex(iChild, 2, pChild);
2203 pChild->enmState = kDbgGuiStatsNodeState_kVisible;
2204
2205 /* Any subsequent nodes that also needs refreshing? */
2206 int const iRightCol = pChild->enmType != STAMTYPE_PROFILE && pChild->enmType != STAMTYPE_PROFILE_ADV ? 4 : 7;
2207 if (iRightCol == 4)
2208 while ( iChild + 1 < pNode->cChildren
2209 && (pChild = pNode->papChildren[iChild + 1])->enmState == kDbgGuiStatsNodeState_kRefresh
2210 && pChild->enmType != STAMTYPE_PROFILE
2211 && pChild->enmType != STAMTYPE_PROFILE_ADV)
2212 iChild++;
2213 else
2214 while ( iChild + 1 < pNode->cChildren
2215 && (pChild = pNode->papChildren[iChild + 1])->enmState == kDbgGuiStatsNodeState_kRefresh
2216 && ( pChild->enmType == STAMTYPE_PROFILE
2217 || pChild->enmType == STAMTYPE_PROFILE_ADV))
2218 iChild++;
2219
2220 /* emit the refresh signal */
2221 QModelIndex const BottomRight = createIndex(iChild, iRightCol, pNode->papChildren[iChild]);
2222 emit dataChanged(TopLeft, BottomRight);
2223 iChild++;
2224 }
2225 }
2226 }
2227
2228 /*
2229 * If a_pSubTree is not an intermediate node, invalidate it explicitly.
2230 */
2231 if (a_pSubTree && a_pSubTree->enmType != STAMTYPE_INVALID)
2232 {
2233 int const iRightCol = a_pSubTree->enmType != STAMTYPE_PROFILE && a_pSubTree->enmType != STAMTYPE_PROFILE_ADV
2234 ? 4 : 7;
2235 QModelIndex const BottomRight = createIndex(a_pSubTree->iSelf, iRightCol, a_pSubTree);
2236 QModelIndex const TopLeft = createIndex(a_pSubTree->iSelf, 2, a_pSubTree);
2237 emit dataChanged(TopLeft, BottomRight);
2238 }
2239 }
2240
2241 return m_fUpdateInsertRemove;
2242}
2243
2244
2245bool
2246VBoxDbgStatsModel::updateStatsByPattern(const QString &a_rPatStr, PDBGGUISTATSNODE a_pSubTree /*= NULL*/)
2247{
2248 /* stub */
2249 RT_NOREF(a_rPatStr, a_pSubTree);
2250 return false;
2251}
2252
2253
2254void
2255VBoxDbgStatsModel::updateStatsByIndex(QModelIndex const &a_rIndex)
2256{
2257 PDBGGUISTATSNODE pNode = nodeFromIndex(a_rIndex);
2258 if (pNode == m_pRoot || !a_rIndex.isValid())
2259 updateStatsByPattern(QString());
2260 else if (pNode)
2261 /** @todo this doesn't quite work if pNode is excluded by the m_PatStr. */
2262 updateStatsByPattern(getNodePattern(pNode, true /*fSubTree*/), pNode);
2263}
2264
2265
2266void
2267VBoxDbgStatsModel::resetStatsByPattern(QString const &a_rPatStr)
2268{
2269 /* stub */
2270 NOREF(a_rPatStr);
2271}
2272
2273
2274void
2275VBoxDbgStatsModel::resetStatsByIndex(QModelIndex const &a_rIndex, bool fSubTree /*= true*/)
2276{
2277 PCDBGGUISTATSNODE pNode = nodeFromIndex(a_rIndex);
2278 if (pNode == m_pRoot || !a_rIndex.isValid())
2279 {
2280 /* The root can't be reset, so only take action if fSubTree is set. */
2281 if (fSubTree)
2282 resetStatsByPattern(QString());
2283 }
2284 else if (pNode)
2285 resetStatsByPattern(getNodePattern(pNode, fSubTree));
2286}
2287
2288
2289void
2290VBoxDbgStatsModel::iterateStatsByPattern(QString const &a_rPatStr, VBoxDbgStatsModel::FNITERATOR *a_pfnCallback, void *a_pvUser,
2291 bool a_fMatchChildren /*= true*/)
2292{
2293 const QByteArray &PatBytes = a_rPatStr.toUtf8();
2294 const char * const pszPattern = PatBytes.constData();
2295 size_t const cchPattern = strlen(pszPattern);
2296
2297 DBGGUISTATSSTACK Stack;
2298 Stack.a[0].pNode = m_pRoot;
2299 Stack.a[0].iChild = 0;
2300 Stack.a[0].cchName = 0;
2301 Stack.iTop = 0;
2302
2303 char szName[1024];
2304 szName[0] = '\0';
2305
2306 while (Stack.iTop >= 0)
2307 {
2308 /* get top element */
2309 PDBGGUISTATSNODE const pNode = Stack.a[Stack.iTop].pNode;
2310 uint16_t cchName = Stack.a[Stack.iTop].cchName;
2311 uint32_t const iChild = Stack.a[Stack.iTop].iChild++;
2312 if (iChild < pNode->cChildren)
2313 {
2314 PDBGGUISTATSNODE pChild = pNode->papChildren[iChild];
2315
2316 /* Build the name and match the pattern. */
2317 Assert(cchName + 1 + pChild->cchName < sizeof(szName));
2318 szName[cchName++] = '/';
2319 memcpy(&szName[cchName], pChild->pszName, pChild->cchName);
2320 cchName += (uint16_t)pChild->cchName;
2321 szName[cchName] = '\0';
2322
2323 if (RTStrSimplePatternMultiMatch(pszPattern, cchPattern, szName, cchName, NULL))
2324 {
2325 /* Do callback. */
2326 QModelIndex const Index = createIndex(iChild, 0, pChild);
2327 if (!a_pfnCallback(pChild, Index, szName, a_pvUser))
2328 return;
2329 if (!a_fMatchChildren)
2330 continue;
2331 }
2332
2333 /* push */
2334 Stack.iTop++;
2335 Assert(Stack.iTop < (int32_t)RT_ELEMENTS(Stack.a));
2336 Stack.a[Stack.iTop].pNode = pChild;
2337 Stack.a[Stack.iTop].iChild = 0;
2338 Stack.a[Stack.iTop].cchName = cchName;
2339 }
2340 else
2341 {
2342 /* pop */
2343 Stack.iTop--;
2344 }
2345 }
2346}
2347
2348
2349QModelIndex
2350VBoxDbgStatsModel::getRootIndex(void) const
2351{
2352 if (!m_pRoot)
2353 return QModelIndex();
2354 return createIndex(0, 0, m_pRoot);
2355}
2356
2357
2358void
2359VBoxDbgStatsModel::setRootNode(PDBGGUISTATSNODE a_pRoot)
2360{
2361 PDBGGUISTATSNODE pOldTree = m_pRoot;
2362 m_pRoot = a_pRoot;
2363 destroyTree(pOldTree);
2364 beginResetModel();
2365 endResetModel();
2366}
2367
2368
2369Qt::ItemFlags
2370VBoxDbgStatsModel::flags(const QModelIndex &a_rIndex) const
2371{
2372 Qt::ItemFlags fFlags = QAbstractItemModel::flags(a_rIndex);
2373 return fFlags;
2374}
2375
2376
2377int
2378VBoxDbgStatsModel::columnCount(const QModelIndex &a_rParent) const
2379{
2380 NOREF(a_rParent);
2381 return DBGGUI_STATS_COLUMNS;
2382}
2383
2384
2385int
2386VBoxDbgStatsModel::rowCount(const QModelIndex &a_rParent) const
2387{
2388 PDBGGUISTATSNODE pParent = nodeFromIndex(a_rParent);
2389 return pParent ? pParent->cChildren : 1 /* root */;
2390}
2391
2392
2393bool
2394VBoxDbgStatsModel::hasChildren(const QModelIndex &a_rParent) const
2395{
2396 PDBGGUISTATSNODE pParent = nodeFromIndex(a_rParent);
2397 return pParent ? pParent->cChildren > 0 : true /* root */;
2398}
2399
2400
2401QModelIndex
2402VBoxDbgStatsModel::index(int iRow, int iColumn, const QModelIndex &a_rParent) const
2403{
2404 PDBGGUISTATSNODE pParent = nodeFromIndex(a_rParent);
2405 if (pParent)
2406 {
2407 AssertMsgReturn((unsigned)iRow < pParent->cChildren,
2408 ("iRow=%d >= cChildren=%u (iColumn=%d)\n", iRow, (unsigned)pParent->cChildren, iColumn),
2409 QModelIndex());
2410 AssertMsgReturn((unsigned)iColumn < DBGGUI_STATS_COLUMNS, ("iColumn=%d (iRow=%d)\n", iColumn, iRow), QModelIndex());
2411
2412 PDBGGUISTATSNODE pChild = pParent->papChildren[iRow];
2413 return createIndex(iRow, iColumn, pChild);
2414 }
2415
2416 /* root? */
2417 AssertReturn(a_rParent.isValid() || (iRow == 0 && iColumn >= 0), QModelIndex());
2418 AssertMsgReturn(iRow == 0 && (unsigned)iColumn < DBGGUI_STATS_COLUMNS, ("iRow=%d iColumn=%d", iRow, iColumn), QModelIndex());
2419 return createIndex(0, iColumn, m_pRoot);
2420}
2421
2422
2423QModelIndex
2424VBoxDbgStatsModel::parent(const QModelIndex &a_rChild) const
2425{
2426 PDBGGUISTATSNODE pChild = nodeFromIndex(a_rChild);
2427 if (!pChild)
2428 {
2429 Log(("parent: invalid child\n"));
2430 return QModelIndex(); /* bug */
2431 }
2432 PDBGGUISTATSNODE pParent = pChild->pParent;
2433 if (!pParent)
2434 return QModelIndex(); /* ultimate root */
2435
2436 return createIndex(pParent->iSelf, 0, pParent);
2437}
2438
2439
2440QVariant
2441VBoxDbgStatsModel::headerData(int a_iSection, Qt::Orientation a_eOrientation, int a_eRole) const
2442{
2443 if ( a_eOrientation == Qt::Horizontal
2444 && a_eRole == Qt::DisplayRole)
2445 switch (a_iSection)
2446 {
2447 case 0: return tr("Name");
2448 case 1: return tr("Unit");
2449 case 2: return tr("Value/Times");
2450 case 3: return tr("dInt");
2451 case 4: return tr("Min");
2452 case 5: return tr("Average");
2453 case 6: return tr("Max");
2454 case 7: return tr("Total");
2455 case 8: return tr("Description");
2456 default:
2457 AssertCompile(DBGGUI_STATS_COLUMNS == 9);
2458 return QVariant(); /* bug */
2459 }
2460 else if ( a_eOrientation == Qt::Horizontal
2461 && a_eRole == Qt::TextAlignmentRole)
2462 switch (a_iSection)
2463 {
2464 case 0:
2465 case 1:
2466 return QVariant();
2467 case 2:
2468 case 3:
2469 case 4:
2470 case 5:
2471 case 6:
2472 case 7:
2473 return (int)(Qt::AlignRight | Qt::AlignVCenter);
2474 case 8:
2475 return QVariant();
2476 default:
2477 AssertCompile(DBGGUI_STATS_COLUMNS == 9);
2478 return QVariant(); /* bug */
2479 }
2480
2481 return QVariant();
2482}
2483
2484
2485/*static*/ QString
2486VBoxDbgStatsModel::strUnit(PCDBGGUISTATSNODE pNode)
2487{
2488 return pNode->pszUnit;
2489}
2490
2491
2492/*static*/ QString
2493VBoxDbgStatsModel::strValueTimes(PCDBGGUISTATSNODE pNode)
2494{
2495 char sz[128];
2496
2497 switch (pNode->enmType)
2498 {
2499 case STAMTYPE_COUNTER:
2500 return formatNumber(sz, pNode->Data.Counter.c);
2501
2502 case STAMTYPE_PROFILE:
2503 case STAMTYPE_PROFILE_ADV:
2504 return formatNumber(sz, pNode->Data.Profile.cPeriods);
2505
2506 case STAMTYPE_RATIO_U32:
2507 case STAMTYPE_RATIO_U32_RESET:
2508 {
2509 char szTmp[64];
2510 char *psz = formatNumber(szTmp, pNode->Data.RatioU32.u32A);
2511 size_t off = strlen(psz);
2512 memcpy(sz, psz, off);
2513 sz[off++] = ':';
2514 strcpy(&sz[off], formatNumber(szTmp, pNode->Data.RatioU32.u32B));
2515 return sz;
2516 }
2517
2518 case STAMTYPE_CALLBACK:
2519 return *pNode->Data.pStr;
2520
2521 case STAMTYPE_U8:
2522 case STAMTYPE_U8_RESET:
2523 return formatNumber(sz, pNode->Data.u8);
2524
2525 case STAMTYPE_X8:
2526 case STAMTYPE_X8_RESET:
2527 return formatHexNumber(sz, pNode->Data.u8, 2);
2528
2529 case STAMTYPE_U16:
2530 case STAMTYPE_U16_RESET:
2531 return formatNumber(sz, pNode->Data.u16);
2532
2533 case STAMTYPE_X16:
2534 case STAMTYPE_X16_RESET:
2535 return formatHexNumber(sz, pNode->Data.u16, 4);
2536
2537 case STAMTYPE_U32:
2538 case STAMTYPE_U32_RESET:
2539 return formatNumber(sz, pNode->Data.u32);
2540
2541 case STAMTYPE_X32:
2542 case STAMTYPE_X32_RESET:
2543 return formatHexNumber(sz, pNode->Data.u32, 8);
2544
2545 case STAMTYPE_U64:
2546 case STAMTYPE_U64_RESET:
2547 return formatNumber(sz, pNode->Data.u64);
2548
2549 case STAMTYPE_X64:
2550 case STAMTYPE_X64_RESET:
2551 return formatHexNumber(sz, pNode->Data.u64, 16);
2552
2553 case STAMTYPE_BOOL:
2554 case STAMTYPE_BOOL_RESET:
2555 return pNode->Data.f ? "true" : "false";
2556
2557 default:
2558 AssertMsgFailed(("%d\n", pNode->enmType));
2559 RT_FALL_THRU();
2560 case STAMTYPE_INVALID:
2561 return "";
2562 }
2563}
2564
2565
2566/*static*/ uint64_t
2567VBoxDbgStatsModel::getValueTimesAsUInt(PCDBGGUISTATSNODE pNode)
2568{
2569 switch (pNode->enmType)
2570 {
2571 case STAMTYPE_COUNTER:
2572 return pNode->Data.Counter.c;
2573
2574 case STAMTYPE_PROFILE:
2575 case STAMTYPE_PROFILE_ADV:
2576 return pNode->Data.Profile.cPeriods;
2577
2578 case STAMTYPE_RATIO_U32:
2579 case STAMTYPE_RATIO_U32_RESET:
2580 return RT_MAKE_U64(pNode->Data.RatioU32.u32A, pNode->Data.RatioU32.u32B);
2581
2582 case STAMTYPE_CALLBACK:
2583 return UINT64_MAX;
2584
2585 case STAMTYPE_U8:
2586 case STAMTYPE_U8_RESET:
2587 case STAMTYPE_X8:
2588 case STAMTYPE_X8_RESET:
2589 return pNode->Data.u8;
2590
2591 case STAMTYPE_U16:
2592 case STAMTYPE_U16_RESET:
2593 case STAMTYPE_X16:
2594 case STAMTYPE_X16_RESET:
2595 return pNode->Data.u16;
2596
2597 case STAMTYPE_U32:
2598 case STAMTYPE_U32_RESET:
2599 case STAMTYPE_X32:
2600 case STAMTYPE_X32_RESET:
2601 return pNode->Data.u32;
2602
2603 case STAMTYPE_U64:
2604 case STAMTYPE_U64_RESET:
2605 case STAMTYPE_X64:
2606 case STAMTYPE_X64_RESET:
2607 return pNode->Data.u64;
2608
2609 case STAMTYPE_BOOL:
2610 case STAMTYPE_BOOL_RESET:
2611 return pNode->Data.f;
2612
2613 default:
2614 AssertMsgFailed(("%d\n", pNode->enmType));
2615 RT_FALL_THRU();
2616 case STAMTYPE_INVALID:
2617 return UINT64_MAX;
2618 }
2619}
2620
2621
2622/*static*/ uint64_t
2623VBoxDbgStatsModel::getValueOrAvgAsUInt(PCDBGGUISTATSNODE pNode)
2624{
2625 switch (pNode->enmType)
2626 {
2627 case STAMTYPE_COUNTER:
2628 return pNode->Data.Counter.c;
2629
2630 case STAMTYPE_PROFILE:
2631 case STAMTYPE_PROFILE_ADV:
2632 if (pNode->Data.Profile.cPeriods)
2633 return pNode->Data.Profile.cTicks / pNode->Data.Profile.cPeriods;
2634 return 0;
2635
2636 case STAMTYPE_RATIO_U32:
2637 case STAMTYPE_RATIO_U32_RESET:
2638 return RT_MAKE_U64(pNode->Data.RatioU32.u32A, pNode->Data.RatioU32.u32B);
2639
2640 case STAMTYPE_CALLBACK:
2641 return UINT64_MAX;
2642
2643 case STAMTYPE_U8:
2644 case STAMTYPE_U8_RESET:
2645 case STAMTYPE_X8:
2646 case STAMTYPE_X8_RESET:
2647 return pNode->Data.u8;
2648
2649 case STAMTYPE_U16:
2650 case STAMTYPE_U16_RESET:
2651 case STAMTYPE_X16:
2652 case STAMTYPE_X16_RESET:
2653 return pNode->Data.u16;
2654
2655 case STAMTYPE_U32:
2656 case STAMTYPE_U32_RESET:
2657 case STAMTYPE_X32:
2658 case STAMTYPE_X32_RESET:
2659 return pNode->Data.u32;
2660
2661 case STAMTYPE_U64:
2662 case STAMTYPE_U64_RESET:
2663 case STAMTYPE_X64:
2664 case STAMTYPE_X64_RESET:
2665 return pNode->Data.u64;
2666
2667 case STAMTYPE_BOOL:
2668 case STAMTYPE_BOOL_RESET:
2669 return pNode->Data.f;
2670
2671 default:
2672 AssertMsgFailed(("%d\n", pNode->enmType));
2673 RT_FALL_THRU();
2674 case STAMTYPE_INVALID:
2675 return UINT64_MAX;
2676 }
2677}
2678
2679
2680/*static*/ QString
2681VBoxDbgStatsModel::strMinValue(PCDBGGUISTATSNODE pNode)
2682{
2683 char sz[128];
2684
2685 switch (pNode->enmType)
2686 {
2687 case STAMTYPE_PROFILE:
2688 case STAMTYPE_PROFILE_ADV:
2689 if (pNode->Data.Profile.cPeriods)
2690 return formatNumber(sz, pNode->Data.Profile.cTicksMin);
2691 return "0"; /* cTicksMin is set to UINT64_MAX */
2692 default:
2693 return "";
2694 }
2695}
2696
2697
2698/*static*/ uint64_t
2699VBoxDbgStatsModel::getMinValueAsUInt(PCDBGGUISTATSNODE pNode)
2700{
2701 switch (pNode->enmType)
2702 {
2703 case STAMTYPE_PROFILE:
2704 case STAMTYPE_PROFILE_ADV:
2705 if (pNode->Data.Profile.cPeriods)
2706 return pNode->Data.Profile.cTicksMin;
2707 return 0; /* cTicksMin is set to UINT64_MAX */
2708 default:
2709 return UINT64_MAX;
2710 }
2711}
2712
2713
2714/*static*/ QString
2715VBoxDbgStatsModel::strAvgValue(PCDBGGUISTATSNODE pNode)
2716{
2717 char sz[128];
2718
2719 switch (pNode->enmType)
2720 {
2721 case STAMTYPE_PROFILE:
2722 case STAMTYPE_PROFILE_ADV:
2723 if (pNode->Data.Profile.cPeriods)
2724 return formatNumber(sz, pNode->Data.Profile.cTicks / pNode->Data.Profile.cPeriods);
2725 return "0";
2726 default:
2727 return "";
2728 }
2729}
2730
2731
2732/*static*/ uint64_t
2733VBoxDbgStatsModel::getAvgValueAsUInt(PCDBGGUISTATSNODE pNode)
2734{
2735 switch (pNode->enmType)
2736 {
2737 case STAMTYPE_PROFILE:
2738 case STAMTYPE_PROFILE_ADV:
2739 if (pNode->Data.Profile.cPeriods)
2740 return pNode->Data.Profile.cTicks / pNode->Data.Profile.cPeriods;
2741 return 0;
2742 default:
2743 return UINT64_MAX;
2744 }
2745}
2746
2747
2748
2749/*static*/ QString
2750VBoxDbgStatsModel::strMaxValue(PCDBGGUISTATSNODE pNode)
2751{
2752 char sz[128];
2753
2754 switch (pNode->enmType)
2755 {
2756 case STAMTYPE_PROFILE:
2757 case STAMTYPE_PROFILE_ADV:
2758 return formatNumber(sz, pNode->Data.Profile.cTicksMax);
2759 default:
2760 return "";
2761 }
2762}
2763
2764
2765/*static*/ uint64_t
2766VBoxDbgStatsModel::getMaxValueAsUInt(PCDBGGUISTATSNODE pNode)
2767{
2768 switch (pNode->enmType)
2769 {
2770 case STAMTYPE_PROFILE:
2771 case STAMTYPE_PROFILE_ADV:
2772 return pNode->Data.Profile.cTicksMax;
2773 default:
2774 return UINT64_MAX;
2775 }
2776}
2777
2778
2779/*static*/ QString
2780VBoxDbgStatsModel::strTotalValue(PCDBGGUISTATSNODE pNode)
2781{
2782 char sz[128];
2783
2784 switch (pNode->enmType)
2785 {
2786 case STAMTYPE_PROFILE:
2787 case STAMTYPE_PROFILE_ADV:
2788 return formatNumber(sz, pNode->Data.Profile.cTicks);
2789 default:
2790 return "";
2791 }
2792}
2793
2794
2795/*static*/ uint64_t
2796VBoxDbgStatsModel::getTotalValueAsUInt(PCDBGGUISTATSNODE pNode)
2797{
2798 switch (pNode->enmType)
2799 {
2800 case STAMTYPE_PROFILE:
2801 case STAMTYPE_PROFILE_ADV:
2802 return pNode->Data.Profile.cTicks;
2803 default:
2804 return UINT64_MAX;
2805 }
2806}
2807
2808
2809/*static*/ QString
2810VBoxDbgStatsModel::strDeltaValue(PCDBGGUISTATSNODE pNode)
2811{
2812 switch (pNode->enmType)
2813 {
2814 case STAMTYPE_PROFILE:
2815 case STAMTYPE_PROFILE_ADV:
2816 case STAMTYPE_COUNTER:
2817 case STAMTYPE_RATIO_U32:
2818 case STAMTYPE_RATIO_U32_RESET:
2819 case STAMTYPE_U8:
2820 case STAMTYPE_U8_RESET:
2821 case STAMTYPE_X8:
2822 case STAMTYPE_X8_RESET:
2823 case STAMTYPE_U16:
2824 case STAMTYPE_U16_RESET:
2825 case STAMTYPE_X16:
2826 case STAMTYPE_X16_RESET:
2827 case STAMTYPE_U32:
2828 case STAMTYPE_U32_RESET:
2829 case STAMTYPE_X32:
2830 case STAMTYPE_X32_RESET:
2831 case STAMTYPE_U64:
2832 case STAMTYPE_U64_RESET:
2833 case STAMTYPE_X64:
2834 case STAMTYPE_X64_RESET:
2835 case STAMTYPE_BOOL:
2836 case STAMTYPE_BOOL_RESET:
2837 if (pNode->i64Delta)
2838 {
2839 char sz[128];
2840 return formatNumberSigned(sz, pNode->i64Delta, true /*fPositivePlus*/);
2841 }
2842 return "0";
2843 case STAMTYPE_INTERNAL_SUM:
2844 case STAMTYPE_INTERNAL_PCT_OF_SUM:
2845 case STAMTYPE_END:
2846 AssertFailed(); RT_FALL_THRU();
2847 case STAMTYPE_CALLBACK:
2848 case STAMTYPE_INVALID:
2849 break;
2850 }
2851 return "";
2852}
2853
2854
2855QVariant
2856VBoxDbgStatsModel::data(const QModelIndex &a_rIndex, int a_eRole) const
2857{
2858 unsigned iCol = a_rIndex.column();
2859 AssertMsgReturn(iCol < DBGGUI_STATS_COLUMNS, ("%d\n", iCol), QVariant());
2860 Log4(("Model::data(%p(%d,%d), %d)\n", nodeFromIndex(a_rIndex), iCol, a_rIndex.row(), a_eRole));
2861
2862 if (a_eRole == Qt::DisplayRole)
2863 {
2864 PDBGGUISTATSNODE pNode = nodeFromIndex(a_rIndex);
2865 AssertReturn(pNode, QVariant());
2866
2867 switch (iCol)
2868 {
2869 case 0:
2870 if (!pNode->pFilter)
2871 return QString(pNode->pszName);
2872 return QString(pNode->pszName) + " (*)";
2873 case 1:
2874 return strUnit(pNode);
2875 case 2:
2876 return strValueTimes(pNode);
2877 case 3:
2878 return strDeltaValue(pNode);
2879 case 4:
2880 return strMinValue(pNode);
2881 case 5:
2882 return strAvgValue(pNode);
2883 case 6:
2884 return strMaxValue(pNode);
2885 case 7:
2886 return strTotalValue(pNode);
2887 case 8:
2888 return pNode->pDescStr ? QString(*pNode->pDescStr) : QString("");
2889 default:
2890 AssertCompile(DBGGUI_STATS_COLUMNS == 9);
2891 return QVariant();
2892 }
2893 }
2894 else if (a_eRole == Qt::TextAlignmentRole)
2895 switch (iCol)
2896 {
2897 case 0:
2898 case 1:
2899 return QVariant();
2900 case 2:
2901 case 3:
2902 case 4:
2903 case 5:
2904 case 6:
2905 case 7:
2906 return (int)(Qt::AlignRight | Qt::AlignVCenter);
2907 case 8:
2908 return QVariant();
2909 default:
2910 AssertCompile(DBGGUI_STATS_COLUMNS == 9);
2911 return QVariant(); /* bug */
2912 }
2913 return QVariant();
2914}
2915
2916
2917/*static*/ void
2918VBoxDbgStatsModel::stringifyNodeNoRecursion(PDBGGUISTATSNODE a_pNode, QString &a_rString)
2919{
2920 /*
2921 * Get the path, padding it to 32-chars and add it to the string.
2922 */
2923 char szBuf[1024];
2924 ssize_t off = getNodePath(a_pNode, szBuf, sizeof(szBuf) - 2);
2925 AssertReturnVoid(off >= 0);
2926 if (off < 32)
2927 {
2928 memset(&szBuf[off], ' ', 32 - off);
2929 szBuf[32] = '\0';
2930 off = 32;
2931 }
2932 szBuf[off++] = ' ';
2933 szBuf[off] = '\0';
2934 a_rString += szBuf;
2935
2936 /*
2937 * The following is derived from stamR3PrintOne, except
2938 * we print to szBuf, do no visibility checks and can skip
2939 * the path bit.
2940 */
2941 switch (a_pNode->enmType)
2942 {
2943 case STAMTYPE_COUNTER:
2944 RTStrPrintf(szBuf, sizeof(szBuf), "%8llu %s", a_pNode->Data.Counter.c, a_pNode->pszUnit);
2945 break;
2946
2947 case STAMTYPE_PROFILE:
2948 case STAMTYPE_PROFILE_ADV:
2949 {
2950 uint64_t u64 = a_pNode->Data.Profile.cPeriods ? a_pNode->Data.Profile.cPeriods : 1;
2951 RTStrPrintf(szBuf, sizeof(szBuf),
2952 "%8llu %s (%12llu ticks, %7llu times, max %9llu, min %7lld)",
2953 a_pNode->Data.Profile.cTicks / u64, a_pNode->pszUnit,
2954 a_pNode->Data.Profile.cTicks, a_pNode->Data.Profile.cPeriods, a_pNode->Data.Profile.cTicksMax, a_pNode->Data.Profile.cTicksMin);
2955 break;
2956 }
2957
2958 case STAMTYPE_RATIO_U32:
2959 case STAMTYPE_RATIO_U32_RESET:
2960 RTStrPrintf(szBuf, sizeof(szBuf),
2961 "%8u:%-8u %s",
2962 a_pNode->Data.RatioU32.u32A, a_pNode->Data.RatioU32.u32B, a_pNode->pszUnit);
2963 break;
2964
2965 case STAMTYPE_CALLBACK:
2966 if (a_pNode->Data.pStr)
2967 a_rString += *a_pNode->Data.pStr;
2968 RTStrPrintf(szBuf, sizeof(szBuf), " %s", a_pNode->pszUnit);
2969 break;
2970
2971 case STAMTYPE_U8:
2972 case STAMTYPE_U8_RESET:
2973 RTStrPrintf(szBuf, sizeof(szBuf), "%8u %s", a_pNode->Data.u8, a_pNode->pszUnit);
2974 break;
2975
2976 case STAMTYPE_X8:
2977 case STAMTYPE_X8_RESET:
2978 RTStrPrintf(szBuf, sizeof(szBuf), "%8x %s", a_pNode->Data.u8, a_pNode->pszUnit);
2979 break;
2980
2981 case STAMTYPE_U16:
2982 case STAMTYPE_U16_RESET:
2983 RTStrPrintf(szBuf, sizeof(szBuf), "%8u %s", a_pNode->Data.u16, a_pNode->pszUnit);
2984 break;
2985
2986 case STAMTYPE_X16:
2987 case STAMTYPE_X16_RESET:
2988 RTStrPrintf(szBuf, sizeof(szBuf), "%8x %s", a_pNode->Data.u16, a_pNode->pszUnit);
2989 break;
2990
2991 case STAMTYPE_U32:
2992 case STAMTYPE_U32_RESET:
2993 RTStrPrintf(szBuf, sizeof(szBuf), "%8u %s", a_pNode->Data.u32, a_pNode->pszUnit);
2994 break;
2995
2996 case STAMTYPE_X32:
2997 case STAMTYPE_X32_RESET:
2998 RTStrPrintf(szBuf, sizeof(szBuf), "%8x %s", a_pNode->Data.u32, a_pNode->pszUnit);
2999 break;
3000
3001 case STAMTYPE_U64:
3002 case STAMTYPE_U64_RESET:
3003 RTStrPrintf(szBuf, sizeof(szBuf), "%8llu %s", a_pNode->Data.u64, a_pNode->pszUnit);
3004 break;
3005
3006 case STAMTYPE_X64:
3007 case STAMTYPE_X64_RESET:
3008 RTStrPrintf(szBuf, sizeof(szBuf), "%8llx %s", a_pNode->Data.u64, a_pNode->pszUnit);
3009 break;
3010
3011 case STAMTYPE_BOOL:
3012 case STAMTYPE_BOOL_RESET:
3013 RTStrPrintf(szBuf, sizeof(szBuf), "%s %s", a_pNode->Data.f ? "true " : "false ", a_pNode->pszUnit);
3014 break;
3015
3016 default:
3017 AssertMsgFailed(("enmType=%d\n", a_pNode->enmType));
3018 return;
3019 }
3020
3021 a_rString += szBuf;
3022}
3023
3024
3025/*static*/ void
3026VBoxDbgStatsModel::stringifyNode(PDBGGUISTATSNODE a_pNode, QString &a_rString)
3027{
3028 /* this node (if it has data) */
3029 if (a_pNode->enmType != STAMTYPE_INVALID)
3030 {
3031 if (!a_rString.isEmpty())
3032 a_rString += "\n";
3033 stringifyNodeNoRecursion(a_pNode, a_rString);
3034 }
3035
3036 /* the children */
3037 uint32_t const cChildren = a_pNode->cChildren;
3038 for (uint32_t i = 0; i < cChildren; i++)
3039 stringifyNode(a_pNode->papChildren[i], a_rString);
3040}
3041
3042
3043void
3044VBoxDbgStatsModel::stringifyTree(QModelIndex &a_rRoot, QString &a_rString) const
3045{
3046 PDBGGUISTATSNODE pRoot = a_rRoot.isValid() ? nodeFromIndex(a_rRoot) : m_pRoot;
3047 if (pRoot)
3048 stringifyNode(pRoot, a_rString);
3049}
3050
3051
3052void
3053VBoxDbgStatsModel::copyTreeToClipboard(QModelIndex &a_rRoot) const
3054{
3055 QString String;
3056 stringifyTree(a_rRoot, String);
3057
3058 QClipboard *pClipboard = QApplication::clipboard();
3059 if (pClipboard)
3060 pClipboard->setText(String, QClipboard::Clipboard);
3061}
3062
3063
3064/*static*/ void
3065VBoxDbgStatsModel::logNode(PDBGGUISTATSNODE a_pNode, bool a_fReleaseLog)
3066{
3067 /* this node (if it has data) */
3068 if (a_pNode->enmType != STAMTYPE_INVALID)
3069 {
3070 QString SelfStr;
3071 stringifyNodeNoRecursion(a_pNode, SelfStr);
3072 QByteArray SelfByteArray = SelfStr.toUtf8();
3073 if (a_fReleaseLog)
3074 RTLogRelPrintf("%s\n", SelfByteArray.constData());
3075 else
3076 RTLogPrintf("%s\n", SelfByteArray.constData());
3077 }
3078
3079 /* the children */
3080 uint32_t const cChildren = a_pNode->cChildren;
3081 for (uint32_t i = 0; i < cChildren; i++)
3082 logNode(a_pNode->papChildren[i], a_fReleaseLog);
3083}
3084
3085
3086void
3087VBoxDbgStatsModel::logTree(QModelIndex &a_rRoot, bool a_fReleaseLog) const
3088{
3089 PDBGGUISTATSNODE pRoot = a_rRoot.isValid() ? nodeFromIndex(a_rRoot) : m_pRoot;
3090 if (pRoot)
3091 logNode(pRoot, a_fReleaseLog);
3092}
3093
3094
3095
3096
3097
3098
3099
3100/*
3101 *
3102 * V B o x D b g S t a t s M o d e l V M
3103 * V B o x D b g S t a t s M o d e l V M
3104 * V B o x D b g S t a t s M o d e l V M
3105 *
3106 *
3107 */
3108
3109
3110VBoxDbgStatsModelVM::VBoxDbgStatsModelVM(VBoxDbgGui *a_pDbgGui, QString &a_rPatStr, QObject *a_pParent, PCVMMR3VTABLE a_pVMM)
3111 : VBoxDbgStatsModel(a_pParent), VBoxDbgBase(a_pDbgGui), m_pVMM(a_pVMM)
3112{
3113 /*
3114 * Create a model containing the STAM entries matching the pattern.
3115 * (The original idea was to get everything and rely on some hide/visible
3116 * flag that it turned out didn't exist.)
3117 */
3118 PDBGGUISTATSNODE pTree = createNewTree(a_rPatStr);
3119 setRootNode(pTree);
3120}
3121
3122
3123VBoxDbgStatsModelVM::~VBoxDbgStatsModelVM()
3124{
3125 /* nothing to do here. */
3126}
3127
3128
3129bool
3130VBoxDbgStatsModelVM::updateStatsByPattern(const QString &a_rPatStr, PDBGGUISTATSNODE a_pSubTree /*= NULL*/)
3131{
3132 /** @todo the way we update this stuff is independent of the source (XML, file, STAM), our only
3133 * ASSUMPTION is that the input is strictly ordered by (fully slashed) name. So, all this stuff
3134 * should really move up into the parent class. */
3135 bool fRc = updatePrepare(a_pSubTree);
3136 if (fRc)
3137 {
3138 int rc = stamEnum(a_rPatStr, updateCallback, this);
3139 fRc = updateDone(RT_SUCCESS(rc), a_pSubTree);
3140 }
3141 return fRc;
3142}
3143
3144
3145void
3146VBoxDbgStatsModelVM::resetStatsByPattern(QString const &a_rPatStr)
3147{
3148 stamReset(a_rPatStr);
3149}
3150
3151
3152/*static*/ DECLCALLBACK(int)
3153VBoxDbgStatsModelVM::createNewTreeCallback(const char *pszName, STAMTYPE enmType, void *pvSample, STAMUNIT enmUnit,
3154 const char *pszUnit, STAMVISIBILITY enmVisibility, const char *pszDesc, void *pvUser)
3155{
3156 PDBGGUISTATSNODE pRoot = (PDBGGUISTATSNODE)pvUser;
3157 Log3(("createNewTreeCallback: %s\n", pszName));
3158 RT_NOREF(enmUnit);
3159
3160 /*
3161 * Skip the ones which shouldn't be visible in the GUI.
3162 */
3163 if (enmVisibility == STAMVISIBILITY_NOT_GUI)
3164 return 0;
3165
3166 /*
3167 * Perform a mkdir -p like operation till we've walked / created the entire path down
3168 * to the node specfied node. Remember the last node as that will be the one we will
3169 * stuff the data into.
3170 */
3171 AssertReturn(*pszName == '/' && pszName[1] != '/', VERR_INTERNAL_ERROR);
3172 PDBGGUISTATSNODE pNode = pRoot;
3173 const char *pszCur = pszName + 1;
3174 while (*pszCur)
3175 {
3176 /* find the end of this component. */
3177 const char *pszNext = strchr(pszCur, '/');
3178 if (!pszNext)
3179 pszNext = strchr(pszCur, '\0');
3180 size_t cchCur = pszNext - pszCur;
3181
3182 /* Create it if it doesn't exist (it will be last if it exists). */
3183 if ( !pNode->cChildren
3184 || strncmp(pNode->papChildren[pNode->cChildren - 1]->pszName, pszCur, cchCur)
3185 || pNode->papChildren[pNode->cChildren - 1]->pszName[cchCur])
3186 {
3187 pNode = createAndInsertNode(pNode, pszCur, pszNext - pszCur, UINT32_MAX);
3188 if (!pNode)
3189 return VERR_NO_MEMORY;
3190 }
3191 else
3192 pNode = pNode->papChildren[pNode->cChildren - 1];
3193
3194 /* Advance */
3195 pszCur = *pszNext ? pszNext + 1 : pszNext;
3196 }
3197
3198 /*
3199 * Save the data.
3200 */
3201 return initNode(pNode, enmType, pvSample, pszUnit, pszDesc);
3202}
3203
3204
3205PDBGGUISTATSNODE
3206VBoxDbgStatsModelVM::createNewTree(QString &a_rPatStr)
3207{
3208 PDBGGUISTATSNODE pRoot = createRootNode();
3209 if (pRoot)
3210 {
3211 int rc = stamEnum(a_rPatStr, createNewTreeCallback, pRoot);
3212 if (RT_SUCCESS(rc))
3213 return pRoot;
3214
3215 /* failed, cleanup. */
3216 destroyTree(pRoot);
3217 }
3218
3219 return NULL;
3220}
3221
3222
3223
3224
3225
3226
3227
3228
3229/*
3230 *
3231 * V B o x D b g S t a t s S o r t F i l e P r o x y M o d e l
3232 * V B o x D b g S t a t s S o r t F i l e P r o x y M o d e l
3233 * V B o x D b g S t a t s S o r t F i l e P r o x y M o d e l
3234 *
3235 *
3236 */
3237
3238#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3239
3240VBoxDbgStatsSortFileProxyModel::VBoxDbgStatsSortFileProxyModel(QObject *a_pParent)
3241 : QSortFilterProxyModel(a_pParent)
3242 , m_fShowUnusedRows(false)
3243{
3244
3245}
3246
3247
3248bool
3249VBoxDbgStatsSortFileProxyModel::filterAcceptsRow(int a_iSrcRow, const QModelIndex &a_rSrcParent) const
3250{
3251 /*
3252 * Locate the node.
3253 */
3254 PDBGGUISTATSNODE pParent = nodeFromIndex(a_rSrcParent);
3255 if (pParent)
3256 {
3257 if ((unsigned)a_iSrcRow < pParent->cChildren)
3258 {
3259 PDBGGUISTATSNODE const pNode = pParent->papChildren[a_iSrcRow];
3260 if (pNode) /* paranoia */
3261 {
3262 /*
3263 * Apply the global unused-row filter.
3264 */
3265 if (!m_fShowUnusedRows)
3266 {
3267 /* Only relevant for leaf nodes. */
3268 if (pNode->cChildren == 0)
3269 {
3270 if (pNode->enmState != kDbgGuiStatsNodeState_kInvalid)
3271 {
3272 if (pNode->i64Delta == 0)
3273 {
3274 /* Is the cached statistics value zero? */
3275 switch (pNode->enmType)
3276 {
3277 case STAMTYPE_COUNTER:
3278 if (pNode->Data.Counter.c != 0)
3279 break;
3280 return false;
3281
3282 case STAMTYPE_PROFILE:
3283 case STAMTYPE_PROFILE_ADV:
3284 if (pNode->Data.Profile.cPeriods)
3285 break;
3286 return false;
3287
3288 case STAMTYPE_RATIO_U32:
3289 case STAMTYPE_RATIO_U32_RESET:
3290 if (pNode->Data.RatioU32.u32A || pNode->Data.RatioU32.u32B)
3291 break;
3292 return false;
3293
3294 case STAMTYPE_CALLBACK:
3295 if (pNode->Data.pStr && !pNode->Data.pStr->isEmpty())
3296 break;
3297 return false;
3298
3299 case STAMTYPE_U8:
3300 case STAMTYPE_U8_RESET:
3301 case STAMTYPE_X8:
3302 case STAMTYPE_X8_RESET:
3303 if (pNode->Data.u8)
3304 break;
3305 return false;
3306
3307 case STAMTYPE_U16:
3308 case STAMTYPE_U16_RESET:
3309 case STAMTYPE_X16:
3310 case STAMTYPE_X16_RESET:
3311 if (pNode->Data.u16)
3312 break;
3313 return false;
3314
3315 case STAMTYPE_U32:
3316 case STAMTYPE_U32_RESET:
3317 case STAMTYPE_X32:
3318 case STAMTYPE_X32_RESET:
3319 if (pNode->Data.u32)
3320 break;
3321 return false;
3322
3323 case STAMTYPE_U64:
3324 case STAMTYPE_U64_RESET:
3325 case STAMTYPE_X64:
3326 case STAMTYPE_X64_RESET:
3327 if (pNode->Data.u64)
3328 break;
3329 return false;
3330
3331 case STAMTYPE_BOOL:
3332 case STAMTYPE_BOOL_RESET:
3333 /* not possible to detect */
3334 return false;
3335
3336 case STAMTYPE_INVALID:
3337 break;
3338
3339 default:
3340 AssertMsgFailedBreak(("enmType=%d\n", pNode->enmType));
3341 }
3342 }
3343 }
3344 else
3345 return false;
3346 }
3347 }
3348
3349 /*
3350 * Look for additional filtering rules among the ancestors.
3351 */
3352 if (VBoxGuiStatsFilterData::s_cInstances > 0 /* quick & dirty optimization */)
3353 {
3354 VBoxGuiStatsFilterData const *pFilter = pParent->pFilter;
3355 while (!pFilter && (pParent = pParent->pParent) != NULL)
3356 pFilter = pParent->pFilter;
3357 if (pFilter)
3358 {
3359 if (pFilter->uMinValue > 0 || pFilter->uMaxValue != UINT64_MAX)
3360 {
3361 uint64_t const uValue = VBoxDbgStatsModel::getValueTimesAsUInt(pNode);
3362 if ( uValue < pFilter->uMinValue
3363 || uValue > pFilter->uMaxValue)
3364 return false;
3365 }
3366 if (pFilter->pRegexName)
3367 {
3368 if (!pFilter->pRegexName->match(pNode->pszName).hasMatch())
3369 return false;
3370 }
3371 }
3372 }
3373 }
3374 }
3375 }
3376 return true;
3377}
3378
3379
3380bool
3381VBoxDbgStatsSortFileProxyModel::lessThan(const QModelIndex &a_rSrcLeft, const QModelIndex &a_rSrcRight) const
3382{
3383 PCDBGGUISTATSNODE const pLeft = nodeFromIndex(a_rSrcLeft);
3384 PCDBGGUISTATSNODE const pRight = nodeFromIndex(a_rSrcRight);
3385 if (pLeft == pRight)
3386 return false;
3387 if (pLeft && pRight)
3388 {
3389 if (pLeft->pParent == pRight->pParent)
3390 {
3391 switch (a_rSrcLeft.column())
3392 {
3393 case 0:
3394 return RTStrCmp(pLeft->pszName, pRight->pszName) < 0;
3395 case 1:
3396 return RTStrCmp(pLeft->pszUnit, pRight->pszUnit) < 0;
3397 case 2:
3398 return VBoxDbgStatsModel::getValueTimesAsUInt(pLeft) < VBoxDbgStatsModel::getValueTimesAsUInt(pRight);
3399 case 3:
3400 return pLeft->i64Delta < pRight->i64Delta;
3401 case 4:
3402 return VBoxDbgStatsModel::getMinValueAsUInt(pLeft) < VBoxDbgStatsModel::getMinValueAsUInt(pRight);
3403 case 5:
3404 return VBoxDbgStatsModel::getAvgValueAsUInt(pLeft) < VBoxDbgStatsModel::getAvgValueAsUInt(pRight);
3405 case 6:
3406 return VBoxDbgStatsModel::getMaxValueAsUInt(pLeft) < VBoxDbgStatsModel::getMaxValueAsUInt(pRight);
3407 case 7:
3408 return VBoxDbgStatsModel::getTotalValueAsUInt(pLeft) < VBoxDbgStatsModel::getTotalValueAsUInt(pRight);
3409 case 8:
3410 if (pLeft->pDescStr == pRight->pDescStr)
3411 return false;
3412 if (!pLeft->pDescStr)
3413 return true;
3414 if (!pRight->pDescStr)
3415 return false;
3416 return *pLeft->pDescStr < *pRight->pDescStr;
3417 default:
3418 AssertCompile(DBGGUI_STATS_COLUMNS == 9);
3419 return true;
3420 }
3421 }
3422 return false;
3423 }
3424 return !pLeft;
3425}
3426
3427
3428void
3429VBoxDbgStatsSortFileProxyModel::setShowUnusedRows(bool a_fHide)
3430{
3431 if (a_fHide != m_fShowUnusedRows)
3432 {
3433 m_fShowUnusedRows = a_fHide;
3434 invalidateRowsFilter();
3435 }
3436}
3437
3438
3439void
3440VBoxDbgStatsSortFileProxyModel::notifyFilterChanges()
3441{
3442 invalidateRowsFilter();
3443}
3444
3445
3446#endif /* VBOXDBG_WITH_SORTED_AND_FILTERED_STATS */
3447
3448
3449
3450/*
3451 *
3452 * V B o x D b g S t a t s V i e w
3453 * V B o x D b g S t a t s V i e w
3454 * V B o x D b g S t a t s V i e w
3455 *
3456 *
3457 */
3458
3459
3460VBoxDbgStatsView::VBoxDbgStatsView(VBoxDbgGui *a_pDbgGui, VBoxDbgStatsModel *a_pVBoxModel,
3461 VBoxDbgStatsSortFileProxyModel *a_pProxyModel, VBoxDbgStats *a_pParent/* = NULL*/)
3462 : QTreeView(a_pParent)
3463 , VBoxDbgBase(a_pDbgGui)
3464 , m_pVBoxModel(a_pVBoxModel)
3465 , m_pProxyModel(a_pProxyModel)
3466 , m_pModel(NULL)
3467 , m_PatStr()
3468 , m_pParent(a_pParent)
3469 , m_pLeafMenu(NULL)
3470 , m_pBranchMenu(NULL)
3471 , m_pViewMenu(NULL)
3472 , m_pCurMenu(NULL)
3473 , m_CurIndex()
3474{
3475 /*
3476 * Set the model and view defaults.
3477 */
3478 setRootIsDecorated(true);
3479 if (a_pProxyModel)
3480 {
3481 m_pModel = a_pProxyModel;
3482 a_pProxyModel->setSourceModel(a_pVBoxModel);
3483 }
3484 else
3485 m_pModel = a_pVBoxModel;
3486 setModel(m_pModel);
3487 QModelIndex RootIdx = myGetRootIndex(); /* This should really be QModelIndex(), but Qt on darwin does wrong things then. */
3488 setRootIndex(RootIdx);
3489 setItemsExpandable(true);
3490 setAlternatingRowColors(true);
3491 setSelectionBehavior(SelectRows);
3492 setSelectionMode(SingleSelection);
3493 if (a_pProxyModel)
3494 setSortingEnabled(true);
3495
3496 /*
3497 * Create and setup the actions.
3498 */
3499 m_pExpandAct = new QAction("Expand Tree", this);
3500 m_pCollapseAct = new QAction("Collapse Tree", this);
3501 m_pRefreshAct = new QAction("&Refresh", this);
3502 m_pResetAct = new QAction("Rese&t", this);
3503 m_pCopyAct = new QAction("&Copy", this);
3504 m_pToLogAct = new QAction("To &Log", this);
3505 m_pToRelLogAct = new QAction("T&o Release Log", this);
3506 m_pAdjColumnsAct = new QAction("&Adjust Columns", this);
3507#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3508 m_pFilterAct = new QAction("&Filter...", this);
3509#endif
3510
3511 m_pCopyAct->setShortcut(QKeySequence::Copy);
3512 m_pExpandAct->setShortcut(QKeySequence("Ctrl+E"));
3513 m_pCollapseAct->setShortcut(QKeySequence("Ctrl+D"));
3514 m_pRefreshAct->setShortcut(QKeySequence("Ctrl+R"));
3515 m_pResetAct->setShortcut(QKeySequence("Alt+R"));
3516 m_pToLogAct->setShortcut(QKeySequence("Ctrl+Z"));
3517 m_pToRelLogAct->setShortcut(QKeySequence("Alt+Z"));
3518 m_pAdjColumnsAct->setShortcut(QKeySequence("Ctrl+A"));
3519#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3520 //m_pFilterAct->setShortcut(QKeySequence("Ctrl+?"));
3521#endif
3522
3523 addAction(m_pCopyAct);
3524 addAction(m_pExpandAct);
3525 addAction(m_pCollapseAct);
3526 addAction(m_pRefreshAct);
3527 addAction(m_pResetAct);
3528 addAction(m_pToLogAct);
3529 addAction(m_pToRelLogAct);
3530 addAction(m_pAdjColumnsAct);
3531#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3532 addAction(m_pFilterAct);
3533#endif
3534
3535 connect(m_pExpandAct, SIGNAL(triggered(bool)), this, SLOT(actExpand()));
3536 connect(m_pCollapseAct, SIGNAL(triggered(bool)), this, SLOT(actCollapse()));
3537 connect(m_pRefreshAct, SIGNAL(triggered(bool)), this, SLOT(actRefresh()));
3538 connect(m_pResetAct, SIGNAL(triggered(bool)), this, SLOT(actReset()));
3539 connect(m_pCopyAct, SIGNAL(triggered(bool)), this, SLOT(actCopy()));
3540 connect(m_pToLogAct, SIGNAL(triggered(bool)), this, SLOT(actToLog()));
3541 connect(m_pToRelLogAct, SIGNAL(triggered(bool)), this, SLOT(actToRelLog()));
3542 connect(m_pAdjColumnsAct, SIGNAL(triggered(bool)), this, SLOT(actAdjColumns()));
3543#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3544 connect(m_pFilterAct, SIGNAL(triggered(bool)), this, SLOT(actFilter()));
3545#endif
3546
3547
3548 /*
3549 * Create the menus and populate them.
3550 */
3551 setContextMenuPolicy(Qt::DefaultContextMenu);
3552
3553 m_pLeafMenu = new QMenu();
3554 m_pLeafMenu->addAction(m_pCopyAct);
3555 m_pLeafMenu->addAction(m_pRefreshAct);
3556 m_pLeafMenu->addAction(m_pResetAct);
3557 m_pLeafMenu->addAction(m_pToLogAct);
3558 m_pLeafMenu->addAction(m_pToRelLogAct);
3559
3560 m_pBranchMenu = new QMenu(this);
3561 m_pBranchMenu->addAction(m_pCopyAct);
3562 m_pBranchMenu->addAction(m_pRefreshAct);
3563 m_pBranchMenu->addAction(m_pResetAct);
3564 m_pBranchMenu->addAction(m_pToLogAct);
3565 m_pBranchMenu->addAction(m_pToRelLogAct);
3566 m_pBranchMenu->addSeparator();
3567 m_pBranchMenu->addAction(m_pExpandAct);
3568 m_pBranchMenu->addAction(m_pCollapseAct);
3569#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3570 m_pBranchMenu->addSeparator();
3571 m_pBranchMenu->addAction(m_pFilterAct);
3572#endif
3573
3574 m_pViewMenu = new QMenu();
3575 m_pViewMenu->addAction(m_pCopyAct);
3576 m_pViewMenu->addAction(m_pRefreshAct);
3577 m_pViewMenu->addAction(m_pResetAct);
3578 m_pViewMenu->addAction(m_pToLogAct);
3579 m_pViewMenu->addAction(m_pToRelLogAct);
3580 m_pViewMenu->addSeparator();
3581 m_pViewMenu->addAction(m_pExpandAct);
3582 m_pViewMenu->addAction(m_pCollapseAct);
3583 m_pViewMenu->addSeparator();
3584 m_pViewMenu->addAction(m_pAdjColumnsAct);
3585#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3586 m_pViewMenu->addAction(m_pFilterAct);
3587#endif
3588
3589 /* the header menu */
3590 QHeaderView *pHdrView = header();
3591 pHdrView->setContextMenuPolicy(Qt::CustomContextMenu);
3592 connect(pHdrView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(headerContextMenuRequested(const QPoint &)));
3593}
3594
3595
3596VBoxDbgStatsView::~VBoxDbgStatsView()
3597{
3598 m_pParent = NULL;
3599 m_pCurMenu = NULL;
3600 m_CurIndex = QModelIndex();
3601
3602#define DELETE_IT(m) if (m) { delete m; m = NULL; } else do {} while (0)
3603 DELETE_IT(m_pProxyModel);
3604 DELETE_IT(m_pVBoxModel);
3605
3606 DELETE_IT(m_pLeafMenu);
3607 DELETE_IT(m_pBranchMenu);
3608 DELETE_IT(m_pViewMenu);
3609
3610 DELETE_IT(m_pExpandAct);
3611 DELETE_IT(m_pCollapseAct);
3612 DELETE_IT(m_pRefreshAct);
3613 DELETE_IT(m_pResetAct);
3614 DELETE_IT(m_pCopyAct);
3615 DELETE_IT(m_pToLogAct);
3616 DELETE_IT(m_pToRelLogAct);
3617 DELETE_IT(m_pAdjColumnsAct);
3618 DELETE_IT(m_pFilterAct);
3619#undef DELETE_IT
3620}
3621
3622
3623void
3624VBoxDbgStatsView::updateStats(const QString &rPatStr)
3625{
3626 m_PatStr = rPatStr;
3627 if (m_pVBoxModel->updateStatsByPattern(rPatStr))
3628 setRootIndex(myGetRootIndex()); /* hack */
3629}
3630
3631
3632void
3633VBoxDbgStatsView::setShowUnusedRows(bool a_fHide)
3634{
3635#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3636 if (m_pProxyModel)
3637 m_pProxyModel->setShowUnusedRows(a_fHide);
3638#else
3639 RT_NOREF_PV(a_fHide);
3640#endif
3641}
3642
3643
3644void
3645VBoxDbgStatsView::resizeColumnsToContent()
3646{
3647 for (int i = 0; i <= 8; i++)
3648 {
3649 resizeColumnToContents(i);
3650 /* Some extra room for distinguishing numbers better in Value, Min, Avg, Max, Total, dInt columns. */
3651 if (i >= 2 && i <= 7)
3652 setColumnWidth(i, columnWidth(i) + 10);
3653 }
3654}
3655
3656
3657/*static*/ bool
3658VBoxDbgStatsView::expandMatchingCallback(PDBGGUISTATSNODE pNode, QModelIndex const &a_rIndex,
3659 const char *pszFullName, void *pvUser)
3660{
3661 VBoxDbgStatsView *pThis = (VBoxDbgStatsView *)pvUser;
3662
3663 QModelIndex ParentIndex; /* this isn't 100% optimal */
3664 if (pThis->m_pProxyModel)
3665 {
3666 QModelIndex const ProxyIndex = pThis->m_pProxyModel->mapFromSource(a_rIndex);
3667
3668 ParentIndex = pThis->m_pModel->parent(ProxyIndex);
3669 }
3670 else
3671 {
3672 pThis->setExpanded(a_rIndex, true);
3673
3674 ParentIndex = pThis->m_pModel->parent(a_rIndex);
3675 }
3676 while (ParentIndex.isValid() && !pThis->isExpanded(ParentIndex))
3677 {
3678 pThis->setExpanded(ParentIndex, true);
3679 ParentIndex = pThis->m_pModel->parent(ParentIndex);
3680 }
3681
3682 RT_NOREF(pNode, pszFullName);
3683 return true;
3684}
3685
3686
3687void
3688VBoxDbgStatsView::expandMatching(const QString &rPatStr)
3689{
3690 m_pVBoxModel->iterateStatsByPattern(rPatStr, expandMatchingCallback, this);
3691}
3692
3693
3694void
3695VBoxDbgStatsView::setSubTreeExpanded(QModelIndex const &a_rIndex, bool a_fExpanded)
3696{
3697 int cRows = m_pModel->rowCount(a_rIndex);
3698 if (a_rIndex.model())
3699 for (int i = 0; i < cRows; i++)
3700 setSubTreeExpanded(a_rIndex.model()->index(i, 0, a_rIndex), a_fExpanded);
3701 setExpanded(a_rIndex, a_fExpanded);
3702}
3703
3704
3705void
3706VBoxDbgStatsView::contextMenuEvent(QContextMenuEvent *a_pEvt)
3707{
3708 /*
3709 * Get the selected item.
3710 * If it's a mouse event select the item under the cursor (if any).
3711 */
3712 QModelIndex Idx;
3713 if (a_pEvt->reason() == QContextMenuEvent::Mouse)
3714 {
3715 Idx = indexAt(a_pEvt->pos());
3716 if (Idx.isValid())
3717 setCurrentIndex(Idx);
3718 }
3719 else
3720 {
3721 QModelIndexList SelIdx = selectedIndexes();
3722 if (!SelIdx.isEmpty())
3723 Idx = SelIdx.at(0);
3724 }
3725
3726 /*
3727 * Popup the corresponding menu.
3728 */
3729 QMenu *pMenu;
3730 if (!Idx.isValid())
3731 pMenu = m_pViewMenu;
3732 else if (m_pModel->hasChildren(Idx))
3733 pMenu = m_pBranchMenu;
3734 else
3735 pMenu = m_pLeafMenu;
3736 if (pMenu)
3737 {
3738 m_pRefreshAct->setEnabled(!Idx.isValid() || Idx == myGetRootIndex());
3739 m_CurIndex = Idx;
3740 m_pCurMenu = pMenu;
3741
3742 pMenu->exec(a_pEvt->globalPos());
3743
3744 m_pCurMenu = NULL;
3745 m_CurIndex = QModelIndex();
3746 if (m_pRefreshAct)
3747 m_pRefreshAct->setEnabled(true);
3748 }
3749 a_pEvt->accept();
3750}
3751
3752
3753QModelIndex
3754VBoxDbgStatsView::myGetRootIndex(void) const
3755{
3756 if (!m_pProxyModel)
3757 return m_pVBoxModel->getRootIndex();
3758 return m_pProxyModel->mapFromSource(m_pVBoxModel->getRootIndex());
3759}
3760
3761
3762void
3763VBoxDbgStatsView::headerContextMenuRequested(const QPoint &a_rPos)
3764{
3765 /*
3766 * Show the view menu.
3767 */
3768 if (m_pViewMenu)
3769 {
3770 m_pRefreshAct->setEnabled(true);
3771 m_CurIndex = myGetRootIndex();
3772 m_pCurMenu = m_pViewMenu;
3773
3774 m_pViewMenu->exec(header()->mapToGlobal(a_rPos));
3775
3776 m_pCurMenu = NULL;
3777 m_CurIndex = QModelIndex();
3778 if (m_pRefreshAct)
3779 m_pRefreshAct->setEnabled(true);
3780 }
3781}
3782
3783
3784void
3785VBoxDbgStatsView::actExpand()
3786{
3787 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3788 if (Idx.isValid())
3789 setSubTreeExpanded(Idx, true /* a_fExpanded */);
3790}
3791
3792
3793void
3794VBoxDbgStatsView::actCollapse()
3795{
3796 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3797 if (Idx.isValid())
3798 setSubTreeExpanded(Idx, false /* a_fExpanded */);
3799}
3800
3801
3802void
3803VBoxDbgStatsView::actRefresh()
3804{
3805 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3806 if (!Idx.isValid() || Idx == myGetRootIndex())
3807 {
3808 if (m_pVBoxModel->updateStatsByPattern(m_PatStr))
3809 setRootIndex(myGetRootIndex()); /* hack */
3810 }
3811 else
3812 {
3813 if (m_pProxyModel)
3814 Idx = m_pProxyModel->mapToSource(Idx);
3815 m_pVBoxModel->updateStatsByIndex(Idx);
3816 }
3817}
3818
3819
3820void
3821VBoxDbgStatsView::actReset()
3822{
3823 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3824 if (!Idx.isValid() || Idx == myGetRootIndex())
3825 m_pVBoxModel->resetStatsByPattern(m_PatStr);
3826 else
3827 {
3828 if (m_pProxyModel)
3829 Idx = m_pProxyModel->mapToSource(Idx);
3830 m_pVBoxModel->resetStatsByIndex(Idx);
3831 }
3832}
3833
3834
3835void
3836VBoxDbgStatsView::actCopy()
3837{
3838 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3839 if (m_pProxyModel)
3840 Idx = m_pProxyModel->mapToSource(Idx);
3841 m_pVBoxModel->copyTreeToClipboard(Idx);
3842}
3843
3844
3845void
3846VBoxDbgStatsView::actToLog()
3847{
3848 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3849 if (m_pProxyModel)
3850 Idx = m_pProxyModel->mapToSource(Idx);
3851 m_pVBoxModel->logTree(Idx, false /* a_fReleaseLog */);
3852}
3853
3854
3855void
3856VBoxDbgStatsView::actToRelLog()
3857{
3858 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3859 if (m_pProxyModel)
3860 Idx = m_pProxyModel->mapToSource(Idx);
3861 m_pVBoxModel->logTree(Idx, true /* a_fReleaseLog */);
3862}
3863
3864
3865void
3866VBoxDbgStatsView::actAdjColumns()
3867{
3868 resizeColumnsToContent();
3869}
3870
3871
3872void
3873VBoxDbgStatsView::actFilter()
3874{
3875#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
3876 /*
3877 * Get the node it applies to.
3878 */
3879 QModelIndex Idx = m_pCurMenu ? m_CurIndex : currentIndex();
3880 if (Idx.isValid())
3881 Idx == myGetRootIndex();
3882 Idx = m_pProxyModel->mapToSource(Idx);
3883 PDBGGUISTATSNODE pNode = m_pVBoxModel->nodeFromIndex(Idx);
3884 if (pNode)
3885 {
3886 /*
3887 * Display dialog (modal).
3888 */
3889 VBoxDbgStatsFilterDialog Dialog(this, pNode);
3890 if (Dialog.exec() == QDialog::Accepted)
3891 {
3892 /** @todo it is possible that pNode is invalid now! */
3893 VBoxGuiStatsFilterData * const pOldFilter = pNode->pFilter;
3894 pNode->pFilter = Dialog.dupFilterData();
3895 if (pOldFilter)
3896 delete pOldFilter;
3897 m_pProxyModel->notifyFilterChanges();
3898 }
3899 }
3900#endif
3901}
3902
3903
3904
3905
3906
3907
3908/*
3909 *
3910 * V B o x D b g S t a t s F i l t e r D i a l o g
3911 * V B o x D b g S t a t s F i l t e r D i a l o g
3912 * V B o x D b g S t a t s F i l t e r D i a l o g
3913 *
3914 *
3915 */
3916
3917/* static */ QRegularExpression const VBoxDbgStatsFilterDialog::s_UInt64ValidatorRegExp("^([0-9]*|0[Xx][0-9a-fA-F]*)$");
3918
3919
3920/*static*/ QLineEdit *
3921VBoxDbgStatsFilterDialog::createUInt64LineEdit(uint64_t uValue)
3922{
3923 QLineEdit *pRet = new QLineEdit;
3924 if (uValue == 0 || uValue == UINT64_MAX)
3925 pRet->setText("");
3926 else
3927 pRet->setText(QString().number(uValue));
3928 pRet->setValidator(new QRegularExpressionValidator(s_UInt64ValidatorRegExp));
3929 return pRet;
3930}
3931
3932
3933VBoxDbgStatsFilterDialog::VBoxDbgStatsFilterDialog(QWidget *a_pParent, PCDBGGUISTATSNODE a_pNode)
3934 : QDialog(a_pParent)
3935{
3936 /* Set the window title. */
3937 static char s_szTitlePfx[] = "Filtering - ";
3938 char szTitle[1024 + 128];
3939 memcpy(szTitle, s_szTitlePfx, sizeof(s_szTitlePfx));
3940 VBoxDbgStatsModel::getNodePath(a_pNode, &szTitle[sizeof(s_szTitlePfx) - 1], sizeof(szTitle) - sizeof(s_szTitlePfx));
3941 setWindowTitle(szTitle);
3942
3943
3944 /* Copy the old data if any. */
3945 VBoxGuiStatsFilterData const * const pOldFilter = a_pNode->pFilter;
3946 if (pOldFilter)
3947 {
3948 m_Data.uMinValue = pOldFilter->uMinValue;
3949 m_Data.uMaxValue = pOldFilter->uMaxValue;
3950 if (pOldFilter->pRegexName)
3951 m_Data.pRegexName = new QRegularExpression(*pOldFilter->pRegexName);
3952 }
3953
3954 /* Configure the dialog... */
3955 QVBoxLayout *pMainLayout = new QVBoxLayout(this);
3956
3957 /* The value / average range: */
3958 QGroupBox *pValueAvgGrpBox = new QGroupBox("Value / Average");
3959 QGridLayout *pValAvgLayout = new QGridLayout;
3960 QLabel *pLabel = new QLabel("Min");
3961 m_pValueAvgMin = createUInt64LineEdit(m_Data.uMinValue);
3962 pLabel->setBuddy(m_pValueAvgMin);
3963 pValAvgLayout->addWidget(pLabel, 0, 0);
3964 pValAvgLayout->addWidget(m_pValueAvgMin, 0, 1);
3965
3966 pLabel = new QLabel("Max");
3967 m_pValueAvgMax = createUInt64LineEdit(m_Data.uMaxValue);
3968 pLabel->setBuddy(m_pValueAvgMax);
3969 pValAvgLayout->addWidget(pLabel, 1, 0);
3970 pValAvgLayout->addWidget(m_pValueAvgMax, 1, 1);
3971
3972 pValueAvgGrpBox->setLayout(pValAvgLayout);
3973 pMainLayout->addWidget(pValueAvgGrpBox);
3974
3975 /* The name filter. */
3976 QGroupBox *pNameGrpBox = new QGroupBox("Name RegExp");
3977 QHBoxLayout *pNameLayout = new QHBoxLayout();
3978 m_pNameRegExp = new QLineEdit;
3979 if (m_Data.pRegexName)
3980 m_pNameRegExp->setText(m_Data.pRegexName->pattern());
3981 else
3982 m_pNameRegExp->setText("");
3983 m_pNameRegExp->setToolTip("Regular expression matching basenames (no parent) to show.");
3984 pNameLayout->addWidget(m_pNameRegExp);
3985 pNameGrpBox->setLayout(pNameLayout);
3986 pMainLayout->addWidget(pNameGrpBox);
3987
3988 /* Buttons. */
3989 QDialogButtonBox *pButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
3990 pButtonBox->button(QDialogButtonBox::Ok)->setDefault(true);
3991 connect(pButtonBox, &QDialogButtonBox::rejected, this, &VBoxDbgStatsFilterDialog::reject);
3992 connect(pButtonBox, &QDialogButtonBox::accepted, this, &VBoxDbgStatsFilterDialog::validateAndAccept);
3993 pMainLayout->addWidget(pButtonBox);
3994}
3995
3996
3997VBoxDbgStatsFilterDialog::~VBoxDbgStatsFilterDialog()
3998{
3999 /* anything? */
4000}
4001
4002
4003VBoxGuiStatsFilterData *
4004VBoxDbgStatsFilterDialog::dupFilterData(void) const
4005{
4006 if (m_Data.isAllDefaults())
4007 return NULL;
4008 VBoxGuiStatsFilterData *pRet = new VBoxGuiStatsFilterData();
4009 pRet->uMinValue = m_Data.uMinValue;
4010 pRet->uMaxValue = m_Data.uMaxValue;
4011 if (m_Data.pRegexName)
4012 pRet->pRegexName = new QRegularExpression(*m_Data.pRegexName);
4013 return pRet;
4014}
4015
4016
4017uint64_t
4018VBoxDbgStatsFilterDialog::validateUInt64Field(QLineEdit const *a_pField, uint64_t a_uDefault,
4019 const char *a_pszField, QStringList *a_pLstErrors)
4020{
4021 QString Str = a_pField->text().trimmed();
4022 if (!Str.isEmpty())
4023 {
4024 QByteArray const StrAsUtf8 = Str.toUtf8();
4025 const char * const pszString = StrAsUtf8.constData();
4026 uint64_t uValue = a_uDefault;
4027 int vrc = RTStrToUInt64Full(pszString, 0, &uValue);
4028 if (vrc == VINF_SUCCESS)
4029 return uValue;
4030 char szMsg[128];
4031 RTStrPrintf(szMsg, sizeof(szMsg), "Invalid %s value: %Rrc - ", a_pszField, vrc);
4032 a_pLstErrors->append(QString(szMsg) + Str);
4033 }
4034
4035 return a_uDefault;
4036}
4037
4038
4039void
4040VBoxDbgStatsFilterDialog::validateAndAccept()
4041{
4042 QStringList LstErrors;
4043
4044 /* The numeric fields. */
4045 m_Data.uMinValue = validateUInt64Field(m_pValueAvgMin, 0, "minimum value/avg", &LstErrors);
4046 m_Data.uMaxValue = validateUInt64Field(m_pValueAvgMax, UINT64_MAX, "maximum value/avg", &LstErrors);
4047
4048 /* The name regexp. */
4049 QString Str = m_pNameRegExp->text().trimmed();
4050 if (!Str.isEmpty())
4051 {
4052 if (!m_Data.pRegexName)
4053 m_Data.pRegexName = new QRegularExpression();
4054 m_Data.pRegexName->setPattern(Str);
4055 if (!m_Data.pRegexName->isValid())
4056 LstErrors.append("Invalid regular expression");
4057 }
4058 else if (m_Data.pRegexName)
4059 {
4060 delete m_Data.pRegexName;
4061 m_Data.pRegexName = NULL;
4062 }
4063
4064 /* Dismiss the dialog if everything is fine, otherwise complain and keep it open. */
4065 if (LstErrors.isEmpty())
4066 emit accept();
4067 else
4068 {
4069 QMessageBox MsgBox(QMessageBox::Critical, "Invalid input", LstErrors.join("\n"), QMessageBox::Ok);
4070 MsgBox.exec();
4071 }
4072}
4073
4074
4075
4076
4077
4078/*
4079 *
4080 * V B o x D b g S t a t s
4081 * V B o x D b g S t a t s
4082 * V B o x D b g S t a t s
4083 *
4084 *
4085 */
4086
4087
4088VBoxDbgStats::VBoxDbgStats(VBoxDbgGui *a_pDbgGui, const char *pszFilter /*= NULL*/, const char *pszExpand /*= NULL*/,
4089 unsigned uRefreshRate/* = 0*/, QWidget *pParent/* = NULL*/)
4090 : VBoxDbgBaseWindow(a_pDbgGui, pParent, "Statistics")
4091 , m_PatStr(pszFilter), m_pPatCB(NULL), m_uRefreshRate(0), m_pTimer(NULL), m_pView(NULL)
4092{
4093 /* Delete dialog on close: */
4094 setAttribute(Qt::WA_DeleteOnClose);
4095
4096 /*
4097 * On top, a horizontal box with the pattern field, buttons and refresh interval.
4098 */
4099 QHBoxLayout *pHLayout = new QHBoxLayout;
4100
4101 QLabel *pLabel = new QLabel(" Pattern ");
4102 pHLayout->addWidget(pLabel);
4103 pLabel->setMaximumSize(pLabel->sizeHint());
4104 pLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
4105
4106 m_pPatCB = new QComboBox();
4107 pHLayout->addWidget(m_pPatCB);
4108 if (!m_PatStr.isEmpty())
4109 m_pPatCB->addItem(m_PatStr);
4110 m_pPatCB->setDuplicatesEnabled(false);
4111 m_pPatCB->setEditable(true);
4112 m_pPatCB->setCompleter(0);
4113 connect(m_pPatCB, SIGNAL(textActivated(const QString &)), this, SLOT(apply(const QString &)));
4114
4115 QPushButton *pPB = new QPushButton("&All");
4116 pHLayout->addWidget(pPB);
4117 pPB->setMaximumSize(pPB->sizeHint());
4118 connect(pPB, SIGNAL(clicked()), this, SLOT(applyAll()));
4119
4120 pLabel = new QLabel(" Interval ");
4121 pHLayout->addWidget(pLabel);
4122 pLabel->setMaximumSize(pLabel->sizeHint());
4123 pLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
4124
4125 QSpinBox *pSB = new QSpinBox();
4126 pHLayout->addWidget(pSB);
4127 pSB->setMinimum(0);
4128 pSB->setMaximum(60);
4129 pSB->setSingleStep(1);
4130 pSB->setValue(uRefreshRate);
4131 pSB->setSuffix(" s");
4132 pSB->setWrapping(false);
4133 pSB->setButtonSymbols(QSpinBox::PlusMinus);
4134 pSB->setMaximumSize(pSB->sizeHint());
4135 connect(pSB, SIGNAL(valueChanged(int)), this, SLOT(setRefresh(int)));
4136
4137#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
4138 QCheckBox *pCheckBox = new QCheckBox("Show unused");
4139 pHLayout->addWidget(pCheckBox);
4140 pCheckBox->setMaximumSize(pCheckBox->sizeHint());
4141 connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(sltShowUnusedRowsChanged(int)));
4142#endif
4143
4144 /*
4145 * Create the tree view and setup the layout.
4146 */
4147 VBoxDbgStatsModelVM *pModel = new VBoxDbgStatsModelVM(a_pDbgGui, m_PatStr, NULL, a_pDbgGui->getVMMFunctionTable());
4148#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
4149 VBoxDbgStatsSortFileProxyModel *pProxyModel = new VBoxDbgStatsSortFileProxyModel(this);
4150 m_pView = new VBoxDbgStatsView(a_pDbgGui, pModel, pProxyModel, this);
4151#else
4152 m_pView = new VBoxDbgStatsView(a_pDbgGui, pModel, NULL, this);
4153#endif
4154
4155 QWidget *pHBox = new QWidget;
4156 pHBox->setLayout(pHLayout);
4157
4158 QVBoxLayout *pVLayout = new QVBoxLayout;
4159 pVLayout->addWidget(pHBox);
4160 pVLayout->addWidget(m_pView);
4161 setLayout(pVLayout);
4162
4163 /*
4164 * Resize the columns.
4165 * Seems this has to be done with all nodes expanded.
4166 */
4167 m_pView->expandAll();
4168 m_pView->resizeColumnsToContent();
4169 m_pView->collapseAll();
4170
4171 if (pszExpand && *pszExpand)
4172 m_pView->expandMatching(QString(pszExpand));
4173
4174 /*
4175 * Create a refresh timer and start it.
4176 */
4177 m_pTimer = new QTimer(this);
4178 connect(m_pTimer, SIGNAL(timeout()), this, SLOT(refresh()));
4179 setRefresh(uRefreshRate);
4180
4181 /*
4182 * And some shortcuts.
4183 */
4184 m_pFocusToPat = new QAction("", this);
4185 m_pFocusToPat->setShortcut(QKeySequence("Ctrl+L"));
4186 addAction(m_pFocusToPat);
4187 connect(m_pFocusToPat, SIGNAL(triggered(bool)), this, SLOT(actFocusToPat()));
4188}
4189
4190
4191VBoxDbgStats::~VBoxDbgStats()
4192{
4193 if (m_pTimer)
4194 {
4195 delete m_pTimer;
4196 m_pTimer = NULL;
4197 }
4198
4199 if (m_pPatCB)
4200 {
4201 delete m_pPatCB;
4202 m_pPatCB = NULL;
4203 }
4204
4205 if (m_pView)
4206 {
4207 delete m_pView;
4208 m_pView = NULL;
4209 }
4210}
4211
4212
4213void
4214VBoxDbgStats::closeEvent(QCloseEvent *a_pCloseEvt)
4215{
4216 a_pCloseEvt->accept();
4217}
4218
4219
4220void
4221VBoxDbgStats::apply(const QString &Str)
4222{
4223 m_PatStr = Str;
4224 refresh();
4225}
4226
4227
4228void
4229VBoxDbgStats::applyAll()
4230{
4231 apply("");
4232}
4233
4234
4235
4236void
4237VBoxDbgStats::refresh()
4238{
4239 m_pView->updateStats(m_PatStr);
4240}
4241
4242
4243void
4244VBoxDbgStats::setRefresh(int iRefresh)
4245{
4246 if ((unsigned)iRefresh != m_uRefreshRate)
4247 {
4248 if (!m_uRefreshRate || iRefresh)
4249 m_pTimer->start(iRefresh * 1000);
4250 else
4251 m_pTimer->stop();
4252 m_uRefreshRate = iRefresh;
4253 }
4254}
4255
4256
4257void
4258VBoxDbgStats::actFocusToPat()
4259{
4260 if (!m_pPatCB->hasFocus())
4261 m_pPatCB->setFocus(Qt::ShortcutFocusReason);
4262}
4263
4264
4265void
4266VBoxDbgStats::sltShowUnusedRowsChanged(int a_iState)
4267{
4268#ifdef VBOXDBG_WITH_SORTED_AND_FILTERED_STATS
4269 m_pView->setShowUnusedRows(a_iState != Qt::Unchecked);
4270#else
4271 RT_NOREF_PV(a_iState);
4272#endif
4273}
4274
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette