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.9 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 "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 <random>
  25. #include "dataset/util/circular_pool.h"
  26. #include "dataset/util/task_manager.h"
  27. #define SLOT_TASK_MGR 0
  28. namespace mindspore {
  29. namespace dataset {
  30. std::unique_ptr<Services> Services::instance_ = nullptr;
  31. std::once_flag Services::init_instance_flag_;
  32. #if !defined(_WIN32) && !defined(_WIN64)
  33. std::string Services::GetUserName() {
  34. char user[LOGIN_NAME_MAX];
  35. (void)getlogin_r(user, sizeof(user));
  36. return std::string(user);
  37. }
  38. std::string Services::GetHostName() {
  39. char host[LOGIN_NAME_MAX];
  40. (void)gethostname(host, sizeof(host));
  41. return std::string(host);
  42. }
  43. int Services::GetLWP() { return syscall(SYS_gettid); }
  44. #endif
  45. std::string Services::GetUniqueID() {
  46. const std::string kStr = "abcdefghijklmnopqrstuvwxyz0123456789";
  47. #if defined(_WIN32) || defined(_WIN64)
  48. unsigned int number;
  49. rand_s(&number);
  50. std::mt19937 gen{static_cast<uint32_t>(number)};
  51. #else
  52. std::mt19937 gen{std::random_device{"/dev/urandom"}()};
  53. #endif
  54. std::uniform_int_distribution<> dist(0, kStr.size() - 1);
  55. char buffer[UNIQUEID_LEN];
  56. for (int i = 0; i < UNIQUEID_LEN; i++) {
  57. buffer[i] = kStr[dist(gen)];
  58. }
  59. return std::string(buffer, UNIQUEID_LEN);
  60. }
  61. TaskManager &Services::getTaskMgrInstance() {
  62. Services &sm = GetInstance();
  63. return *(static_cast<TaskManager *>(sm.sa_[SLOT_TASK_MGR]));
  64. }
  65. Status Services::CreateAllInstances() {
  66. // In order, TaskMgr, BufferMgr
  67. Status rc;
  68. sa_[SLOT_TASK_MGR] = new (&rc, pool_) TaskManager();
  69. RETURN_IF_NOT_OK(rc);
  70. rc = sa_[SLOT_TASK_MGR]->ServiceStart();
  71. return rc;
  72. }
  73. Services::Services() : pool_(nullptr), sa_{nullptr} {
  74. Status rc = CircularPool::CreateCircularPool(&pool_, -1, 16, true); // each arena 16M
  75. if (rc.IsError()) {
  76. std::terminate();
  77. }
  78. }
  79. Services::~Services() noexcept {
  80. try {
  81. // In reverse order
  82. TaskManager *tm = static_cast<TaskManager *>(sa_[SLOT_TASK_MGR]);
  83. if (tm) {
  84. (void)tm->ServiceStop();
  85. tm->~TaskManager();
  86. pool_->Deallocate(tm);
  87. }
  88. } catch (const std::exception &e) {
  89. // Do nothing.
  90. }
  91. }
  92. } // namespace dataset
  93. } // namespace mindspore