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