OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataMgr.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 
23 #pragma once
24 
25 #include "AbstractBuffer.h"
26 #include "AbstractBufferMgr.h"
27 #include "BufferMgr/Buffer.h"
28 #include "BufferMgr/BufferMgr.h"
29 #include "MemoryLevel.h"
30 #include "OSDependent/heavyai_fs.h"
34 
35 #include <fstream>
36 #include <iomanip>
37 #include <iostream>
38 #include <map>
39 #include <string>
40 #include <unordered_map>
41 #include <vector>
42 
43 namespace File_Namespace {
44 class FileBuffer;
45 class GlobalFileMgr;
46 } // namespace File_Namespace
47 
48 namespace CudaMgr_Namespace {
49 class CudaMgr;
50 }
51 
52 class DeviceAllocator;
53 
54 namespace Buffer_Namespace {
55 class CpuBufferMgr;
56 class GpuCudaBufferMgr;
57 } // namespace Buffer_Namespace
58 
59 namespace Data_Namespace {
60 
61 struct MemoryData {
62  size_t slabNum;
63  int32_t startPage;
64  size_t numPages;
65  uint32_t touch;
66  std::vector<int32_t> chunk_key;
68 };
69 
70 struct MemoryInfo {
71  size_t pageSize;
72  size_t maxNumPages;
75  std::vector<MemoryData> nodeMemoryData;
76 };
77 
80  std::unordered_map<std::string, size_t> items_;
81 
82  public:
84  std::ifstream f("/proc/meminfo");
85  std::stringstream ss;
86  ss << f.rdbuf();
87 
88  for (const std::string& line : split(ss.str(), "\n")) {
89  if (line.empty()) {
90  continue;
91  }
92  const auto nv = split(line, ":", 1);
93  CHECK(nv.size() == 2) << "unexpected line format in /proc/meminfo: " << line;
94  const auto name = strip(nv[0]), value = to_lower(strip(nv[1]));
95  auto v = split(value);
96  CHECK(v.size() == 1 || v.size() == 2)
97  << "unexpected line format in /proc/meminfo: " << line;
98  items_[name] = std::atoll(v[0].c_str());
99  if (v.size() == 2) {
100  CHECK(v[1] == "kb") << "unexpected unit suffix in /proc/meminfo: " << line;
101  items_[name] *= 1024;
102  }
103  }
104  }
105 
106  auto operator[](const std::string& name) { return items_[name]; }
107  auto begin() { return items_.begin(); }
108  auto end() { return items_.end(); }
109 };
110 
113  public:
114  void parseBuddyinfo(); // Set member variables.
115  double getFragmentationPercent() const { return frag_percent_; }
116  size_t getSumAvailPages() const { return sum_avail_pages_; }
117  size_t getSumHighestBlocks() const { return sum_highest_blocks_; }
118 
119  private:
120  double frag_percent_{0}; // Weighted score of available memory.
121  size_t sum_avail_pages_{0}; // Sum of all available non-DMA pages.
122  size_t sum_highest_blocks_{0}; // Sum of highest non-DMA blocks.
123 };
124 
125 class DataMgr {
126  friend class GlobalFileMgr;
127 
128  public:
129  explicit DataMgr(
130  const std::string& dataDir,
131  const SystemParameters& system_parameters,
132  std::unique_ptr<CudaMgr_Namespace::CudaMgr> cudaMgr,
133  const bool useGpus,
134  const size_t reservedGpuMem = (1 << 27),
135  const size_t numReaderThreads = 0, /* 0 means use default for # of reader threads */
136  const File_Namespace::DiskCacheConfig cacheConfig =
138  ~DataMgr();
140  const MemoryLevel memoryLevel,
141  const int deviceId = 0,
142  const size_t page_size = 0);
144  const MemoryLevel memoryLevel,
145  const int deviceId = 0,
146  const size_t numBytes = 0);
147  void deleteChunk(const ChunkKey& key, const MemoryLevel mem_level, const int device_id);
148  void deleteChunksWithPrefix(const ChunkKey& keyPrefix);
149  void deleteChunksWithPrefix(const ChunkKey& keyPrefix, const MemoryLevel memLevel);
150  AbstractBuffer* alloc(const MemoryLevel memoryLevel,
151  const int deviceId,
152  const size_t numBytes);
153  void free(AbstractBuffer* buffer);
154  // copies one buffer to another
155  void copy(AbstractBuffer* destBuffer, AbstractBuffer* srcBuffer);
156  bool isBufferOnDevice(const ChunkKey& key,
157  const MemoryLevel memLevel,
158  const int deviceId);
159  std::vector<MemoryInfo> getMemoryInfo(const MemoryLevel memLevel) const;
160  std::vector<MemoryInfo> getMemoryInfoUnlocked(const MemoryLevel memLevel) const;
161  std::string dumpLevel(const MemoryLevel memLevel);
162  void clearMemory(const MemoryLevel memLevel);
163 
164  const std::map<ChunkKey, File_Namespace::FileBuffer*>& getChunkMap();
165  void checkpoint(const int db_id,
166  const int tb_id); // checkpoint for individual table of DB
167  void checkpoint(const int db_id, const int table_id, const MemoryLevel memory_level);
169  const ChunkKey& keyPrefix);
170  inline bool gpusPresent() const { return hasGpus_; }
171  void removeTableRelatedDS(const int db_id, const int tb_id);
172  void removeMutableTableDiskCacheData(const int db_id, const int tb_id) const;
173  void setTableEpoch(const int db_id, const int tb_id, const int start_epoch);
174  size_t getTableEpoch(const int db_id, const int tb_id);
175  void resetTableEpochFloor(const int32_t db_id, const int32_t tb_id);
176 
177  CudaMgr_Namespace::CudaMgr* getCudaMgr() const { return cudaMgr_.get(); }
179  std::shared_ptr<ForeignStorageInterface> getForeignStorageInterface() const;
180 
181  // database_id, table_id, column_id, fragment_id
182  std::vector<int> levelSizes_;
183 
184  // std::unique_ptr<DeviceAllocator> createGpuAllocator(int device_id);
185  // NOTE(sy): Revisit how DataMgr should handle Cuda streams if Intel ever needs this.
186 
188  size_t free; // available CPU RAM memory in bytes
189  size_t total; // total CPU RAM memory in bytes
190  size_t resident; // resident process memory in bytes
191  size_t vtotal; // total process virtual memory in bytes
192  size_t regular; // process bytes non-shared
193  size_t shared; // process bytes shared (file maps + shmem)
194  double frag; // fragmentation percent
195  size_t avail_pages; // sum of all non-dma pages in /proc/buddyinfo
196  size_t high_blocks; // sum of highest non-dma blocks in /proc/buddyinfo
197  };
198 
200  static size_t getTotalSystemMemory();
201 
203  void resetBufferMgrs(const File_Namespace::DiskCacheConfig& cache_config,
204  const size_t num_reader_threads,
205  const SystemParameters& sys_params);
206 
207  size_t getCpuBufferPoolSize() const;
208  size_t getGpuBufferPoolSize() const;
209 
210  // Used for testing.
212 
213  // Used for testing.
214  Buffer_Namespace::GpuCudaBufferMgr* getGpuBufferMgr(int32_t device_id) const;
215 
216  static void atExitHandler();
217 
218  private:
219  void populateMgrs(const SystemParameters& system_parameters,
220  const size_t userSpecifiedNumReaderThreads,
221  const File_Namespace::DiskCacheConfig& cache_config);
222  void convertDB(const std::string basePath);
223  void checkpoint(); // checkpoint for whole DB, called from convertDB proc only
224  void createTopLevelMetadata() const;
225  void allocateCpuBufferMgr(int32_t device_id,
226  size_t total_cpu_size,
227  size_t min_cpu_slab_size,
228  size_t max_cpu_slab_size,
229  size_t default_cpu_slab_size,
230  size_t page_size,
231  const std::vector<size_t>& cpu_tier_sizes);
232 
233  std::vector<std::vector<AbstractBufferMgr*>> bufferMgrs_;
234  std::unique_ptr<CudaMgr_Namespace::CudaMgr> cudaMgr_;
235  std::string dataDir_;
236  bool hasGpus_;
238  mutable std::mutex buffer_access_mutex_;
239 };
240 
241 std::ostream& operator<<(std::ostream& os, const DataMgr::SystemMemoryUsage&);
242 
243 } // namespace Data_Namespace
double getFragmentationPercent() const
Definition: DataMgr.h:115
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:177
auto operator[](const std::string &name)
Definition: DataMgr.h:106
std::string to_lower(const std::string &str)
std::mutex buffer_access_mutex_
Definition: DataMgr.h:238
std::vector< int > ChunkKey
Definition: types.h:36
std::vector< MemoryData > nodeMemoryData
Definition: DataMgr.h:75
Buffer_Namespace::MemStatus memStatus
Definition: DataMgr.h:67
std::unordered_map< std::string, size_t > items_
Definition: DataMgr.h:80
void deleteChunk(const ChunkKey &key, const MemoryLevel mem_level, const int device_id)
Definition: DataMgr.cpp:597
std::vector< std::vector< AbstractBufferMgr * > > bufferMgrs_
Definition: DataMgr.h:233
std::vector< int > levelSizes_
Definition: DataMgr.h:182
std::string strip(std::string_view str)
trim any whitespace from the left and right ends of a string
std::ostream & operator<<(std::ostream &os, const DataMgr::SystemMemoryUsage &mem_info)
Definition: DataMgr.cpp:712
SystemMemoryUsage getSystemMemoryUsage() const
Definition: DataMgr.cpp:131
PersistentStorageMgr * getPersistentStorageMgr() const
Definition: DataMgr.cpp:729
void clearMemory(const MemoryLevel memLevel)
Definition: DataMgr.cpp:515
std::vector< MemoryInfo > getMemoryInfoUnlocked(const MemoryLevel memLevel) const
Definition: DataMgr.cpp:435
void resetTableEpochFloor(const int32_t db_id, const int32_t tb_id)
Definition: DataMgr.cpp:692
std::string dumpLevel(const MemoryLevel memLevel)
Definition: DataMgr.cpp:499
size_t getCpuBufferPoolSize() const
Definition: DataMgr.cpp:733
void convertDB(const std::string basePath)
Definition: DataMgr.cpp:384
size_t getGpuBufferPoolSize() const
Definition: DataMgr.cpp:738
void allocateCpuBufferMgr(int32_t device_id, size_t total_cpu_size, size_t min_cpu_slab_size, size_t max_cpu_slab_size, size_t default_cpu_slab_size, size_t page_size, const std::vector< size_t > &cpu_tier_sizes)
Definition: DataMgr.cpp:215
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
static size_t getTotalSystemMemory()
Definition: DataMgr.cpp:192
size_t getTableEpoch(const int db_id, const int tb_id)
Definition: DataMgr.cpp:685
Buffer_Namespace::GpuCudaBufferMgr * getGpuBufferMgr(int32_t device_id) const
Definition: DataMgr.cpp:756
std::shared_ptr< ForeignStorageInterface > getForeignStorageInterface() const
Definition: DataMgr.cpp:707
void createTopLevelMetadata() const
Definition: DataMgr.cpp:414
static void atExitHandler()
Definition: DataMgr.cpp:67
void removeMutableTableDiskCacheData(const int db_id, const int tb_id) const
Definition: DataMgr.cpp:674
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cudaMgr_
Definition: DataMgr.h:234
void getChunkMetadataVecForKeyPrefix(ChunkMetadataVector &chunkMetadataVec, const ChunkKey &keyPrefix)
Definition: DataMgr.cpp:546
void populateMgrs(const SystemParameters &system_parameters, const size_t userSpecifiedNumReaderThreads, const File_Namespace::DiskCacheConfig &cache_config)
Definition: DataMgr.cpp:273
std::vector< std::pair< ChunkKey, std::shared_ptr< ChunkMetadata >>> ChunkMetadataVector
const std::map< ChunkKey, File_Namespace::FileBuffer * > & getChunkMap()
An AbstractBuffer is a unit of data management for a data manager.
This file includes the class specification for the buffer manager (BufferMgr), and related data struc...
File_Namespace::GlobalFileMgr * getGlobalFileMgr() const
Definition: DataMgr.cpp:699
Parse /proc/meminfo into key/value pairs.
Definition: DataMgr.h:79
void deleteChunksWithPrefix(const ChunkKey &keyPrefix)
Definition: DataMgr.cpp:572
size_t getSumHighestBlocks() const
Definition: DataMgr.h:117
tuple line
Definition: parse_ast.py:10
bool isBufferOnDevice(const ChunkKey &key, const MemoryLevel memLevel, const int deviceId)
Definition: DataMgr.cpp:539
AbstractBuffer * getChunkBuffer(const ChunkKey &key, const MemoryLevel memoryLevel, const int deviceId=0, const size_t numBytes=0)
Definition: DataMgr.cpp:561
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
std::vector< MemoryInfo > getMemoryInfo(const MemoryLevel memLevel) const
Definition: DataMgr.cpp:430
void removeTableRelatedDS(const int db_id, const int tb_id)
Definition: DataMgr.cpp:669
DataMgr(const std::string &dataDir, const SystemParameters &system_parameters, std::unique_ptr< CudaMgr_Namespace::CudaMgr > cudaMgr, const bool useGpus, const size_t reservedGpuMem=(1<< 27), const size_t numReaderThreads=0, const File_Namespace::DiskCacheConfig cacheConfig=File_Namespace::DiskCacheConfig())
Definition: DataMgr.cpp:77
Buffer_Namespace::CpuBufferMgr * getCpuBufferMgr() const
Definition: DataMgr.cpp:750
#define CHECK(condition)
Definition: Logger.h:291
void copy(AbstractBuffer *destBuffer, AbstractBuffer *srcBuffer)
Definition: DataMgr.cpp:620
bool gpusPresent() const
Definition: DataMgr.h:170
void resetBufferMgrs(const File_Namespace::DiskCacheConfig &cache_config, const size_t num_reader_threads, const SystemParameters &sys_params)
Definition: DataMgr.cpp:249
std::vector< int32_t > chunk_key
Definition: DataMgr.h:66
AbstractBuffer * createChunkBuffer(const ChunkKey &key, const MemoryLevel memoryLevel, const int deviceId=0, const size_t page_size=0)
Definition: DataMgr.cpp:552
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:614
string name
Definition: setup.in.py:72
Parse /proc/buddyinfo into a few fragmentation-related data.
Definition: DataMgr.h:112
void setTableEpoch(const int db_id, const int tb_id, const int start_epoch)
Definition: DataMgr.cpp:678
size_t getSumAvailPages() const
Definition: DataMgr.h:116
friend class GlobalFileMgr
Definition: DataMgr.h:126
AbstractBuffer * alloc(const MemoryLevel memoryLevel, const int deviceId, const size_t numBytes)
Definition: DataMgr.cpp:605
std::string dataDir_
Definition: DataMgr.h:235