1 | /* $Id: ObjectsTracker.h 107254 2024-12-09 13:09:34Z 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 |
|
---|
44 | class ThreadTask;
|
---|
45 | class ObjectTracker;
|
---|
46 | class TrackedObjectsCollector;
|
---|
47 |
|
---|
48 | /////////////////////////////////////////////////////////////////////////////
|
---|
49 | // ObjectTracker
|
---|
50 | /////////////////////////////////////////////////////////////////////////////
|
---|
51 | class ObjectTracker
|
---|
52 | {
|
---|
53 | volatile bool fFinish;
|
---|
54 | RTTHREAD m_Thread;
|
---|
55 | Utf8Str m_strTaskName;
|
---|
56 |
|
---|
57 | public:
|
---|
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 | /////////////////////////////////////////////////////////////////////////////
|
---|
75 | class TrackedObjectData
|
---|
76 | {
|
---|
77 | public:
|
---|
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 | inline bool isIdleTimeStarted() const
|
---|
148 | {
|
---|
149 | return m_fIdleTimeStart;
|
---|
150 | }
|
---|
151 |
|
---|
152 | inline bool isLifeTimeExpired() const
|
---|
153 | {
|
---|
154 | return m_fLifeTimeExpired;
|
---|
155 | }
|
---|
156 |
|
---|
157 | com::Utf8Str updateLastAccessTime();
|
---|
158 | com::Utf8Str initIdleTime();
|
---|
159 | com::Utf8Str creationTimeStr() const;
|
---|
160 | TrackedObjectState_T deletionTime(PRTTIMESPEC aTime) const;
|
---|
161 | TrackedObjectState_T updateState(TrackedObjectState_T aNewState);
|
---|
162 |
|
---|
163 | private:
|
---|
164 | com::Guid m_objId;
|
---|
165 | com::Guid m_classIID;
|
---|
166 | com::Utf8Str m_componentName;
|
---|
167 | RTTIMESPEC m_creationTime;//creation time
|
---|
168 | RTTIMESPEC m_deletionTime;//deletion time (m_creationTime + m_lifeTime + m_idleTime)
|
---|
169 | RTTIMESPEC m_idleTimeStart;//idle time beginning (ref counter is 1)
|
---|
170 | RTTIMESPEC m_lastAccessTime;//last access time
|
---|
171 | uint64_t m_lifeTime;//lifetime after creation in seconds, 0 - live till the VBoxSVC lives
|
---|
172 | uint64_t m_idleTime;//lifetime after out of usage in seconds, 0 - keep forever
|
---|
173 | bool m_fIdleTimeStart;//when ref counter of m_pIface is 1 or m_lifeTime exceeded
|
---|
174 | bool m_fLifeTimeExpired;//set to True only one time during the whole object life
|
---|
175 | TrackedObjectState_T m_state;
|
---|
176 | ComPtr<IUnknown> m_pIface;//keeps a reference to a tracked object
|
---|
177 |
|
---|
178 | private:
|
---|
179 | unsigned long i_checkRefCount(const Guid& aIID);
|
---|
180 |
|
---|
181 | friend DECLCALLBACK(int) ObjectTracker::objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
|
---|
182 | };
|
---|
183 |
|
---|
184 |
|
---|
185 | /////////////////////////////////////////////////////////////////////////////
|
---|
186 | // TrackedObjectsCollector
|
---|
187 | /////////////////////////////////////////////////////////////////////////////
|
---|
188 | class TrackedObjectsCollector
|
---|
189 | {
|
---|
190 | /** Critical section protecting the data in TrackedObjectsCollector */
|
---|
191 | RTCRITSECT m_CritSectData;
|
---|
192 |
|
---|
193 | std::set<com::Utf8Str> m_trackedObjectIds;//Full list of valid + invalid objects
|
---|
194 | std::set<com::Utf8Str> m_trackedInvalidObjectIds;//List of invalid objects only
|
---|
195 | std::map<com::Utf8Str, TrackedObjectData> m_trackedObjectsData;//Mapping Object Id -> Object Data
|
---|
196 |
|
---|
197 | uint64_t m_Added;//Counter of the added objects
|
---|
198 | uint64_t m_Released;//Counter of the released objects
|
---|
199 | bool m_fInitialized;//Sign whether TrackedObjectsCollector is initialized or not
|
---|
200 |
|
---|
201 | public:
|
---|
202 | TrackedObjectsCollector();
|
---|
203 | ~TrackedObjectsCollector();
|
---|
204 |
|
---|
205 | bool init();//must be called after creation and before usage
|
---|
206 | bool uninit();
|
---|
207 |
|
---|
208 | HRESULT setObj (const com::Utf8Str &aObjId,
|
---|
209 | const com::Utf8Str &aClassIID,
|
---|
210 | uint64_t lifeTime,
|
---|
211 | uint64_t afterLifeTime,
|
---|
212 | IUnknown* ptrIface);
|
---|
213 |
|
---|
214 | HRESULT updateObj (const TrackedObjectData& aObjData);
|
---|
215 |
|
---|
216 | HRESULT getObj (const com::Utf8Str &aObjId,
|
---|
217 | TrackedObjectData &aObjData,
|
---|
218 | bool fUpdate = true);
|
---|
219 |
|
---|
220 | HRESULT initObjIdleTime (const com::Utf8Str& aObjId);
|
---|
221 |
|
---|
222 | HRESULT removeObj (const com::Utf8Str &aObjId);
|
---|
223 |
|
---|
224 | HRESULT getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap);
|
---|
225 |
|
---|
226 | HRESULT getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap);
|
---|
227 |
|
---|
228 | bool checkObj(const com::Utf8Str& aObjId);
|
---|
229 |
|
---|
230 | HRESULT tryToRemoveObj(const com::Utf8Str& aObjId);
|
---|
231 |
|
---|
232 | enum TrackedObjectsCollectorState { NormalOperation, ForceClearing, Deletion, Uninitialization };
|
---|
233 |
|
---|
234 | HRESULT clear(TrackedObjectsCollectorState aState = NormalOperation);
|
---|
235 |
|
---|
236 | HRESULT invalidateObj(const com::Utf8Str &aObjId);
|
---|
237 |
|
---|
238 | private:
|
---|
239 | int i_checkInitialization() const;
|
---|
240 | const TrackedObjectData& i_getObj (const com::Utf8Str& aObjId) const;
|
---|
241 | int i_getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap) const;
|
---|
242 | int i_getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap) const;
|
---|
243 | bool i_checkObj(const com::Utf8Str& aObjId) const;
|
---|
244 | int i_clear();
|
---|
245 | };
|
---|
246 | #endif /* !MAIN_INCLUDED_ObjectsTracker_h */
|
---|