OmniSciDB  d2f719934e
 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 145 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 147 of file DataRecycler.h.

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

151  : item_type_(cache_item_type)
152  , total_cache_size_(total_cache_size)
153  , max_cache_item_size_(max_cache_item_size) {
154  // initialize cache metrics for each device: CPU, GPU0, GPU1, ...
155  // Currently we only consider maintaining our cache in CPU-memory
156  for (int gpu_device_identifier = num_gpus; gpu_device_identifier >= 1;
157  --gpu_device_identifier) {
158  cache_metrics_.emplace(gpu_device_identifier,
159  std::vector<std::shared_ptr<CacheItemMetric>>());
160  current_cache_size_in_bytes_.emplace(gpu_device_identifier, 0);
161  }
163  std::vector<std::shared_ptr<CacheItemMetric>>());
165 
166  if (total_cache_size_ < 1024 * 1024 * 256) {
167  LOG(INFO) << "The total cache size of "
168  << DataRecyclerUtil::toStringCacheItemType(cache_item_type)
169  << " is set too low, so we suggest raising it larger than 256MB";
170  }
171 
172  if (max_cache_item_size < 1024 * 1024 * 10) {
173  LOG(INFO)
174  << "The maximum item size of "
175  << DataRecyclerUtil::toStringCacheItemType(cache_item_type)
176  << " that can be cached is set too low, we suggest raising it larger than 10MB";
177  }
178  if (max_cache_item_size > total_cache_size_) {
179  LOG(INFO) << "The maximum item size of "
180  << DataRecyclerUtil::toStringCacheItemType(cache_item_type)
181  << " is set larger than its total cache size, so we force to set the "
182  "maximum item size as equal to the total cache size";
183  max_cache_item_size = total_cache_size_;
184  }
185  }
static std::string_view toStringCacheItemType(CacheItemType item_type)
Definition: DataRecycler.h:127
CacheItemType item_type_
Definition: DataRecycler.h:359
#define LOG(tag)
Definition: Logger.h:205
size_t max_cache_item_size_
Definition: DataRecycler.h:361
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:369
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:366
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:132

+ Here is the call graph for this function:

Member Function Documentation

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

Definition at line 264 of file DataRecycler.h.

References CHECK, CHECK_GT, current_cache_size_in_bytes_, and total_cache_size_.

265  {
266  auto it = current_cache_size_in_bytes_.find(device_identifier);
267  CHECK(it != current_cache_size_in_bytes_.end());
268  auto rem = total_cache_size_ - it->second;
269  CHECK_GT(item_size, rem);
270  return item_size - rem;
271  }
#define CHECK_GT(x, y)
Definition: Logger.h:223
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:369
#define CHECK(condition)
Definition: Logger.h:211
CacheAvailability CacheMetricTracker::canAddItem ( DeviceIdentifier  device_identifier,
size_t  item_size 
) const
inline

Definition at line 288 of file DataRecycler.h.

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

289  {
290  if (item_size > max_cache_item_size_) {
292  }
293  auto current_cache_size = getCurrentCacheSize(device_identifier);
294  CHECK(current_cache_size.has_value());
295  if (*current_cache_size > total_cache_size_) {
297  }
298  auto cache_size_after_addition = *current_cache_size + item_size;
299  if (cache_size_after_addition > total_cache_size_) {
301  }
303  }
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:225
size_t max_cache_item_size_
Definition: DataRecycler.h:361
#define CHECK(condition)
Definition: Logger.h:211

+ Here is the call graph for this function:

void CacheMetricTracker::clearCacheMetricTracker ( )
inline

Definition at line 273 of file DataRecycler.h.

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

Referenced by HashtableRecycler::clearCache().

273  {
274  for (auto& kv : current_cache_size_in_bytes_) {
275  auto cache_item_metrics = getCacheItemMetrics(kv.first);
276  VLOG(1) << "Clear cache of " << DataRecyclerUtil::toStringCacheItemType(item_type_)
277  << " from device [" << kv.first
278  << "] (# cached items: " << cache_item_metrics.size() << ", " << kv.second
279  << " bytes)";
281  CHECK_EQ(getCurrentCacheSize(kv.first).value(), 0u);
282  }
283  for (auto& kv : cache_metrics_) {
284  kv.second.clear();
285  }
286  }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
static std::string_view toStringCacheItemType(CacheItemType item_type)
Definition: DataRecycler.h:127
CacheItemType item_type_
Definition: DataRecycler.h:359
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:225
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:201
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:369
void updateCurrentCacheSize(DeviceIdentifier device_identifier, CacheUpdateAction action, size_t size)
Definition: DataRecycler.h:305
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:366
#define VLOG(n)
Definition: Logger.h:305

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

References cache_metrics_, and getCacheItemMetricImpl().

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

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

References getCacheItemMetricItr().

Referenced by getCacheItemMetric(), and putNewCacheItemMetric().

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

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

Referenced by getCacheItemMetricImpl(), and removeCacheItemMetric().

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

+ Here is the caller graph for this function:

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

Definition at line 201 of file DataRecycler.h.

References cache_metrics_, and CHECK.

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

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

+ Here is the caller graph for this function:

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

Definition at line 225 of file DataRecycler.h.

References current_cache_size_in_bytes_.

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

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

+ Here is the caller graph for this function:

size_t CacheMetricTracker::getMaxCacheItemSize ( ) const
inline

Definition at line 346 of file DataRecycler.h.

References max_cache_item_size_.

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

Definition at line 345 of file DataRecycler.h.

References total_cache_size_.

345 { 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 234 of file DataRecycler.h.

References cache_metrics_, CHECK, and getCacheItemMetricImpl().

238  {
239  auto itr = cache_metrics_.find(device_identifier);
240  CHECK(itr != cache_metrics_.end());
241  if (auto cached_metric = getCacheItemMetricImpl(key, itr->second)) {
242  return cached_metric;
243  }
244  auto cache_metric = std::make_shared<CacheItemMetric>(key, compute_time, mem_size);
245  // we add the item to cache after we create it during query runtime
246  // so it is used at least once
247  cache_metric->incRefCount();
248  return itr->second.emplace_back(std::move(cache_metric));
249  }
static std::shared_ptr< CacheItemMetric > getCacheItemMetricImpl(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:194
CacheMetricInfoMap cache_metrics_
Definition: DataRecycler.h:366
#define CHECK(condition)
Definition: Logger.h:211

+ Here is the call graph for this function:

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

Definition at line 251 of file DataRecycler.h.

References getCacheItemMetricItr(), and getCacheItemMetrics().

251  {
252  auto& cache_metrics = getCacheItemMetrics(device_identifier);
253  auto itr = getCacheItemMetricItr(key, cache_metrics);
254  if (itr != cache_metrics.cend()) {
255  cache_metrics.erase(itr);
256  }
257  }
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:201
static CacheMetricInfoMap::mapped_type::const_iterator getCacheItemMetricItr(QueryPlanHash key, CacheMetricInfoMap::mapped_type const &metrics)
Definition: DataRecycler.h:187

+ Here is the call graph for this function:

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

Definition at line 259 of file DataRecycler.h.

References getCacheItemMetrics().

259  {
260  auto metrics = getCacheItemMetrics(device_identifier);
261  metrics.erase(metrics.begin(), metrics.begin() + offset);
262  }
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:201

+ Here is the call graph for this function:

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

Definition at line 216 of file DataRecycler.h.

References CHECK, current_cache_size_in_bytes_, and total_cache_size_.

Referenced by updateCurrentCacheSize().

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

+ Here is the caller graph for this function:

void CacheMetricTracker::setMaxCacheItemSize ( size_t  new_max_cache_item_size)
inline

Definition at line 352 of file DataRecycler.h.

References max_cache_item_size_.

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

352  {
353  if (new_max_cache_item_size > 0) {
354  max_cache_item_size_ = new_max_cache_item_size;
355  }
356  }
size_t max_cache_item_size_
Definition: DataRecycler.h:361

+ Here is the caller graph for this function:

void CacheMetricTracker::setTotalCacheSize ( size_t  new_total_cache_size)
inline

Definition at line 347 of file DataRecycler.h.

References total_cache_size_.

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

347  {
348  if (new_total_cache_size > 0) {
349  total_cache_size_ = new_total_cache_size;
350  }
351  }

+ Here is the caller graph for this function:

void CacheMetricTracker::sortCacheInfoByQueryMetric ( DeviceIdentifier  device_identifier)
inline

Definition at line 319 of file DataRecycler.h.

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

319  {
320  auto& metric_cache = getCacheItemMetrics(device_identifier);
321  std::sort(metric_cache.begin(),
322  metric_cache.end(),
323  [](const std::shared_ptr<CacheItemMetric>& left,
324  const std::shared_ptr<CacheItemMetric>& right) {
325  auto& elem1_metrics = left->getMetrics();
326  auto& elem2_metrics = right->getMetrics();
327  for (size_t i = 0; i < CacheMetricType::NUM_METRIC_TYPE; ++i) {
328  if (elem1_metrics[i] != elem2_metrics[i]) {
329  return elem1_metrics[i] < elem2_metrics[i];
330  }
331  }
332  return false;
333  });
334  }
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
std::vector< std::shared_ptr< CacheItemMetric > > & getCacheItemMetrics(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:201

+ Here is the call graph for this function:

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

Definition at line 336 of file DataRecycler.h.

References current_cache_size_in_bytes_.

336  {
337  std::ostringstream oss;
338  oss << "Current memory consumption of caches for each device:\n";
339  for (auto& kv : current_cache_size_in_bytes_) {
340  oss << "\t\tDevice " << kv.first << " : " << kv.second << " bytes\n";
341  }
342  return oss.str();
343  }
CacheSizeMap current_cache_size_in_bytes_
Definition: DataRecycler.h:369
void CacheMetricTracker::updateCurrentCacheSize ( DeviceIdentifier  device_identifier,
CacheUpdateAction  action,
size_t  size 
)
inline

Definition at line 305 of file DataRecycler.h.

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

Referenced by clearCacheMetricTracker().

307  {
308  auto current_cache_size = getCurrentCacheSize(device_identifier);
309  CHECK(current_cache_size.has_value());
311  setCurrentCacheSize(device_identifier, current_cache_size.value() + size);
312  } else {
314  CHECK_LE(size, *current_cache_size);
315  setCurrentCacheSize(device_identifier, current_cache_size.value() - size);
316  }
317  }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
std::optional< size_t > getCurrentCacheSize(DeviceIdentifier key) const
Definition: DataRecycler.h:225
void setCurrentCacheSize(DeviceIdentifier device_identifier, size_t bytes)
Definition: DataRecycler.h:216
#define CHECK_LE(x, y)
Definition: Logger.h:222
#define CHECK(condition)
Definition: Logger.h:211

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

Referenced by clearCacheMetricTracker().

size_t CacheMetricTracker::max_cache_item_size_
private

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