The Pedigree Project 0.1
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
CacheManager Class Reference

#include <Cache.h>

+ Inheritance diagram for CacheManager:
+ Collaboration diagram for CacheManager:

Classes

struct  CacheRequest
 
struct  TimerStamp
 

Public Member Functions

void initialise ()
 
MUST_USE_RESULT bool shutdown ()
 
void registerCache (Cache *pCache)
 
void unregisterCache (Cache *pCache)
 
bool trimAll (size_t count=1)
 
virtual void timer (uint64_t delta)
 
void trimThread ()
 
- Public Member Functions inherited from RequestQueue
 RequestQueue (const String &name)
 
virtual void destroy ()
 
MUST_USE_RESULT uint64_t addRequest (size_t priority, uint64_t p1=0, uint64_t p2=0, uint64_t p3=0, uint64_t p4=0, uint64_t p5=0, uint64_t p6=0, uint64_t p7=0, uint64_t p8=0)
 
MUST_USE_RESULT uint64_t addRequest (size_t priority, ActionOnDuplicate action, uint64_t p1=0, uint64_t p2=0, uint64_t p3=0, uint64_t p4=0, uint64_t p5=0, uint64_t p6=0, uint64_t p7=0, uint64_t p8=0)
 
uint64_t addAsyncRequest (size_t priority, uint64_t p1=0, uint64_t p2=0, uint64_t p3=0, uint64_t p4=0, uint64_t p5=0, uint64_t p6=0, uint64_t p7=0, uint64_t p8=0)
 
bool canWaitForCompletion ()
 
MUST_USE_RESULT bool waitForPreallocated (PreallocatedRequest &request)
 
MUST_USE_RESULT PreallocatedPublishResult publishPreallocated (PreallocatedRequest &request, size_t priority, uint64_t p1=0, uint64_t p2=0, uint64_t p3=0, uint64_t p4=0, uint64_t p5=0, uint64_t p6=0, uint64_t p7=0, uint64_t p8=0)
 
MUST_USE_RESULT PreallocatedPublishResult republishPreallocatedWhileReleasing (PreallocatedRequest &request, size_t priority, uint64_t p1=0, uint64_t p2=0, uint64_t p3=0, uint64_t p4=0, uint64_t p5=0, uint64_t p6=0, uint64_t p7=0, uint64_t p8=0)
 
MUST_USE_RESULT bool halt ()
 
MUST_USE_RESULT bool resume ()
 
LifecycleState getLifecycleState ()
 
bool drain ()
 

Static Public Member Functions

static CacheManagerinstance ()
 
static void destroyInstance ()
 

Private Member Functions

void stopPeriodicWork ()
 
void timerTick (uint64_t delta, bool memoryPressure)
 
bool findNextCache (uint64_t afterId, uint64_t maximumId, Cache *&cache, uint64_t &cacheId, bool timersOnly=false)
 
bool takeTimerStamp (TimerStamp &stamp)
 
void dispatchTimer (Cache *cache, const TimerStamp &stamp)
 
bool callbackContext () const
 
bool acquireCache (Cache *cache, uint64_t &generation, OperationBarrier::Lease &lease)
 
bool acquireNextCache (uint64_t afterId, uint64_t maximumId, Cache *&cache, uint64_t &cacheId, OperationBarrier::Lease &lease, bool timersOnly=false)
 
uint64_t cacheGenerationWatermark ()
 
uint64_t addCacheRequest (Cache *cache, bool asynchronous, CacheConstants::CallbackCause cause, uintptr_t key, uintptr_t location=0, bool transferredPin=false, bool onlyIfDirty=false, bool batch=false)
 
virtual uint64_t executeRequest (uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8)
 
virtual void cancelRequest (const Request &request)
 
virtual bool compareRequests (const Request &a, const Request &b)
 

Private Attributes

Tree< uint64_t, Cache * > m_Caches
 
uint64_t m_NextCacheId
 
TimerStamp m_TimerClock
 
uint64_t m_TrimDelta
 
Mutex m_CachesLock
 
Threadm_pTrimThread
 
WaitQueue m_TrimWaiters
 
bool m_bTrimRequested
 
bool m_bActive
 
Timerm_pTimer
 
Atomic< size_t > m_TerminalState
 

Static Private Attributes

static CacheManagerm_Instance = nullptr
 

Friends

class Cache
 
class CacheManagerTestPeer
 

Additional Inherited Members

- Public Types inherited from RequestQueue
enum class  OverrunStatus { Clear , Armed , Stalled , Overloaded }
 
enum class  PreallocatedPublishResult {
  Accepted , TokenBusy , QueueStopped , QueueFull ,
  InvalidPriority
}
 
enum  ActionOnDuplicate { Block , NewRequest , ReturnImmediately }
 
enum class  LifecycleState { Stopped , Accepting , Stopping , Destroyed }
 
- Protected Types inherited from RequestQueue
enum class  NextRequestResult { Item , Empty , Retry }
 
- Protected Member Functions inherited from RequestQueue
virtual bool workerPlacement (ThreadPlacement &) const
 
 RequestQueue (const RequestQueue &)
 
void operator= (const RequestQueue &)
 
int work ()
 
NextRequestResult getNextRequest (Request *&request)
 
bool drainIntakeLocked (size_t priority)
 
void publishRequest (Request *request)
 
RequestfindDuplicate (const Request &request)
 
void discardRequest (Request *request)
 
uint64_t addAsyncRequestInternal (size_t priority, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8)
 
PreallocatedPublishResult publishPreallocatedRequest (PreallocatedRequest &request, PreallocatedRequest::State availableState, size_t priority, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8)
 
void releasePreallocatedRequest (Request *request)
 
void invokeCancelRequest (const Request &request)
 
bool callbackActiveOnCurrentThread () const
 
void releaseRequest (Request *request)
 
uint64_t waitForRequest (Request *request)
 
bool startWorker ()
 
bool stopWorker ()
 
void closePreallocatedAdmission ()
 
void waitForPreallocatedPublishers ()
 
- Static Protected Member Functions inherited from RequestQueue
static int trampoline (void *p)
 
static void completeRequest (Request *request, uint64_t returnValue, bool rejected)
 
static void retainRequest (Request *request)
 
- Protected Attributes inherited from RequestQueue
IntakeLane m_IntakeLanes [REQUEST_QUEUE_NUM_PRIORITIES]
 
Requestm_pRequestQueue [REQUEST_QUEUE_NUM_PRIORITIES]
 
Requestm_pRequestQueueTail [REQUEST_QUEUE_NUM_PRIORITIES]
 
Requestm_pActiveRequest
 
Atomic< size_t > m_State
 
Mutex m_LifecycleMutex
 
WaitQueue m_RequestQueueWaiters
 
WaitQueue m_WorkerWaiters
 
Threadm_pThread
 
Atomic< PerProcessorScheduler * > m_pWorkerScheduler
 
SchedulerWorkerWake m_WorkerWake
 
Atomic< size_t > m_bWorkerReady
 
Atomic< size_t > m_bWorkerActive
 
size_t m_WorkerProgressGeneration
 
RequestQueueOverrunChecker m_OverrunChecker
 
Timerm_pOverrunTimer
 
Atomic< size_t > m_PublicationState
 
size_t m_nMaxAsyncRequests
 
Atomic< size_t > m_nAsyncRequests
 
Atomic< size_t > m_nTotalRequests
 
Atomic< size_t > m_nActiveRequests
 
NormalStaticString m_Name
 
- Static Protected Attributes inherited from RequestQueue
static constexpr size_t PublicationClosed = static_cast<size_t>(1) << ((sizeof(size_t) * 8) - 1)
 
static constexpr size_t PublicationCountMask = ~PublicationClosed
 

Detailed Description

Provides a clean abstraction to a set of data caches.

Definition at line 66 of file Cache.h.

Constructor & Destructor Documentation

◆ CacheManager()

CacheManager::CacheManager ( )

Definition at line 65 of file Cache.cc.

◆ ~CacheManager()

CacheManager::~CacheManager ( )
virtual

Definition at line 82 of file Cache.cc.

Member Function Documentation

◆ acquireCache()

bool CacheManager::acquireCache ( Cache cache,
uint64_t &  generation,
OperationBarrier::Lease lease 
)
private

Pins a registered Cache while a request is being published.

Definition at line 382 of file Cache.cc.

References Tree< K, E >::begin(), Tree< K, E >::end(), and m_CachesLock.

Referenced by addCacheRequest().

+ Here is the caller graph for this function:

◆ acquireNextCache()

bool CacheManager::acquireNextCache ( uint64_t  afterId,
uint64_t  maximumId,
Cache *&  cache,
uint64_t &  cacheId,
OperationBarrier::Lease lease,
bool  timersOnly = false 
)
private

Finds and pins the first registered cache after a stable manager ID.

Definition at line 400 of file Cache.cc.

References findNextCache(), and m_CachesLock.

Referenced by shutdown(), and trimAll().

+ Here is the caller graph for this function:

◆ addCacheRequest()

uint64_t CacheManager::addCacheRequest ( Cache cache,
bool  asynchronous,
CacheConstants::CallbackCause  cause,
uintptr_t  key,
uintptr_t  location = 0,
bool  transferredPin = false,
bool  onlyIfDirty = false,
bool  batch = false 
)
private

Publishes a request which owns the target Cache lifetime.

Definition at line 421 of file Cache.cc.

References acquireCache(), RequestQueue::addAsyncRequest(), RequestQueue::addRequest(), RequestQueue::callbackActiveOnCurrentThread(), Mutex::isOwnedByCurrentThread(), RequestQueue::m_LifecycleMutex, and m_TerminalState.

Referenced by Cache::release(), Cache::sync(), and Cache::timer().

+ Here is the caller graph for this function:

◆ cacheGenerationWatermark()

uint64_t CacheManager::cacheGenerationWatermark ( )
private

Captures the last identity present at the start of a manager scan.

Definition at line 415 of file Cache.cc.

References m_CachesLock, and m_NextCacheId.

Referenced by shutdown(), and trimAll().

+ Here is the caller graph for this function:

◆ callbackContext()

bool CacheManager::callbackContext ( ) const
inlineprivate

Definition at line 142 of file Cache.h.

◆ cancelRequest()

void CacheManager::cancelRequest ( const Request request)
privatevirtual

Releases payload ownership for a request that will not execute.

This runs without the queue guard for rejected candidates (including stopped queues, duplicates and capacity limits) and queued requests cancelled by destroy(). Implementations used by allocation-free producers must not sleep. Derived destructors must call destroy() while their override and member state are still alive. During destroy(), the lifecycle is serialized: halt() and resume() on this queue return false, while recursive destroy() is a fatal contract violation.

Reimplemented from RequestQueue.

Definition at line 513 of file Cache.cc.

◆ compareRequests()

virtual bool CacheManager::compareRequests ( const Request a,
const Request b 
)
inlineprivatevirtual

Used to ensure we only ever fire a WriteBack for the same page once - that is, we don't constantly write back the same page over and over while it's still queued.

Reimplemented from RequestQueue.

Definition at line 175 of file Cache.h.

◆ destroyInstance()

static void CacheManager::destroyInstance ( )
inlinestatic

Definition at line 85 of file Cache.h.

◆ dispatchTimer()

void CacheManager::dispatchTimer ( Cache cache,
const TimerStamp stamp 
)
private

Definition at line 359 of file Cache.cc.

◆ executeRequest()

uint64_t CacheManager::executeRequest ( uint64_t  p1,
uint64_t  p2,
uint64_t  p3,
uint64_t  p4,
uint64_t  p5,
uint64_t  p6,
uint64_t  p7,
uint64_t  p8 
)
privatevirtual

RequestQueue doer - children give us new jobs, and we call out to them when they hit the front of the queue.

Implements RequestQueue.

Definition at line 480 of file Cache.cc.

References Tree< K, E >::begin(), Tree< K, E >::end(), and Cache::executeRequest().

◆ findNextCache()

bool CacheManager::findNextCache ( uint64_t  afterId,
uint64_t  maximumId,
Cache *&  cache,
uint64_t &  cacheId,
bool  timersOnly = false 
)
private

Registry lock must be held in threaded builds.

Definition at line 367 of file Cache.cc.

References Tree< K, E >::lowerBound().

Referenced by acquireNextCache(), shutdown(), and trimAll().

+ Here is the caller graph for this function:

◆ initialise()

void CacheManager::initialise ( )
virtual

Initialises the queue, spawning the worker thread.

Reimplemented from RequestQueue.

Definition at line 192 of file Cache.cc.

References Process::getParent(), Machine::getTimer(), ProcessorBase::information(), RequestQueue::initialise(), and m_bActive.

◆ instance()

static CacheManager & CacheManager::instance ( )
inlinestatic

Definition at line 78 of file Cache.h.

◆ registerCache()

void CacheManager::registerCache ( Cache pCache)

Definition at line 218 of file Cache.cc.

◆ shutdown()

bool CacheManager::shutdown ( )

Stops background work and flushes dirty pages for terminal system shutdown. External cache users must already be quiesced; backend callbacks must remain usable. Retained owners and their page loans keep their storage and registry alive. This does not relax the ordinary destructor's empty-registry contract.

Definition at line 126 of file Cache.cc.

References acquireNextCache(), cacheGenerationWatermark(), Tree< K, E >::count(), RequestQueue::destroy(), findNextCache(), m_CachesLock, m_NextCacheId, and m_TerminalState.

◆ stopPeriodicWork()

void CacheManager::stopPeriodicWork ( )
private

Definition at line 101 of file Cache.cc.

◆ takeTimerStamp()

bool CacheManager::takeTimerStamp ( TimerStamp stamp)
private

Timer waiter lock must be held in threaded builds.

Definition at line 349 of file Cache.cc.

◆ timer()

void CacheManager::timer ( uint64_t  delta)
virtual

Called when the handler is registered with the Timer class and a timer event occurred.

Parameters
[in]deltatime elapsed since the last event, in nanoseconds.

Implements TimerHandler.

Definition at line 292 of file Cache.cc.

References PhysicalMemoryManager::freePageCount(), PhysicalMemoryManager::instance(), and m_TerminalState.

◆ timerTick()

void CacheManager::timerTick ( uint64_t  delta,
bool  memoryPressure 
)
private

Definition at line 305 of file Cache.cc.

◆ trimAll()

bool CacheManager::trimAll ( size_t  count = 1)

Trim each cache we know about until 'count' pages have been evicted.

Definition at line 256 of file Cache.cc.

References acquireNextCache(), cacheGenerationWatermark(), findNextCache(), m_NextCacheId, and m_TerminalState.

◆ trimThread()

void CacheManager::trimThread ( )

Definition at line 539 of file Cache.cc.

◆ unregisterCache()

void CacheManager::unregisterCache ( Cache pCache)

Definition at line 240 of file Cache.cc.

Friends And Related Symbol Documentation

◆ Cache

friend class Cache
friend

Definition at line 71 of file Cache.h.

◆ CacheManagerTestPeer

friend class CacheManagerTestPeer
friend

Definition at line 72 of file Cache.h.

Member Data Documentation

◆ m_bActive

bool CacheManager::m_bActive
private

Protected by m_TrimWaiters when threading is enabled.

Definition at line 199 of file Cache.h.

Referenced by initialise().

◆ m_bTrimRequested

bool CacheManager::m_bTrimRequested
private

Definition at line 195 of file Cache.h.

◆ m_Caches

Tree<uint64_t, Cache*> CacheManager::m_Caches
private

Definition at line 182 of file Cache.h.

◆ m_CachesLock

Mutex CacheManager::m_CachesLock
private

Serialises cache registration with callback admission.

Definition at line 191 of file Cache.h.

Referenced by acquireCache(), acquireNextCache(), cacheGenerationWatermark(), and shutdown().

◆ m_Instance

CacheManager * CacheManager::m_Instance = nullptr
staticprivate

Definition at line 180 of file Cache.h.

◆ m_NextCacheId

uint64_t CacheManager::m_NextCacheId
private

Monotonic identity used to walk caches without retaining iterators.

Definition at line 185 of file Cache.h.

Referenced by cacheGenerationWatermark(), shutdown(), and trimAll().

◆ m_pTimer

Timer* CacheManager::m_pTimer
private

Definition at line 201 of file Cache.h.

◆ m_pTrimThread

Thread* CacheManager::m_pTrimThread
private

Definition at line 193 of file Cache.h.

◆ m_TerminalState

Atomic<size_t> CacheManager::m_TerminalState
private

0 running, 1 terminal drain, 2 completed, 3 writeback failure.

Definition at line 203 of file Cache.h.

Referenced by addCacheRequest(), shutdown(), timer(), and trimAll().

◆ m_TimerClock

TimerStamp CacheManager::m_TimerClock
private

Definition at line 186 of file Cache.h.

◆ m_TrimDelta

uint64_t CacheManager::m_TrimDelta
private

Definition at line 187 of file Cache.h.

◆ m_TrimWaiters

WaitQueue CacheManager::m_TrimWaiters
private

Definition at line 194 of file Cache.h.


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