Akumuli
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
util.h
1 
23 #pragma once
24 
25 #include "akumuli.h"
26 #include <apr_general.h>
27 #include <apr_mmap.h>
28 #include <atomic>
29 #include <boost/throw_exception.hpp>
30 #include <ostream>
31 #include <random>
32 #include <stdexcept>
33 #include <tuple>
34 #include <vector>
35 
36 namespace Akumuli {
38 std::string apr_error_message(apr_status_t status);
39 
41 void set_panic_handler(aku_panic_handler_t new_panic_handler);
42 
46 struct AprException : public std::runtime_error {
47  apr_status_t status;
48 
53  AprException(apr_status_t status, const char* message);
54 };
55 
59 struct Exception : public std::runtime_error {
63  Exception(const char* message);
64 };
65 
66 std::ostream& operator<<(std::ostream& str, Exception const& except);
67 
68 
73  apr_pool_t* mem_pool_; //< local memory pool
74  apr_mmap_t* mmap_;
75  apr_file_t* fp_;
76  apr_finfo_t finfo_;
77  apr_status_t status_;
78  std::string path_;
79  const bool enable_huge_tlb_;
80 
81 public:
82  MemoryMappedFile(const char* file_name, bool enable_huge_tlb);
84  void move_file(const char* new_name);
85  void delete_file();
86  void* get_pointer() const;
87  size_t get_size() const;
89  apr_status_t flush(size_t from, size_t to);
91  apr_status_t flush();
92  bool is_bad() const;
93  std::string error_message() const;
94  void panic_if_bad();
95  apr_status_t status_code() const;
99  aku_Status protect_all();
101  aku_Status unprotect_all();
102 
103 private:
105  apr_status_t map_file();
107  void free_resources(int cnt);
108 };
109 
111 int64_t log2(int64_t value);
112 
113 std::tuple<bool, aku_Status> page_in_core(const void* addr);
114 
115 size_t get_page_size();
116 
117 const void* align_to_page(const void* ptr, size_t get_page_size);
118 
119 void* align_to_page(void* ptr, size_t get_page_size);
120 
121 void prefetch_mem(const void* ptr, size_t mem_size);
122 
129 class PageInfo {
130  std::vector<unsigned char> data_;
131  size_t page_size_;
132  const void* base_addr_;
133  size_t len_bytes_;
134 
135  void fill_mem();
136 
137 public:
142  PageInfo(const void* addr, size_t len_bytes);
143 
145  aku_Status refresh(const void* addr);
146 
148  bool in_core(const void* addr);
149 
151  bool swapped();
152 };
153 
154 class Rand {
155  std::ranlux48_base rand_;
156 
157 public:
158  Rand();
159  uint32_t operator()();
160 };
161 
165 class RWLock {
166  // TODO: specializations for different platforms
167  pthread_rwlock_t rwlock_;
168 
169 public:
170  RWLock();
171 
172  ~RWLock();
173 
174  void rdlock();
175 
176  bool try_rdlock();
177 
178  void wrlock();
179 
180  bool try_wrlock();
181 
182  void unlock();
183 };
184 }
185 
190 #define AKU_PANIC(msg) BOOST_THROW_EXCEPTION(Akumuli::Exception(msg));
191 
197 #define AKU_APR_PANIC(status, msg) BOOST_THROW_EXCEPTION(Akumuli::AprException(status, msg));
198 
200 #define AKU_UNUSED(x) (void)(x)
201 
202 #ifdef __GNUC__
203 #define AKU_LIKELY(x) __builtin_expect((x), 1)
204 #define AKU_UNLIKELY(x) __builtin_expect((x), 0)
205 #else
206 #define AKU_LIKELY(x) (x)
207 #define AKU_UNLIKELY(x) (x)
208 #endif
bool swapped()
Check if underlying memory is swapped to disk.
Definition: util.cpp:350
int64_t log2(int64_t value)
Fast integer logarithm.
Definition: util.cpp:295
Definition: util.h:59
aku_Status protect_all()
Protect page from writing.
Definition: util.cpp:240
Definition: util.h:154
void set_panic_handler(aku_panic_handler_t new_panic_handler)
Definition: util.cpp:48
apr_status_t flush()
Flush full page.
Definition: util.cpp:236
AprException(apr_status_t status, const char *message)
Definition: util.cpp:53
Definition: util.h:46
Definition: util.h:165
Definition: util.h:129
PageInfo(const void *addr, size_t len_bytes)
Definition: util.cpp:340
std::string apr_error_message(apr_status_t status)
Definition: util.cpp:34
void remap_file_destructive()
Remap file in a destructive way (all file content is lost)
Definition: util.cpp:132
aku_Status refresh(const void *addr)
Query data from OS.
Definition: util.cpp:358
Exception(const char *message)
Definition: util.cpp:65
aku_Status unprotect_all()
Make page available for writing.
Definition: util.cpp:257
bool in_core(const void *addr)
Check if memory address is in core.
Definition: util.cpp:379
Definition: util.h:72