1 | /* $Id: ObjectsTracker.h 106907 2024-11-09 01:43:54Z 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 | enum State { Invalid, Valid };
|
---|
79 |
|
---|
80 | TrackedObjectData();
|
---|
81 |
|
---|
82 | explicit
|
---|
83 | TrackedObjectData(const com::Guid &aObjId,
|
---|
84 | const com::Guid &aClassIID,
|
---|
85 | uint64_t aLifeTime,
|
---|
86 | uint64_t aIdleTime,
|
---|
87 | IUnknown* aPtr);
|
---|
88 |
|
---|
89 | TrackedObjectData(const TrackedObjectData & that);
|
---|
90 |
|
---|
91 | ~TrackedObjectData();
|
---|
92 | TrackedObjectData& operator =(const TrackedObjectData &that);
|
---|
93 |
|
---|
94 | inline const ComPtr<IUnknown>& getInterface() const
|
---|
95 | {
|
---|
96 | return m_pIface;
|
---|
97 | }
|
---|
98 |
|
---|
99 | inline com::Guid objectId() const
|
---|
100 | {
|
---|
101 | return m_objId;
|
---|
102 | }
|
---|
103 |
|
---|
104 | inline com::Guid classIID() const
|
---|
105 | {
|
---|
106 | return m_classIID;
|
---|
107 | }
|
---|
108 |
|
---|
109 | inline com::Utf8Str objectIdStr() const
|
---|
110 | {
|
---|
111 | return m_objId.toString();
|
---|
112 | }
|
---|
113 |
|
---|
114 | inline com::Utf8Str classIIDStr() const
|
---|
115 | {
|
---|
116 | return m_classIID.toString();
|
---|
117 | }
|
---|
118 |
|
---|
119 | inline State state() const
|
---|
120 | {
|
---|
121 | return m_State;
|
---|
122 | }
|
---|
123 |
|
---|
124 | inline State resetState()
|
---|
125 | {
|
---|
126 | return m_State = Invalid;
|
---|
127 | }
|
---|
128 |
|
---|
129 | com::Utf8Str updateLastAccessTime();
|
---|
130 | com::Utf8Str initIdleTime();
|
---|
131 | com::Utf8Str creationTimeStr() const;
|
---|
132 |
|
---|
133 | private:
|
---|
134 | com::Guid m_objId;
|
---|
135 | com::Guid m_classIID;
|
---|
136 | com::Utf8Str m_componentName;
|
---|
137 | RTTIMESPEC m_creationTime;//creation time
|
---|
138 | RTTIMESPEC m_idleTimeStart;//idle time beginning (ref counter is 1)
|
---|
139 | RTTIMESPEC m_lastAccessTime;//last access time
|
---|
140 | uint64_t m_lifeTime;//lifetime after creation in seconds, 0 - live till the VBoxSVC lives
|
---|
141 | uint64_t m_idleTime;//lifetime after out of usage in seconds, 0 - keep forever
|
---|
142 | bool m_fIdleTimeStart;//when ref counter of m_pIface is 1 or m_lifeTime exceeded
|
---|
143 | State m_State;//state may have only 2 variants Valid or Invalid. State has only one transition from Valid to Invalid
|
---|
144 | ComPtr<IUnknown> m_pIface;//keeps a reference to a tracked object
|
---|
145 |
|
---|
146 | private:
|
---|
147 | unsigned long i_checkRefCount(const Guid& aIID);
|
---|
148 |
|
---|
149 | friend DECLCALLBACK(int) ObjectTracker::objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
|
---|
150 | };
|
---|
151 |
|
---|
152 |
|
---|
153 | /////////////////////////////////////////////////////////////////////////////
|
---|
154 | // TrackedObjectsCollector
|
---|
155 | /////////////////////////////////////////////////////////////////////////////
|
---|
156 | class TrackedObjectsCollector
|
---|
157 | {
|
---|
158 | /** Critical section protecting the data in TrackedObjectsCollector */
|
---|
159 | RTCRITSECT m_CritSectData;
|
---|
160 |
|
---|
161 | std::set<com::Utf8Str> m_trackedObjectIds;//Full list of valid + invalid objects
|
---|
162 | std::set<com::Utf8Str> m_trackedInvalidObjectIds;//List of invalid objects only
|
---|
163 | std::map<com::Utf8Str, TrackedObjectData> m_trackedObjectsData;//Mapping Object Id -> Object Data
|
---|
164 |
|
---|
165 | uint64_t m_Added;//Counter of the added objects
|
---|
166 | uint64_t m_Released;//Counter of the released objects
|
---|
167 | bool m_fInitialized;//Sign whether TrackedObjectsCollector is initialized or not
|
---|
168 |
|
---|
169 | public:
|
---|
170 | TrackedObjectsCollector();
|
---|
171 | ~TrackedObjectsCollector();
|
---|
172 |
|
---|
173 | bool init();//must be called after creation and before usage
|
---|
174 | bool uninit();
|
---|
175 |
|
---|
176 | HRESULT setObj (const com::Utf8Str &aObjId,
|
---|
177 | const com::Utf8Str &aClassIID,
|
---|
178 | uint64_t lifeTime,
|
---|
179 | uint64_t afterLifeTime,
|
---|
180 | IUnknown* ptrIface);
|
---|
181 |
|
---|
182 | HRESULT getObj (const com::Utf8Str &aObjId,
|
---|
183 | TrackedObjectData &aObjData,
|
---|
184 | bool fUpdate = true);
|
---|
185 |
|
---|
186 | HRESULT initObjIdleTime (const com::Utf8Str& aObjId);
|
---|
187 |
|
---|
188 | HRESULT removeObj (const com::Utf8Str &aObjId);
|
---|
189 |
|
---|
190 | HRESULT getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap);
|
---|
191 |
|
---|
192 | HRESULT getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap);
|
---|
193 |
|
---|
194 | bool checkObj(const com::Utf8Str& aObjId);
|
---|
195 |
|
---|
196 | HRESULT tryToRemoveObj(const com::Utf8Str& aObjId);
|
---|
197 |
|
---|
198 | enum TrackedObjectsCollectorState { NormalOperation, ForceClearing, Deletion, Uninitialization };
|
---|
199 |
|
---|
200 | HRESULT clear(TrackedObjectsCollectorState aState = NormalOperation);
|
---|
201 |
|
---|
202 | HRESULT invalidateObj(const com::Utf8Str &aObjId);
|
---|
203 |
|
---|
204 | private:
|
---|
205 | int i_checkInitialization() const;
|
---|
206 | const TrackedObjectData& i_getObj (const com::Utf8Str& aObjId) const;
|
---|
207 | int i_getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap) const;
|
---|
208 | int i_getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap) const;
|
---|
209 | bool i_checkObj(const com::Utf8Str& aObjId) const;
|
---|
210 | int i_clear();
|
---|
211 | };
|
---|
212 | #endif /* !MAIN_INCLUDED_ObjectsTracker_h */
|
---|