1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <atomic>
18 #include "src/actor/actormgr.h"
19 #include "src/actor/iomgr.h"
20 #include "include/mindrt.hpp"
21 #include "include/mindrt.h"
22
23 extern "C" {
MindrtInitializeC(const struct MindrtConfig * config)24 int MindrtInitializeC(const struct MindrtConfig *config) {
25 if (config == nullptr) {
26 return -1;
27 }
28
29 if (config->threadCount == 0) {
30 return -1;
31 }
32
33 if (config->httpKmsgFlag != 0 && config->httpKmsgFlag != 1) {
34 return -1;
35 }
36 mindspore::SetHttpKmsgFlag(config->httpKmsgFlag);
37
38 return mindspore::Initialize(std::string(config->tcpUrl), std::string(config->tcpUrlAdv), std::string(config->udpUrl),
39 std::string(config->udpUrlAdv), config->threadCount);
40 }
41
MindrtFinalizeC()42 void MindrtFinalizeC() { mindspore::Finalize(); }
43 }
44
45 namespace mindspore {
46 namespace local {
47 static MindrtAddress g_mindrtAddress = MindrtAddress();
48 static std::atomic_bool g_finalizeMindrtStatus(false);
49 } // namespace local
50
GetMindrtAddress()51 const MindrtAddress &GetMindrtAddress() { return local::g_mindrtAddress; }
52
53 class MindrtExit {
54 public:
MindrtExit()55 MindrtExit() { MS_LOG(DEBUG) << "trace: enter MindrtExit()."; }
~MindrtExit()56 ~MindrtExit() {
57 MS_LOG(DEBUG) << "trace: enter ~MindrtExit().";
58 mindspore::Finalize();
59 }
60 };
61
InitializeImp(const std::string & tcpUrl,const std::string & tcpUrlAdv,const std::string & udpUrl,const std::string & udpUrlAdv,int threadCount)62 int InitializeImp(const std::string &tcpUrl, const std::string &tcpUrlAdv, const std::string &udpUrl,
63 const std::string &udpUrlAdv, int threadCount) {
64 MS_LOG(DEBUG) << "mindrt starts.";
65 auto ret = ActorMgr::GetActorMgrRef()->Initialize();
66 MS_LOG(DEBUG) << "mindrt has started.";
67 return ret;
68 }
69
Initialize(const std::string & tcpUrl,const std::string & tcpUrlAdv,const std::string & udpUrl,const std::string & udpUrlAdv,int threadCount)70 int Initialize(const std::string &tcpUrl, const std::string &tcpUrlAdv, const std::string &udpUrl,
71 const std::string &udpUrlAdv, int threadCount) {
72 /* support repeat initialize */
73 int result = InitializeImp(tcpUrl, tcpUrlAdv, udpUrl, udpUrlAdv, threadCount);
74 static MindrtExit mindrtExit;
75 return result;
76 }
77
Spawn(const ActorReference actor,bool sharedThread)78 AID Spawn(const ActorReference actor, bool sharedThread) {
79 if (actor == nullptr) {
80 MS_LOG(ERROR) << "Actor is nullptr.";
81 MINDRT_EXIT("Actor is nullptr.");
82 }
83 if (local::g_finalizeMindrtStatus.load() == true) {
84 return actor->GetAID();
85 } else {
86 auto actor_mgr = actor->get_actor_mgr();
87 if (actor_mgr != nullptr) {
88 return actor_mgr->Spawn(actor, sharedThread);
89 }
90 return ActorMgr::GetActorMgrRef()->Spawn(actor, sharedThread);
91 }
92 }
93
Await(const ActorReference & actor)94 void Await(const ActorReference &actor) { ActorMgr::GetActorMgrRef()->Wait(actor->GetAID()); }
95
Await(const AID & actor)96 void Await(const AID &actor) { ActorMgr::GetActorMgrRef()->Wait(actor); }
97
98 // brief get actor with aid
GetActor(const AID & actor)99 ActorReference GetActor(const AID &actor) { return ActorMgr::GetActorMgrRef()->GetActor(actor); }
100
Terminate(const AID & actor,const std::shared_ptr<ActorMgr> & actor_mgr)101 void Terminate(const AID &actor, const std::shared_ptr<ActorMgr> &actor_mgr) {
102 if (actor_mgr != nullptr) {
103 actor_mgr->Terminate(actor);
104 return;
105 }
106 ActorMgr::GetActorMgrRef()->Terminate(actor);
107 }
108
TerminateAll()109 void TerminateAll() { mindspore::ActorMgr::GetActorMgrRef()->TerminateAll(); }
110
Finalize()111 void Finalize() {
112 bool inite = false;
113 if (local::g_finalizeMindrtStatus.compare_exchange_strong(inite, true) == false) {
114 MS_LOG(DEBUG) << "mindrt has been Finalized.";
115 return;
116 }
117 MS_LOG(DEBUG) << "mindrt starts to finalize.";
118 mindspore::ActorMgr::GetActorMgrRef()->Finalize();
119
120 MS_LOG(DEBUG) << "mindrt has been finalized.";
121 // flush the log in cache to disk before exiting.
122 FlushHLogCache();
123 }
124
SetDelegate(const std::string & delegate)125 void SetDelegate(const std::string &delegate) { mindspore::ActorMgr::GetActorMgrRef()->SetDelegate(delegate); }
126
127 static int g_mindrtLogPid = 1;
SetLogPID(int pid)128 void SetLogPID(int pid) {
129 MS_LOG(DEBUG) << "Set Mindrt log PID:" << pid;
130 g_mindrtLogPid = pid;
131 }
GetLogPID()132 int GetLogPID() { return g_mindrtLogPid; }
133
134 static int g_httpKmsgEnable = -1;
SetHttpKmsgFlag(int flag)135 void SetHttpKmsgFlag(int flag) {
136 MS_LOG(DEBUG) << "Set Mindrt http message format:" << flag;
137 g_httpKmsgEnable = flag;
138 }
GetHttpKmsgFlag()139 int GetHttpKmsgFlag() { return g_httpKmsgEnable; }
140 } // namespace mindspore
141