VirtualBox

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

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

VBoxDbg,FE/Qt: Made it possible to configure the sub-tree filtering via the command line. bugref:10376

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