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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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)
  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. #if !defined(_WIN32) && !defined(_WIN64)
  32. std::string Services::GetUserName() {
  33. char user[LOGIN_NAME_MAX];
  34. (void)getlogin_r(user, sizeof(user));
  35. return std::string(user);
  36. }
  37. std::string Services::GetHostName() {
  38. char host[LOGIN_NAME_MAX];
  39. (void)gethostname(host, sizeof(host));
  40. return std::string(host);
  41. }
  42. int Services::GetLWP() { return syscall(SYS_gettid); }
  43. #endif
  44. std::string Services::GetUniqueID() {
  45. const std::string kStr = "abcdefghijklmnopqrstuvwxyz0123456789";
  46. std::mt19937 gen = GetRandomDevice();
  47. std::uniform_int_distribution<uint32_t> dist(0, kStr.size() - 1);
  48. char buffer[UNIQUEID_LEN];
  49. for (int i = 0; i < UNIQUEID_LEN; i++) {
  50. buffer[i] = kStr[dist(gen)];
  51. }
  52. return std::string(buffer, UNIQUEID_LEN);
  53. }
  54. Status Services::CreateAllInstances() {
  55. // First one is always the TaskManager
  56. RETURN_IF_NOT_OK(TaskManager::CreateInstance());
  57. TaskManager &tm = TaskManager::GetInstance();
  58. RETURN_IF_NOT_OK(tm.ServiceStart());
  59. return Status::OK();
  60. }
  61. Services::Services() : pool_(nullptr) {
  62. Status rc = CircularPool::CreateCircularPool(&pool_, -1, 16, true); // each arena 16M
  63. if (rc.IsError()) {
  64. std::terminate();
  65. }
  66. }
  67. Services::~Services() noexcept {
  68. // Shutdown in reverse order.
  69. auto n = hook_.size();
  70. while (n > 0) {
  71. hook_.pop_back();
  72. n = hook_.size();
  73. }
  74. }
  75. } // namespace dataset
  76. } // namespace mindspore