Panda3D
adaptiveLru.I
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 adaptiveLru.I
10  * @author drose
11  * @date 2008-09-03
12  */
13 
14 /**
15  * Returns the total size of all objects currently active on the LRU.
16  */
17 INLINE size_t AdaptiveLru::
18 get_total_size() const {
19  LightMutexHolder holder(_lock);
20  return _total_size;
21 }
22 
23 /**
24  * Returns the max size of all objects that are allowed to be active on the
25  * LRU.
26  */
27 INLINE size_t AdaptiveLru::
28 get_max_size() const {
29  LightMutexHolder holder(_lock);
30  return _max_size;
31 }
32 
33 /**
34  * Changes the max size of all objects that are allowed to be active on the
35  * LRU.
36  *
37  * If the size is (size_t)-1, there is no limit.
38  */
39 INLINE void AdaptiveLru::
40 set_max_size(size_t max_size) {
41  LightMutexHolder holder(_lock);
42  _max_size = max_size;
43  if (_total_size > _max_size) {
44  do_evict_to(_max_size, false);
45  }
46 }
47 
48 /**
49  * Evicts a sequence of objects if the queue is full.
50  */
51 INLINE void AdaptiveLru::
53  LightMutexHolder holder(_lock);
54  if (_total_size > _max_size) {
55  do_evict_to(_max_size, false);
56  }
57 }
58 
59 /**
60  * Evicts a sequence of objects until the queue fits within the indicated
61  * target size, regardless of its normal max size.
62  */
63 INLINE void AdaptiveLru::
64 evict_to(size_t target_size) {
65  LightMutexHolder holder(_lock);
66  if (_total_size > target_size) {
67  do_evict_to(target_size, true);
68  }
69 }
70 
71 /**
72  * Checks that the LRU is internally self-consistent. Returns true if
73  * successful, false if there is some problem.
74  */
75 INLINE bool AdaptiveLru::
77  LightMutexHolder holder(_lock);
78  return do_validate();
79 }
80 
81 /**
82  * Specifies the weight value used to compute the exponential moving average.
83  */
84 INLINE void AdaptiveLru::
85 set_weight(PN_stdfloat weight) {
86  _weight = weight;
87 }
88 
89 /**
90  * Returns the weight value used to compute the exponential moving average.
91  */
92 INLINE PN_stdfloat AdaptiveLru::
93 get_weight() const {
94  return _weight;
95 }
96 
97 /**
98  * Specifies the maximum number of pages the AdaptiveLru will update each
99  * frame. This is a performance optimization: keeping this number low limits
100  * the impact of the AdaptiveLru's adaptive algorithm.
101  */
102 INLINE void AdaptiveLru::
103 set_max_updates_per_frame(int max_updates_per_frame) {
104  _max_updates_per_frame = max_updates_per_frame;
105 }
106 
107 /**
108  * Returns the maximum number of pages the AdaptiveLru will update each frame.
109  */
110 INLINE int AdaptiveLru::
112  return _max_updates_per_frame;
113 }
114 
115 /**
116  *
117  */
118 INLINE PN_stdfloat AdaptiveLru::
119 calculate_exponential_moving_average(PN_stdfloat value, PN_stdfloat average) const {
120  return ((value - average) * _weight) + average;
121 }
122 
123 /**
124  * Returns the LRU that manages this page, or NULL if it is not currently
125  * managed by any LRU.
126  */
128 get_lru() const {
129  return _lru;
130 }
131 
132 /**
133  * Removes the page from its AdaptiveLru.
134  */
135 INLINE void AdaptiveLruPage::
137  enqueue_lru(nullptr);
138 }
139 
140 /**
141  * To be called when the page is used; this will move it to the tail of the
142  * AdaptiveLru queue it is already on.
143  *
144  * This method is const because it's not technically modifying the contents of
145  * the page itself.
146  */
147 INLINE void AdaptiveLruPage::
148 mark_used_lru() const {
149  if (_lru != nullptr) {
150  ((AdaptiveLruPage *)this)->mark_used_lru(_lru);
151  }
152 }
153 
154 /**
155  * To be called when the page is used; this will move it to the tail of the
156  * specified AdaptiveLru queue.
157  */
158 INLINE void AdaptiveLruPage::
160  enqueue_lru(lru);
161 }
162 
163 /**
164  * Returns the size of this page as reported to the LRU, presumably in bytes.
165  */
166 INLINE size_t AdaptiveLruPage::
167 get_lru_size() const {
168  return _lru_size;
169 }
170 
171 /**
172  * Specifies the size of this page, presumably in bytes, although any unit is
173  * possible.
174  */
175 INLINE void AdaptiveLruPage::
176 set_lru_size(size_t lru_size) {
177  if (_lru != nullptr) {
178  LightMutexHolder holder(_lru->_lock);
179  _lru->_total_size -= _lru_size;
180  _lru->_total_size += lru_size;
181  _lru_size = lru_size;
182  } else {
183  _lru_size = lru_size;
184  }
185 }
size_t get_total_size() const
Returns the total size of all objects currently active on the LRU.
Definition: adaptiveLru.I:18
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:40
PN_stdfloat get_weight() const
Returns the weight value used to compute the exponential moving average.
Definition: adaptiveLru.I:93
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:176
size_t get_lru_size() const
Returns the size of this page as reported to the LRU, presumably in bytes.
Definition: adaptiveLru.I:167
bool do_validate()
Checks that the LRU is internally consistent.
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:148
void dequeue_lru()
Removes the page from its AdaptiveLru.
Definition: adaptiveLru.I:136
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:76
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:28
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:103
One atomic piece that may be managed by a AdaptiveLru chain.
Definition: adaptiveLru.h:135
A basic LRU-type algorithm, except that it is adaptive and attempts to avoid evicting pages that have...
Definition: adaptiveLru.h:45
void set_weight(PN_stdfloat weight)
Specifies the weight value used to compute the exponential moving average.
Definition: adaptiveLru.I:85
int get_max_updates_per_frame() const
Returns the maximum number of pages the AdaptiveLru will update each frame.
Definition: adaptiveLru.I:111
void consider_evict()
Evicts a sequence of objects if the queue is full.
Definition: adaptiveLru.I:52
void evict_to(size_t target_size)
Evicts a sequence of objects until the queue fits within the indicated target size,...
Definition: adaptiveLru.I:64
void do_evict_to(size_t target_size, bool hard_evict)
Evicts pages until the LRU is within the indicated size.
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:128