Panda3D
Public Types | Public Member Functions | Public Attributes | Friends | List of all members
AdaptiveLru Class Reference

A basic LRU-type algorithm, except that it is adaptive and attempts to avoid evicting pages that have been used more frequently (even if less recently) than other pages. More...

#include "adaptiveLru.h"

Inheritance diagram for AdaptiveLru:
Namable MemoryBase

Public Types

enum  LruPagePriority {
  LPP_Highest = 0, LPP_High = 10, LPP_New = 20, LPP_Normal = 25,
  LPP_Intermediate = 30, LPP_Low = 40, LPP_TotalPriorities = 50
}
 

Public Member Functions

 AdaptiveLru (const string &name, size_t max_size)
 
void begin_epoch ()
 Marks the end of the previous epoch and the beginning of the next one. More...
 
PN_stdfloat calculate_exponential_moving_average (PN_stdfloat value, PN_stdfloat average) const
 
void consider_evict ()
 Evicts a sequence of objects if the queue is full. More...
 
size_t count_active_size () const
 Returns the total size of the pages that were enqueued since the last call to begin_epoch(). More...
 
void do_access_page (AdaptiveLruPage *page)
 Marks a page accessed. More...
 
void do_add_page (AdaptiveLruPage *page)
 Adds a new page the the LRU. More...
 
void do_evict_to (size_t target_size, bool hard_evict)
 Evicts pages until the LRU is within the indicated size. More...
 
void do_partial_lru_update (int num_updates)
 This only updates a number of pages up to the specified maximum_updates. More...
 
void do_remove_page (AdaptiveLruPage *page)
 Removes a page from the LRU. More...
 
bool do_validate ()
 Checks that the LRU is internally consistent. More...
 
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. More...
 
size_t get_max_size () const
 Returns the max size of all objects that are allowed to be active on the LRU. More...
 
int get_max_updates_per_frame () const
 Returns the maximum number of pages the AdaptiveLru will update each frame. More...
 
size_t get_total_size () const
 Returns the total size of all objects currently active on the LRU. More...
 
PN_stdfloat get_weight () const
 Returns the weight value used to compute the exponential moving average. More...
 
void output (ostream &out) const
 
void set_max_size (size_t max_size)
 Changes the max size of all objects that are allowed to be active on the LRU. More...
 
void set_max_updates_per_frame (int max_updates_per_frame)
 Specifies the maximum number of pages the AdaptiveLru will update each frame. More...
 
void set_weight (PN_stdfloat weight)
 Specifies the weight value used to compute the exponential moving average. More...
 
void update_page (AdaptiveLruPage *page)
 This updates the page's average utilization. More...
 
bool validate ()
 Checks that the LRU is internally self-consistent. More...
 
void write (ostream &out, int indent_level) const
 
- Public Member Functions inherited from Namable
 Namable (const string &initial_name="")
 
 Namable (const Namable &copy)
 
void clear_name ()
 Resets the Namable's name to empty. More...
 
const string & get_name () const
 
bool has_name () const
 Returns true if the Namable has a nonempty name set, false if the name is empty. More...
 
Namableoperator= (const Namable &other)
 
void output (ostream &out) const
 Outputs the Namable. More...
 
void set_name (const string &name)
 

Public Attributes

unsigned int _current_frame_identifier
 
LightMutex _lock
 
size_t _max_size
 
int _max_updates_per_frame
 
AdaptiveLruPageDynamicList _page_array [LPP_TotalPriorities]
 
AdaptiveLruPageStaticList _static_list
 
size_t _total_size
 
double _weight
 

Friends

class AdaptiveLruPage
 

Additional Inherited Members

- Static Public Member Functions inherited from Namable
static TypeHandle get_class_type ()
 
static void init_type ()
 

Detailed Description

A basic LRU-type algorithm, except that it is adaptive and attempts to avoid evicting pages that have been used more frequently (even if less recently) than other pages.

The interface is designed to be identical to that for SimpleLru, so that it may be used as a drop-in replacement.

Definition at line 49 of file adaptiveLru.h.

Member Function Documentation

◆ begin_epoch()

void AdaptiveLru::begin_epoch ( )

Marks the end of the previous epoch and the beginning of the next one.

This will evict any objects that are pending eviction, and also update any internal bookkeeping.

Definition at line 237 of file adaptiveLru.cxx.

References do_add_page(), ClockObject::get_frame_count(), and ClockObject::get_global_clock().

Referenced by count_active_size(), and AdaptiveLruPage::get_num_inactive_frames().

◆ consider_evict()

void AdaptiveLru::consider_evict ( )
inline

Evicts a sequence of objects if the queue is full.

Definition at line 63 of file adaptiveLru.I.

References do_evict_to(), and evict_to().

Referenced by set_max_size().

◆ count_active_size()

size_t AdaptiveLru::count_active_size ( ) const

Returns the total size of the pages that were enqueued since the last call to begin_epoch().

Definition at line 213 of file adaptiveLru.cxx.

References begin_epoch().

Referenced by AdaptiveLruPage::enqueue_lru().

◆ do_access_page()

void AdaptiveLru::do_access_page ( AdaptiveLruPage page)

Marks a page accessed.

Definition at line 334 of file adaptiveLru.cxx.

References do_evict_to().

Referenced by do_remove_page(), and AdaptiveLruPage::enqueue_lru().

◆ do_add_page()

void AdaptiveLru::do_add_page ( AdaptiveLruPage page)

Adds a new page the the LRU.

Definition at line 304 of file adaptiveLru.cxx.

References do_remove_page().

Referenced by begin_epoch().

◆ do_evict_to()

void AdaptiveLru::do_evict_to ( size_t  target_size,
bool  hard_evict 
)

Evicts pages until the LRU is within the indicated size.

Assumes the lock is already held. If hard_evict is false, does not evict "active" pages that were added within this epoch.

Definition at line 366 of file adaptiveLru.cxx.

References do_validate(), and AdaptiveLruPage::evict_lru().

Referenced by consider_evict(), do_access_page(), evict_to(), and set_max_size().

◆ do_partial_lru_update()

void AdaptiveLru::do_partial_lru_update ( int  num_updates)

This only updates a number of pages up to the specified maximum_updates.

Assumes the lock is held.

Definition at line 80 of file adaptiveLru.cxx.

References update_page().

◆ do_remove_page()

void AdaptiveLru::do_remove_page ( AdaptiveLruPage page)

Removes a page from the LRU.

Definition at line 319 of file adaptiveLru.cxx.

References do_access_page().

Referenced by do_add_page().

◆ do_validate()

bool AdaptiveLru::do_validate ( )

Checks that the LRU is internally consistent.

Assume the lock is already held.

Definition at line 421 of file adaptiveLru.cxx.

References AdaptiveLruPage::evict_lru(), and AdaptiveLruPage::get_lru_size().

Referenced by do_evict_to(), and validate().

◆ evict_to()

void AdaptiveLru::evict_to ( size_t  target_size)
inline

Evicts a sequence of objects until the queue fits within the indicated target size, regardless of its normal max size.

Definition at line 78 of file adaptiveLru.I.

References do_evict_to(), and validate().

Referenced by consider_evict().

◆ get_max_size()

size_t AdaptiveLru::get_max_size ( ) const
inline

Returns the max size of all objects that are allowed to be active on the LRU.

Definition at line 35 of file adaptiveLru.I.

References set_max_size().

Referenced by PreparedGraphicsObjects::get_graphics_memory_limit(), and get_total_size().

◆ get_max_updates_per_frame()

int AdaptiveLru::get_max_updates_per_frame ( ) const
inline

Returns the maximum number of pages the AdaptiveLru will update each frame.

Definition at line 140 of file adaptiveLru.I.

References AdaptiveLruPage::get_lru().

Referenced by set_max_updates_per_frame().

◆ get_total_size()

size_t AdaptiveLru::get_total_size ( ) const
inline

Returns the total size of all objects currently active on the LRU.

Definition at line 23 of file adaptiveLru.I.

References get_max_size().

◆ get_weight()

PN_stdfloat AdaptiveLru::get_weight ( ) const
inline

Returns the weight value used to compute the exponential moving average.

Definition at line 116 of file adaptiveLru.I.

References set_max_updates_per_frame().

Referenced by set_weight().

◆ set_max_size()

void AdaptiveLru::set_max_size ( size_t  max_size)
inline

Changes the max size of all objects that are allowed to be active on the LRU.

If the size is (size_t)-1, there is no limit.

Definition at line 49 of file adaptiveLru.I.

References consider_evict(), and do_evict_to().

Referenced by get_max_size().

◆ set_max_updates_per_frame()

void AdaptiveLru::set_max_updates_per_frame ( int  max_updates_per_frame)
inline

Specifies the maximum number of pages the AdaptiveLru will update each frame.

This is a performance optimization: keeping this number low limits the impact of the AdaptiveLru's adaptive algorithm.

Definition at line 129 of file adaptiveLru.I.

References get_max_updates_per_frame().

Referenced by get_weight().

◆ set_weight()

void AdaptiveLru::set_weight ( PN_stdfloat  weight)
inline

Specifies the weight value used to compute the exponential moving average.

Definition at line 105 of file adaptiveLru.I.

References get_weight().

Referenced by validate().

◆ update_page()

void AdaptiveLru::update_page ( AdaptiveLruPage page)

This updates the page's average utilization.

Priority LPP_New is considered to be average usage of 1.0 (which means the page is used once per frame on average). Priorities < LPP_New are for pages used more than once per frame and Priorities > LPP_New are for pages used less than once per frame.

Assumes the lock is held.

Definition at line 119 of file adaptiveLru.cxx.

References AdaptiveLruPage::enqueue_lru().

Referenced by do_partial_lru_update().

◆ validate()

bool AdaptiveLru::validate ( )
inline

Checks that the LRU is internally self-consistent.

Returns true if successful, false if there is some problem.

Definition at line 93 of file adaptiveLru.I.

References do_validate(), and set_weight().

Referenced by evict_to(), and AdaptiveLruPage::get_num_inactive_frames().


The documentation for this class was generated from the following files: