VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/ObjectsTracker.cpp@ 106904

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

Main/ObjectsTracker.h/cpp: Another build fix attempt - the extpack solaris VM doesn't know the C++11 cbegin/cend methods either. sigh. jiraref:VBP-1187

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.5 KB
Line 
1/* $Id: ObjectsTracker.cpp 106904 2024-11-09 01:02:33Z vboxsync $ */
2/** @file
3 * VirtualBox Object tracker implementation
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#define LOG_GROUP LOG_GROUP_MAIN
29
30#include "LoggingNew.h"
31#include "VirtualBoxBase.h"
32#include "ObjectsTracker.h"
33#include <iprt/log.h>
34#include <iprt/stream.h>
35#include <iprt/time.h>
36#include <iprt/asm.h>
37#include <stdexcept>
38
39typedef std::map<com::Utf8Str, TrackedObjectData>::iterator IterTrObjData_T;
40typedef std::map<com::Utf8Str, TrackedObjectData>::const_iterator ConstIterTrObjData_T;
41
42/////////////////////////////////////////////////////////////////////////////
43// TrackedObjectData
44/////////////////////////////////////////////////////////////////////////////
45TrackedObjectData::TrackedObjectData():
46 m_componentName("noname"),
47 m_lifeTime(0),
48 m_idleTime(1),
49 m_State(TrackedObjectData::Invalid),
50 m_pIface(NULL)
51 {
52}
53
54TrackedObjectData::TrackedObjectData(const com::Guid &aObjId,
55 const com::Guid &aClassIID,
56 uint64_t aLifeTime,
57 uint64_t aIdleTime,
58 IUnknown* aPtr):
59
60 m_objId(aObjId),
61 m_classIID(aClassIID),
62 m_componentName("noname"),
63 m_lifeTime(aLifeTime),
64 m_idleTime(aIdleTime),
65 m_fIdleTimeStart(false),
66 m_pIface(aPtr)
67{
68 RTTimeNow(unconst(&m_creationTime));
69 m_lastAccessTime = m_creationTime;
70 m_State = TrackedObjectData::Valid;
71 Log2(("%s constructor \n", __FUNCTION__));
72}
73
74TrackedObjectData::TrackedObjectData(const TrackedObjectData & that)
75{
76 LogFlowFuncEnter();
77 if (this != &that)
78 {
79 m_objId = that.m_objId;
80 m_classIID = that.m_classIID;
81 m_componentName = that.m_componentName;
82 m_lifeTime = that.m_lifeTime;
83 m_idleTime = that.m_idleTime;
84 m_pIface = that.m_pIface;
85 m_creationTime = that.m_creationTime;
86 m_lastAccessTime = that.m_lastAccessTime;
87 m_idleTimeStart = that.m_idleTimeStart;
88 m_fIdleTimeStart = that.m_fIdleTimeStart;
89 m_State = that.m_State;
90 }
91}
92
93TrackedObjectData::~TrackedObjectData()
94{
95 Log2(("%s destructor\n", __FUNCTION__));
96 Log2(("DELETED Object %s (class IID %s)\n",
97 m_objId.toString().c_str(),
98 m_classIID.toString().c_str()
99 ));
100}
101
102TrackedObjectData &TrackedObjectData::operator=(const TrackedObjectData & that)
103{
104 LogFlowFuncEnter();
105 if (this != &that)
106 {
107 m_objId = that.m_objId;
108 m_classIID = that.m_classIID;
109 m_componentName = that.m_componentName;
110 m_lifeTime = that.m_lifeTime;
111 m_idleTime = that.m_idleTime;
112 m_pIface = that.m_pIface;
113 m_creationTime = that.m_creationTime;
114 m_lastAccessTime = that.m_lastAccessTime;
115 m_idleTimeStart = that.m_idleTimeStart;
116 m_fIdleTimeStart = that.m_fIdleTimeStart;
117 m_State = that.m_State;
118 }
119
120 return *this;
121}
122
123com::Utf8Str TrackedObjectData::updateLastAccessTime()
124{
125 RTTimeNow(&m_lastAccessTime);
126
127 char szTime[RTTIME_STR_LEN];
128 RTTimeSpecToString(&m_lastAccessTime, szTime, sizeof(szTime));
129 return com::Utf8Str(szTime);
130}
131
132/** @todo r=bird: why on earth does this return a string? */
133com::Utf8Str TrackedObjectData::initIdleTime()
134{
135 if (!m_fIdleTimeStart)
136 {
137 RTTimeNow(unconst(&m_idleTimeStart));
138 m_fIdleTimeStart = true;
139 }
140
141 char szTime[RTTIME_STR_LEN];
142 RTTimeSpecToString(&m_idleTimeStart, szTime, sizeof(szTime));
143 return com::Utf8Str(szTime);
144}
145
146com::Utf8Str TrackedObjectData::creationTimeStr() const
147{
148 char szCreationTime[RTTIME_STR_LEN];
149 RTTimeSpecToString(&m_creationTime, szCreationTime, sizeof(szCreationTime));
150
151 return com::Utf8Str(szCreationTime);
152}
153
154/* No locking here, be aware */
155unsigned long TrackedObjectData::i_checkRefCount(const Guid& aIID)
156{
157 ULONG cRefs = 0;
158 if (aIID == m_classIID)
159 {
160 m_pIface->AddRef();
161 cRefs = m_pIface->Release();
162
163 Log2(("**** Object %s (class IID %s) (refcount %lu) ****\n",
164 m_objId.toString().c_str(),
165 m_classIID.toString().c_str(),
166 cRefs
167 ));
168 }
169 return cRefs;
170}
171
172/////////////////////////////////////////////////////////////////////////////
173// TrackedObjectsCollector
174/////////////////////////////////////////////////////////////////////////////
175class TrackedObjectsCollector;
176extern TrackedObjectsCollector gTrackedObjectsCollector;
177
178TrackedObjectsCollector::TrackedObjectsCollector(): m_fInitialized(false)
179{
180 Log2(("%s constructor \n", __FUNCTION__));
181}
182
183TrackedObjectsCollector::~TrackedObjectsCollector()
184{
185 Log2(("%s destructor \n", __FUNCTION__));
186
187 int vrc = i_checkInitialization();
188
189 /*
190 * Someone forgot to call uninit()
191 */
192 if (RT_SUCCESS(vrc))
193 {
194 if (m_trackedObjectsData.size() != 0)
195 {
196 if (m_trackedObjectsData.size() > 1)
197 LogRel(("%u objects are still presented in the collector\n", m_trackedObjectsData.size()));
198 else
199 LogRel(("%u object is still presented in the collector\n", m_trackedObjectsData.size()));
200
201 m_Released += m_trackedObjectsData.size();
202 m_trackedObjectsData.clear();
203 }
204
205 LogRel(("The Objects Collector history data: added objects %u, released objects %u\n", m_Added, m_Released));
206
207 /* Try to delete m_CritSectData */
208 RTCritSectDelete(&m_CritSectData);
209 }
210}
211
212bool TrackedObjectsCollector::init(){
213 m_fInitialized = false;
214
215 /* Create the critical section protecting the m_trackedObjectsData */
216 int vrc = RTCritSectInit(&m_CritSectData);
217
218 if (RT_SUCCESS(vrc))
219 {
220 /*
221 * TrackedObjectsCollector initialization occurs only when new instance of VirtualBox is created.
222 * At this moment nobody uses the TrackedObjectsCollector and we can call the functions without any locking.
223 */
224 vrc = i_clear();//just in case
225 if (RT_SUCCESS(vrc))
226 {
227 m_fInitialized = true;
228 LogRel(("The collector has been initialized.\n"));
229 }
230 }
231
232 return m_fInitialized;
233}
234
235bool TrackedObjectsCollector::uninit()
236{
237 clear(Uninitialization);
238
239
240 /* Deletion the critical section protecting the m_trackedObjectsData */
241 int vrc = RTCritSectDelete(&m_CritSectData);
242
243 if (RT_SUCCESS(vrc))
244 m_fInitialized = false;
245
246 return m_fInitialized;
247}
248
249int TrackedObjectsCollector::i_checkInitialization() const
250{
251 int vrc = VINF_SUCCESS;
252 if (!m_fInitialized)
253 {
254 Log2(("The collector is in the uninitialized state...\n"));
255 vrc = VERR_INVALID_STATE;
256 }
257
258 return vrc;
259}
260
261HRESULT TrackedObjectsCollector::setObj (const com::Utf8Str &aObjId,
262 const com::Utf8Str &aClassIID,
263 uint64_t lifeTime,
264 uint64_t idleTime,
265 IUnknown* ptrIface)
266{
267 LogFlowFuncEnter();
268
269 HRESULT hrc = S_OK;
270 int vrc = i_checkInitialization();
271 if (RT_FAILURE(vrc))
272 return VBOX_E_INVALID_OBJECT_STATE;
273
274 /* Enter critical section here */
275 RTCritSectEnter(&m_CritSectData);
276
277 com::Guid idObj(aObjId);
278 com::Guid classIID(aClassIID);
279 std::pair < std::set<com::Utf8Str>::iterator, bool > opRes = m_trackedObjectIds.insert(aObjId);
280
281 /*
282 * The case for updating the tracked object data.
283 * The Id is presented in the m_trackedObjectIds. The original object is removed from m_trackedObjectsData.
284 */
285 if (!opRes.second)
286 {
287 Log2(("UPDATING TrackedObjectData: object Id %s, class IID %s, life time %i, idle time %i\n",
288 aObjId.c_str(), aClassIID.c_str(), lifeTime, idleTime));
289
290 m_trackedObjectsData.erase(aObjId.c_str());
291 /* decrease the counter */
292 --m_Added;
293 }
294 else if (LogIs2Enabled())
295 {
296 char szCreationTime[RTTIME_STR_LEN];
297 RTTIMESPEC time;
298 RTTimeSpecToString(RTTimeNow(&time), szCreationTime, sizeof(szCreationTime));
299 Log2(("ADDED TrackedObjectData: creation time %s, object Id %s, class IID %s\n",
300 szCreationTime, aObjId.c_str(), aClassIID.c_str()));
301 }
302
303 /* Data is stored in the m_trackedObjectsData under the passed Id. */
304 m_trackedObjectsData.insert(std::make_pair(aObjId.c_str(),
305 TrackedObjectData(idObj,
306 classIID,
307 lifeTime,
308 idleTime,
309 ptrIface)));
310
311 /* increase the counter */
312 ++m_Added;
313
314 /* Leave critical section here */
315 RTCritSectLeave(&m_CritSectData);
316
317 return hrc;
318}
319
320HRESULT TrackedObjectsCollector::getObj(const com::Utf8Str &aObjId,
321 TrackedObjectData &aObjData,
322 bool fUpdate)
323{
324 LogFlowFuncEnter();
325
326 int vrc = i_checkInitialization();
327 if (RT_FAILURE(vrc))
328 return VBOX_E_INVALID_OBJECT_STATE;
329
330 /* Enter critical section here */
331 RTCritSectEnter(&m_CritSectData);
332
333 HRESULT hrc = E_FAIL;
334
335 IterTrObjData_T pIter = m_trackedObjectsData.find(aObjId);
336 if (pIter != m_trackedObjectsData.end() && fUpdate == true)
337 {
338 /* Update some fields in the found object if needed. in instance, the last access time */
339 com::Utf8Str lat = pIter->second.updateLastAccessTime(); /* Update the access time */
340 Log2(("The updated last access time is %s\n", lat.c_str()));
341 vrc = VINF_SUCCESS;
342 }
343
344 if (RT_SUCCESS(vrc)) /** @todo r=bird: This won't ever fail. Did you mixup vrc and hrc above? */
345 {
346 /** @todo r=bird: Why do three lookups? */
347 if ( i_getObj(aObjId).getInterface().isNotNull() )
348 {
349 /* Excessive check because user may get only the valid objects Ids. But for 200% assurance it's here */
350 if (i_getObj(aObjId).state() == TrackedObjectData::Valid)
351 {
352 aObjData = i_getObj(aObjId);
353 hrc = S_OK;
354 }
355 else
356 hrc = VBOX_E_INVALID_OBJECT_STATE;
357 }
358 else
359 hrc = VBOX_E_OBJECT_NOT_FOUND;
360 }
361
362 /* Leave critical section here */
363 RTCritSectLeave(&m_CritSectData);
364
365 return hrc;
366}
367
368const TrackedObjectData &TrackedObjectsCollector::i_getObj(const com::Utf8Str &aObjId) const
369{
370 /* No check for existence of aObjId */
371#if 0 /* the solaris VM's stl_map.h code doesn't have any at() function. */
372 return m_trackedObjectsData.at(aObjId);
373#else
374 ConstIterTrObjData_T const Iter = m_trackedObjectsData.find(aObjId);
375 if (Iter == m_trackedObjectsData.end())
376 throw std::out_of_range(aObjId.c_str());
377 return (*Iter).second;
378#endif
379}
380
381HRESULT TrackedObjectsCollector::initObjIdleTime(const com::Utf8Str &aObjId)
382{
383 LogFlowFuncEnter();
384
385 HRESULT hrc = S_OK;
386 int vrc = i_checkInitialization();
387 if (RT_FAILURE(vrc))
388 return VBOX_E_INVALID_OBJECT_STATE;
389
390 vrc = VERR_NOT_FOUND;
391 /* Enter critical section here */
392 RTCritSectEnter(&m_CritSectData);
393
394 IterTrObjData_T pIter = m_trackedObjectsData.find(aObjId);
395 if (pIter != m_trackedObjectsData.end())
396 {
397 /* Init idle time only once, next time returns the initialization time */
398 com::Utf8Str strTime = pIter->second.initIdleTime();
399 Log2(("The idle time start is %s\n", strTime.c_str()));
400 vrc = VINF_SUCCESS;
401 }
402
403 if (RT_FAILURE(vrc))
404 hrc = VBOX_E_OBJECT_NOT_FOUND;
405
406 /* Leave critical section here */
407 RTCritSectLeave(&m_CritSectData);
408
409 return hrc;
410}
411
412HRESULT TrackedObjectsCollector::removeObj(const com::Utf8Str &aObjId)
413{
414 Log2(("%s: object Id %s\n", __FUNCTION__, aObjId.c_str()));
415
416 HRESULT hrc = S_OK;
417 int vrc = i_checkInitialization();
418 if (RT_FAILURE(vrc))
419 return VBOX_E_INVALID_OBJECT_STATE;
420
421 vrc = VERR_NOT_FOUND;
422
423 /* Enter critical section here */
424 RTCritSectEnter(&m_CritSectData);
425
426 IterTrObjData_T Iter = m_trackedObjectsData.find(aObjId);
427 if (Iter != m_trackedObjectsData.end())
428 {
429 Log2(("RELEASED TrackedObjectData: creation time %s, object Id %s, class IID %s\n",
430 Iter->second.creationTimeStr().c_str(), Iter->second.objectIdStr().c_str(), Iter->second.classIIDStr().c_str()));
431
432 m_trackedObjectsData.erase(Iter);
433 m_trackedObjectIds.erase(aObjId);
434 m_trackedInvalidObjectIds.erase(aObjId);
435
436 /* increase the counter */
437 ++m_Released;
438
439 vrc = VINF_SUCCESS;
440 }
441
442 if (RT_FAILURE(vrc))
443 hrc = VBOX_E_OBJECT_NOT_FOUND;
444
445 /* Leave critical section here */
446 RTCritSectLeave(&m_CritSectData);
447
448 return hrc;
449}
450
451HRESULT TrackedObjectsCollector::getAllObjIds (std::vector<com::Utf8Str>& aObjIdMap)
452{
453 HRESULT hrc = S_OK;
454 int vrc = i_checkInitialization();
455 if (RT_FAILURE(vrc))
456 return VBOX_E_INVALID_OBJECT_STATE;
457
458 /* Enter critical section here */
459 RTCritSectEnter(&m_CritSectData);
460 vrc = i_getAllObjIds(aObjIdMap);
461 if (RT_FAILURE(vrc))
462 hrc = VBOX_E_OBJECT_NOT_FOUND;
463 /* Leave critical section here */
464 RTCritSectLeave(&m_CritSectData);
465
466 return hrc;
467}
468
469int TrackedObjectsCollector::i_getAllObjIds(std::vector<com::Utf8Str> &aObjIdMap) const
470{
471 //for (const com::Utf8Str &item : m_trackedObjectIds) - the gcc in the solaris VM doesn't grok this.
472 for (std::set<com::Utf8Str>::const_iterator Iter = m_trackedObjectIds.begin();
473 Iter != m_trackedObjectIds.end();
474 ++Iter)
475 {
476 if (!m_trackedInvalidObjectIds.count(*Iter))
477 aObjIdMap.push_back(*Iter);
478 }
479
480 return aObjIdMap.size() > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
481}
482
483HRESULT TrackedObjectsCollector::getObjIdsByClassIID (const Guid& iid,
484 std::vector<com::Utf8Str>& aObjIdMap)
485{
486 Log2(("%s: Getting all objects Ids with Class IID %s\n", __FUNCTION__, iid.toString().c_str()));
487
488 HRESULT hrc = S_OK;
489 int vrc = i_checkInitialization();
490 if (RT_FAILURE(vrc))
491 return VBOX_E_INVALID_OBJECT_STATE;
492
493 /* Enter critical section here */
494 RTCritSectEnter(&m_CritSectData);
495
496 vrc = i_getObjIdsByClassIID (iid, aObjIdMap);
497 if (RT_FAILURE(vrc))
498 hrc = VBOX_E_OBJECT_NOT_FOUND;
499 /* Leave critical section here */
500 RTCritSectLeave(&m_CritSectData);
501
502 return hrc;
503}
504
505int TrackedObjectsCollector::i_getObjIdsByClassIID(const Guid &aIId, std::vector<com::Utf8Str> &aObjIdMap) const
506{
507 //for (const std::pair<const com::Utf8Str, TrackedObjectData> &item : m_trackedObjectsData) - the gcc in the solaris VM doesn't grok this.
508 for (ConstIterTrObjData_T Iter = m_trackedObjectsData.begin();
509 Iter != m_trackedObjectsData.end();
510 ++Iter)
511 {
512 /* IID found and the object is valid */
513 if (Iter->second.classIID() == aIId && !m_trackedInvalidObjectIds.count(Iter->first))
514 aObjIdMap.push_back(Iter->first);
515 }
516
517 return aObjIdMap.size() > 0 ? VINF_SUCCESS : VERR_NOT_FOUND;
518}
519
520bool TrackedObjectsCollector::checkObj(const com::Utf8Str &aObjId)
521{
522 Log2(("%s: Checking object with Id %s\n", __FUNCTION__, aObjId.c_str()));
523
524 int vrc = i_checkInitialization();
525 if (RT_FAILURE(vrc))
526 return false;
527
528 RTCritSectEnter(&m_CritSectData);
529 bool res = i_checkObj(aObjId);
530 RTCritSectLeave(&m_CritSectData);
531 return res;
532}
533
534bool TrackedObjectsCollector::i_checkObj(const com::Utf8Str& aObjId) const
535{
536 return m_trackedObjectIds.count(aObjId.c_str()) > 0 ? true : false;
537}
538
539HRESULT TrackedObjectsCollector::clear(TrackedObjectsCollectorState aState)
540{
541 LogFlowFuncEnter();
542
543 HRESULT hrc = S_OK;
544 int vrc = i_checkInitialization();
545 if (RT_FAILURE(vrc))
546 return VBOX_E_INVALID_OBJECT_STATE;
547
548 if (aState != Uninitialization)
549 {
550 /* Enter critical section here */
551 vrc = RTCritSectEnter(&m_CritSectData);
552 if (RT_FAILURE(vrc))
553 {
554 Log2(("%s: Coudn't enter into the critical section (%Rrc)\n", __FUNCTION__, vrc));
555 return E_ABORT;
556 }
557 }
558
559 if (m_trackedObjectsData.size() != 0)
560 {
561 if (m_trackedObjectsData.size() > 1)
562 Log2(("%u objects are still presented in the Objects Collector, clearing...\n", m_trackedObjectsData.size()));
563 else
564 Log2(("%u object is still presented in the Objects Collector, clearing...\n", m_trackedObjectsData.size()));
565
566 vrc = i_clear();
567 /* Ignore setting hrc */
568 if (RT_FAILURE(vrc))
569 LogRel(("Something wrong with clearing the Objects Collector\n"));
570 }
571
572 Log2(("The Objects Collector history data: added objects %u, released objects %u\n", m_Added, m_Released));
573
574 if (aState != Uninitialization)
575 {
576 /* Leave critical section here */
577 RTCritSectLeave(&m_CritSectData);
578 }
579
580 return hrc;
581}
582
583int TrackedObjectsCollector::i_clear()
584{
585 int vrc = VINF_SUCCESS;
586 try
587 {
588 m_Released += m_trackedObjectsData.size();
589 m_trackedObjectsData.clear();
590 m_trackedObjectIds.clear();
591 m_trackedInvalidObjectIds.clear();
592 }
593 catch (...)
594 {
595 vrc = VERR_GENERAL_FAILURE;
596 }
597 return vrc;
598}
599
600HRESULT TrackedObjectsCollector::tryToRemoveObj(const com::Utf8Str& aObjId)
601{
602 LogFlowFuncEnter();
603
604 HRESULT hrc = S_OK;
605 int vrc = i_checkInitialization();
606 if (RT_FAILURE(vrc))
607 return VBOX_E_INVALID_OBJECT_STATE;
608
609 /* Enter critical section here */
610 RTCritSectEnter(&m_CritSectData);
611
612 IterTrObjData_T pIter = m_trackedObjectsData.find(aObjId.c_str());
613 if (pIter != m_trackedObjectsData.end())
614 {
615 pIter->second.getInterface()->AddRef();
616 ULONG cRefs = pIter->second.getInterface()->Release();
617
618 if (cRefs > 1)
619 {
620 Log2(("Object %s with class IID %s can't be released if refcount is more than 1 (now %lu)\n",
621 pIter->second.objectIdStr().c_str(),
622 pIter->second.classIIDStr().c_str(),
623 cRefs
624 ));
625 hrc = E_FAIL;
626 }
627 else
628 {
629 Log2(("Object %s with class IID %s is released (refcount %lu)\n",
630 pIter->second.objectIdStr().c_str(),
631 pIter->second.classIIDStr().c_str(),
632 cRefs
633 ));
634 m_trackedObjectsData.erase(pIter);
635 m_trackedObjectIds.erase(aObjId);
636 m_trackedInvalidObjectIds.erase(aObjId);
637
638 /* increase the counter */
639 ++m_Released;
640 }
641 }
642 else
643 hrc = VBOX_E_OBJECT_NOT_FOUND;
644
645 /* Leave critical section here */
646 RTCritSectLeave(&m_CritSectData);
647
648 return hrc;
649}
650
651/**
652 * Invalidate the tracked object.
653 * Works ONLY in conjunction with setTracked()!
654 */
655HRESULT TrackedObjectsCollector::invalidateObj(const com::Utf8Str &aObjId)
656{
657 LogFlowFuncEnter();
658
659 int vrc = i_checkInitialization();
660 if (RT_FAILURE(vrc))
661 return VBOX_E_INVALID_OBJECT_STATE;
662
663 /* Enter critical section here */
664 RTCritSectEnter(&m_CritSectData);
665
666 HRESULT hrc = VBOX_E_OBJECT_NOT_FOUND;
667 IterTrObjData_T pIter = m_trackedObjectsData.find(aObjId);
668 if (pIter != m_trackedObjectsData.end())
669 {
670 pIter->second.resetState();
671 m_trackedInvalidObjectIds.insert(aObjId);
672 hrc = S_OK;
673 }
674
675 /* Leave critical section here */
676 RTCritSectLeave(&m_CritSectData);
677
678 return hrc;
679}
680
681/////////////////////////////////////////////////////////////////////////////
682// ObjectTracker
683/////////////////////////////////////////////////////////////////////////////
684ObjectTracker::~ObjectTracker()
685{
686 LogFlowFuncEnter();
687 LogRel(("Start waiting the ObjectTracker thread termination\n"));
688 RTThreadWait(m_Thread, 30000, NULL);
689 LogRel(("Finished waiting the ObjectTracker thread termination\n"));
690 m_Thread = NIL_RTTHREAD;
691}
692
693bool ObjectTracker::init()
694{
695 LogFlowFuncEnter();
696 return true;
697}
698
699bool ObjectTracker::finish()
700{
701 LogFlowFuncEnter();
702 ASMAtomicWriteBool(&fFinish, true);
703 return true;
704}
705
706bool ObjectTracker::isFinished()
707{
708 LogFlowFuncEnter();
709 return ASMAtomicReadBool(&fFinish);
710}
711
712/*static*/
713DECLCALLBACK(int) ObjectTracker::objectTrackerTask(RTTHREAD ThreadSelf, void *pvUser)
714{
715 HRESULT hrc = S_OK;
716
717 NOREF(ThreadSelf);
718 ObjectTracker* const master = (ObjectTracker*)pvUser;
719
720 LogRel(("Starting the ObjectTracker thread %s\n", master->getTaskName().c_str()));
721
722 while (master != NULL && master->isFinished() != true)
723 {
724 std::vector<com::Utf8Str> lObjIdMap;
725 hrc = gTrackedObjectsCollector.getAllObjIds(lObjIdMap);
726
727 //for (const com::Utf8Str& item : lObjIdMap) - the gcc in the solaris VM doesn't grok this.
728 for (std::vector<com::Utf8Str>::const_iterator Iter = lObjIdMap.begin(); Iter != lObjIdMap.end(); ++Iter)
729 {
730 TrackedObjectData temp;
731 if(gTrackedObjectsCollector.checkObj(*Iter))
732 {
733 hrc = gTrackedObjectsCollector.getObj(*Iter, temp);
734 if (SUCCEEDED(hrc))
735 {
736 Log2(("Tracked Object with ID %s was found:\n", temp.m_objId.toString().c_str()));
737
738 RTTIMESPEC now;
739 int64_t currTime = RTTimeSpecGetMilli(RTTimeNow(&now));
740 int64_t creationTime = RTTimeSpecGetMilli(&temp.m_creationTime);
741 int64_t lifeTime = (int64_t)temp.m_lifeTime*1000; //convert to milliseconds
742
743 int64_t remainingLifeTime = ((creationTime + lifeTime) - currTime)/1000;
744
745 /* lock? */
746 temp.m_pIface->AddRef();
747 unsigned long cRefs = temp.m_pIface->Release();
748
749 /* cRefs > 2 because we created the temporarily object temp */
750 Log2(("Object %s (class IID %s): refcount %lu, remaining life time is %ld sec\n",
751 temp.m_objId.toString().c_str(),
752 temp.m_classIID.toString().c_str(),
753 cRefs - 1,
754 remainingLifeTime
755 ));
756
757 bool fLifeTimeEnd = (currTime - creationTime) > lifeTime ? true : false;
758
759 if (!fLifeTimeEnd)
760 {
761 if (cRefs <= 2 || remainingLifeTime <= 0)
762 {
763 if (temp.m_fIdleTimeStart == false)
764 {
765 gTrackedObjectsCollector.initObjIdleTime(*Iter);
766 Log2(("Idle time for the object with Id %s has been started\n", Iter->c_str()));
767 }
768 else
769 {
770 int64_t idleTime = (int64_t)temp.m_idleTime*1000; //convert to milliseconds
771 int64_t idleTimeStart = RTTimeSpecGetMilli(&temp.m_idleTimeStart);
772 bool fObsolete = (currTime - idleTimeStart) > idleTime ? true : false;
773 if (fObsolete)
774 {
775 Log2(("Object with Id %s removed from Object Collector "
776 "(recount is %u, idle time exceeded %u sec)\n", Iter->c_str(), cRefs - 1, temp.m_idleTime));
777 gTrackedObjectsCollector.removeObj(*Iter);
778 }
779 }
780 }
781 }
782 else
783 {
784 if (cRefs <= 2)
785 {
786 /*
787 * Special case for the objects with lifeTime == 0.
788 * It's intended for such objects like Mediums or Machines or others.
789 * The objects which live from the beginning but may be deleted by user manually.
790 * for this object the idle time starts when user deletes it.
791 */
792 if (lifeTime == 0)
793 {
794 lifeTime = currTime - creationTime;//in milliseconds
795 /* if lifeTime < 1000 msec (1 sec) set minimal lifeTime to 60 sec (1 min)*/
796 lifeTime = lifeTime < 1000 ? 60 : lifeTime/1000;
797 /* Updating the object data */
798 gTrackedObjectsCollector.setObj(temp.objectIdStr(),
799 temp.classIIDStr(),
800 (uint64_t)lifeTime,
801 temp.m_idleTime,
802 temp.m_pIface);
803 }
804 else
805 {
806 Log2(("Object with Id %s removed from Object Collector "
807 "(lifetime exceeded %u sec)\n", Iter->c_str(), temp.m_lifeTime));
808 gTrackedObjectsCollector.removeObj(*Iter);
809 }
810 }
811 }
812 }
813 }
814 else
815 Log2(("Tracked Object with ID %s was not found\n", Iter->c_str()));
816 }
817
818 //sleep 1 sec
819 RTThreadSleep(RT_MS_1SEC);//1 sec
820 }
821
822 LogRel(("Finishing the object tracker thread %s\n", master->getTaskName().c_str()));
823
824 return 0;
825}
826
827int ObjectTracker::createThread()
828{
829 int vrc = RTThreadCreate(&m_Thread, objectTrackerTask, this, 0,
830 RTTHREADTYPE_INFREQUENT_POLLER,
831 RTTHREADFLAGS_WAITABLE,
832 "ObjTracker");
833
834 return vrc;
835}
836
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