1 /**
2 * Copyright 2019 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "minddata/dataset/util/services.h"
17
18 #include <climits>
19 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
20 #include <sys/syscall.h>
21 #else
22 #include <stdlib.h>
23 #endif
24 #include <unistd.h>
25 #include "./securec.h"
26 #include "minddata/dataset/util/circular_pool.h"
27 #include "minddata/dataset/util/random.h"
28 #include "minddata/dataset/util/task_manager.h"
29
30 #if defined(__APPLE__)
31 #define LOGIN_NAME_MAX 256
32 #endif
33
34 namespace mindspore {
35 namespace dataset {
36 std::unique_ptr<Services> Services::instance_ = nullptr;
37 std::once_flag Services::init_instance_flag_;
38 std::map<std::string, uint64_t> Services::unique_id_list_ = {};
39 uint64_t Services::unique_id_count_ = 0;
40 std::mutex Services::unique_id_mutex_;
41
42 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
GetUserName()43 std::string Services::GetUserName() {
44 char user[LOGIN_NAME_MAX];
45 (void)getlogin_r(user, sizeof(user));
46 return std::string(user);
47 }
48
GetHostName()49 std::string Services::GetHostName() {
50 char host[LOGIN_NAME_MAX];
51 (void)gethostname(host, sizeof(host));
52 return std::string(host);
53 }
54
GetLWP()55 int Services::GetLWP() { return syscall(SYS_gettid); }
56 #endif
57
GetUniqueID()58 std::string Services::GetUniqueID() {
59 const std::string kStr = "abcdefghijklmnopqrstuvwxyz0123456789";
60 std::mt19937 gen = GetRandomDevice();
61 std::uniform_int_distribution<uint32_t> dist(0, kStr.size() - 1);
62 char buffer[UNIQUEID_LEN];
63 {
64 std::unique_lock<std::mutex> lock(unique_id_mutex_);
65 while (true) {
66 auto ret = memset_s(buffer, UNIQUEID_LEN, 0, UNIQUEID_LEN);
67 if (ret != 0) {
68 MS_LOG(ERROR) << "memset_s error, errorno(" << ret << ")";
69 return std::string("");
70 }
71 for (int i = 0; i < UNIQUEID_LEN; i++) {
72 buffer[i] = kStr[dist(gen)];
73 }
74 if (unique_id_list_.find(std::string(buffer, UNIQUEID_LEN)) != unique_id_list_.end()) {
75 continue;
76 }
77 unique_id_list_[std::string(buffer, UNIQUEID_LEN)] = unique_id_count_;
78 unique_id_count_++;
79 // Temporary solution to solve a long stability memory increasing problem that
80 // we limit the size of unique_id_list_ not to greater than UNIQUEID_LIST_LIMITS(1024).
81 if (unique_id_list_.size() >= UNIQUEID_LIST_LIMITS) {
82 for (auto iter = unique_id_list_.begin(); iter != unique_id_list_.end();) {
83 if (iter->second < UNIQUEID_HALF_INDEX) {
84 iter = unique_id_list_.erase(iter);
85 unique_id_count_--;
86 } else {
87 iter->second -= UNIQUEID_HALF_INDEX;
88 iter++;
89 }
90 }
91 }
92 MS_LOG(DEBUG) << "unique_id_list_ size is " << unique_id_list_.size() << ", count is " << unique_id_count_;
93 break;
94 }
95 }
96 return std::string(buffer, UNIQUEID_LEN);
97 }
98
CreateAllInstances()99 Status Services::CreateAllInstances() {
100 // First one is always the TaskManager
101 RETURN_IF_NOT_OK(TaskManager::CreateInstance());
102 TaskManager &tm = TaskManager::GetInstance();
103 RETURN_IF_NOT_OK(tm.ServiceStart());
104 return Status::OK();
105 }
106
Services()107 Services::Services() : pool_(nullptr) {
108 Status rc = CircularPool::CreateCircularPool(&pool_, -1, 16, true); // each arena 16M
109 if (rc.IsError()) {
110 std::terminate();
111 }
112 }
113
~Services()114 Services::~Services() noexcept {
115 // Shutdown in reverse order.
116 auto n = hook_.size();
117 while (n > 0) {
118 hook_.pop_back();
119 n = hook_.size();
120 }
121 }
122 } // namespace dataset
123 } // namespace mindspore
124