OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CodeCacheAccessor< CompilationContext > Class Template Reference

#include <CodeCacheAccessor.h>

+ Collaboration diagram for CodeCacheAccessor< CompilationContext >:

Public Member Functions

 CodeCacheAccessor (EvictionMetricType eviction_metric_type, size_t max_cache_size, std::string name)
 
CodeCacheVal< CompilationContextget_value (const CodeCacheKey &key)
 
bool put (const CodeCacheKey &key, CodeCacheVal< CompilationContext > &value)
 
CodeCacheVal
< CompilationContext > * 
get_or_wait (const CodeCacheKey &key)
 
void reset (const CodeCacheKey &key, CodeCacheVal< CompilationContext > value)
 
void erase (const CodeCacheKey &key)
 
void clear ()
 
size_t computeNumEntriesToEvict (const float fraction)
 
void evictEntries (const size_t n)
 
size_t getCacheSize ()
 
void resetCache (size_t new_max_size)
 
CodeCacheMetric getCodeCacheMetric ()
 

Private Attributes

CodeCache< CompilationContextcode_cache_
 
EvictionMetricType const eviction_metric_type_
 
int64_t get_count_
 
int64_t found_count_
 
int64_t put_count_
 
int64_t ignore_count_
 
int64_t overwrite_count_
 
int64_t evict_count_
 
const std::string name_
 
std::mutex code_cache_mutex_
 
std::condition_variable compilation_cv_
 

Friends

std::ostream & operator<< (std::ostream &os, CodeCacheAccessor &c)
 

Detailed Description

template<typename CompilationContext>
class CodeCacheAccessor< CompilationContext >

Definition at line 38 of file CodeCacheAccessor.h.

Constructor & Destructor Documentation

template<typename CompilationContext >
CodeCacheAccessor< CompilationContext >::CodeCacheAccessor ( EvictionMetricType  eviction_metric_type,
size_t  max_cache_size,
std::string  name 
)
inline

Definition at line 40 of file CodeCacheAccessor.h.

References EntryCount, CodeCacheAccessor< CompilationContext >::eviction_metric_type_, logger::INFO, LOG, and CodeCacheAccessor< CompilationContext >::name_.

43  : code_cache_(eviction_metric_type, max_cache_size)
44  , eviction_metric_type_(eviction_metric_type)
45  , get_count_(0)
46  , found_count_(0)
47  , put_count_(0)
48  , ignore_count_(0)
49  , overwrite_count_(0)
50  , evict_count_(0)
51  , name_(std::move(name)) {
52  std::ostringstream oss;
53  std::string eviction_type = eviction_metric_type_ == EvictionMetricType::EntryCount
54  ? "EntryCount"
55  : "ByteSize";
56  oss << "Initialize a code cache (name: " << name_
57  << ", eviction_metric_type: " << eviction_type
58  << ", max_cache_size: " << max_cache_size << ")";
59  LOG(INFO) << oss.str();
60  }
#define LOG(tag)
Definition: Logger.h:285
EvictionMetricType const eviction_metric_type_
CodeCache< CompilationContext > code_cache_
const std::string name_
string name
Definition: setup.in.py:72

Member Function Documentation

template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::clear ( )

Definition at line 113 of file CodeCacheAccessor.cpp.

113  {
114  std::lock_guard<std::mutex> lock(code_cache_mutex_);
115  code_cache_.clear();
116 }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
size_t CodeCacheAccessor< CompilationContext >::computeNumEntriesToEvict ( const float  fraction)
inline

Definition at line 73 of file CodeCacheAccessor.h.

References CodeCacheAccessor< CompilationContext >::code_cache_, and CodeCacheAccessor< CompilationContext >::code_cache_mutex_.

73  {
74  std::lock_guard<std::mutex> lock(code_cache_mutex_);
75  return code_cache_.computeNumEntriesToEvict(fraction);
76  }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::erase ( const CodeCacheKey key)

Definition at line 106 of file CodeCacheAccessor.cpp.

106  {
107  std::lock_guard<std::mutex> lock(code_cache_mutex_);
108  code_cache_.erase(key);
109  compilation_cv_.notify_all(); // notify waiting get_or_wait(..) calls
110 }
std::mutex code_cache_mutex_
std::condition_variable compilation_cv_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::evictEntries ( const size_t  n)
inline

Definition at line 78 of file CodeCacheAccessor.h.

References CodeCacheAccessor< CompilationContext >::code_cache_, CodeCacheAccessor< CompilationContext >::code_cache_mutex_, and CodeCacheAccessor< CompilationContext >::evict_count_.

78  {
79  std::lock_guard<std::mutex> lock(code_cache_mutex_);
80  evict_count_++;
81  code_cache_.evictNEntries(n);
82  }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
constexpr double n
Definition: Utm.h:38
template<typename CompilationContext >
CodeCacheVal< CompilationContext > * CodeCacheAccessor< CompilationContext >::get_or_wait ( const CodeCacheKey key)

Definition at line 59 of file CodeCacheAccessor.cpp.

References CHECK, and VLOG.

60  {
61  std::unique_lock<std::mutex> lk(code_cache_mutex_);
62  get_count_++;
63  if (auto* cached_code = code_cache_.get(key)) {
64  if (!cached_code->get()) {
65  // Wait until the compiling thread puts code to cache. TODO:
66  // this wait also locks other unrelated get_or_wait calls on
67  // different keys. This is suboptimal as it will block also
68  // independent get_or_wait(other_key) calls. To fix this (it
69  // likely also requires using ORCJIT to enable concurrent
70  // compilations), we'll need a key specific mutex or use some
71  // other approach that would allow threads with other keys to
72  // proceed.
73  compilation_cv_.wait(lk, [=] { return cached_code->get(); });
74  // Don't ignore spurious awakenings as the support for such
75  // events has not been implemented:
76  CHECK(cached_code->get());
77  }
78  found_count_++;
79  VLOG(1) << name_ << ": Reuse a cached compiled code";
80  return cached_code;
81  }
82  // This is the first time the key is used to acquire code from
83  // cache. Put null value to cache so that other threads acquiring
84  // the same key will wait (see above) until the code is put to the
85  // cache:
86  CodeCacheVal<CompilationContext> not_a_code(nullptr);
87  evict_count_ += code_cache_.put(key, std::move(not_a_code));
88  // returning nullptr will notify caller to trigger code compilation
89  // for the given key:
90  return nullptr;
91 }
std::shared_ptr< CC > CodeCacheVal
Definition: CodeCache.h:26
std::mutex code_cache_mutex_
std::condition_variable compilation_cv_
CodeCache< CompilationContext > code_cache_
#define CHECK(condition)
Definition: Logger.h:291
const std::string name_
#define VLOG(n)
Definition: Logger.h:388
template<typename CompilationContext >
CodeCacheVal< CompilationContext > CodeCacheAccessor< CompilationContext >::get_value ( const CodeCacheKey key)

Definition at line 21 of file CodeCacheAccessor.cpp.

References VLOG.

22  {
23  std::lock_guard<std::mutex> lock(code_cache_mutex_);
24  get_count_++;
25  auto it = code_cache_.find(key);
26  if (it != code_cache_.cend()) {
27  found_count_++;
28  VLOG(1) << name_ << ": Reuse cached compiled kernel";
29  return it->second;
30  }
31  return {};
32 }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
const std::string name_
#define VLOG(n)
Definition: Logger.h:388
template<typename CompilationContext >
size_t CodeCacheAccessor< CompilationContext >::getCacheSize ( )
inline

Definition at line 93 of file CodeCacheAccessor.h.

References CodeCacheAccessor< CompilationContext >::code_cache_, and CodeCacheAccessor< CompilationContext >::code_cache_mutex_.

93  {
94  std::lock_guard<std::mutex> lock(code_cache_mutex_);
95  return code_cache_.size();
96  }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
bool CodeCacheAccessor< CompilationContext >::put ( const CodeCacheKey key,
CodeCacheVal< CompilationContext > &  value 
)

Definition at line 35 of file CodeCacheAccessor.cpp.

References LOG, VLOG, and logger::WARNING.

36  {
37  bool warn = false;
38  {
39  std::lock_guard<std::mutex> lock(code_cache_mutex_);
40  // if key is in cache, put is no-op
41  auto it = code_cache_.find(key);
42  put_count_++;
43  if (it == code_cache_.cend()) {
44  VLOG(1) << name_ << ": Add compiled kernel to code cache";
45  evict_count_ += code_cache_.put(key, value);
46  } else {
47  ignore_count_++;
48  warn = true;
49  }
50  }
51  if (warn) {
52  LOG(WARNING) << *this << ": code already in cache, ignoring.\n";
53  return false;
54  }
55  return true;
56 }
std::mutex code_cache_mutex_
#define LOG(tag)
Definition: Logger.h:285
CodeCache< CompilationContext > code_cache_
const std::string name_
#define VLOG(n)
Definition: Logger.h:388
template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::reset ( const CodeCacheKey key,
CodeCacheVal< CompilationContext value 
)

Definition at line 94 of file CodeCacheAccessor.cpp.

References CHECK, and run_benchmark_import::result.

96  {
97  std::lock_guard<std::mutex> lock(code_cache_mutex_);
98  auto result = code_cache_.get(key);
99  CHECK(result); // get_or_wait has put not_a_code to code cache
100  CHECK(!result->get()); // ensure that result really contains not_a_code per get_or_wait
101  *result = std::move(value); // set actual code
102  compilation_cv_.notify_all(); // notify waiting get_or_wait(..) calls
103 }
std::mutex code_cache_mutex_
std::condition_variable compilation_cv_
CodeCache< CompilationContext > code_cache_
#define CHECK(condition)
Definition: Logger.h:291
template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::resetCache ( size_t  new_max_size)
inline

Definition at line 98 of file CodeCacheAccessor.h.

References CHECK, CodeCacheAccessor< CompilationContext >::code_cache_, CodeCacheAccessor< CompilationContext >::code_cache_mutex_, CodeCacheAccessor< CompilationContext >::eviction_metric_type_, and g_is_test_env.

98  {
99  CHECK(g_is_test_env) << "Call the resetCache function from non-test env.";
100  std::lock_guard<std::mutex> lock(code_cache_mutex_);
102  }
std::mutex code_cache_mutex_
bool g_is_test_env
Definition: Execute.cpp:149
EvictionMetricType const eviction_metric_type_
CodeCache< CompilationContext > code_cache_
#define CHECK(condition)
Definition: Logger.h:291

Friends And Related Function Documentation

template<typename CompilationContext >
std::ostream& operator<< ( std::ostream &  os,
CodeCacheAccessor< CompilationContext > &  c 
)
friend

Definition at line 84 of file CodeCacheAccessor.h.

84  {
85  std::lock_guard<std::mutex> lock(c.code_cache_mutex_);
86  os << "CodeCacheAccessor<" << c.name_ << ">[current size=" << c.code_cache_.size()
87  << ", total get/found count=" << c.get_count_ << "/" << c.found_count_
88  << ", total put/ignore/overwrite count=" << c.put_count_ << "/" << c.ignore_count_
89  << "/" << c.overwrite_count_ << ", total evict count=" << c.evict_count_ << "]";
90  return os;
91  }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
const std::string name_

Member Data Documentation

template<typename CompilationContext >
std::condition_variable CodeCacheAccessor< CompilationContext >::compilation_cv_
private

Definition at line 126 of file CodeCacheAccessor.h.

template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::evict_count_
private
template<typename CompilationContext >
EvictionMetricType const CodeCacheAccessor< CompilationContext >::eviction_metric_type_
private
template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::found_count_
private
template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::get_count_
private
template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::ignore_count_
private
template<typename CompilationContext >
const std::string CodeCacheAccessor< CompilationContext >::name_
private
template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::overwrite_count_
private
template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::put_count_
private

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