OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InternalMemoryStatsDataWrapper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, 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 
18 
19 #include "Catalog/SysCatalog.h"
20 #include "ImportExport/Importer.h"
21 
22 namespace foreign_storage {
25 
27  const int db_id,
28  const ForeignTable* foreign_table)
29  : InternalSystemDataWrapper(db_id, foreign_table) {}
30 
31 namespace {
33  const std::map<std::string, std::vector<MemoryInfo>>& memory_info_by_device_type,
34  std::map<std::string, import_export::TypedImportBuffer*>& import_buffers) {
35  for (const auto& [device_type, memory_info_vector] : memory_info_by_device_type) {
36  int32_t device_id{0};
37  for (const auto& memory_info : memory_info_vector) {
38  size_t used_page_count{0}, free_page_count{0};
39  for (const auto& memory_data : memory_info.nodeMemoryData) {
40  if (memory_data.memStatus == Buffer_Namespace::MemStatus::FREE) {
41  free_page_count += memory_data.numPages;
42  } else {
43  used_page_count += memory_data.numPages;
44  }
45  }
46  if (import_buffers.find("node") != import_buffers.end()) {
47  import_buffers["node"]->addString("Server");
48  }
49  if (import_buffers.find("device_id") != import_buffers.end()) {
50  import_buffers["device_id"]->addInt(device_id);
51  }
52  if (import_buffers.find("device_type") != import_buffers.end()) {
53  import_buffers["device_type"]->addString(device_type);
54  }
55  if (import_buffers.find("max_page_count") != import_buffers.end()) {
56  import_buffers["max_page_count"]->addBigint(memory_info.maxNumPages);
57  }
58  if (import_buffers.find("page_size") != import_buffers.end()) {
59  import_buffers["page_size"]->addBigint(memory_info.pageSize);
60  }
61  if (import_buffers.find("allocated_page_count") != import_buffers.end()) {
62  import_buffers["allocated_page_count"]->addBigint(memory_info.numPageAllocated);
63  }
64  if (import_buffers.find("used_page_count") != import_buffers.end()) {
65  import_buffers["used_page_count"]->addBigint(used_page_count);
66  }
67  if (import_buffers.find("free_page_count") != import_buffers.end()) {
68  import_buffers["free_page_count"]->addBigint(free_page_count);
69  }
70  device_id++;
71  }
72  }
73 }
74 
76  import_buffer->add_value(import_buffer->getColumnDesc(), "", true, {});
77 }
78 
80  const std::map<std::string, std::vector<MemoryInfo>>& memory_info_by_device_type,
81  std::map<std::string, import_export::TypedImportBuffer*>& import_buffers) {
82  for (const auto& [device_type, memory_info_vector] : memory_info_by_device_type) {
83  int32_t device_id{0};
84  for (const auto& memory_info : memory_info_vector) {
85  for (const auto& memory_data : memory_info.nodeMemoryData) {
86  if (import_buffers.find("node") != import_buffers.end()) {
87  import_buffers["node"]->addString("Server");
88  }
89  const auto& chunk_key = memory_data.chunk_key;
90  if (import_buffers.find("database_id") != import_buffers.end()) {
91  auto import_buffer = import_buffers["database_id"];
92  if (chunk_key.empty()) {
93  set_null(import_buffer);
94  } else {
95  import_buffer->addInt(chunk_key[CHUNK_KEY_DB_IDX]);
96  }
97  }
98  if (import_buffers.find("table_id") != import_buffers.end()) {
99  auto import_buffer = import_buffers["table_id"];
100  if (chunk_key.empty()) {
101  set_null(import_buffer);
102  } else {
103  import_buffer->addInt(chunk_key[CHUNK_KEY_TABLE_IDX]);
104  }
105  }
106  if (import_buffers.find("column_id") != import_buffers.end()) {
107  auto import_buffer = import_buffers["column_id"];
108  if (chunk_key.empty()) {
109  set_null(import_buffer);
110  } else {
111  import_buffer->addInt(chunk_key[CHUNK_KEY_COLUMN_IDX]);
112  }
113  }
114  if (import_buffers.find("chunk_key") != import_buffers.end()) {
115  import_buffers["chunk_key"]->addArray(ArrayDatum(
116  chunk_key.size() * sizeof(int32_t),
117  reinterpret_cast<int8_t*>(const_cast<int32_t*>(chunk_key.data())),
118  DoNothingDeleter()));
119  }
120  if (import_buffers.find("device_id") != import_buffers.end()) {
121  import_buffers["device_id"]->addInt(device_id);
122  }
123  if (import_buffers.find("device_type") != import_buffers.end()) {
124  import_buffers["device_type"]->addString(device_type);
125  }
126  if (import_buffers.find("memory_status") != import_buffers.end()) {
127  auto memory_status =
128  (memory_data.memStatus == Buffer_Namespace::MemStatus::FREE ? "FREE"
129  : "USED");
130  import_buffers["memory_status"]->addString(memory_status);
131  }
132  if (import_buffers.find("page_count") != import_buffers.end()) {
133  import_buffers["page_count"]->addBigint(memory_data.numPages);
134  }
135  if (import_buffers.find("page_size") != import_buffers.end()) {
136  import_buffers["page_size"]->addBigint(memory_info.pageSize);
137  }
138  if (import_buffers.find("slab_id") != import_buffers.end()) {
139  import_buffers["slab_id"]->addInt(memory_data.slabNum);
140  }
141  if (import_buffers.find("start_page") != import_buffers.end()) {
142  import_buffers["start_page"]->addBigint(memory_data.startPage);
143  }
144  if (import_buffers.find("last_touch_epoch") != import_buffers.end()) {
145  import_buffers["last_touch_epoch"]->addBigint(memory_data.touch);
146  }
147  }
148  device_id++;
149  }
150  }
151 }
152 } // namespace
153 
155  const std::string& table_name) {
157  row_count_ = 0;
158  const auto& data_mgr = Catalog_Namespace::SysCatalog::instance().getDataMgr();
159  // DataMgr::getMemoryInfoUnlocked() is used here because a lock on buffer_access_mutex_
160  // is already acquired in DataMgr::getChunkMetadataVecForKeyPrefix()
162  data_mgr.getMemoryInfoUnlocked(MemoryLevel::CPU_LEVEL);
164  data_mgr.getMemoryInfoUnlocked(MemoryLevel::GPU_LEVEL);
166  for (const auto& [device_type, memory_info_vector] : memory_info_by_device_type_) {
167  row_count_ += memory_info_vector.size();
168  }
169  } else if (foreign_table_->tableName ==
171  for (auto& [device_type, memory_info_vector] : memory_info_by_device_type_) {
172  for (auto& memory_info : memory_info_vector) {
173  for (auto& memory_data : memory_info.nodeMemoryData) {
174  if (memory_data.memStatus == Buffer_Namespace::MemStatus::FREE) {
175  memory_data.chunk_key.clear();
176  } else {
177  CHECK_GE(memory_data.chunk_key.size(), static_cast<size_t>(4));
178  }
179  }
180  row_count_ += memory_info.nodeMemoryData.size();
181  }
182  }
183  } else {
184  UNREACHABLE() << "Unexpected table name: " << table_name;
185  }
186 }
187 
189  const std::string& table_name,
190  std::map<std::string, import_export::TypedImportBuffer*>& import_buffers) {
193  import_buffers);
194  } else if (foreign_table_->tableName ==
197  import_buffers);
198  } else {
199  UNREACHABLE() << "Unexpected table name: " << foreign_table_->tableName;
200  }
201 }
202 } // namespace foreign_storage
static constexpr const char * MEMORY_DETAILS_SYS_TABLE_NAME
Definition: Catalog.h:105
std::string tableName
void initializeObjectsForTable(const std::string &table_name) override
void add_value(const ColumnDescriptor *cd, const std::string_view val, const bool is_null, const CopyParams &copy_params)
Definition: Importer.cpp:549
#define CHUNK_KEY_DB_IDX
Definition: types.h:39
#define UNREACHABLE()
Definition: Logger.h:255
#define CHECK_GE(x, y)
Definition: Logger.h:224
std::conditional_t< is_cuda_compiler(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
Definition: sqltypes.h:208
Data_Namespace::DataMgr & getDataMgr() const
Definition: SysCatalog.h:217
static SysCatalog & instance()
Definition: SysCatalog.h:326
This file contains the class specification and related data structures for SysCatalog.
CONSTEXPR DEVICE void set_null(T &value)
#define CHUNK_KEY_TABLE_IDX
Definition: types.h:40
static constexpr const char * MEMORY_SUMMARY_SYS_TABLE_NAME
Definition: Catalog.h:104
std::map< std::string, std::vector< MemoryInfo > > memory_info_by_device_type_
void populate_import_buffers_for_memory_details(const std::map< std::string, std::vector< MemoryInfo >> &memory_info_by_device_type, std::map< std::string, import_export::TypedImportBuffer * > &import_buffers)
const ColumnDescriptor * getColumnDesc() const
Definition: Importer.h:311
void populateChunkBuffersForTable(const std::string &table_name, std::map< std::string, import_export::TypedImportBuffer * > &import_buffers) override
#define CHUNK_KEY_COLUMN_IDX
Definition: types.h:41
void populate_import_buffers_for_memory_summary(const std::map< std::string, std::vector< MemoryInfo >> &memory_info_by_device_type, std::map< std::string, import_export::TypedImportBuffer * > &import_buffers)