• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "securec.h"
25 #include "minddata/dataset/util/circular_pool.h"
26 #include "minddata/dataset/util/random.h"
27 #include "minddata/dataset/util/task_manager.h"
28 
29 #if defined(__APPLE__)
30 #define LOGIN_NAME_MAX 256
31 #endif
32 
33 namespace mindspore {
34 namespace dataset {
35 std::unique_ptr<Services> Services::instance_ = nullptr;
36 std::once_flag Services::init_instance_flag_;
37 std::map<std::string, uint64_t> Services::unique_id_list_ = {};
38 uint64_t Services::unique_id_count_ = 0;
39 std::mutex Services::unique_id_mutex_;
40 
41 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
GetUserName()42 std::string Services::GetUserName() {
43   char user[LOGIN_NAME_MAX];
44   (void)getlogin_r(user, sizeof(user));
45   return std::string(user);
46 }
47 
GetHostName()48 std::string Services::GetHostName() {
49   char host[LOGIN_NAME_MAX];
50   (void)gethostname(host, sizeof(host));
51   return std::string(host);
52 }
53 
GetLWP()54 int Services::GetLWP() { return syscall(SYS_gettid); }
55 #endif
56 
GetUniqueID()57 std::string Services::GetUniqueID() {
58   const std::string kStr = "abcdefghijklmnopqrstuvwxyz0123456789";
59   std::mt19937 gen = GetRandomDevice();
60   std::uniform_int_distribution<uint32_t> dist(0, kStr.size() - 1);
61   char buffer[UNIQUEID_LEN];
62   {
63     std::unique_lock<std::mutex> lock(unique_id_mutex_);
64     while (true) {
65       auto ret = memset_s(buffer, UNIQUEID_LEN, 0, UNIQUEID_LEN);
66       if (ret != EOK) {
67         MS_LOG(ERROR) << "memset_s error, errorno(" << ret << ")";
68         return std::string("");
69       }
70       for (int i = 0; i < UNIQUEID_LEN; i++) {
71         buffer[i] = kStr[dist(gen)];
72       }
73       if (unique_id_list_.find(std::string(buffer, UNIQUEID_LEN)) != unique_id_list_.end()) {
74         continue;
75       }
76       unique_id_list_[std::string(buffer, UNIQUEID_LEN)] = unique_id_count_;
77       unique_id_count_++;
78       // Temporary solution to solve a long stability memory increasing problem that
79       // we limit the size of unique_id_list_ not to greater than UNIQUEID_LIST_LIMITS(1024).
80       if (unique_id_list_.size() >= UNIQUEID_LIST_LIMITS) {
81         for (auto iter = unique_id_list_.begin(); iter != unique_id_list_.end();) {
82           if (iter->second < UNIQUEID_HALF_INDEX) {
83             iter = unique_id_list_.erase(iter);
84             unique_id_count_--;
85           } else {
86             iter->second -= UNIQUEID_HALF_INDEX;
87             iter++;
88           }
89         }
90       }
91       MS_LOG(DEBUG) << "unique_id_list_ size is " << unique_id_list_.size() << ", count is " << unique_id_count_;
92       break;
93     }
94   }
95   return std::string(buffer, UNIQUEID_LEN);
96 }
97 
CreateAllInstances()98 Status Services::CreateAllInstances() {
99   // First one is always the TaskManager
100   RETURN_IF_NOT_OK(TaskManager::CreateInstance());
101   TaskManager &tm = TaskManager::GetInstance();
102   RETURN_IF_NOT_OK(tm.ServiceStart());
103   return Status::OK();
104 }
105 
Services()106 Services::Services() : pool_(nullptr) {
107   Status rc = CircularPool::CreateCircularPool(&pool_, -1, 16, true);  // each arena 16M
108   if (rc.IsError()) {
109     std::terminate();
110   }
111 }
112 
~Services()113 Services::~Services() noexcept {
114   // Shutdown in reverse order.
115   auto n = hook_.size();
116   while (n > 0) {
117     hook_.pop_back();
118     n = hook_.size();
119   }
120 }
121 }  // namespace dataset
122 }  // namespace mindspore
123