Panda3D
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  */
32 GeomCacheManager::
33 GeomCacheManager() :
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  */
47 GeomCacheManager::
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  */
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  */
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  */
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 
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 }
get_frame_count
Returns the number of times tick() has been called since the ClockObject was created,...
Definition: clockObject.h:94
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:215
This object contains a single cache entry in the GeomCacheManager.
This is used to keep track of, and limit the size of, the cache of munged vertices,...
void flush()
Immediately empties all elements in the cache.
void evict_old_entries()
Trims the cache size down to get_max_size() by evicting old cache entries as needed.
static GeomCacheManager * get_global_ptr()
Returns the global cache manager pointer.
Similar to MutexHolder, but for a light mutex.
Similar to MutexHolder, but for a light reentrant mutex.
static bool is_connected()
Returns true if the client believes it is connected to a working PStatServer, false otherwise.
Definition: pStatClient.h:291
A lightweight class that represents a single element that may be timed and/or counted via stats.
void ref() const
Explicitly increments the reference count.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.