OmniSciDB  c1a53651b2
 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 (size_t cache_size, std::string name="")
 
CodeCacheVal< CompilationContextget_value (const CodeCacheKey &key)
 
void put (const CodeCacheKey &key, CodeCacheVal< CompilationContext > &value)
 
CodeCacheVal
< CompilationContext > * 
get_or_wait (const CodeCacheKey &key)
 
void swap (const CodeCacheKey &key, CodeCacheVal< CompilationContext > &&value)
 
void clear ()
 
void evictFractionEntries (const float fraction)
 

Private Attributes

CodeCache< CompilationContextcode_cache_
 
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 25 of file CodeCacheAccessor.h.

Constructor & Destructor Documentation

template<typename CompilationContext >
CodeCacheAccessor< CompilationContext >::CodeCacheAccessor ( size_t  cache_size,
std::string  name = "" 
)
inline

Definition at line 27 of file CodeCacheAccessor.h.

28  : code_cache_(cache_size)
29  , get_count_(0)
30  , found_count_(0)
31  , put_count_(0)
32  , ignore_count_(0)
33  , overwrite_count_(0)
34  , evict_count_(0)
35  , name_(std::move(name)) {}
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 102 of file CodeCacheAccessor.cpp.

102  {
103  std::lock_guard<std::mutex> lock(code_cache_mutex_);
104  code_cache_.clear();
105 }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::evictFractionEntries ( const float  fraction)
inline

Definition at line 46 of file CodeCacheAccessor.h.

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

46  {
47  std::lock_guard<std::mutex> lock(code_cache_mutex_);
48  evict_count_++;
49  code_cache_.evictFractionEntries(fraction);
50  }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
CodeCacheVal< CompilationContext > * CodeCacheAccessor< CompilationContext >::get_or_wait ( const CodeCacheKey key)

Definition at line 55 of file CodeCacheAccessor.cpp.

References CHECK.

56  {
57  CodeCacheVal<CompilationContext>* cached_code = nullptr;
58  std::unique_lock<std::mutex> lk(code_cache_mutex_);
59  get_count_++;
60  cached_code = code_cache_.get(key);
61  if (cached_code) {
62  if (cached_code->get()) {
63  found_count_++;
64  return cached_code;
65  } else {
66  // Wait until compiling thread inserts code to cache. TODO: this
67  // wait also locks other unrelated get_or_wait calls on
68  // different keys. This is suboptimal as it will block in
69  // addition to others threads get_or_wait(key) calls, also
70  // independent get_or_wait(other_key) calls. To fix this, we'll
71  // need a key specific mutex or use some other approach that
72  // would allow threads with other keys to proceed.
73  compilation_cv_.wait(lk, [&] { return !!(code_cache_.get(key)->get()); });
74  cached_code = code_cache_.get(key);
75  CHECK(cached_code->get());
76  found_count_++;
77  return cached_code;
78  }
79  }
80  // This is the first time the key is used to acquire code from
81  // cache. Insert null value to cache so that other threads acquiring
82  // the same key will wait (see above) until the code is inserted to
83  // cache (by put method below):
84  CodeCacheVal<CompilationContext> not_a_code(nullptr);
85  code_cache_.put(key, std::move(not_a_code));
86  return nullptr; // caller must trigger code compilation for the given key
87 }
std::shared_ptr< CC > CodeCacheVal
Definition: CodeCache.h:27
std::mutex code_cache_mutex_
std::condition_variable compilation_cv_
CodeCache< CompilationContext > code_cache_
#define CHECK(condition)
Definition: Logger.h:291
template<typename CompilationContext >
CodeCacheVal< CompilationContext > CodeCacheAccessor< CompilationContext >::get_value ( const CodeCacheKey key)

Definition at line 21 of file CodeCacheAccessor.cpp.

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  return it->second;
29  }
30  return {};
31 }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
template<typename CompilationContext >
void CodeCacheAccessor< CompilationContext >::put ( const CodeCacheKey key,
CodeCacheVal< CompilationContext > &  value 
)

Definition at line 34 of file CodeCacheAccessor.cpp.

References LOG, and logger::WARNING.

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

Definition at line 90 of file CodeCacheAccessor.cpp.

References CHECK, and run_benchmark_import::result.

92  {
93  std::lock_guard<std::mutex> lock(code_cache_mutex_);
94  auto result = code_cache_.get(key);
95  CHECK(result); // get_or_wait has inserted not_a_code to code cache
96  CHECK(!result->get()); // ensure that result really contains not_a_code per get_or_wait
97  result->swap(value); // swap not_a_code with actual code
98  compilation_cv_.notify_all(); // notify waiting get_or_wait(..) calls
99 }
std::mutex code_cache_mutex_
std::condition_variable compilation_cv_
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 52 of file CodeCacheAccessor.h.

52  {
53  std::lock_guard<std::mutex> lock(c.code_cache_mutex_);
54  os << "CodeCacheAccessor<" << c.name_ << ">[current size=" << c.code_cache_.size()
55  << ", total get/found count=" << c.get_count_ << "/" << c.found_count_
56  << ", total put/ignore/overwrite count=" << c.put_count_ << "/" << c.ignore_count_
57  << "/" << c.overwrite_count_ << ", total evict count=" << c.evict_count_ << "]";
58  return os;
59  }
std::mutex code_cache_mutex_
CodeCache< CompilationContext > code_cache_
const std::string name_

Member Data Documentation

template<typename CompilationContext >
CodeCache<CompilationContext> CodeCacheAccessor< CompilationContext >::code_cache_
private
template<typename CompilationContext >
std::mutex CodeCacheAccessor< CompilationContext >::code_cache_mutex_
private
template<typename CompilationContext >
std::condition_variable CodeCacheAccessor< CompilationContext >::compilation_cv_
private

Definition at line 72 of file CodeCacheAccessor.h.

template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::evict_count_
private
template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::found_count_
private

Definition at line 64 of file CodeCacheAccessor.h.

template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::get_count_
private

Definition at line 64 of file CodeCacheAccessor.h.

template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::ignore_count_
private

Definition at line 64 of file CodeCacheAccessor.h.

template<typename CompilationContext >
const std::string CodeCacheAccessor< CompilationContext >::name_
private

Definition at line 67 of file CodeCacheAccessor.h.

template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::overwrite_count_
private

Definition at line 64 of file CodeCacheAccessor.h.

template<typename CompilationContext >
int64_t CodeCacheAccessor< CompilationContext >::put_count_
private

Definition at line 64 of file CodeCacheAccessor.h.


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