Panda3D
Loading...
Searching...
No Matches
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 */
17INLINE size_t AdaptiveLru::
18get_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 */
27INLINE size_t AdaptiveLru::
28get_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 */
39INLINE void AdaptiveLru::
40set_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 */
51INLINE 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 */
63INLINE void AdaptiveLru::
64evict_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 */
75INLINE bool AdaptiveLru::
76validate() {
77 LightMutexHolder holder(_lock);
78 return do_validate();
79}
80
81/**
82 * Specifies the weight value used to compute the exponential moving average.
83 */
84INLINE void AdaptiveLru::
85set_weight(PN_stdfloat weight) {
86 _weight = weight;
87}
88
89/**
90 * Returns the weight value used to compute the exponential moving average.
91 */
92INLINE PN_stdfloat AdaptiveLru::
93get_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 */
102INLINE void AdaptiveLru::
103set_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 */
112 return _max_updates_per_frame;
113}
114
115/**
116 *
117 */
118INLINE PN_stdfloat AdaptiveLru::
119calculate_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 */
128get_lru() const {
129 return _lru;
130}
131
132/**
133 * Removes the page from its AdaptiveLru.
134 */
136dequeue_lru() {
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 */
148mark_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 */
160 enqueue_lru(lru);
161}
162
163/**
164 * Returns the size of this page as reported to the LRU, presumably in bytes.
165 */
166INLINE size_t AdaptiveLruPage::
167get_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 */
176set_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}
One atomic piece that may be managed by a AdaptiveLru chain.
AdaptiveLru * get_lru() const
Returns the LRU that manages this page, or NULL if it is not currently managed by any LRU.
size_t get_lru_size() const
Returns the size of this page as reported to the LRU, presumably in bytes.
void set_lru_size(size_t lru_size)
Specifies the size of this page, presumably in bytes, although any unit is possible.
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...
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...
void dequeue_lru()
Removes the page from its AdaptiveLru.
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
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
bool do_validate()
Checks that the LRU is internally consistent.
void set_max_updates_per_frame(int max_updates_per_frame)
Specifies the maximum number of pages the AdaptiveLru will update each frame.
int get_max_updates_per_frame() const
Returns the maximum number of pages the AdaptiveLru will update each frame.
PN_stdfloat get_weight() const
Returns the weight value used to compute the exponential moving average.
Definition adaptiveLru.I:93
void do_evict_to(size_t target_size, bool hard_evict)
Evicts pages until the LRU is within the indicated size.
size_t get_total_size() const
Returns the total size of all objects currently active on the LRU.
Definition adaptiveLru.I:18
void consider_evict()
Evicts a sequence of objects if the queue is full.
Definition adaptiveLru.I:52
bool validate()
Checks that the LRU is internally self-consistent.
Definition adaptiveLru.I:76
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
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.