OmniSciDB  72c90bc290
 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 183 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 185 of file DataRecycler.h.

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

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

References CHECK, CHECK_LE, current_cache_size_in_bytes_, and total_cache_size_.

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

Definition at line 333 of file DataRecycler.h.

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

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

+ Here is the call graph for this function:

void CacheMetricTracker::clearCacheMetricTracker ( )
inline

Definition at line 317 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().

317  {
318  for (auto& kv : current_cache_size_in_bytes_) {
319  auto cache_item_metrics = getCacheItemMetrics(kv.first);
320  if (kv.first > 0) {
321  VLOG(1) << "[" << item_type_ << "]"
322  << "] clear cache metrics (# items: " << kv.first << ", " << kv.second
323  << " bytes)";
324  }
326  CHECK_EQ(getCurrentCacheSize(kv.first).value(), 0u);
327  }
328  for (auto& kv : cache_metrics_) {
329  kv.second.clear();
330  }
331  }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
CacheItemType item_type_
Definition: DataRecycler.h:408
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:260
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:236
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:418
void updateCurrentCacheSize(DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
Definition: DataRecycler.h:354
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:415
#define VLOG(n)
Definition: Logger.h:388

+ 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 243 of file DataRecycler.h.

References cache_metrics_, and getCacheItemMetricImpl().

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

+ 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 229 of file DataRecycler.h.

References getCacheItemMetricItr().

Referenced by getCacheItemMetric(), and putNewCacheItemMetric().

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

+ 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 222 of file DataRecycler.h.

Referenced by getCacheItemMetricImpl(), and removeCacheItemMetric().

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

+ Here is the caller graph for this function:

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

Definition at line 236 of file DataRecycler.h.

References cache_metrics_, and CHECK.

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

237  {
238  auto itr = cache_metrics_.find(device_identifier);
239  CHECK(itr != cache_metrics_.end());
240  return itr->second;
241  }
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:415
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the caller graph for this function:

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

Definition at line 260 of file DataRecycler.h.

References current_cache_size_in_bytes_.

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

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

+ Here is the caller graph for this function:

size_t CacheMetricTracker::getMaxCacheItemSize ( ) const
inline

Definition at line 395 of file DataRecycler.h.

References max_cache_item_size_.

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

Definition at line 394 of file DataRecycler.h.

References total_cache_size_.

394 { 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 269 of file DataRecycler.h.

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

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

+ Here is the call graph for this function:

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

Definition at line 294 of file DataRecycler.h.

References getCacheItemMetricItr(), and getCacheItemMetrics().

Referenced by putNewCacheItemMetric().

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

+ 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 302 of file DataRecycler.h.

References getCacheItemMetrics().

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

+ Here is the call graph for this function:

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

Definition at line 251 of file DataRecycler.h.

References CHECK, current_cache_size_in_bytes_, and total_cache_size_.

Referenced by updateCurrentCacheSize().

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

+ Here is the caller graph for this function:

void CacheMetricTracker::setMaxCacheItemSize ( size_t  new_max_cache_item_size)
inline

Definition at line 401 of file DataRecycler.h.

References max_cache_item_size_.

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

401  {
402  if (new_max_cache_item_size > 0) {
403  max_cache_item_size_ = new_max_cache_item_size;
404  }
405  }
size_t max_cache_item_size_
Definition: DataRecycler.h:410

+ Here is the caller graph for this function:

void CacheMetricTracker::setTotalCacheSize ( size_t  new_total_cache_size)
inline

Definition at line 396 of file DataRecycler.h.

References total_cache_size_.

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

396  {
397  if (new_total_cache_size > 0) {
398  total_cache_size_ = new_total_cache_size;
399  }
400  }

+ Here is the caller graph for this function:

void CacheMetricTracker::sortCacheInfoByQueryMetric ( DeviceIdentifier  device_identifier)
inline

Definition at line 368 of file DataRecycler.h.

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

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

+ Here is the call graph for this function:

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

Definition at line 385 of file DataRecycler.h.

References current_cache_size_in_bytes_.

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

Definition at line 354 of file DataRecycler.h.

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

Referenced by clearCacheMetricTracker(), and putNewCacheItemMetric().

356  {
357  auto current_cache_size = getCurrentCacheSize(device_identifier);
358  CHECK(current_cache_size.has_value());
360  setCurrentCacheSize(device_identifier, current_cache_size.value() + size);
361  } else {
363  CHECK_LE(size, *current_cache_size);
364  setCurrentCacheSize(device_identifier, current_cache_size.value() - size);
365  }
366  }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:260
void setCurrentCacheSize(DeviceIdentifier device_identifier, size_t bytes)
Definition: DataRecycler.h:251
#define CHECK_LE(x, y)
Definition: Logger.h:304
#define CHECK(condition)
Definition: Logger.h:291

+ 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 408 of file DataRecycler.h.

Referenced by clearCacheMetricTracker().

size_t CacheMetricTracker::max_cache_item_size_
private

Definition at line 410 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: