VirtualBox

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

Last change on this file since 106690 was 106690, checked in by vboxsync, 3 months ago

scm

  • 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 106690 2024-10-25 11:40:21Z 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 <unordered_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 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();
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 State state() const
118 {
119 return m_State;
120 }
121
122 inline State resetState()
123 {
124 return m_State = Invalid;
125 }
126
127 com::Utf8Str updateLastAccessTime();
128 com::Utf8Str initIdleTime();
129 com::Utf8Str creationTimeStr() const;
130
131private:
132 com::Guid m_objId;
133 com::Guid m_classIID;
134 com::Utf8Str m_componentName;
135 RTTIMESPEC m_creationTime;//creation time
136 RTTIMESPEC m_idleTimeStart;//idle time beginning (ref counter is 1)
137 RTTIMESPEC m_lastAccessTime;//last access time
138 uint64_t m_lifeTime;//lifetime after creation in seconds, 0 - live till the VBoxSVC lives
139 uint64_t m_idleTime;//lifetime after out of usage in seconds, 0 - keep forever
140 bool m_fIdleTimeStart;//when ref counter of m_pIface is 1 or m_lifeTime exceeded
141 State m_State;//state may have only 2 variants Valid or Invalid. State has only one transition from Valid to Invalid
142 ComPtr<IUnknown> m_pIface;//keeps a reference to a tracked object
143
144private:
145 unsigned long i_checkRefCount(const Guid& aIID);
146
147 friend DECLCALLBACK(int) ObjectTracker::objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser);
148};
149
150/////////////////////////////////////////////////////////////////////////////
151// TrackedObjectsCollector
152/////////////////////////////////////////////////////////////////////////////
153class TrackedObjectsCollector
154{
155 /** Critical section protecting the data in TrackedObjectsCollector */
156 RTCRITSECT m_CritSectData;
157
158 std::set<com::Utf8Str> m_trackedObjectIds;//Full list of valid + invalid objects
159 std::set<com::Utf8Str> m_trackedInvalidObjectIds;//List of invalid objects only
160 std::unordered_map<std::string, TrackedObjectData> m_trackedObjectsData;//Mapping Object Id -> Object Data
161
162 uint64_t m_Added;//Counter of the added objects
163 uint64_t m_Released;//Counter of the released objects
164 bool m_fInitialized;//Sign whether TrackedObjectsCollector is initialized or not
165
166public:
167 TrackedObjectsCollector();
168 ~TrackedObjectsCollector();
169
170 bool init();//must be called after creation and before usage
171 bool uninit();
172
173 HRESULT setObj (const com::Utf8Str &aObjId,
174 const com::Utf8Str &aClassIID,
175 uint64_t lifeTime,
176 uint64_t afterLifeTime,
177 IUnknown* ptrIface);
178
179 HRESULT getObj (const com::Utf8Str &aObjId,
180 TrackedObjectData &aObjData,
181 bool fUpdate = true);
182
183 HRESULT initObjIdleTime (const com::Utf8Str& aObjId);
184
185 HRESULT removeObj (const com::Utf8Str &aObjId);
186
187 HRESULT getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap);
188
189 HRESULT getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap);
190
191 bool checkObj(const com::Utf8Str& aObjId);
192
193 HRESULT tryToRemoveObj(const com::Utf8Str& aObjId);
194
195 enum TrackedObjectsCollectorState { NormalOperation, ForceClearing, Deletion, Uninitialization };
196
197 HRESULT clear(TrackedObjectsCollectorState aState = NormalOperation);
198
199 HRESULT invalidateObj(const com::Utf8Str &aObjId);
200
201private:
202 int i_checkInitialization() const;
203 const TrackedObjectData& i_getObj (const com::Utf8Str& aObjId) const;
204 int i_getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap) const;
205 int i_getObjIdsByClassIID (const com::Guid& iid, std::vector<com::Utf8Str>& aObjIdMap) const;
206 bool i_checkObj(const com::Utf8Str& aObjId) const;
207 int i_clear();
208};
209#endif /* MAIN_INCLUDED_ObjectsTracker_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette