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.

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

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

J
jinhai 已提交
24
namespace milvus {
G
groot 已提交
25 26
namespace cache {

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

G
groot 已提交
31
CpuCacheMgr::CpuCacheMgr() {
S
starlord 已提交
32
    server::Config& config = server::Config::GetInstance();
33 34
    Status s;

Y
yudong.cai 已提交
35 36
    int32_t cpu_cache_cap;
    s = config.GetCacheConfigCpuCacheCapacity(cpu_cache_cap);
37 38 39
    if (!s.ok()) {
        SERVER_LOG_ERROR << s.message();
    }
Y
yudong.cai 已提交
40
    int64_t cap = cpu_cache_cap * unit;
S
starlord 已提交
41
    cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL << 32);
S
starlord 已提交
42

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

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

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

    return nullptr;
}

S
starlord 已提交
72 73
}  // namespace cache
}  // namespace milvus