CpuCacheMgr.cpp 1.8 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 19 20

#include "CpuCacheMgr.h"
#include "server/ServerConfig.h"
S
starlord 已提交
21
#include "utils/Log.h"
G
groot 已提交
22 23

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

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

G
groot 已提交
31
CpuCacheMgr::CpuCacheMgr() {
Y
yudong.cai 已提交
32 33
    server::ServerConfig& config = server::ServerConfig::GetInstance();
    int64_t cap = config.GetCacheConfigCpuMemCapacity() * unit;
S
starlord 已提交
34
    cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL<<32);
S
starlord 已提交
35

Y
yudong.cai 已提交
36 37 38
    float cpu_mem_threshold = config.GetCacheConfigCpuMemThreshold();
    if (cpu_mem_threshold > 0.0 && cpu_mem_threshold <= 1.0) {
        cache_->set_freemem_percent(cpu_mem_threshold);
S
starlord 已提交
39
    } else {
Y
yudong.cai 已提交
40 41
        SERVER_LOG_ERROR << "Invalid cpu_mem_threshold: " << cpu_mem_threshold
                         << ", by default set to " << cache_->freemem_percent();
S
starlord 已提交
42
    }
G
groot 已提交
43 44
}

S
starlord 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
CpuCacheMgr* CpuCacheMgr::GetInstance() {
    static CpuCacheMgr s_mgr;
    return &s_mgr;
}

engine::VecIndexPtr CpuCacheMgr::GetIndex(const std::string& key) {
    DataObjPtr obj = GetItem(key);
    if(obj != nullptr) {
        return obj->data();
    }

    return nullptr;
}

G
groot 已提交
59 60 61
}
}
}