Panda3D

Loading...
Searching...
No Matches
geomCacheManager.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file geomCacheManager.cxx
10 * @author drose
11 * @date 2005-03-11
12 */
13
14#include "geomCacheManager.h"
15#include "geomCacheEntry.h"
16#include "geomMunger.h"
17#include "lightMutexHolder.h"
18#include "lightReMutexHolder.h"
19#include "clockObject.h"
20
21 GeomCacheManager *GeomCacheManager::_global_ptr = nullptr;
22
23 PStatCollector GeomCacheManager::_geom_cache_size_pcollector("Geom cache size");
24 PStatCollector GeomCacheManager::_geom_cache_active_pcollector("Geom cache size:Active");
25 PStatCollector GeomCacheManager::_geom_cache_record_pcollector("Geom cache operations:record");
26 PStatCollector GeomCacheManager::_geom_cache_erase_pcollector("Geom cache operations:erase");
27 PStatCollector GeomCacheManager::_geom_cache_evict_pcollector("Geom cache operations:evict");
28
29/**
30 *
31 */
32GeomCacheManager::
33GeomCacheManager() :
34 _lock("GeomCacheManager"),
35 _total_size(0)
36{
37 // We deliberately hang on to this pointer forever.
38 _list = new GeomCacheEntry;
39 _list->ref();
40 _list->_next = _list;
41 _list->_prev = _list;
42}
43
44/**
45 *
46 */
47GeomCacheManager::
48~GeomCacheManager() {
49 // Shouldn't be deleting this global object.
50 nassert_raise("attempt to delete GeomCacheManager");
51}
52
53/**
54 * Immediately empties all elements in the cache.
55 */
56 void GeomCacheManager::
57 flush() {
58 // Prevent deadlock
59 LightReMutexHolder registry_holder(GeomMunger::get_registry()->_registry_lock);
60
61 LightMutexHolder holder(_lock);
62 evict_old_entries(0, false);
63}
64
65/**
66 * Returns the global cache manager pointer.
67 */
68GeomCacheManager *GeomCacheManager::
69 get_global_ptr() {
70 if (_global_ptr == nullptr) {
71 _global_ptr = new GeomCacheManager;
72 }
73 return _global_ptr;
74}
75
76/**
77 * Trims the cache size down to the specified size by evicting old cache
78 * entries as needed. It is assumed that you already hold the lock before
79 * calling this method.
80 */
81 void GeomCacheManager::
82 evict_old_entries(int max_size, bool keep_current) {
83 int current_frame = ClockObject::get_global_clock()->get_frame_count();
84 int min_frames = geom_cache_min_frames;
85
86 while (_total_size > max_size) {
87 PT(GeomCacheEntry) entry = _list->_next;
88 nassertv(entry != _list);
89
90 if (keep_current && current_frame - entry->_last_frame_used < min_frames) {
91 // Never mind, this one is too new.
92 if (gobj_cat.is_debug()) {
93 gobj_cat.debug()
94 << "Oldest element in cache is "
95 << current_frame - entry->_last_frame_used
96 << " frames; keeping cache at " << _total_size << " entries.\n";
97 }
98 break;
99 }
100
101 entry->unref();
102
103 if (gobj_cat.is_debug()) {
104 gobj_cat.debug()
105 << "cache total_size = " << _total_size << " entries, max_size = "
106 << max_size << ", removing " << *entry << "\n";
107 }
108
109 entry->evict_callback();
110
111 if (PStatClient::is_connected()) {
112 if (entry->_last_frame_used == current_frame) {
113 GeomCacheManager::_geom_cache_active_pcollector.sub_level(1);
114 }
115 }
116
117 --_total_size;
118 entry->remove_from_list();
119 _geom_cache_evict_pcollector.add_level(1);
120 }
121 _geom_cache_size_pcollector.set_level(_total_size);
122}
ClockObject::get_frame_count
get_frame_count
Returns the number of times tick() has been called since the ClockObject was created,...
Definition clockObject.h:94
ClockObject::get_global_clock
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition clockObject.I:215
GeomCacheEntry
This object contains a single cache entry in the GeomCacheManager.
Definition geomCacheEntry.h:31
GeomCacheManager
This is used to keep track of, and limit the size of, the cache of munged vertices,...
Definition geomCacheManager.h:37
GeomCacheManager::flush
void flush()
Immediately empties all elements in the cache.
Definition geomCacheManager.cxx:57
GeomCacheManager::evict_old_entries
void evict_old_entries()
Trims the cache size down to get_max_size() by evicting old cache entries as needed.
Definition geomCacheManager.I:53
GeomCacheManager::get_global_ptr
static GeomCacheManager * get_global_ptr()
Returns the global cache manager pointer.
Definition geomCacheManager.cxx:69
LightMutexHolder
Similar to MutexHolder, but for a light mutex.
Definition lightMutexHolder.h:25
LightReMutexHolder
Similar to MutexHolder, but for a light reentrant mutex.
Definition lightReMutexHolder.h:25
PStatClient::is_connected
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise.
Definition pStatClient.h:294
PStatCollector
A lightweight class that represents a single element that may be timed and/or counted via stats.
Definition pStatCollector.h:43
ReferenceCount::ref
void ref() const
Explicitly increments the reference count.
Definition referenceCount.I:151
clockObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomCacheEntry.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomCacheManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomMunger.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
lightMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
lightReMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.