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_daemon_service.h"
12 : #include <algorithm>
13 : #include "sal.h"
14 : #include "log.h"
15 :
16 : namespace Hccl {
17 :
18 : constexpr u32 TEN_MILLISECOND_OF_USLEEP = 10000;
19 : std::mutex AicpuDaemonService::mutexForFuncs_;
20 :
21 14 : AicpuDaemonService& AicpuDaemonService::GetInstance()
22 : {
23 14 : static AicpuDaemonService daemonService;
24 14 : return daemonService;
25 : }
26 :
27 1 : void AicpuDaemonService::ServiceRun(void* info)
28 : {
29 3 : HCCL_RUN_INFO("Start background thread");
30 1 : auto commandToBackGroud = static_cast<CommandToBackGroud*>(info);
31 : while (true) {
32 2 : if (*commandToBackGroud == CommandToBackGroud::Stop) {
33 3 : HCCL_RUN_INFO("Background thread returned");
34 1 : break;
35 : }
36 :
37 1 : std::unique_lock<std::mutex> lock(mutexForFuncs_);
38 5 : for (auto& func : daemonFuncs) {
39 4 : func->Call();
40 4 : if (needBreak) {
41 0 : break;
42 : }
43 : }
44 1 : lock.unlock();
45 1 : if (needBreak) {
46 0 : HCCL_RUN_INFO("Background thread needBreak");
47 0 : break;
48 : }
49 :
50 1 : SaluSleep(TEN_MILLISECOND_OF_USLEEP);
51 2 : }
52 3 : HCCL_RUN_INFO("Exit background thread");
53 1 : }
54 :
55 2 : void AicpuDaemonService::ServiceStop(void* info) const
56 : {
57 2 : auto commandToBackGroud = static_cast<CommandToBackGroud*>(info);
58 2 : *commandToBackGroud = CommandToBackGroud::Stop;
59 6 : HCCL_INFO("Stop background thread");
60 2 : }
61 :
62 4 : void AicpuDaemonService::Register(DaemonFunc* daemonFunc)
63 : {
64 4 : if (daemonFunc == nullptr) {
65 0 : HCCL_ERROR("[AicpuDaemonService][Register] daemonFunc is nullptr");
66 0 : return;
67 : }
68 4 : std::unique_lock<std::mutex> lock(mutexForFuncs_);
69 4 : if (std::find(daemonFuncs.begin(), daemonFuncs.end(), daemonFunc) != daemonFuncs.end()) {
70 0 : HCCL_INFO("Background thread daemonFunc already registered, daemonFunc[%p]", daemonFunc);
71 0 : return;
72 : }
73 4 : daemonFuncs.push_back(daemonFunc);
74 12 : HCCL_INFO("Background thread register daemonFunc, daemonFunc[%p]", daemonFunc);
75 4 : }
76 :
77 0 : void AicpuDaemonService::Unregister(DaemonFunc* daemonFunc)
78 : {
79 0 : if (daemonFunc == nullptr) {
80 0 : HCCL_ERROR("[AicpuDaemonService][Unregister] daemonFunc is nullptr");
81 0 : return;
82 : }
83 0 : std::unique_lock<std::mutex> lock(mutexForFuncs_);
84 0 : auto it = std::remove(daemonFuncs.begin(), daemonFuncs.end(), daemonFunc);
85 0 : if (it != daemonFuncs.end()) {
86 0 : daemonFuncs.erase(it, daemonFuncs.end());
87 0 : HCCL_INFO("Background thread unregister daemonFunc, daemonFunc[%p]", daemonFunc);
88 : }
89 0 : }
90 :
91 1 : void AicpuDaemonService::Break()
92 : {
93 1 : needBreak = true;
94 3 : HCCL_INFO("Background thread received break");
95 1 : }
96 : } // namespace Hccl
|