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& para, 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& para, 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) : exitScope_(exitScope) {}
81 :
82 41 : ~ScopeGuard() { exitScope_(); }
83 :
84 : private:
85 : ScopeGuard(ScopeGuard const&) = delete;
86 : ScopeGuard& operator=(ScopeGuard const&) = delete;
87 : ScopeGuard(ScopeGuard&&) = delete;
88 : ScopeGuard& operator=(ScopeGuard&&) = delete;
89 :
90 : std::function<void()> exitScope_;
91 : };
92 :
93 : // spin lock
94 : class SpinLock {
95 : public:
96 174 : SpinLock() = default;
97 :
98 : ~SpinLock() = default;
99 :
100 59 : void Lock()
101 : {
102 118 : while (lock_.test_and_set()) {
103 : }
104 59 : }
105 :
106 59 : void Unlock() { lock_.clear(); }
107 :
108 : private:
109 : SpinLock(SpinLock const&) = delete;
110 : SpinLock& operator=(SpinLock const&) = delete;
111 : SpinLock(SpinLock&&) = delete;
112 : SpinLock& operator=(SpinLock&&) = delete;
113 :
114 : std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
115 : };
116 :
117 : class GlobalCfg {
118 : public:
119 1 : explicit GlobalCfg() = default;
120 1 : virtual ~GlobalCfg() = default;
121 740 : inline static GlobalCfg& GetInstance()
122 : {
123 753 : static GlobalCfg globalCfg;
124 740 : return globalCfg;
125 : }
126 :
127 37 : inline void SetNumaFlag(const bool numaFlag) { numaFlag_ = numaFlag; }
128 :
129 591 : inline bool GetNumaFlag() const { return numaFlag_; }
130 :
131 29 : inline void RecordDeviceId(const uint32_t deviceId, const uint32_t resIndex, const uint32_t groupId)
132 : {
133 29 : deviceIdToResIndex_[deviceId] = std::make_pair(resIndex, groupId);
134 29 : }
135 :
136 38 : inline uint32_t GetResIndexByDeviceId(const uint32_t deviceId) { return deviceIdToResIndex_[deviceId].first; }
137 :
138 30 : inline uint32_t GetGroupIdByDeviceId(const uint32_t deviceId) { return deviceIdToResIndex_[deviceId].second; }
139 :
140 : private:
141 : bool numaFlag_{false};
142 : std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> deviceIdToResIndex_;
143 : };
144 :
145 : } // namespace bqs
146 : #endif // QUEUE_SCHEDULE_BQS_STATUS_H
|