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 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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)
  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. namespace mindspore {
  29. namespace dataset {
  30. std::unique_ptr<Services> Services::instance_ = nullptr;
  31. std::once_flag Services::init_instance_flag_;
  32. std::set<std::string> Services::unique_id_list_ = {};
  33. std::mutex Services::unique_id_mutex_;
  34. #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID)
  35. std::string Services::GetUserName() {
  36. char user[LOGIN_NAME_MAX];
  37. (void)getlogin_r(user, sizeof(user));
  38. return std::string(user);
  39. }
  40. std::string Services::GetHostName() {
  41. char host[LOGIN_NAME_MAX];
  42. (void)gethostname(host, sizeof(host));
  43. return std::string(host);
  44. }
  45. int Services::GetLWP() { return syscall(SYS_gettid); }
  46. #endif
  47. std::string Services::GetUniqueID() {
  48. const std::string kStr = "abcdefghijklmnopqrstuvwxyz0123456789";
  49. std::mt19937 gen = GetRandomDevice();
  50. std::uniform_int_distribution<uint32_t> dist(0, kStr.size() - 1);
  51. char buffer[UNIQUEID_LEN];
  52. {
  53. std::unique_lock<std::mutex> lock(unique_id_mutex_);
  54. while (true) {
  55. auto ret = memset_s(buffer, UNIQUEID_LEN, 0, UNIQUEID_LEN);
  56. if (ret != 0) {
  57. MS_LOG(ERROR) << "memset_s error, errorno(" << ret << ")";
  58. return std::string("");
  59. }
  60. for (int i = 0; i < UNIQUEID_LEN; i++) {
  61. buffer[i] = kStr[dist(gen)];
  62. }
  63. if (unique_id_list_.find(std::string(buffer, UNIQUEID_LEN)) != unique_id_list_.end()) {
  64. continue;
  65. }
  66. unique_id_list_.insert(std::string(buffer, UNIQUEID_LEN));
  67. break;
  68. }
  69. }
  70. return std::string(buffer, UNIQUEID_LEN);
  71. }
  72. Status Services::CreateAllInstances() {
  73. // First one is always the TaskManager
  74. RETURN_IF_NOT_OK(TaskManager::CreateInstance());
  75. TaskManager &tm = TaskManager::GetInstance();
  76. RETURN_IF_NOT_OK(tm.ServiceStart());
  77. return Status::OK();
  78. }
  79. Services::Services() : pool_(nullptr) {
  80. Status rc = CircularPool::CreateCircularPool(&pool_, -1, 16, true); // each arena 16M
  81. if (rc.IsError()) {
  82. std::terminate();
  83. }
  84. }
  85. Services::~Services() noexcept {
  86. // Shutdown in reverse order.
  87. auto n = hook_.size();
  88. while (n > 0) {
  89. hook_.pop_back();
  90. n = hook_.size();
  91. }
  92. }
  93. } // namespace dataset
  94. } // namespace mindspore