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.1 kB

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