CpuCacheMgr.cpp 2.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

G
groot 已提交
18

S
starlord 已提交
19
#include "cache/CpuCacheMgr.h"
20
#include "server/Config.h"
S
starlord 已提交
21
#include "utils/Log.h"
G
groot 已提交
22

S
starlord 已提交
23 24
#include <utility>

G
groot 已提交
25
namespace zilliz {
J
jinhai 已提交
26
namespace milvus {
G
groot 已提交
27 28
namespace cache {

J
jinhai 已提交
29
namespace {
S
starlord 已提交
30
constexpr int64_t unit = 1024 * 1024 * 1024;
J
jinhai 已提交
31 32
}

G
groot 已提交
33
CpuCacheMgr::CpuCacheMgr() {
S
starlord 已提交
34
    server::Config &config = server::Config::GetInstance();
35 36 37 38 39 40 41 42
    Status s;

    int32_t cpu_mem_cap;
    s = config.GetCacheConfigCpuMemCapacity(cpu_mem_cap);
    if (!s.ok()) {
        SERVER_LOG_ERROR << s.message();
    }
    int64_t cap = cpu_mem_cap * unit;
S
starlord 已提交
43
    cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL << 32);
S
starlord 已提交
44

45 46 47 48 49
    float cpu_mem_threshold;
    s = config.GetCacheConfigCpuMemThreshold(cpu_mem_threshold);
    if (!s.ok()) {
        SERVER_LOG_ERROR << s.message();
    }
Y
yudong.cai 已提交
50 51
    if (cpu_mem_threshold > 0.0 && cpu_mem_threshold <= 1.0) {
        cache_->set_freemem_percent(cpu_mem_threshold);
S
starlord 已提交
52
    } else {
Y
yudong.cai 已提交
53 54
        SERVER_LOG_ERROR << "Invalid cpu_mem_threshold: " << cpu_mem_threshold
                         << ", by default set to " << cache_->freemem_percent();
S
starlord 已提交
55
    }
G
groot 已提交
56 57
}

S
starlord 已提交
58 59
CpuCacheMgr *
CpuCacheMgr::GetInstance() {
S
starlord 已提交
60 61 62 63
    static CpuCacheMgr s_mgr;
    return &s_mgr;
}

S
starlord 已提交
64 65
engine::VecIndexPtr
CpuCacheMgr::GetIndex(const std::string &key) {
S
starlord 已提交
66
    DataObjPtr obj = GetItem(key);
S
starlord 已提交
67
    if (obj != nullptr) {
S
starlord 已提交
68 69 70 71 72 73
        return obj->data();
    }

    return nullptr;
}

S
starlord 已提交
74 75 76
} // namespace cache
} // namespace milvus
} // namespace zilliz