VirtualBox

source: vbox/trunk/src/VBox/Main/include/ObjectsTracker.h@ 106624

Last change on this file since 106624 was 106624, checked in by vboxsync, 6 months ago

Jira task VBP-1187. Added ObjectsTracker.h and ObjectsTracker.cpp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/* $Id: ObjectsTracker.h 106624 2024-10-23 15:37:20Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox Object tracker definitions
5 */
6
7/*
8 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
9 *
10 * This file is part of VirtualBox base platform packages, as
11 * available from https://www.virtualbox.org.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation, in version 3 of the
16 * License.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses>.
25 *
26 * SPDX-License-Identifier: GPL-3.0-only
27 */
28
29#ifndef MAIN_INCLUDED_ObjectsTracker_h
30#define MAIN_INCLUDED_ObjectsTracker_h
31#ifndef RT_WITHOUT_PRAGMA_ONCE
32# pragma once
33#endif
34
35#include <set>
36#include <unordered_map>
37#include <vector>
38#include <string>
39
40#include <iprt/time.h>
41#include <iprt/cpp/utils.h>
42
43#include "ThreadTask.h"
44
45class ThreadTask;
46class ObjectTracker;
47class TrackedObjectsCollector;
48
49/////////////////////////////////////////////////////////////////////////////
50// ObjectTracker
51/////////////////////////////////////////////////////////////////////////////
52class ObjectTracker
53{
54 volatile bool fFinish;
55 RTTHREAD m_Thread;
56 Utf8Str m_strTaskName;
57
58public:
59 ObjectTracker(): fFinish(false), m_Thread(NIL_RTTHREAD), m_strTaskName("ObjTracker"){};
60 ObjectTracker(PRTTHREAD pThread): fFinish(false), m_Thread(*pThread){};
61
62 ~ObjectTracker();
63
64 inline Utf8Str getTaskName() const { return m_strTaskName; }
65 bool init();
66 bool finish();
67 bool isFinished();
68 int createThread();
69
70 static DECLCALLBACK(int) objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
71};
72
73/////////////////////////////////////////////////////////////////////////////
74// TrackedObjectData
75/////////////////////////////////////////////////////////////////////////////
76class TrackedObjectData
77{
78public:
79 enum State { Invalid, Valid };
80
81 TrackedObjectData();
82
83 explicit
84 TrackedObjectData(const com::Guid &aObjId,
85 const com::Guid &aClassIID,
86 uint64_t aLifeTime,
87 uint64_t aIdleTime,
88 IUnknown* aPtr);
89
90 ~TrackedObjectData();
91 TrackedObjectData& operator =(const TrackedObjectData &that);
92
93 inline const ComPtr<IUnknown>& getInterface() const
94 {
95 return m_pIface;
96 }
97
98 inline com::Guid objectId() const
99 {
100 return m_objId;
101 }
102
103 inline com::Guid classIID() const
104 {
105 return m_classIID;
106 }
107
108 inline com::Utf8Str objectIdStr() const
109 {
110 return m_objId.toString();
111 }
112
113 inline com::Utf8Str classIIDStr() const
114 {
115 return m_classIID.toString();
116 }
117
118 inline State state() const
119 {
120 return m_State;
121 }
122
123 inline State resetState()
124 {
125 return m_State = Invalid;
126 }
127
128 com::Utf8Str updateLastAccessTime();
129 com::Utf8Str initIdleTime();
130 com::Utf8Str creationTimeStr() const;
131
132private:
133 com::Guid m_objId;
134 com::Guid m_classIID;
135 com::Utf8Str m_componentName;
136 RTTIMESPEC m_creationTime;//creation time
137 RTTIMESPEC m_idleTimeStart;//idle time beginning (ref counter is 1)
138 RTTIMESPEC m_lastAccessTime;//last access time
139 uint64_t m_lifeTime;//lifetime after creation in seconds, 0 - live till the VBoxSVC lives
140 uint64_t m_idleTime;//lifetime after out of usage in seconds, 0 - keep forever
141 bool m_fIdleTimeStart;//when ref counter of m_pIface is 1 or m_lifeTime exceeded
142 State m_State;//state may have only 2 variants Valid or Invalid. State has only one transition from Valid to Invalid
143 ComPtr<IUnknown> m_pIface;//keeps a reference to a tracked object
144
145private:
146 unsigned long i_checkRefCount(const Guid& aIID);
147
148 friend DECLCALLBACK(int) ObjectTracker::objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
149};
150
151/////////////////////////////////////////////////////////////////////////////
152// TrackedObjectsCollector
153/////////////////////////////////////////////////////////////////////////////
154class TrackedObjectsCollector
155{
156 /** Critical section protecting the data in TrackedObjectsCollector */
157 RTCRITSECT m_CritSectData;
158
159 std::set<com::Utf8Str> m_trackedObjectIds;//Full list of valid + invalid objects
160 std::set<com::Utf8Str> m_trackedInvalidObjectIds;//List of invalid objects only
161 std::unordered_map<std::string, TrackedObjectData> m_trackedObjectsData;//Mapping Object Id -> Object Data
162
163 uint64_t m_Added;//Counter of the added objects
164 uint64_t m_Released;//Counter of the released objects
165 bool m_fInitialized;//Sign whether TrackedObjectsCollector is initialized or not
166
167public:
168 TrackedObjectsCollector();
169 ~TrackedObjectsCollector();
170
171 bool init();//must be called after creation and before usage
172 bool uninit();
173
174 HRESULT setObj (const com::Utf8Str &aObjId,
175 const com::Utf8Str &aClassIID,
176 uint64_t lifeTime,
177 uint64_t afterLifeTime,
178 IUnknown* ptrIface);
179
180 HRESULT getObj (const com::Utf8Str &aObjId,
181 TrackedObjectData &aObjData,
182 bool fUpdate = true);
183
184 HRESULT initObjIdleTime (const com::Utf8Str& aObjId);
185
186 HRESULT removeObj (const com::Utf8Str &aObjId);
187
188 HRESULT getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap);
189
190 HRESULT getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap);
191
192 bool checkObj(const com::Utf8Str& aObjId);
193
194 HRESULT tryToRemoveObj(const com::Utf8Str& aObjId);
195
196 enum TrackedObjectsCollectorState { NormalOperation, ForceClearing, Deletion, Uninitialization };
197
198 HRESULT clear(TrackedObjectsCollectorState aState = NormalOperation);
199
200 HRESULT invalidateObj(const com::Utf8Str &aObjId);
201
202private:
203 int i_checkInitialization() const;
204 const TrackedObjectData& i_getObj (const com::Utf8Str& aObjId) const;
205 int i_getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap) const;
206 int i_getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap) const;
207 bool i_checkObj(const com::Utf8Str& aObjId) const;
208 int i_clear();
209};
210#endif //MAIN_INCLUDED_ObjectsTracker_h
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