ob_sql_mem_mgr_processor.h 5.6 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef OB_SQL_MEM_MGR_PROCESSOR_H
#define OB_SQL_MEM_MGR_PROCESSOR_H

#include "ob_tenant_sql_memory_manager.h"
#include "sql/engine/basic/ob_chunk_row_store.h"

namespace oceanbase {
namespace sql {

class ObSqlMemMgrProcessor : public ObSqlMemoryCallback {
G
gm 已提交
23
private:
O
oceanbase-admin 已提交
24 25
  using PredFunc = std::function<bool(int64_t)>;

G
gm 已提交
26
public:
O
oceanbase-admin 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  ObSqlMemMgrProcessor(ObSqlWorkAreaProfile& profile)
      : profile_(profile),
        sql_mem_mgr_(nullptr),
        mem_callback_(nullptr),
        tenant_id_(OB_INVALID_ID),
        periodic_cnt_(1024),
        origin_max_mem_size_(0),
        default_available_mem_size_(0),
        is_auto_mgr_(false),
        dir_id_(0)
  {}
  virtual ~ObSqlMemMgrProcessor()
  {}

  void set_sql_mem_mgr(ObTenantSqlMemoryManager* sql_mem_mgr)
  {
    sql_mem_mgr_ = sql_mem_mgr;
  }

  OB_INLINE ObTenantSqlMemoryManager* get_sql_mem_mgr()
  {
    if (nullptr == sql_mem_mgr_) {
      sql_mem_mgr_ = MTL_GET(ObTenantSqlMemoryManager*);
      if (OB_NOT_NULL(sql_mem_mgr_)) {
        mem_callback_ = sql_mem_mgr_->get_sql_memory_callback();
      }
    }
    return sql_mem_mgr_;
  }

  // register and set cache size
  int init(ObIAllocator* allocator, uint64_t tenant_id, int64_t cache_size, const ObPhyOperatorType op_type,
      const uint64_t op_id, ObExecContext* exec_ctx);
  void destroy()
  {
    if (0 < profile_.mem_used_ && OB_NOT_NULL(mem_callback_)) {
      mem_callback_->free(profile_.mem_used_);
    }
    mem_callback_ = nullptr;
    sql_mem_mgr_ = nullptr;
    profile_.mem_used_ = 0;
  }
  void reset()
  {
    periodic_cnt_ = 1024;
    is_auto_mgr_ = false;
  }
  OB_INLINE bool is_auto_mgr() const
  {
    return is_auto_mgr_;
  }
  int get_max_available_mem_size(ObIAllocator* allocator);
  int update_max_available_mem_size_periodically(ObIAllocator* allocator, PredFunc predicate, bool& updated);
  int update_cache_size(ObIAllocator* allocator, int64_t cache_size);
  int extend_max_memory_size(
      ObIAllocator* allocator, PredFunc dump_fun, bool& need_dump, int64_t mem_used, int64_t max_times = 1024);

  int update_used_mem_size(int64_t used_size);

  void set_periodic_cnt(int64_t cnt)
  {
    periodic_cnt_ = cnt;
  }
  int64_t get_periodic_cnt() const
  {
    return periodic_cnt_;
  }

  double get_data_ratio() const
  {
    return profile_.data_ratio_;
  }
  void set_data_ratio(double ratio)
  {
    profile_.data_ratio_ = ratio;
  }

  void unregister_profile();
  void set_default_usable_mem_size(int64_t max_usable_mem_size)
  {
    default_available_mem_size_ = max_usable_mem_size;
  }
  int64_t get_mem_bound() const
  {
    return is_auto_mgr_ ? profile_.get_expect_size() : default_available_mem_size_;
  }

  void alloc(int64_t size)
  {
    profile_.delta_size_ += size;
    update_delta_size(profile_.delta_size_);
  }

  void free(int64_t size)
  {
    profile_.delta_size_ -= size;
    update_delta_size(profile_.delta_size_);
  }

  void dumped(int64_t size)
  {
    profile_.dumped_size_ += size;
    if (OB_NOT_NULL(mem_callback_)) {
      mem_callback_->dumped(size);
    }
  }

  void reset_delta_size()
  {
    profile_.delta_size_ = 0;
  }
  void reset_mem_used()
  {
    profile_.mem_used_ = 0;
  }
  int64_t get_mem_used() const
  {
    return profile_.mem_used_;
  }
  int64_t get_delta_size() const
  {
    return profile_.delta_size_;
  }
  int64_t get_data_size() const
  {
    return profile_.data_size_ + profile_.delta_size_;
  }
  void update_delta_size(int64_t delta_size)
  {
    if (delta_size > 0 && delta_size >= UPDATED_DELTA_SIZE) {
      if (OB_NOT_NULL(mem_callback_)) {
        mem_callback_->alloc(delta_size);
      }
      profile_.mem_used_ += delta_size;
      if (profile_.max_mem_used_ < profile_.mem_used_) {
        profile_.max_mem_used_ = profile_.mem_used_;
      }
      profile_.delta_size_ = 0;
      profile_.data_size_ += delta_size;
    } else if (delta_size < 0 && -delta_size >= UPDATED_DELTA_SIZE) {
      if (OB_NOT_NULL(mem_callback_)) {
        mem_callback_->free(-delta_size);
      }
      profile_.mem_used_ += delta_size;
      profile_.delta_size_ = 0;
      profile_.data_size_ += delta_size;
    }
  }

  ObSqlWorkAreaProfile& get_profile() const
  {
    return profile_;
  }

  int alloc_dir_id(int64_t& dir_id);
O
obdev 已提交
182
  int64_t get_dir_id()
O
oceanbase-admin 已提交
183 184 185 186 187 188 189 190
  {
    return dir_id_;
  }
  void set_number_pass(int32_t num_pass)
  {
    profile_.set_number_pass(num_pass);
  }

G
gm 已提交
191
private:
O
oceanbase-admin 已提交
192 193
  int try_upgrade_auto_mgr(ObIAllocator* allocator, int64_t mem_used);

G
gm 已提交
194
private:
O
oceanbase-admin 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  static const int64_t MAX_SQL_MEM_SIZE = 2 * 1024 * 1024;  // 2M
  static const int64_t UPDATED_DELTA_SIZE = 1 * 1024 * 1024;
  static const int64_t EXTEND_RATIO = 10;
  ObSqlWorkAreaProfile& profile_;
  ObTenantSqlMemoryManager* sql_mem_mgr_;
  ObSqlMemoryCallback* mem_callback_;
  uint64_t tenant_id_;
  int64_t periodic_cnt_;
  int64_t origin_max_mem_size_;
  int64_t default_available_mem_size_;
  bool is_auto_mgr_;
  int64_t dir_id_;
};

class ObSqlWorkareaUtil {
G
gm 已提交
210
public:
O
oceanbase-admin 已提交
211 212 213 214 215 216
  static int get_workarea_size(const ObSqlWorkAreaType wa_type, const int64_t tenant_id, int64_t& value);
};

}  // namespace sql
}  // namespace oceanbase
#endif /* OB_SQL_MEM_MGR_PROCESSOR_H */