VirtualBox

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

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

scm

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