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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_ 18 19 #include <algorithm> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 #include "minddata/dataset/util/memory_pool.h" 26 #include "minddata/dataset/util/allocator.h" 27 #include "minddata/dataset/util/service.h" 28 29 #define UNIQUEID_LEN 36 30 #define UNIQUEID_LIST_LIMITS 1024 31 #define UNIQUEID_HALF_INDEX ((UNIQUEID_LIST_LIMITS) / 2) 32 namespace mindspore { 33 namespace dataset { 34 class TaskManager; 35 36 class Services { 37 public: CreateInstance()38 static Status CreateInstance() { 39 std::call_once(init_instance_flag_, [&]() -> Status { 40 instance_.reset(new Services()); 41 return (instance_->CreateAllInstances()); 42 }); 43 44 if (instance_ == nullptr) { 45 instance_.reset(new Services()); 46 return (instance_->CreateAllInstances()); 47 } 48 49 return Status::OK(); 50 } 51 GetInstance()52 static Services &GetInstance() { 53 if (instance_ == nullptr) { 54 if (!CreateInstance()) { 55 std::terminate(); 56 } 57 } 58 return *instance_; 59 } 60 61 Services(const Services &) = delete; 62 63 Services &operator=(const Services &) = delete; 64 65 ~Services() noexcept; 66 GetServiceMemPool()67 std::shared_ptr<MemoryPool> GetServiceMemPool() { return pool_; } 68 69 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__) 70 static std::string GetUserName(); 71 72 static std::string GetHostName(); 73 74 static int GetLWP(); 75 #endif 76 77 static std::string GetUniqueID(); 78 79 template <typename T> GetAllocator()80 static Allocator<T> GetAllocator() { 81 return Allocator<T>(Services::GetInstance().GetServiceMemPool()); 82 } 83 84 /// \brief Add a new service to the start up list. 85 /// \tparam T Class that implements Service 86 /// \return Status object and where the service is located in the hook_ list 87 template <typename T, typename... Args> AddHook(T ** out,Args &&...args)88 Status AddHook(T **out, Args &&... args) { 89 RETURN_UNEXPECTED_IF_NULL(out); 90 try { 91 (*out) = new T(std::forward<Args>(args)...); 92 std::unique_ptr<T> svc(*out); 93 hook_.push_back(std::move(svc)); 94 } catch (const std::bad_alloc &e) { 95 return Status(StatusCode::kMDOutOfMemory); 96 } 97 return Status::OK(); 98 } 99 100 private: 101 static std::once_flag init_instance_flag_; 102 static std::unique_ptr<Services> instance_; 103 static std::map<std::string, uint64_t> unique_id_list_; 104 static uint64_t unique_id_count_; 105 static std::mutex unique_id_mutex_; 106 // A small pool used for small objects that last until the 107 // Services Manager shuts down. Used by all sub-services. 108 std::shared_ptr<MemoryPool> pool_; 109 std::vector<std::unique_ptr<Service>> hook_; 110 111 Services(); 112 113 Status CreateAllInstances(); 114 }; 115 } // namespace dataset 116 } // namespace mindspore 117 118 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SERVICES_H_ 119