1 | /* $Id: ObjectsTracker.h 106901 2024-11-08 23:39:18Z 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 | /** The string representation for object IDs in the internal map.
|
---|
153 | * @todo r=bird: Why are you using std::string here? We use
|
---|
154 | * should use com::Utf8Str where possible. In this case, though, it's a
|
---|
155 | * com::Guid which would have much more efficient storage compared
|
---|
156 | * to both the string variants... */
|
---|
157 | #if 0 /* The solaris VM seems to have trouble with this or something related to it. */
|
---|
158 | typedef std::string ObjIdString_T;
|
---|
159 | #else
|
---|
160 | typedef com::Utf8Str ObjIdString_T;
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | /////////////////////////////////////////////////////////////////////////////
|
---|
164 | // TrackedObjectsCollector
|
---|
165 | /////////////////////////////////////////////////////////////////////////////
|
---|
166 | class TrackedObjectsCollector
|
---|
167 | {
|
---|
168 | /** Critical section protecting the data in TrackedObjectsCollector */
|
---|
169 | RTCRITSECT m_CritSectData;
|
---|
170 |
|
---|
171 | std::set<com::Utf8Str> m_trackedObjectIds;//Full list of valid + invalid objects
|
---|
172 | std::set<com::Utf8Str> m_trackedInvalidObjectIds;//List of invalid objects only
|
---|
173 | std::map<ObjIdString_T, TrackedObjectData> m_trackedObjectsData;//Mapping Object Id -> Object Data
|
---|
174 |
|
---|
175 | uint64_t m_Added;//Counter of the added objects
|
---|
176 | uint64_t m_Released;//Counter of the released objects
|
---|
177 | bool m_fInitialized;//Sign whether TrackedObjectsCollector is initialized or not
|
---|
178 |
|
---|
179 | public:
|
---|
180 | TrackedObjectsCollector();
|
---|
181 | ~TrackedObjectsCollector();
|
---|
182 |
|
---|
183 | bool init();//must be called after creation and before usage
|
---|
184 | bool uninit();
|
---|
185 |
|
---|
186 | HRESULT setObj (const com::Utf8Str &aObjId,
|
---|
187 | const com::Utf8Str &aClassIID,
|
---|
188 | uint64_t lifeTime,
|
---|
189 | uint64_t afterLifeTime,
|
---|
190 | IUnknown* ptrIface);
|
---|
191 |
|
---|
192 | HRESULT getObj (const com::Utf8Str &aObjId,
|
---|
193 | TrackedObjectData &aObjData,
|
---|
194 | bool fUpdate = true);
|
---|
195 |
|
---|
196 | HRESULT initObjIdleTime (const com::Utf8Str& aObjId);
|
---|
197 |
|
---|
198 | HRESULT removeObj (const com::Utf8Str &aObjId);
|
---|
199 |
|
---|
200 | HRESULT getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap);
|
---|
201 |
|
---|
202 | HRESULT getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap);
|
---|
203 |
|
---|
204 | bool checkObj(const com::Utf8Str& aObjId);
|
---|
205 |
|
---|
206 | HRESULT tryToRemoveObj(const com::Utf8Str& aObjId);
|
---|
207 |
|
---|
208 | enum TrackedObjectsCollectorState { NormalOperation, ForceClearing, Deletion, Uninitialization };
|
---|
209 |
|
---|
210 | HRESULT clear(TrackedObjectsCollectorState aState = NormalOperation);
|
---|
211 |
|
---|
212 | HRESULT invalidateObj(const com::Utf8Str &aObjId);
|
---|
213 |
|
---|
214 | private:
|
---|
215 | int i_checkInitialization() const;
|
---|
216 | const TrackedObjectData& i_getObj (const com::Utf8Str& aObjId) const;
|
---|
217 | int i_getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap) const;
|
---|
218 | int i_getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap) const;
|
---|
219 | bool i_checkObj(const com::Utf8Str& aObjId) const;
|
---|
220 | int i_clear();
|
---|
221 | };
|
---|
222 | #endif /* !MAIN_INCLUDED_ObjectsTracker_h */
|
---|