Browse Source

!16049 [MS][LITE]fix memory leak caused by unterminated threads

From: @probiotics_53
Reviewed-by: @zhang_xue_tong,@zhanghaibo5
Signed-off-by: @zhang_xue_tong
pull/16049/MERGE
mindspore-ci-bot Gitee 4 years ago
parent
commit
b077b53c09
7 changed files with 20 additions and 1 deletions
  1. +3
    -0
      mindspore/core/mindrt/include/mindrt.hpp
  2. +2
    -0
      mindspore/core/mindrt/src/actor/actormgr.cc
  3. +1
    -0
      mindspore/core/mindrt/src/actor/actormgr.h
  4. +9
    -1
      mindspore/core/mindrt/src/actor/actorthread.cc
  5. +2
    -0
      mindspore/core/mindrt/src/actor/actorthread.h
  6. +2
    -0
      mindspore/core/mindrt/src/mindrt.cc
  7. +1
    -0
      mindspore/lite/src/lite_mindrt.cc

+ 3
- 0
mindspore/core/mindrt/include/mindrt.hpp View File

@@ -33,6 +33,9 @@ struct MindrtAddress {
int Initialize(const std::string &tcpUrl, const std::string &tcpUrlAdv = "", const std::string &udpUrl = "",
const std::string &udpUrlAdv = "", int threadCount = 0);

// brief terminate the threads for current session
void TerminateCurThreads(int threadCount = 0);

// brief spawn a process to run an actor
AID Spawn(ActorReference actor, bool sharedThread = true, bool start = true);



+ 2
- 0
mindspore/core/mindrt/src/actor/actormgr.cc View File

@@ -100,6 +100,8 @@ void ActorMgr::TerminateAll() {

void ActorMgr::Initialize(int threadCount) { threadPool.AddThread(threadCount); }

void ActorMgr::TerminateCurThreads(int threadCount) { threadPool.TerminateThread(threadCount); }

void ActorMgr::Finalize() {
this->TerminateAll();
MS_LOG(INFO) << "mindrt Actors finish exiting.";


+ 1
- 0
mindspore/core/mindrt/src/actor/actormgr.h View File

@@ -48,6 +48,7 @@ class ActorMgr {

void Finalize();
void Initialize(int threadCount);
void TerminateCurThreads(int threadCount);
void RemoveActor(const std::string &name);
ActorReference GetActor(const AID &id);
const std::string GetUrl(const std::string &protocol = "tcp");


+ 9
- 1
mindspore/core/mindrt/src/actor/actorthread.cc View File

@@ -59,7 +59,8 @@ ActorThread::ActorThread() : readyActors(), workers() {
ActorThread::~ActorThread() {}
void ActorThread::AddThread(int threadCount) {
std::unique_lock<std::mutex> lock(initLock_);
for (int i = 0; i < threadCount; ++i) {
int threadsNeed = threadCount - (workers.size() - threadsInUse_);
for (int i = 0; i < threadsNeed; ++i) {
if (workers.size() >= maxThreads_) {
MS_LOG(DEBUG) << "threads number in mindrt reach upper limit. maxThreads:" << maxThreads_;
break;
@@ -68,8 +69,15 @@ void ActorThread::AddThread(int threadCount) {
MINDRT_OOM_EXIT(worker)

workers.push_back(std::move(worker));
threadsInUse_ += 1;
}
}

void ActorThread::TerminateThread(int threadCount) {
// temp scheme, not actually terminate the threads when current session destructs
threadsInUse_ -= threadCount;
}

void ActorThread::Finalize() {
MS_LOG(INFO) << "Actor's threads are exiting.";
// terminate all thread; enqueue nullptr actor to terminate;


+ 2
- 0
mindspore/core/mindrt/src/actor/actorthread.h View File

@@ -33,6 +33,7 @@ class ActorThread {
~ActorThread();
void Finalize();
void AddThread(int threadCount);
void TerminateThread(int threadCount);
void EnqueReadyActor(const std::shared_ptr<ActorBase> &actor);

private:
@@ -46,6 +47,7 @@ class ActorThread {
std::list<std::unique_ptr<std::thread>> workers;
std::string threadName;

size_t threadsInUse_ = 0;
size_t maxThreads_;
std::mutex initLock_;
};


+ 2
- 0
mindspore/core/mindrt/src/mindrt.cc View File

@@ -59,6 +59,8 @@ const MindrtAddress &GetMindrtAddress() {

void SetThreadCount(int threadCount) { ActorMgr::GetActorMgrRef()->Initialize(threadCount); }

void TerminateCurThreads(int threadCount) { ActorMgr::GetActorMgrRef()->TerminateCurThreads(threadCount); }

class MindrtExit {
public:
MindrtExit() { MS_LOG(DEBUG) << "trace: enter MindrtExit()---------"; }


+ 1
- 0
mindspore/lite/src/lite_mindrt.cc View File

@@ -71,6 +71,7 @@ void MindrtTerminate(std::vector<std::shared_ptr<LiteOpActor>> actor_list) {
for (auto actor : actor_list) {
mindspore::Terminate(actor->GetAID());
}
mindspore::TerminateCurThreads(1);
return;
}



Loading…
Cancel
Save