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 : #include "aicpu_pulse.h"
12 :
13 : #include <mutex>
14 : #include <string>
15 : #include <unordered_map>
16 :
17 : #include "aicpu_sharder_log.h"
18 :
19 : namespace {
20 : static std::unordered_map<std::string, PulseNotifyFunc> pulseNotifyFuncMap;
21 : static std::mutex g_mtx;
22 : } // namespace
23 :
24 6 : __attribute__((visibility("default"))) void AicpuPulseNotify()
25 : {
26 6 : const std::unique_lock<std::mutex> lck(g_mtx);
27 8 : for (auto& notifyFunc : pulseNotifyFuncMap) {
28 2 : AICPUE_LOGD("Aicpu pulse notify %s start.", notifyFunc.first.c_str());
29 2 : notifyFunc.second();
30 2 : AICPUE_LOGD("Aicpu pulse notify %s end.", notifyFunc.first.c_str());
31 : }
32 6 : }
33 :
34 5 : __attribute__((visibility("default"))) int32_t RegisterPulseNotifyFunc(
35 : const char_t* const name, const PulseNotifyFunc func)
36 : {
37 5 : if (name == nullptr) {
38 1 : AICPUE_LOGE("Register pulse notify func failed as param name is null");
39 1 : return -1;
40 : }
41 :
42 4 : if (func == nullptr) {
43 1 : AICPUE_LOGE("Register pulse notify func for %s failed as param func is null", name);
44 1 : return -1;
45 : }
46 :
47 3 : const std::unique_lock<std::mutex> lck(g_mtx);
48 3 : const auto ret = pulseNotifyFuncMap.emplace(name, func);
49 3 : if (!ret.second) {
50 1 : AICPUE_LOGE("Register pulse notify func for %s failed.", name);
51 1 : return -1;
52 : }
53 2 : AICPUE_LOGI("Register pulse notify func for %s success.", name);
54 2 : return 0;
55 3 : }
56 :
57 36 : __attribute__((visibility("default"))) void ClearPulseNotifyFunc()
58 : {
59 36 : AICPUE_LOGI("Clear pulse notify func.");
60 36 : const std::unique_lock<std::mutex> lck(g_mtx);
61 36 : pulseNotifyFuncMap.clear();
62 36 : }
|