Panda3D
 All Classes Functions Variables Enumerations
adaptiveLru.I
1 // Filename: adaptiveLru.I
2 // Created by: drose (03Sep08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: AdaptiveLru::get_total_size
18 // Access: Published
19 // Description: Returns the total size of all objects currently
20 // active on the LRU.
21 ////////////////////////////////////////////////////////////////////
22 INLINE size_t AdaptiveLru::
23 get_total_size() const {
24  LightMutexHolder holder(_lock);
25  return _total_size;
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: AdaptiveLru::get_max_size
30 // Access: Published
31 // Description: Returns the max size of all objects that are allowed
32 // to be active on the LRU.
33 ////////////////////////////////////////////////////////////////////
34 INLINE size_t AdaptiveLru::
35 get_max_size() const {
36  LightMutexHolder holder(_lock);
37  return _max_size;
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: AdaptiveLru::set_max_size
42 // Access: Published
43 // Description: Changes the max size of all objects that are allowed
44 // to be active on the LRU.
45 //
46 // If the size is (size_t)-1, there is no limit.
47 ////////////////////////////////////////////////////////////////////
48 INLINE void AdaptiveLru::
49 set_max_size(size_t max_size) {
50  LightMutexHolder holder(_lock);
51  _max_size = max_size;
52  if (_total_size > _max_size) {
53  do_evict_to(_max_size, false);
54  }
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: AdaptiveLru::consider_evict
59 // Access: Published
60 // Description: Evicts a sequence of objects if the queue is full.
61 ////////////////////////////////////////////////////////////////////
62 INLINE void AdaptiveLru::
64  LightMutexHolder holder(_lock);
65  if (_total_size > _max_size) {
66  do_evict_to(_max_size, false);
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: AdaptiveLru::evict_to
72 // Access: Published
73 // Description: Evicts a sequence of objects until the queue fits
74 // within the indicated target size, regardless of its
75 // normal max size.
76 ////////////////////////////////////////////////////////////////////
77 INLINE void AdaptiveLru::
78 evict_to(size_t target_size) {
79  LightMutexHolder holder(_lock);
80  if (_total_size > target_size) {
81  do_evict_to(target_size, true);
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: AdaptiveLru::validate
87 // Access: Published
88 // Description: Checks that the LRU is internally self-consistent.
89 // Returns true if successful, false if there is some
90 // problem.
91 ////////////////////////////////////////////////////////////////////
92 INLINE bool AdaptiveLru::
94  LightMutexHolder holder(_lock);
95  return do_validate();
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: AdaptiveLru::set_weight
100 // Access: Published
101 // Description: Specifies the weight value used to compute the
102 // exponential moving average.
103 ////////////////////////////////////////////////////////////////////
104 INLINE void AdaptiveLru::
105 set_weight(PN_stdfloat weight) {
106  _weight = weight;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: AdaptiveLru::get_weight
111 // Access: Published
112 // Description: Returns the weight value used to compute the
113 // exponential moving average.
114 ////////////////////////////////////////////////////////////////////
115 INLINE PN_stdfloat AdaptiveLru::
116 get_weight() const {
117  return _weight;
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: AdaptiveLru::set_max_updates_per_frame
122 // Access: Published
123 // Description: Specifies the maximum number of pages the AdaptiveLru
124 // will update each frame. This is a performance
125 // optimization: keeping this number low limits the
126 // impact of the AdaptiveLru's adaptive algorithm.
127 ////////////////////////////////////////////////////////////////////
128 INLINE void AdaptiveLru::
129 set_max_updates_per_frame(int max_updates_per_frame) {
130  _max_updates_per_frame = max_updates_per_frame;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: AdaptiveLru::get_max_updates_per_frame
135 // Access: Published
136 // Description: Returns the maximum number of pages the AdaptiveLru
137 // will update each frame.
138 ////////////////////////////////////////////////////////////////////
139 INLINE int AdaptiveLru::
141  return _max_updates_per_frame;
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: AdaptiveLru::calculate_exponential_moving_average
146 // Access: Private
147 // Description:
148 ////////////////////////////////////////////////////////////////////
149 INLINE PN_stdfloat AdaptiveLru::
150 calculate_exponential_moving_average(PN_stdfloat value, PN_stdfloat average) const {
151  return ((value - average) * _weight) + average;
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: AdaptiveLruPage::enqueue_lru
156 // Access: Published
157 // Description: Returns the LRU that manages this page, or NULL if it
158 // is not currently managed by any LRU.
159 ////////////////////////////////////////////////////////////////////
161 get_lru() const {
162  return _lru;
163 }
164 
165 ////////////////////////////////////////////////////////////////////
166 // Function: AdaptiveLruPage::dequeue_lru
167 // Access: Published
168 // Description: Removes the page from its AdaptiveLru.
169 ////////////////////////////////////////////////////////////////////
170 INLINE void AdaptiveLruPage::
172  enqueue_lru(NULL);
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: AdaptiveLruPage::mark_used_lru
177 // Access: Published
178 // Description: To be called when the page is used; this will move it
179 // to the tail of the AdaptiveLru queue it is already on.
180 //
181 // This method is const because it's not technically
182 // modifying the contents of the page itself.
183 ////////////////////////////////////////////////////////////////////
184 INLINE void AdaptiveLruPage::
185 mark_used_lru() const {
186  if (_lru != (AdaptiveLru *)NULL) {
187  ((AdaptiveLruPage *)this)->mark_used_lru(_lru);
188  }
189 }
190 
191 ////////////////////////////////////////////////////////////////////
192 // Function: AdaptiveLruPage::mark_used_lru
193 // Access: Published
194 // Description: To be called when the page is used; this will move it
195 // to the tail of the specified AdaptiveLru queue.
196 ////////////////////////////////////////////////////////////////////
197 INLINE void AdaptiveLruPage::
199  enqueue_lru(lru);
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: AdaptiveLruPage::get_lru_size
204 // Access: Published
205 // Description: Returns the size of this page as reported to the LRU,
206 // presumably in bytes.
207 ////////////////////////////////////////////////////////////////////
208 INLINE size_t AdaptiveLruPage::
209 get_lru_size() const {
210  return _lru_size;
211 }
212 
213 ////////////////////////////////////////////////////////////////////
214 // Function: AdaptiveLruPage::set_lru_size
215 // Access: Published
216 // Description: Specifies the size of this page, presumably in bytes,
217 // although any unit is possible.
218 ////////////////////////////////////////////////////////////////////
219 INLINE void AdaptiveLruPage::
220 set_lru_size(size_t lru_size) {
221  if (_lru != (AdaptiveLru *)NULL) {
222  LightMutexHolder holder(_lru->_lock);
223  _lru->_total_size -= _lru_size;
224  _lru->_total_size += lru_size;
225  _lru_size = lru_size;
226  } else {
227  _lru_size = lru_size;
228  }
229 }
PN_stdfloat get_weight() const
Returns the weight value used to compute the exponential moving average.
Definition: adaptiveLru.I:116
int get_max_updates_per_frame() const
Returns the maximum number of pages the AdaptiveLru will update each frame.
Definition: adaptiveLru.I:140
void set_max_size(size_t max_size)
Changes the max size of all objects that are allowed to be active on the LRU.
Definition: adaptiveLru.I:49
void set_lru_size(size_t lru_size)
Specifies the size of this page, presumably in bytes, although any unit is possible.
Definition: adaptiveLru.I:220
size_t get_lru_size() const
Returns the size of this page as reported to the LRU, presumably in bytes.
Definition: adaptiveLru.I:209
bool do_validate()
Checks that the LRU is internally consistent.
void dequeue_lru()
Removes the page from its AdaptiveLru.
Definition: adaptiveLru.I:171
void enqueue_lru(AdaptiveLru *lru)
Adds the page to the LRU for the first time, or marks it recently-accessed if it has already been add...
bool validate()
Checks that the LRU is internally self-consistent.
Definition: adaptiveLru.I:93
AdaptiveLru * get_lru() const
Returns the LRU that manages this page, or NULL if it is not currently managed by any LRU...
Definition: adaptiveLru.I:161
Similar to MutexHolder, but for a light mutex.
void set_max_updates_per_frame(int max_updates_per_frame)
Specifies the maximum number of pages the AdaptiveLru will update each frame.
Definition: adaptiveLru.I:129
One atomic piece that may be managed by a AdaptiveLru chain.
Definition: adaptiveLru.h:145
size_t get_total_size() const
Returns the total size of all objects currently active on the LRU.
Definition: adaptiveLru.I:23
A basic LRU-type algorithm, except that it is adaptive and attempts to avoid evicting pages that have...
Definition: adaptiveLru.h:49
void set_weight(PN_stdfloat weight)
Specifies the weight value used to compute the exponential moving average.
Definition: adaptiveLru.I:105
void consider_evict()
Evicts a sequence of objects if the queue is full.
Definition: adaptiveLru.I:63
void evict_to(size_t target_size)
Evicts a sequence of objects until the queue fits within the indicated target size, regardless of its normal max size.
Definition: adaptiveLru.I:78
void do_evict_to(size_t target_size, bool hard_evict)
Evicts pages until the LRU is within the indicated size.
void mark_used_lru() const
To be called when the page is used; this will move it to the tail of the AdaptiveLru queue it is alre...
Definition: adaptiveLru.I:185
size_t get_max_size() const
Returns the max size of all objects that are allowed to be active on the LRU.
Definition: adaptiveLru.I:35