OmniSciDB  c1a53651b2
 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 <string>
22 #include "QueryEngine/CodeCache.h"
23 
24 template <typename CompilationContext>
26  public:
27  CodeCacheAccessor(size_t cache_size, std::string name = "")
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)) {}
36 
37  // TODO: replace get_value/put with get_or_wait/swap workflow.
39  void put(const CodeCacheKey& key, CodeCacheVal<CompilationContext>& value);
40 
41  // get_or_wait and swap should be used in pair.
43  void swap(const CodeCacheKey& key, CodeCacheVal<CompilationContext>&& value);
44  void clear();
45 
46  void evictFractionEntries(const float fraction) {
47  std::lock_guard<std::mutex> lock(code_cache_mutex_);
48  evict_count_++;
49  code_cache_.evictFractionEntries(fraction);
50  }
51 
52  friend std::ostream& operator<<(std::ostream& os, CodeCacheAccessor& c) {
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  }
60 
61  private:
63  // cumulative statistics of code cache usage
66  // name of the code cache
67  const std::string name_;
68  // used to lock any access to the code cache:
69  std::mutex code_cache_mutex_;
70  // cv is used to wait until another thread finishes compilation and
71  // inserts a code to cache
72  std::condition_variable compilation_cv_;
73 };
74 
75 #endif
std::shared_ptr< CC > CodeCacheVal
Definition: CodeCache.h:27
CodeCacheVal< CompilationContext > get_value(const CodeCacheKey &key)
std::mutex code_cache_mutex_
std::vector< std::string > CodeCacheKey
Definition: CodeCache.h:25
friend std::ostream & operator<<(std::ostream &os, CodeCacheAccessor &c)
void swap(const CodeCacheKey &key, CodeCacheVal< CompilationContext > &&value)
std::condition_variable compilation_cv_
CodeCache< CompilationContext > code_cache_
void evictFractionEntries(const float fraction)
CodeCacheAccessor(size_t cache_size, std::string name="")
const std::string name_
CodeCacheVal< CompilationContext > * get_or_wait(const CodeCacheKey &key)
string name
Definition: setup.in.py:72
void put(const CodeCacheKey &key, CodeCacheVal< CompilationContext > &value)