CommonUtil.cpp 7.0 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "utils/CommonUtil.h"
13 14
#include "cache/CpuCacheMgr.h"
#include "cache/GpuCacheMgr.h"
15
#include "config/Config.h"
G
groot 已提交
16
#include "utils/Log.h"
G
groot 已提交
17 18

#include <dirent.h>
S
starlord 已提交
19
#include <pwd.h>
G
groot 已提交
20
#include <string.h>
S
starlord 已提交
21 22
#include <sys/stat.h>
#include <sys/sysinfo.h>
G
groot 已提交
23
#include <time.h>
S
starlord 已提交
24 25 26
#include <unistd.h>
#include <iostream>
#include <thread>
27
#include <vector>
G
groot 已提交
28 29 30 31 32 33 34 35 36 37 38

#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

S
shengjh 已提交
39 40
#include <fiu-local.h>

J
jinhai 已提交
41
namespace milvus {
G
groot 已提交
42 43 44 45
namespace server {

namespace fs = boost::filesystem;

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

S
starlord 已提交
53
    return ret == 0;  // succeed 0, failed -1
G
groot 已提交
54 55
}

S
starlord 已提交
56
bool
Y
yudong.cai 已提交
57
CommonUtil::GetSystemAvailableThreads(int64_t& thread_count) {
S
starlord 已提交
58
    // threadCnt = std::thread::hardware_concurrency();
S
starlord 已提交
59 60
    thread_count = sysconf(_SC_NPROCESSORS_CONF);
    thread_count *= THREAD_MULTIPLY_CPU;
S
shengjh 已提交
61 62
    fiu_do_on("CommonUtil.GetSystemAvailableThreads.zero_thread", thread_count = 0);

S
starlord 已提交
63 64 65
    if (thread_count == 0) {
        thread_count = 8;
    }
G
groot 已提交
66 67 68 69

    return true;
}

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

    closedir(dp);
    return true;
}

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

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

    fs::path fs_path(path);
    fs::path parent_path = fs_path.parent_path();
S
starlord 已提交
95
    Status err_status = CreateDirectory(parent_path.string());
S
shengjh 已提交
96
    fiu_do_on("CommonUtil.CreateDirectory.create_parent_fail", err_status = Status(SERVER_INVALID_ARGUMENT, ""));
S
starlord 已提交
97
    if (!err_status.ok()) {
S
starlord 已提交
98
        return err_status;
G
groot 已提交
99 100
    }

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

S
starlord 已提交
106
    int makeOK = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IROTH);
S
shengjh 已提交
107
    fiu_do_on("CommonUtil.CreateDirectory.create_dir_fail", makeOK = 1);
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
    const size_t buf_len = 1024;
    char buf[buf_len];
J
JUN 已提交
185
    ssize_t cnt = readlink("/proc/self/exe", buf, buf_len);
S
shengjh 已提交
186
    fiu_do_on("CommonUtil.GetExePath.readlink_fail", cnt = -1);
S
starlord 已提交
187
    if (cnt < 0 || cnt >= buf_len) {
G
groot 已提交
188 189 190
        return "";
    }

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

    std::string exe_path = buf;
S
shengjh 已提交
194
    fiu_do_on("CommonUtil.GetExePath.exe_path_error", exe_path = "/");
A
ABNER-1 已提交
195
    if (exe_path.rfind('/') != exe_path.length() - 1) {
G
groot 已提交
196
        std::string sub_str = exe_path.substr(0, exe_path.rfind('/'));
G
groot 已提交
197 198
        return sub_str + "/";
    }
G
groot 已提交
199
    return exe_path;
G
groot 已提交
200
}
G
groot 已提交
201

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

S
starlord 已提交
208 209
    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 已提交
210
    if (ret <= 0) {
G
groot 已提交
211 212 213 214 215 216 217 218 219 220
        return false;
    }

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

    return true;
}

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

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

F
feisiyicl 已提交
231
#ifdef ENABLE_CPU_PROFILING
C
Cai Yudong 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
std::string
CommonUtil::GetCurrentTimeStr() {
    time_t tt;
    time(&tt);
    tt = tt + 8 * 60;
    tm t;
    gmtime_r(&tt, &t);

    std::string str = std::to_string(t.tm_year + 1900) + "_" + std::to_string(t.tm_mon + 1) + "_" +
                      std::to_string(t.tm_mday) + "_" + std::to_string(t.tm_hour) + "_" + std::to_string(t.tm_min) +
                      "_" + std::to_string(t.tm_sec);
    return str;
}
#endif

247 248 249
void
CommonUtil::EraseFromCache(const std::string& item_key) {
    if (item_key.empty()) {
250
        LOG_SERVER_ERROR_ << "Empty key cannot be erased from cache";
251 252 253 254 255 256 257 258
        return;
    }

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

#ifdef MILVUS_GPU_VERSION
    server::Config& config = server::Config::GetInstance();
    std::vector<int64_t> gpus;
G
Gxz 已提交
259
    config.GetGpuResourceConfigSearchResources(gpus);
260 261 262 263 264 265
    for (auto& gpu : gpus) {
        cache::GpuCacheMgr::GetInstance(gpu)->EraseItem(item_key);
    }
#endif
}

S
starlord 已提交
266 267
}  // namespace server
}  // namespace milvus