OmniSciDB  085a039ca4
 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 149 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 151 of file DataRecycler.h.

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

155  : item_type_(cache_item_type)
156  , total_cache_size_(total_cache_size)
157  , max_cache_item_size_(max_cache_item_size) {
158  // initialize cache metrics for each device: CPU, GPU0, GPU1, ...
159  // Currently we only consider maintaining our cache in CPU-memory
160  for (int gpu_device_identifier = num_gpus; gpu_device_identifier >= 1;
161  --gpu_device_identifier) {
162  cache_metrics_.emplace(gpu_device_identifier,
163  std::vector<std::shared_ptr<CacheItemMetric>>());
164  current_cache_size_in_bytes_.emplace(gpu_device_identifier, 0);
165  }
167  std::vector<std::shared_ptr<CacheItemMetric>>());
169 
170  if (total_cache_size_ < 1024 * 1024 * 256) {
171  LOG(INFO) << "The total cache size of " << cache_item_type
172  << " is set too low, so we suggest raising it larger than 256MB";
173  }
174 
175  if (max_cache_item_size < 1024 * 1024 * 10) {
176  LOG(INFO)
177  << "The maximum item size of " << cache_item_type
178  << " that can be cached is set too low, we suggest raising it larger than 10MB";
179  }
180  if (max_cache_item_size > total_cache_size_) {
181  LOG(INFO) << "The maximum item size of " << cache_item_type
182  << " is set larger than its total cache size, so we force to set the "
183  "maximum item size as equal to the total cache size";
184  max_cache_item_size = total_cache_size_;
185  }
186  }
CacheItemType item_type_
Definition: DataRecycler.h:372
#define LOG(tag)
Definition: Logger.h:217
size_t max_cache_item_size_
Definition: DataRecycler.h:374
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:382
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:379
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 273 of file DataRecycler.h.

References CHECK, CHECK_LE, current_cache_size_in_bytes_, and total_cache_size_.

274  {
275  auto it = current_cache_size_in_bytes_.find(device_identifier);
276  CHECK(it != current_cache_size_in_bytes_.end());
277  CHECK_LE(item_size, total_cache_size_);
278  const auto current_cache_size = it->second;
279  long rem = total_cache_size_ - current_cache_size;
280  return rem < 0 ? item_size : item_size - rem;
281  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:382
#define CHECK_LE(x, y)
Definition: Logger.h:234
#define CHECK(condition)
Definition: Logger.h:223
CacheAvailability CacheMetricTracker::canAddItem ( DeviceIdentifier  device_identifier,
size_t  item_size 
) const
inline

Definition at line 297 of file DataRecycler.h.

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

298  {
299  if (item_size > max_cache_item_size_ || item_size > total_cache_size_) {
301  }
302  // now we know that a cache can hold the new item since its size is less than
303  // per-item maximum size limit
304  // check if we need to remove some (or all) of cached item to make a room
305  // for the new item
306  auto current_cache_size = getCurrentCacheSize(device_identifier);
307  CHECK(current_cache_size.has_value());
308  auto cache_size_after_addition = *current_cache_size + item_size;
309  if (cache_size_after_addition > total_cache_size_) {
310  // if so, we need to remove the item to hold the new one within the cache
312  }
313  // cache has a sufficient space to hold the new item
314  // thus, there is no need to remove cached item
316  }
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:226
size_t max_cache_item_size_
Definition: DataRecycler.h:374
#define CHECK(condition)
Definition: Logger.h:223

+ Here is the call graph for this function:

void CacheMetricTracker::clearCacheMetricTracker ( )
inline

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

283  {
284  for (auto& kv : current_cache_size_in_bytes_) {
285  auto cache_item_metrics = getCacheItemMetrics(kv.first);
286  VLOG(1) << "Clear cache of " << item_type_ << " from device [" << kv.first
287  << "] (# cached items: " << cache_item_metrics.size() << ", " << kv.second
288  << " bytes)";
290  CHECK_EQ(getCurrentCacheSize(kv.first).value(), 0u);
291  }
292  for (auto& kv : cache_metrics_) {
293  kv.second.clear();
294  }
295  }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
CacheItemType item_type_
Definition: DataRecycler.h:372
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:226
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:202
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:382
void updateCurrentCacheSize(DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
Definition: DataRecycler.h:318
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:379
#define VLOG(n)
Definition: Logger.h:317

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

References cache_metrics_, and getCacheItemMetricImpl().

211  {
212  auto itr = cache_metrics_.find(device_identifier);
213  return itr == cache_metrics_.cend() ? nullptr
214  : getCacheItemMetricImpl(key, itr->second);
215  }
static std::shared_ptr< CacheItemMetric > getCacheItemMetricImpl(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:195
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:379

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

References getCacheItemMetricItr().

Referenced by getCacheItemMetric(), and putNewCacheItemMetric().

197  {
198  auto itr = getCacheItemMetricItr(key, metrics);
199  return itr == metrics.cend() ? nullptr : *itr;
200  }
static CacheMetricInfoMap::mapped_type::const_iterator getCacheItemMetricItr(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:188

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

Referenced by getCacheItemMetricImpl(), and removeCacheItemMetric().

190  {
191  auto same_hash = [key](auto itr) { return itr->getQueryPlanHash() == key; };
192  return std::find_if(metrics.cbegin(), metrics.cend(), same_hash);
193  }

+ Here is the caller graph for this function:

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

Definition at line 202 of file DataRecycler.h.

References cache_metrics_, and CHECK.

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

203  {
204  auto itr = cache_metrics_.find(device_identifier);
205  CHECK(itr != cache_metrics_.end());
206  return itr->second;
207  }
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:379
#define CHECK(condition)
Definition: Logger.h:223

+ Here is the caller graph for this function:

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

Definition at line 226 of file DataRecycler.h.

References current_cache_size_in_bytes_.

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

226  {
227  auto same_hash = [key](auto itr) { return itr.first == key; };
228  auto itr = std::find_if(current_cache_size_in_bytes_.cbegin(),
230  same_hash);
231  return itr == current_cache_size_in_bytes_.cend() ? std::nullopt
232  : std::make_optional(itr->second);
233  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:382

+ Here is the caller graph for this function:

size_t CacheMetricTracker::getMaxCacheItemSize ( ) const
inline

Definition at line 359 of file DataRecycler.h.

References max_cache_item_size_.

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

Definition at line 358 of file DataRecycler.h.

References total_cache_size_.

358 { 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 235 of file DataRecycler.h.

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

239  {
240  auto itr = cache_metrics_.find(device_identifier);
241  CHECK(itr != cache_metrics_.end());
242  if (auto cached_metric = getCacheItemMetricImpl(key, itr->second)) {
243  if (cached_metric->getMemSize() != mem_size) {
245  device_identifier, CacheUpdateAction::REMOVE, cached_metric->getMemSize());
246  removeCacheItemMetric(key, device_identifier);
247  } else {
248  cached_metric->incRefCount();
249  return cached_metric;
250  }
251  }
252  auto cache_metric = std::make_shared<CacheItemMetric>(key, compute_time, mem_size);
253  updateCurrentCacheSize(device_identifier, CacheUpdateAction::ADD, mem_size);
254  // we add the item to cache after we create it during query runtime
255  // so it is used at least once
256  cache_metric->incRefCount();
257  return itr->second.emplace_back(std::move(cache_metric));
258  }
void removeCacheItemMetric(QueryPlanHash key, DeviceIdentifier device_identifier)
Definition: DataRecycler.h:260
void updateCurrentCacheSize(DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
Definition: DataRecycler.h:318
static std::shared_ptr< CacheItemMetric > getCacheItemMetricImpl(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:195
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:379
#define CHECK(condition)
Definition: Logger.h:223

+ Here is the call graph for this function:

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

Definition at line 260 of file DataRecycler.h.

References getCacheItemMetricItr(), and getCacheItemMetrics().

Referenced by putNewCacheItemMetric().

260  {
261  auto& cache_metrics = getCacheItemMetrics(device_identifier);
262  auto itr = getCacheItemMetricItr(key, cache_metrics);
263  if (itr != cache_metrics.cend()) {
264  cache_metrics.erase(itr);
265  }
266  }
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:202
static CacheMetricInfoMap::mapped_type::const_iterator getCacheItemMetricItr(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:188

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

References getCacheItemMetrics().

268  {
269  auto metrics = getCacheItemMetrics(device_identifier);
270  metrics.erase(metrics.begin(), metrics.begin() + offset);
271  }
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:202

+ Here is the call graph for this function:

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

Definition at line 217 of file DataRecycler.h.

References CHECK, current_cache_size_in_bytes_, and total_cache_size_.

Referenced by updateCurrentCacheSize().

217  {
218  if (bytes > total_cache_size_) {
219  return;
220  }
221  auto itr = current_cache_size_in_bytes_.find(device_identifier);
222  CHECK(itr != current_cache_size_in_bytes_.end());
223  itr->second = bytes;
224  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:382
#define CHECK(condition)
Definition: Logger.h:223

+ Here is the caller graph for this function:

void CacheMetricTracker::setMaxCacheItemSize ( size_t  new_max_cache_item_size)
inline

Definition at line 365 of file DataRecycler.h.

References max_cache_item_size_.

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

365  {
366  if (new_max_cache_item_size > 0) {
367  max_cache_item_size_ = new_max_cache_item_size;
368  }
369  }
size_t max_cache_item_size_
Definition: DataRecycler.h:374

+ Here is the caller graph for this function:

void CacheMetricTracker::setTotalCacheSize ( size_t  new_total_cache_size)
inline

Definition at line 360 of file DataRecycler.h.

References total_cache_size_.

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

360  {
361  if (new_total_cache_size > 0) {
362  total_cache_size_ = new_total_cache_size;
363  }
364  }

+ Here is the caller graph for this function:

void CacheMetricTracker::sortCacheInfoByQueryMetric ( DeviceIdentifier  device_identifier)
inline

Definition at line 332 of file DataRecycler.h.

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

332  {
333  auto& metric_cache = getCacheItemMetrics(device_identifier);
334  std::sort(metric_cache.begin(),
335  metric_cache.end(),
336  [](const std::shared_ptr<CacheItemMetric>& left,
337  const std::shared_ptr<CacheItemMetric>& right) {
338  auto& elem1_metrics = left->getMetrics();
339  auto& elem2_metrics = right->getMetrics();
340  for (size_t i = 0; i < CacheMetricType::NUM_METRIC_TYPE; ++i) {
341  if (elem1_metrics[i] != elem2_metrics[i]) {
342  return elem1_metrics[i] < elem2_metrics[i];
343  }
344  }
345  return false;
346  });
347  }
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:202

+ Here is the call graph for this function:

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

Definition at line 349 of file DataRecycler.h.

References current_cache_size_in_bytes_.

349  {
350  std::ostringstream oss;
351  oss << "Current memory consumption of caches for each device:\n";
352  for (auto& kv : current_cache_size_in_bytes_) {
353  oss << "\t\tDevice " << kv.first << " : " << kv.second << " bytes\n";
354  }
355  return oss.str();
356  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:382
void CacheMetricTracker::updateCurrentCacheSize ( DeviceIdentifier  device_identifier,
CacheUpdateAction  action,
size_t  size 
)
inline

Definition at line 318 of file DataRecycler.h.

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

Referenced by clearCacheMetricTracker(), and putNewCacheItemMetric().

320  {
321  auto current_cache_size = getCurrentCacheSize(device_identifier);
322  CHECK(current_cache_size.has_value());
324  setCurrentCacheSize(device_identifier, current_cache_size.value() + size);
325  } else {
327  CHECK_LE(size, *current_cache_size);
328  setCurrentCacheSize(device_identifier, current_cache_size.value() - size);
329  }
330  }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:226
void setCurrentCacheSize(DeviceIdentifier device_identifier, size_t bytes)
Definition: DataRecycler.h:217
#define CHECK_LE(x, y)
Definition: Logger.h:234
#define CHECK(condition)
Definition: Logger.h:223

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

Referenced by clearCacheMetricTracker().

size_t CacheMetricTracker::max_cache_item_size_
private

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