Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef QUEUE_SCHEDULE_BQS_UTIL_H
12 : #define QUEUE_SCHEDULE_BQS_UTIL_H
13 :
14 : #include <cstdint>
15 : #include <atomic>
16 : #include <functional>
17 : #include <unordered_map>
18 : #include <string>
19 : #include <cstring>
20 : namespace bqs {
21 : constexpr uint32_t LOCAL_Q = 0;
22 : constexpr uint32_t CLIENT_Q = 1;
23 : constexpr int32_t MAX_ENV_CHAR_NUM = 1024;
24 :
25 : enum class RunContext {
26 : DEVICE = 0,
27 : HOST,
28 : };
29 :
30 : RunContext GetRunContext();
31 :
32 : /**
33 : * it is used to get current time
34 : * @return uint64_t tick
35 : */
36 : uint64_t GetNowTime();
37 :
38 : /**
39 : * @ingroup
40 : * @brief read env value
41 : * @param [in] envName : env to get
42 : * @param [out] envValue : result
43 : * @return : bool,Whether get the env success
44 : */
45 20 : static inline void GetEnvVal(const std::string &env, std::string &val)
46 : {
47 20 : const char *const tmpVal = std::getenv(env.c_str());
48 20 : if ((tmpVal == nullptr) || (strnlen(tmpVal, MAX_ENV_CHAR_NUM) >= MAX_ENV_CHAR_NUM)) {
49 1 : val = "";
50 : } else {
51 19 : val = tmpVal;
52 : }
53 20 : }
54 :
55 240 : static inline bool TransStrToInt(const std::string ¶, int32_t &value)
56 : {
57 : try {
58 240 : value = std::stoi(para);
59 35 : } catch (...) {
60 35 : return false;
61 35 : }
62 :
63 205 : return true;
64 : }
65 :
66 7 : static inline bool TransStrToull(const std::string ¶, uint64_t &value)
67 : {
68 : try {
69 7 : value = std::stoull(para);
70 3 : } catch (...) {
71 3 : return false;
72 3 : }
73 :
74 4 : return true;
75 : }
76 :
77 : // scope guard
78 : class ScopeGuard {
79 : public:
80 41 : explicit ScopeGuard(const std::function<void()> exitScope)
81 41 : : exitScope_(exitScope)
82 41 : {}
83 :
84 41 : ~ScopeGuard()
85 : {
86 41 : exitScope_();
87 41 : }
88 :
89 : private:
90 : ScopeGuard(ScopeGuard const&) = delete;
91 : ScopeGuard& operator=(ScopeGuard const&) = delete;
92 : ScopeGuard(ScopeGuard&&) = delete;
93 : ScopeGuard& operator=(ScopeGuard&&) = delete;
94 :
95 : std::function<void()> exitScope_;
96 : };
97 :
98 : // spin lock
99 : class SpinLock {
100 : public:
101 174 : SpinLock() = default;
102 :
103 : ~SpinLock() = default;
104 :
105 59 : void Lock()
106 : {
107 118 : while (lock_.test_and_set()) {}
108 59 : }
109 :
110 59 : void Unlock()
111 : {
112 59 : lock_.clear();
113 59 : }
114 :
115 : private:
116 : SpinLock(SpinLock const&) = delete;
117 : SpinLock& operator=(SpinLock const&) = delete;
118 : SpinLock(SpinLock&&) = delete;
119 : SpinLock& operator=(SpinLock&&) = delete;
120 :
121 : std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
122 : };
123 :
124 : class GlobalCfg {
125 : public:
126 1 : explicit GlobalCfg() = default;
127 1 : virtual ~GlobalCfg() = default;
128 740 : inline static GlobalCfg &GetInstance()
129 : {
130 753 : static GlobalCfg globalCfg;
131 740 : return globalCfg;
132 : }
133 :
134 37 : inline void SetNumaFlag(const bool numaFlag)
135 : {
136 37 : numaFlag_ = numaFlag;
137 37 : }
138 :
139 591 : inline bool GetNumaFlag() const
140 : {
141 591 : return numaFlag_;
142 : }
143 :
144 29 : inline void RecordDeviceId(const uint32_t deviceId, const uint32_t resIndex, const uint32_t groupId)
145 : {
146 29 : deviceIdToResIndex_[deviceId] = std::make_pair(resIndex, groupId);
147 29 : }
148 :
149 38 : inline uint32_t GetResIndexByDeviceId(const uint32_t deviceId)
150 : {
151 38 : return deviceIdToResIndex_[deviceId].first;
152 : }
153 :
154 30 : inline uint32_t GetGroupIdByDeviceId(const uint32_t deviceId)
155 : {
156 30 : return deviceIdToResIndex_[deviceId].second;
157 : }
158 :
159 : private:
160 : bool numaFlag_{false};
161 : std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> deviceIdToResIndex_;
162 : };
163 :
164 : } // namespace bqs
165 : #endif // QUEUE_SCHEDULE_BQS_STATUS_H
|