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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "dataset/util/circular_pool.h"
  25. #include "dataset/util/random.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. std::mt19937 gen = GetRandomDevice();
  48. std::uniform_int_distribution<uint32_t> dist(0, kStr.size() - 1);
  49. char buffer[UNIQUEID_LEN];
  50. for (int i = 0; i < UNIQUEID_LEN; i++) {
  51. buffer[i] = kStr[dist(gen)];
  52. }
  53. return std::string(buffer, UNIQUEID_LEN);
  54. }
  55. TaskManager &Services::getTaskMgrInstance() {
  56. Services &sm = GetInstance();
  57. return *(static_cast<TaskManager *>(sm.sa_[SLOT_TASK_MGR]));
  58. }
  59. Status Services::CreateAllInstances() {
  60. // In order, TaskMgr, BufferMgr
  61. Status rc;
  62. sa_[SLOT_TASK_MGR] = new (&rc, pool_) TaskManager();
  63. RETURN_IF_NOT_OK(rc);
  64. rc = sa_[SLOT_TASK_MGR]->ServiceStart();
  65. return rc;
  66. }
  67. Services::Services() : pool_(nullptr), sa_{nullptr} {
  68. Status rc = CircularPool::CreateCircularPool(&pool_, -1, 16, true); // each arena 16M
  69. if (rc.IsError()) {
  70. std::terminate();
  71. }
  72. }
  73. Services::~Services() noexcept {
  74. try {
  75. // In reverse order
  76. TaskManager *tm = static_cast<TaskManager *>(sa_[SLOT_TASK_MGR]);
  77. if (tm) {
  78. (void)tm->ServiceStop();
  79. tm->~TaskManager();
  80. pool_->Deallocate(tm);
  81. }
  82. } catch (const std::exception &e) {
  83. // Do nothing.
  84. }
  85. }
  86. } // namespace dataset
  87. } // namespace mindspore