CommonUtil.cpp 6.3 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 "utils/CommonUtil.h"
19 20 21
#include "cache/CpuCacheMgr.h"
#include "cache/GpuCacheMgr.h"
#include "server/Config.h"
G
groot 已提交
22
#include "utils/Log.h"
G
groot 已提交
23 24

#include <dirent.h>
S
starlord 已提交
25
#include <pwd.h>
G
groot 已提交
26
#include <string.h>
S
starlord 已提交
27 28
#include <sys/stat.h>
#include <sys/sysinfo.h>
G
groot 已提交
29
#include <time.h>
S
starlord 已提交
30 31 32
#include <unistd.h>
#include <iostream>
#include <thread>
33
#include <vector>
G
groot 已提交
34 35 36 37 38 39 40 41 42 43 44

#include "boost/filesystem.hpp"

#if defined(__x86_64__)
#define THREAD_MULTIPLY_CPU 1
#elif defined(__powerpc64__)
#define THREAD_MULTIPLY_CPU 4
#else
#define THREAD_MULTIPLY_CPU 1
#endif

J
jinhai 已提交
45
namespace milvus {
G
groot 已提交
46 47 48 49
namespace server {

namespace fs = boost::filesystem;

S
starlord 已提交
50
bool
S
starlord 已提交
51
CommonUtil::GetSystemMemInfo(uint64_t& total_mem, uint64_t& free_mem) {
G
groot 已提交
52 53
    struct sysinfo info;
    int ret = sysinfo(&info);
S
starlord 已提交
54 55
    total_mem = info.totalram;
    free_mem = info.freeram;
G
groot 已提交
56

S
starlord 已提交
57
    return ret == 0;  // succeed 0, failed -1
G
groot 已提交
58 59
}

S
starlord 已提交
60
bool
Y
yudong.cai 已提交
61
CommonUtil::GetSystemAvailableThreads(int64_t& thread_count) {
S
starlord 已提交
62
    // threadCnt = std::thread::hardware_concurrency();
S
starlord 已提交
63 64
    thread_count = sysconf(_SC_NPROCESSORS_CONF);
    thread_count *= THREAD_MULTIPLY_CPU;
S
starlord 已提交
65 66 67
    if (thread_count == 0) {
        thread_count = 8;
    }
G
groot 已提交
68 69 70 71

    return true;
}

S
starlord 已提交
72
bool
S
starlord 已提交
73 74
CommonUtil::IsDirectoryExist(const std::string& path) {
    DIR* dp = nullptr;
G
groot 已提交
75 76 77 78 79 80 81 82
    if ((dp = opendir(path.c_str())) == nullptr) {
        return false;
    }

    closedir(dp);
    return true;
}

S
starlord 已提交
83
Status
S
starlord 已提交
84
CommonUtil::CreateDirectory(const std::string& path) {
S
starlord 已提交
85
    if (path.empty()) {
S
starlord 已提交
86
        return Status::OK();
S
starlord 已提交
87 88
    }

S
starlord 已提交
89 90 91
    struct stat directory_stat;
    int status = stat(path.c_str(), &directory_stat);
    if (status == 0) {
S
starlord 已提交
92
        return Status::OK();  // already exist
G
groot 已提交
93 94 95 96
    }

    fs::path fs_path(path);
    fs::path parent_path = fs_path.parent_path();
S
starlord 已提交
97
    Status err_status = CreateDirectory(parent_path.string());
S
starlord 已提交
98
    if (!err_status.ok()) {
S
starlord 已提交
99
        return err_status;
G
groot 已提交
100 101
    }

S
starlord 已提交
102 103
    status = stat(path.c_str(), &directory_stat);
    if (status == 0) {
S
starlord 已提交
104
        return Status::OK();  // already exist
G
groot 已提交
105 106
    }

S
starlord 已提交
107
    int makeOK = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IROTH);
G
groot 已提交
108
    if (makeOK != 0) {
S
starlord 已提交
109
        return Status(SERVER_UNEXPECTED_ERROR, "failed to create directory: " + path);
G
groot 已提交
110 111
    }

S
starlord 已提交
112
    return Status::OK();
G
groot 已提交
113 114
}

G
groot 已提交
115
namespace {
S
starlord 已提交
116
void
S
starlord 已提交
117 118 119
RemoveDirectory(const std::string& path) {
    DIR* dir = nullptr;
    struct dirent* dmsg;
S
starlord 已提交
120 121 122 123 124 125
    const int32_t buf_size = 256;
    char file_name[buf_size];

    std::string folder_name = path + "/%s";
    if ((dir = opendir(path.c_str())) != nullptr) {
        while ((dmsg = readdir(dir)) != nullptr) {
S
starlord 已提交
126
            if (strcmp(dmsg->d_name, ".") != 0 && strcmp(dmsg->d_name, "..") != 0) {
S
starlord 已提交
127 128 129 130
                snprintf(file_name, buf_size, folder_name.c_str(), dmsg->d_name);
                std::string tmp = file_name;
                if (tmp.find(".") == std::string::npos) {
                    RemoveDirectory(file_name);
G
groot 已提交
131
                }
S
starlord 已提交
132
                remove(file_name);
G
groot 已提交
133 134
            }
        }
S
starlord 已提交
135
    }
G
groot 已提交
136

S
starlord 已提交
137 138
    if (dir != nullptr) {
        closedir(dir);
G
groot 已提交
139
    }
S
starlord 已提交
140
    remove(path.c_str());
G
groot 已提交
141
}
S
starlord 已提交
142
}  // namespace
G
groot 已提交
143

S
starlord 已提交
144
Status
S
starlord 已提交
145
CommonUtil::DeleteDirectory(const std::string& path) {
S
starlord 已提交
146
    if (path.empty()) {
S
starlord 已提交
147
        return Status::OK();
S
starlord 已提交
148 149
    }

S
starlord 已提交
150 151
    struct stat directory_stat;
    int statOK = stat(path.c_str(), &directory_stat);
S
starlord 已提交
152 153 154
    if (statOK != 0) {
        return Status::OK();
    }
G
groot 已提交
155 156

    RemoveDirectory(path);
S
starlord 已提交
157
    return Status::OK();
G
groot 已提交
158 159
}

S
starlord 已提交
160
bool
S
starlord 已提交
161
CommonUtil::IsFileExist(const std::string& path) {
G
groot 已提交
162 163 164
    return (access(path.c_str(), F_OK) == 0);
}

S
starlord 已提交
165
uint64_t
S
starlord 已提交
166
CommonUtil::GetFileSize(const std::string& path) {
S
starlord 已提交
167 168
    struct stat file_info;
    if (stat(path.c_str(), &file_info) < 0) {
S
starlord 已提交
169 170
        return 0;
    }
S
starlord 已提交
171 172

    return static_cast<uint64_t>(file_info.st_size);
S
starlord 已提交
173 174
}

S
starlord 已提交
175 176
std::string
CommonUtil::GetFileName(std::string filename) {
S
starlord 已提交
177 178 179 180
    int pos = filename.find_last_of('/');
    return filename.substr(pos + 1);
}

S
starlord 已提交
181 182
std::string
CommonUtil::GetExePath() {
G
groot 已提交
183 184 185
    const size_t buf_len = 1024;
    char buf[buf_len];
    size_t cnt = readlink("/proc/self/exe", buf, buf_len);
S
starlord 已提交
186
    if (cnt < 0 || cnt >= buf_len) {
G
groot 已提交
187 188 189
        return "";
    }

G
groot 已提交
190 191 192
    buf[cnt] = '\0';

    std::string exe_path = buf;
S
starlord 已提交
193
    if (exe_path.rfind('/') != exe_path.length()) {
G
groot 已提交
194
        std::string sub_str = exe_path.substr(0, exe_path.rfind('/'));
G
groot 已提交
195 196
        return sub_str + "/";
    }
G
groot 已提交
197
    return exe_path;
G
groot 已提交
198
}
G
groot 已提交
199

S
starlord 已提交
200
bool
S
starlord 已提交
201 202
CommonUtil::TimeStrToTime(const std::string& time_str, time_t& time_integer, tm& time_struct,
                          const std::string& format) {
G
groot 已提交
203 204 205
    time_integer = 0;
    memset(&time_struct, 0, sizeof(tm));

S
starlord 已提交
206 207
    int ret = sscanf(time_str.c_str(), format.c_str(), &(time_struct.tm_year), &(time_struct.tm_mon),
                     &(time_struct.tm_mday), &(time_struct.tm_hour), &(time_struct.tm_min), &(time_struct.tm_sec));
S
starlord 已提交
208
    if (ret <= 0) {
G
groot 已提交
209 210 211 212 213 214 215 216 217 218
        return false;
    }

    time_struct.tm_year -= 1900;
    time_struct.tm_mon--;
    time_integer = mktime(&time_struct);

    return true;
}

S
starlord 已提交
219
void
S
starlord 已提交
220
CommonUtil::ConvertTime(time_t time_integer, tm& time_struct) {
S
starlord 已提交
221
    localtime_r(&time_integer, &time_struct);
G
groot 已提交
222 223
}

S
starlord 已提交
224
void
S
starlord 已提交
225
CommonUtil::ConvertTime(tm time_struct, time_t& time_integer) {
G
groot 已提交
226
    time_integer = mktime(&time_struct);
G
groot 已提交
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
void
CommonUtil::EraseFromCache(const std::string& item_key) {
    if (item_key.empty()) {
        SERVER_LOG_ERROR << "Empty key cannot be erased from cache";
        return;
    }

    cache::CpuCacheMgr::GetInstance()->EraseItem(item_key);

#ifdef MILVUS_GPU_VERSION
    server::Config& config = server::Config::GetInstance();
    std::vector<int64_t> gpus;
    Status s = config.GetGpuResourceConfigSearchResources(gpus);
    for (auto& gpu : gpus) {
        cache::GpuCacheMgr::GetInstance(gpu)->EraseItem(item_key);
    }
#endif
}

S
starlord 已提交
248 249
}  // namespace server
}  // namespace milvus