YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_memctx.cc
Line
Count
Source
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#include "yb/yql/pggate/pg_memctx.h"
17
18
#include "yb/util/status.h"
19
20
#include "yb/yql/pggate/pg_tabledesc.h"
21
22
namespace yb {
23
namespace pggate {
24
25
218k
PgMemctx::PgMemctx() {
26
218k
}
27
28
218k
PgMemctx::~PgMemctx() {
29
218k
  Clear();
30
218k
}
31
32
namespace {
33
  // Table of memory contexts.
34
  // - Although defined in Yugabyte, this table is owned and managed by Postgres process.
35
  //   Other processes cannot control "pggate::PgMemctx" to avoid memory violations.
36
  // - Table "postgres_process_memctxs" is to help releasing the references to PgStatement when
37
  //   Postgres Process (a C program) is exiting.
38
  // - Transaction layer and Postgres BOTH hold references to PgStatement objects, and those
39
  //   PgGate objects wouldn't be destroyed unless both layers release their references or both
40
  //   layers are terminated.
41
  std::unordered_map<PgMemctx *, PgMemctx::SharedPtr> postgres_process_memctxs;
42
} // namespace
43
44
218k
PgMemctx *PgMemctx::Create() {
45
218k
  auto memctx = std::make_shared<PgMemctx>();
46
218k
  postgres_process_memctxs[memctx.get()] = memctx;
47
218k
  return memctx.get();
48
218k
}
49
50
215k
Status PgMemctx::Destroy(PgMemctx *handle) {
51
215k
  if (handle) {
52
215k
    SCHECK(postgres_process_memctxs.find(handle) != postgres_process_memctxs.end(),
53
215k
           InternalError, "Invalid memory context handle");
54
215k
    postgres_process_memctxs.erase(handle);
55
215k
  }
56
215k
  return Status::OK();
57
215k
}
58
59
1.17M
Status PgMemctx::Reset(PgMemctx *handle) {
60
1.17M
  if (handle) {
61
1.17M
    SCHECK(postgres_process_memctxs.find(handle) != postgres_process_memctxs.end(),
62
1.17M
           InternalError, "Invalid memory context handle");
63
1.17M
    handle->Clear();
64
1.17M
  }
65
1.17M
  return Status::OK();
66
1.17M
}
67
68
2.69M
void PgMemctx::Register(Registrable *obj) {
69
2.69M
  registered_objects_.push_back(*obj);
70
2.69M
  obj->memctx_ = this;
71
2.69M
}
72
73
2.30M
void PgMemctx::Destroy(Registrable *obj) {
74
2.30M
  auto& objs = obj->memctx_->registered_objects_;
75
2.30M
  objs.erase_and_dispose(objs.iterator_to(*obj), std::default_delete<Registrable>());
76
2.30M
}
77
78
1.39M
void PgMemctx::Clear() {
79
1.39M
  tabledesc_map_.clear();
80
1.39M
  registered_objects_.clear_and_dispose(std::default_delete<Registrable>());
81
1.39M
}
82
83
1.03M
void PgMemctx::Cache(size_t hash_id, const PgTableDescPtr &table_desc) {
84
  // Add table descriptor to table.
85
1.03M
  tabledesc_map_[hash_id] = table_desc;
86
1.03M
}
87
88
5.96M
void PgMemctx::GetCache(size_t hash_id, PgTableDesc **handle) {
89
  // Read table descriptor to table.
90
5.96M
  const auto iter = tabledesc_map_.find(hash_id);
91
5.96M
  if (iter == tabledesc_map_.end()) {
92
1.03M
    *handle = nullptr;
93
4.93M
  } else {
94
4.93M
    *handle = iter->second.get();
95
4.93M
  }
96
5.96M
}
97
98
1.65k
void ClearGlobalPgMemctxMap() {
99
1.65k
  postgres_process_memctxs.clear();
100
1.65k
}
101
102
}  // namespace pggate
103
}  // namespace yb