You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

services.cc 4.0 kB

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