CommonUtil.cpp 5.7 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"
G
groot 已提交
19
#include "utils/Log.h"
G
groot 已提交
20 21

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

#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

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

namespace fs = boost::filesystem;

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

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

S
starlord 已提交
57
bool
S
starlord 已提交
58 59
CommonUtil::GetSystemAvailableThreads(uint32_t& thread_count) {
    // threadCnt = std::thread::hardware_concurrency();
S
starlord 已提交
60 61
    thread_count = sysconf(_SC_NPROCESSORS_CONF);
    thread_count *= THREAD_MULTIPLY_CPU;
S
starlord 已提交
62 63 64
    if (thread_count == 0) {
        thread_count = 8;
    }
G
groot 已提交
65 66 67 68

    return true;
}

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

    closedir(dp);
    return true;
}

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

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

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

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

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

S
starlord 已提交
109
    return Status::OK();
G
groot 已提交
110 111
}

G
groot 已提交
112
namespace {
S
starlord 已提交
113
void
S
starlord 已提交
114 115 116
RemoveDirectory(const std::string& path) {
    DIR* dir = nullptr;
    struct dirent* dmsg;
S
starlord 已提交
117 118 119 120 121 122
    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 已提交
123
            if (strcmp(dmsg->d_name, ".") != 0 && strcmp(dmsg->d_name, "..") != 0) {
S
starlord 已提交
124 125 126 127
                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 已提交
128
                }
S
starlord 已提交
129
                remove(file_name);
G
groot 已提交
130 131
            }
        }
S
starlord 已提交
132
    }
G
groot 已提交
133

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

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

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

    RemoveDirectory(path);
S
starlord 已提交
154
    return Status::OK();
G
groot 已提交
155 156
}

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

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

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

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

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

G
groot 已提交
187 188 189
    buf[cnt] = '\0';

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

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

S
starlord 已提交
203 204
    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 已提交
205
    if (ret <= 0) {
G
groot 已提交
206 207 208 209 210 211 212 213 214 215
        return false;
    }

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

    return true;
}

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

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

S
starlord 已提交
226 227 228
}  // namespace server
}  // namespace milvus
}  // namespace zilliz