OmniSciDB  ca0c39ec8f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CacheMetricTracker Class Reference

#include <DataRecycler.h>

Public Member Functions

 CacheMetricTracker (CacheItemType cache_item_type, size_t total_cache_size, size_t max_cache_item_size, int num_gpus=0)
 
std::vector< std::shared_ptr
< CacheItemMetric > > & 
getCacheItemMetrics (DeviceIdentifier device_identifier)
 
std::shared_ptr< CacheItemMetricgetCacheItemMetric (QueryPlanHash key, DeviceIdentifier device_identifier) const
 
void setCurrentCacheSize (DeviceIdentifier device_identifier, size_t bytes)
 
std::optional< size_t > getCurrentCacheSize (DeviceIdentifier key) const
 
std::shared_ptr< CacheItemMetricputNewCacheItemMetric (QueryPlanHash key, DeviceIdentifier device_identifier, size_t mem_size, size_t compute_time)
 
void removeCacheItemMetric (QueryPlanHash key, DeviceIdentifier device_identifier)
 
void removeMetricFromBeginning (DeviceIdentifier device_identifier, int offset)
 
size_t calculateRequiredSpaceForItemAddition (DeviceIdentifier device_identifier, size_t item_size) const
 
void clearCacheMetricTracker ()
 
CacheAvailability canAddItem (DeviceIdentifier device_identifier, size_t item_size) const
 
void updateCurrentCacheSize (DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
 
void sortCacheInfoByQueryMetric (DeviceIdentifier device_identifier)
 
std::string toString () const
 
size_t getTotalCacheSize () const
 
size_t getMaxCacheItemSize () const
 
void setTotalCacheSize (size_t new_total_cache_size)
 
void setMaxCacheItemSize (size_t new_max_cache_item_size)
 

Static Public Member Functions

static
CacheMetricInfoMap::mapped_type::const_iterator 
getCacheItemMetricItr (QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
 
static std::shared_ptr
< CacheItemMetric
getCacheItemMetricImpl (QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
 

Private Attributes

CacheItemType item_type_
 
size_t total_cache_size_
 
size_t max_cache_item_size_
 
CacheMetricInfoMap cache_metrics_
 
CacheSizeMap current_cache_size_in_bytes_
 

Detailed Description

Definition at line 185 of file DataRecycler.h.

Constructor & Destructor Documentation

CacheMetricTracker::CacheMetricTracker ( CacheItemType  cache_item_type,
size_t  total_cache_size,
size_t  max_cache_item_size,
int  num_gpus = 0 
)
inline

Definition at line 187 of file DataRecycler.h.

References cache_metrics_, DataRecyclerUtil::CPU_DEVICE_IDENTIFIER, current_cache_size_in_bytes_, logger::INFO, LOG, and total_cache_size_.

191  : item_type_(cache_item_type)
192  , total_cache_size_(total_cache_size)
193  , max_cache_item_size_(max_cache_item_size) {
194  // initialize cache metrics for each device: CPU, GPU0, GPU1, ...
195  // Currently we only consider maintaining our cache in CPU-memory
196  for (int gpu_device_identifier = num_gpus; gpu_device_identifier >= 1;
197  --gpu_device_identifier) {
198  cache_metrics_.emplace(gpu_device_identifier,
199  std::vector<std::shared_ptr<CacheItemMetric>>());
200  current_cache_size_in_bytes_.emplace(gpu_device_identifier, 0);
201  }
203  std::vector<std::shared_ptr<CacheItemMetric>>());
205 
206  if (total_cache_size_ < 1024 * 1024 * 256) {
207  LOG(INFO) << "The total cache size of " << cache_item_type
208  << " is set too low, so we suggest raising it larger than 256MB";
209  }
210 
211  if (max_cache_item_size < 1024 * 1024 * 10) {
212  LOG(INFO)
213  << "The maximum item size of " << cache_item_type
214  << " that can be cached is set too low, we suggest raising it larger than 10MB";
215  }
216  if (max_cache_item_size > total_cache_size_) {
217  LOG(INFO) << "The maximum item size of " << cache_item_type
218  << " is set larger than its total cache size, so we force to set the "
219  "maximum item size as equal to the total cache size";
220  max_cache_item_size = total_cache_size_;
221  }
222  }
CacheItemType item_type_
Definition: DataRecycler.h:410
#define LOG(tag)
Definition: Logger.h:216
size_t max_cache_item_size_
Definition: DataRecycler.h:412
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:420
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:417
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136

Member Function Documentation

size_t CacheMetricTracker::calculateRequiredSpaceForItemAddition ( DeviceIdentifier  device_identifier,
size_t  item_size 
) const
inline

Definition at line 309 of file DataRecycler.h.

References CHECK, CHECK_LE, current_cache_size_in_bytes_, and total_cache_size_.

310  {
311  auto it = current_cache_size_in_bytes_.find(device_identifier);
312  CHECK(it != current_cache_size_in_bytes_.end());
313  CHECK_LE(item_size, total_cache_size_);
314  const auto current_cache_size = it->second;
315  long rem = total_cache_size_ - current_cache_size;
316  return rem < 0 ? item_size : item_size - rem;
317  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:420
#define CHECK_LE(x, y)
Definition: Logger.h:233
#define CHECK(condition)
Definition: Logger.h:222
CacheAvailability CacheMetricTracker::canAddItem ( DeviceIdentifier  device_identifier,
size_t  item_size 
) const
inline

Definition at line 335 of file DataRecycler.h.

References AVAILABLE, AVAILABLE_AFTER_CLEANUP, CHECK, getCurrentCacheSize(), max_cache_item_size_, total_cache_size_, and UNAVAILABLE.

336  {
337  if (item_size > max_cache_item_size_ || item_size > total_cache_size_) {
339  }
340  // now we know that a cache can hold the new item since its size is less than
341  // per-item maximum size limit
342  // check if we need to remove some (or all) of cached item to make a room
343  // for the new item
344  auto current_cache_size = getCurrentCacheSize(device_identifier);
345  CHECK(current_cache_size.has_value());
346  auto cache_size_after_addition = *current_cache_size + item_size;
347  if (cache_size_after_addition > total_cache_size_) {
348  // if so, we need to remove the item to hold the new one within the cache
350  }
351  // cache has a sufficient space to hold the new item
352  // thus, there is no need to remove cached item
354  }
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:262
size_t max_cache_item_size_
Definition: DataRecycler.h:412
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

void CacheMetricTracker::clearCacheMetricTracker ( )
inline

Definition at line 319 of file DataRecycler.h.

References cache_metrics_, CHECK_EQ, current_cache_size_in_bytes_, getCacheItemMetrics(), getCurrentCacheSize(), item_type_, REMOVE, updateCurrentCacheSize(), and VLOG.

Referenced by ResultSetRecycler::clearCache(), and HashtableRecycler::clearCache().

319  {
320  for (auto& kv : current_cache_size_in_bytes_) {
321  auto cache_item_metrics = getCacheItemMetrics(kv.first);
322  if (kv.first > 0) {
323  VLOG(1) << "[" << item_type_ << "]"
324  << "] clear cache metrics (# items: " << kv.first << ", " << kv.second
325  << " bytes)";
326  }
328  CHECK_EQ(getCurrentCacheSize(kv.first).value(), 0u);
329  }
330  for (auto& kv : cache_metrics_) {
331  kv.second.clear();
332  }
333  }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CacheItemType item_type_
Definition: DataRecycler.h:410
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:262
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:238
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:420
void updateCurrentCacheSize(DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
Definition: DataRecycler.h:356
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:417
#define VLOG(n)
Definition: Logger.h:316

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::shared_ptr<CacheItemMetric> CacheMetricTracker::getCacheItemMetric ( QueryPlanHash  key,
DeviceIdentifier  device_identifier 
) const
inline

Definition at line 245 of file DataRecycler.h.

References cache_metrics_, and getCacheItemMetricImpl().

247  {
248  auto itr = cache_metrics_.find(device_identifier);
249  return itr == cache_metrics_.cend() ? nullptr
250  : getCacheItemMetricImpl(key, itr->second);
251  }
static std::shared_ptr< CacheItemMetric > getCacheItemMetricImpl(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:231
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:417

+ Here is the call graph for this function:

static std::shared_ptr<CacheItemMetric> CacheMetricTracker::getCacheItemMetricImpl ( QueryPlanHash  key,
CacheMetricInfoMap::mapped_type const &  metrics 
)
inlinestatic

Definition at line 231 of file DataRecycler.h.

References getCacheItemMetricItr().

Referenced by getCacheItemMetric(), and putNewCacheItemMetric().

233  {
234  auto itr = getCacheItemMetricItr(key, metrics);
235  return itr == metrics.cend() ? nullptr : *itr;
236  }
static CacheMetricInfoMap::mapped_type::const_iterator getCacheItemMetricItr(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:224

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static CacheMetricInfoMap::mapped_type::const_iterator CacheMetricTracker::getCacheItemMetricItr ( QueryPlanHash  key,
CacheMetricInfoMap::mapped_type const &  metrics 
)
inlinestatic

Definition at line 224 of file DataRecycler.h.

Referenced by getCacheItemMetricImpl(), and removeCacheItemMetric().

226  {
227  auto same_hash = [key](auto itr) { return itr->getQueryPlanHash() == key; };
228  return std::find_if(metrics.cbegin(), metrics.cend(), same_hash);
229  }

+ Here is the caller graph for this function:

std::vector<std::shared_ptr<CacheItemMetric> >& CacheMetricTracker::getCacheItemMetrics ( DeviceIdentifier  device_identifier)
inline

Definition at line 238 of file DataRecycler.h.

References cache_metrics_, and CHECK.

Referenced by clearCacheMetricTracker(), removeCacheItemMetric(), removeMetricFromBeginning(), and sortCacheInfoByQueryMetric().

239  {
240  auto itr = cache_metrics_.find(device_identifier);
241  CHECK(itr != cache_metrics_.end());
242  return itr->second;
243  }
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:417
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the caller graph for this function:

std::optional<size_t> CacheMetricTracker::getCurrentCacheSize ( DeviceIdentifier  key) const
inline

Definition at line 262 of file DataRecycler.h.

References current_cache_size_in_bytes_.

Referenced by canAddItem(), clearCacheMetricTracker(), and updateCurrentCacheSize().

262  {
263  auto same_hash = [key](auto itr) { return itr.first == key; };
264  auto itr = std::find_if(current_cache_size_in_bytes_.cbegin(),
266  same_hash);
267  return itr == current_cache_size_in_bytes_.cend() ? std::nullopt
268  : std::make_optional(itr->second);
269  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:420

+ Here is the caller graph for this function:

size_t CacheMetricTracker::getMaxCacheItemSize ( ) const
inline

Definition at line 397 of file DataRecycler.h.

References max_cache_item_size_.

397 { return max_cache_item_size_; }
size_t max_cache_item_size_
Definition: DataRecycler.h:412
size_t CacheMetricTracker::getTotalCacheSize ( ) const
inline

Definition at line 396 of file DataRecycler.h.

References total_cache_size_.

396 { return total_cache_size_; }
std::shared_ptr<CacheItemMetric> CacheMetricTracker::putNewCacheItemMetric ( QueryPlanHash  key,
DeviceIdentifier  device_identifier,
size_t  mem_size,
size_t  compute_time 
)
inline

Definition at line 271 of file DataRecycler.h.

References ADD, cache_metrics_, CHECK, getCacheItemMetricImpl(), REMOVE, removeCacheItemMetric(), and updateCurrentCacheSize().

275  {
276  auto itr = cache_metrics_.find(device_identifier);
277  CHECK(itr != cache_metrics_.end());
278  if (auto cached_metric = getCacheItemMetricImpl(key, itr->second)) {
279  if (cached_metric->getMemSize() != mem_size) {
281  device_identifier, CacheUpdateAction::REMOVE, cached_metric->getMemSize());
282  removeCacheItemMetric(key, device_identifier);
283  } else {
284  cached_metric->incRefCount();
285  return cached_metric;
286  }
287  }
288  auto cache_metric = std::make_shared<CacheItemMetric>(key, compute_time, mem_size);
289  updateCurrentCacheSize(device_identifier, CacheUpdateAction::ADD, mem_size);
290  // we add the item to cache after we create it during query runtime
291  // so it is used at least once
292  cache_metric->incRefCount();
293  return itr->second.emplace_back(std::move(cache_metric));
294  }
void removeCacheItemMetric(QueryPlanHash key, DeviceIdentifier device_identifier)
Definition: DataRecycler.h:296
void updateCurrentCacheSize(DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
Definition: DataRecycler.h:356
static std::shared_ptr< CacheItemMetric > getCacheItemMetricImpl(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:231
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:417
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

void CacheMetricTracker::removeCacheItemMetric ( QueryPlanHash  key,
DeviceIdentifier  device_identifier 
)
inline

Definition at line 296 of file DataRecycler.h.

References getCacheItemMetricItr(), and getCacheItemMetrics().

Referenced by putNewCacheItemMetric().

296  {
297  auto& cache_metrics = getCacheItemMetrics(device_identifier);
298  auto itr = getCacheItemMetricItr(key, cache_metrics);
299  if (itr != cache_metrics.cend()) {
300  cache_metrics.erase(itr);
301  }
302  }
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:238
static CacheMetricInfoMap::mapped_type::const_iterator getCacheItemMetricItr(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:224

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CacheMetricTracker::removeMetricFromBeginning ( DeviceIdentifier  device_identifier,
int  offset 
)
inline

Definition at line 304 of file DataRecycler.h.

References getCacheItemMetrics().

304  {
305  auto metrics = getCacheItemMetrics(device_identifier);
306  metrics.erase(metrics.begin(), metrics.begin() + offset);
307  }
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:238

+ Here is the call graph for this function:

void CacheMetricTracker::setCurrentCacheSize ( DeviceIdentifier  device_identifier,
size_t  bytes 
)
inline

Definition at line 253 of file DataRecycler.h.

References CHECK, current_cache_size_in_bytes_, and total_cache_size_.

Referenced by updateCurrentCacheSize().

253  {
254  if (bytes > total_cache_size_) {
255  return;
256  }
257  auto itr = current_cache_size_in_bytes_.find(device_identifier);
258  CHECK(itr != current_cache_size_in_bytes_.end());
259  itr->second = bytes;
260  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:420
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the caller graph for this function:

void CacheMetricTracker::setMaxCacheItemSize ( size_t  new_max_cache_item_size)
inline

Definition at line 403 of file DataRecycler.h.

References max_cache_item_size_.

Referenced by DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::setMaxCacheItemSize().

403  {
404  if (new_max_cache_item_size > 0) {
405  max_cache_item_size_ = new_max_cache_item_size;
406  }
407  }
size_t max_cache_item_size_
Definition: DataRecycler.h:412

+ Here is the caller graph for this function:

void CacheMetricTracker::setTotalCacheSize ( size_t  new_total_cache_size)
inline

Definition at line 398 of file DataRecycler.h.

References total_cache_size_.

Referenced by DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::setTotalCacheSize().

398  {
399  if (new_total_cache_size > 0) {
400  total_cache_size_ = new_total_cache_size;
401  }
402  }

+ Here is the caller graph for this function:

void CacheMetricTracker::sortCacheInfoByQueryMetric ( DeviceIdentifier  device_identifier)
inline

Definition at line 370 of file DataRecycler.h.

References getCacheItemMetrics(), NUM_METRIC_TYPE, and gpu_enabled::sort().

370  {
371  auto& metric_cache = getCacheItemMetrics(device_identifier);
372  std::sort(metric_cache.begin(),
373  metric_cache.end(),
374  [](const std::shared_ptr<CacheItemMetric>& left,
375  const std::shared_ptr<CacheItemMetric>& right) {
376  auto& elem1_metrics = left->getMetrics();
377  auto& elem2_metrics = right->getMetrics();
378  for (size_t i = 0; i < CacheMetricType::NUM_METRIC_TYPE; ++i) {
379  if (elem1_metrics[i] != elem2_metrics[i]) {
380  return elem1_metrics[i] < elem2_metrics[i];
381  }
382  }
383  return false;
384  });
385  }
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:238

+ Here is the call graph for this function:

std::string CacheMetricTracker::toString ( ) const
inline

Definition at line 387 of file DataRecycler.h.

References current_cache_size_in_bytes_.

387  {
388  std::ostringstream oss;
389  oss << "Current memory consumption of caches for each device:\n";
390  for (auto& kv : current_cache_size_in_bytes_) {
391  oss << "\t\tDevice " << kv.first << " : " << kv.second << " bytes\n";
392  }
393  return oss.str();
394  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:420
void CacheMetricTracker::updateCurrentCacheSize ( DeviceIdentifier  device_identifier,
CacheUpdateAction  action,
size_t  size 
)
inline

Definition at line 356 of file DataRecycler.h.

References ADD, CHECK, CHECK_EQ, CHECK_LE, getCurrentCacheSize(), REMOVE, and setCurrentCacheSize().

Referenced by clearCacheMetricTracker(), and putNewCacheItemMetric().

358  {
359  auto current_cache_size = getCurrentCacheSize(device_identifier);
360  CHECK(current_cache_size.has_value());
362  setCurrentCacheSize(device_identifier, current_cache_size.value() + size);
363  } else {
365  CHECK_LE(size, *current_cache_size);
366  setCurrentCacheSize(device_identifier, current_cache_size.value() - size);
367  }
368  }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:262
void setCurrentCacheSize(DeviceIdentifier device_identifier, size_t bytes)
Definition: DataRecycler.h:253
#define CHECK_LE(x, y)
Definition: Logger.h:233
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

CacheMetricInfoMap CacheMetricTracker::cache_metrics_
private
CacheSizeMap CacheMetricTracker::current_cache_size_in_bytes_
private
CacheItemType CacheMetricTracker::item_type_
private

Definition at line 410 of file DataRecycler.h.

Referenced by clearCacheMetricTracker().

size_t CacheMetricTracker::max_cache_item_size_
private

Definition at line 412 of file DataRecycler.h.

Referenced by canAddItem(), getMaxCacheItemSize(), and setMaxCacheItemSize().

size_t CacheMetricTracker::total_cache_size_
private

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