VirtualBox

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

Last change on this file since 107240 was 107240, checked in by vboxsync, 5 months ago

bugref:10806. New TrackedObjectState enum usage. Added member m_deletionTime. Added the functions updateState, deletionTime. Added getters idleTime, lifeTime, deletionTime, creationTime. Fixed the function resetState and getter state.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/* $Id: ObjectsTracker.h 107240 2024-12-06 10:46:24Z vboxsync $ */
2/** @file
3 * VirtualBox Object tracker definitions
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
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#ifndef MAIN_INCLUDED_ObjectsTracker_h
29#define MAIN_INCLUDED_ObjectsTracker_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <set>
35#include <map>
36#include <vector>
37#include <string>
38
39#include <iprt/time.h>
40#include <iprt/cpp/utils.h>
41
42#include "ThreadTask.h"
43
44class ThreadTask;
45class ObjectTracker;
46class TrackedObjectsCollector;
47
48/////////////////////////////////////////////////////////////////////////////
49// ObjectTracker
50/////////////////////////////////////////////////////////////////////////////
51class ObjectTracker
52{
53 volatile bool fFinish;
54 RTTHREAD m_Thread;
55 Utf8Str m_strTaskName;
56
57public:
58 ObjectTracker(): fFinish(false), m_Thread(NIL_RTTHREAD), m_strTaskName("ObjTracker"){};
59 ObjectTracker(PRTTHREAD pThread): fFinish(false), m_Thread(*pThread){};
60
61 ~ObjectTracker();
62
63 inline Utf8Str getTaskName() const { return m_strTaskName; }
64 bool init();
65 bool finish();
66 bool isFinished();
67 int createThread();
68
69 static DECLCALLBACK(int) objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
70};
71
72/////////////////////////////////////////////////////////////////////////////
73// TrackedObjectData
74/////////////////////////////////////////////////////////////////////////////
75class TrackedObjectData
76{
77public:
78 TrackedObjectData();
79
80 explicit
81 TrackedObjectData(const com::Guid &aObjId,
82 const com::Guid &aClassIID,
83 uint64_t aLifeTime,
84 uint64_t aIdleTime,
85 IUnknown* aPtr);
86
87 TrackedObjectData(const TrackedObjectData & that);
88
89 ~TrackedObjectData();
90 TrackedObjectData& operator =(const TrackedObjectData &that);
91
92 inline const ComPtr<IUnknown>& getInterface() const
93 {
94 return m_pIface;
95 }
96
97 inline com::Guid objectId() const
98 {
99 return m_objId;
100 }
101
102 inline com::Guid classIID() const
103 {
104 return m_classIID;
105 }
106
107 inline com::Utf8Str objectIdStr() const
108 {
109 return m_objId.toString();
110 }
111
112 inline com::Utf8Str classIIDStr() const
113 {
114 return m_classIID.toString();
115 }
116
117 inline TrackedObjectState_T state() const
118 {
119 return m_state;
120 }
121
122 inline TrackedObjectState_T resetState()
123 {
124 return m_state = TrackedObjectState_Invalid;
125 }
126
127 inline RTTIMESPEC creationTime() const
128 {
129 return m_creationTime;
130 }
131
132 inline RTTIMESPEC deletionTime() const
133 {
134 return m_deletionTime;
135 }
136
137 inline uint64_t lifeTime() const
138 {
139 return m_lifeTime;
140 }
141
142 inline uint64_t idleTime() const
143 {
144 return m_idleTime;
145 }
146
147 com::Utf8Str updateLastAccessTime();
148 com::Utf8Str initIdleTime();
149 com::Utf8Str creationTimeStr() const;
150 TrackedObjectState_T deletionTime(PRTTIMESPEC aTime) const;
151 TrackedObjectState_T updateState(TrackedObjectState_T aNewState);
152
153private:
154 com::Guid m_objId;
155 com::Guid m_classIID;
156 com::Utf8Str m_componentName;
157 RTTIMESPEC m_creationTime;//creation time
158 RTTIMESPEC m_idleTimeStart;//idle time beginning (ref counter is 1)
159 RTTIMESPEC m_deletionTime;//deletion time (m_creationTime + m_lifeTime + m_idleTime)
160 RTTIMESPEC m_lastAccessTime;//last access time
161 uint64_t m_lifeTime;//lifetime after creation in seconds, 0 - live till the VBoxSVC lives
162 uint64_t m_idleTime;//lifetime after out of usage in seconds, 0 - keep forever
163 bool m_fIdleTimeStart;//when ref counter of m_pIface is 1 or m_lifeTime exceeded
164 TrackedObjectState_T m_state;
165 ComPtr<IUnknown> m_pIface;//keeps a reference to a tracked object
166
167private:
168 unsigned long i_checkRefCount(const Guid& aIID);
169
170 friend DECLCALLBACK(int) ObjectTracker::objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
171};
172
173
174/////////////////////////////////////////////////////////////////////////////
175// TrackedObjectsCollector
176/////////////////////////////////////////////////////////////////////////////
177class TrackedObjectsCollector
178{
179 /** Critical section protecting the data in TrackedObjectsCollector */
180 RTCRITSECT m_CritSectData;
181
182 std::set<com::Utf8Str> m_trackedObjectIds;//Full list of valid + invalid objects
183 std::set<com::Utf8Str> m_trackedInvalidObjectIds;//List of invalid objects only
184 std::map<com::Utf8Str, TrackedObjectData> m_trackedObjectsData;//Mapping Object Id -> Object Data
185
186 uint64_t m_Added;//Counter of the added objects
187 uint64_t m_Released;//Counter of the released objects
188 bool m_fInitialized;//Sign whether TrackedObjectsCollector is initialized or not
189
190public:
191 TrackedObjectsCollector();
192 ~TrackedObjectsCollector();
193
194 bool init();//must be called after creation and before usage
195 bool uninit();
196
197 HRESULT setObj (const com::Utf8Str &aObjId,
198 const com::Utf8Str &aClassIID,
199 uint64_t lifeTime,
200 uint64_t afterLifeTime,
201 IUnknown* ptrIface);
202
203 HRESULT getObj (const com::Utf8Str &aObjId,
204 TrackedObjectData &aObjData,
205 bool fUpdate = true);
206
207 HRESULT initObjIdleTime (const com::Utf8Str& aObjId);
208
209 HRESULT removeObj (const com::Utf8Str &aObjId);
210
211 HRESULT getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap);
212
213 HRESULT getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap);
214
215 bool checkObj(const com::Utf8Str& aObjId);
216
217 HRESULT tryToRemoveObj(const com::Utf8Str& aObjId);
218
219 enum TrackedObjectsCollectorState { NormalOperation, ForceClearing, Deletion, Uninitialization };
220
221 HRESULT clear(TrackedObjectsCollectorState aState = NormalOperation);
222
223 HRESULT invalidateObj(const com::Utf8Str &aObjId);
224
225private:
226 int i_checkInitialization() const;
227 const TrackedObjectData& i_getObj (const com::Utf8Str& aObjId) const;
228 int i_getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap) const;
229 int i_getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap) const;
230 bool i_checkObj(const com::Utf8Str& aObjId) const;
231 int i_clear();
232};
233#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