OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CodeCacheAccessor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef QUERYENGINE_CODECACHEACCESSOR_HPP
18 #define QUERYENGINE_CODECACHEACCESSOR_HPP
19 
20 #include <mutex>
21 #include <sstream>
22 #include <string>
23 
24 #include "QueryEngine/CodeCache.h"
25 
26 extern bool g_is_test_env;
27 
29  int64_t get_count{0};
30  int64_t found_count{0};
31  int64_t put_count{0};
32  int64_t ignore_count{0};
33  int64_t overwrite_count{0};
34  int64_t evict_count{0};
35 };
36 
37 template <typename CompilationContext>
39  public:
40  CodeCacheAccessor(EvictionMetricType eviction_metric_type,
41  size_t max_cache_size,
42  std::string 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  }
61 
62  // TODO: replace get_value/put with get_or_wait/reset workflow.
64  bool put(const CodeCacheKey& key, CodeCacheVal<CompilationContext>& value);
65 
66  // get_or_wait and reset/erase should be used in pair.
68  void reset(const CodeCacheKey& key, CodeCacheVal<CompilationContext> value);
69  void erase(const CodeCacheKey& key);
70 
71  void clear();
72 
73  size_t computeNumEntriesToEvict(const float fraction) {
74  std::lock_guard<std::mutex> lock(code_cache_mutex_);
75  return code_cache_.computeNumEntriesToEvict(fraction);
76  }
77 
78  void evictEntries(const size_t n) {
79  std::lock_guard<std::mutex> lock(code_cache_mutex_);
80  evict_count_++;
81  code_cache_.evictNEntries(n);
82  }
83 
84  friend std::ostream& operator<<(std::ostream& os, CodeCacheAccessor& c) {
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  }
92 
93  size_t getCacheSize() {
94  std::lock_guard<std::mutex> lock(code_cache_mutex_);
95  return code_cache_.size();
96  }
97 
98  void resetCache(size_t new_max_size) {
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  }
103 
105  std::lock_guard<std::mutex> lock(code_cache_mutex_);
106  return {get_count_,
107  found_count_,
108  put_count_,
111  evict_count_};
112  }
113 
114  private:
117  // cumulative statistics of code cache usage
119  evict_count_;
120  // name of the code cache
121  const std::string name_;
122  // used to lock any access to the code cache:
123  std::mutex code_cache_mutex_;
124  // cv is used to wait until another thread finishes compilation and
125  // inserts a code to cache
126  std::condition_variable compilation_cv_;
127 };
128 
129 #endif
std::shared_ptr< CC > CodeCacheVal
Definition: CodeCache.h:26
CodeCacheVal< CompilationContext > get_value(const CodeCacheKey &key)
std::mutex code_cache_mutex_
void resetCache(size_t new_max_size)
#define LOG(tag)
Definition: Logger.h:285
std::vector< std::string > CodeCacheKey
Definition: CodeCache.h:24
void evictEntries(const size_t n)
bool put(const CodeCacheKey &key, CodeCacheVal< CompilationContext > &value)
bool g_is_test_env
Definition: Execute.cpp:149
size_t computeNumEntriesToEvict(const float fraction)
EvictionMetricType const eviction_metric_type_
friend std::ostream & operator<<(std::ostream &os, CodeCacheAccessor &c)
void reset(const CodeCacheKey &key, CodeCacheVal< CompilationContext > value)
CodeCacheMetric getCodeCacheMetric()
std::condition_variable compilation_cv_
CodeCache< CompilationContext > code_cache_
CodeCacheAccessor(EvictionMetricType eviction_metric_type, size_t max_cache_size, std::string name)
#define CHECK(condition)
Definition: Logger.h:291
const std::string name_
CodeCacheVal< CompilationContext > * get_or_wait(const CodeCacheKey &key)
EvictionMetricType
Definition: LruCache.h:22
string name
Definition: setup.in.py:72
constexpr double n
Definition: Utm.h:38
void erase(const CodeCacheKey &key)