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

#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 已提交
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
S
starlord 已提交
57 58
CommonUtil::GetSystemAvailableThreads(uint32_t& thread_count) {
    // threadCnt = std::thread::hardware_concurrency();
S
starlord 已提交
59 60
    thread_count = sysconf(_SC_NPROCESSORS_CONF);
    thread_count *= THREAD_MULTIPLY_CPU;
S
starlord 已提交
61 62 63
    if (thread_count == 0) {
        thread_count = 8;
    }
G
groot 已提交
64 65 66 67

    return true;
}

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

    closedir(dp);
    return true;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return true;
}

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

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

S
starlord 已提交
225 226
}  // namespace server
}  // namespace milvus