|
@@ -3,6 +3,7 @@
|
|
|
#include "../DataManager/DataManager.h"
|
|
|
#include "../DataManager/DataPackage.h"
|
|
|
#include "../LogRecorder/LogOutput.h"
|
|
|
+#include "../ThreadGuardian/ThreadGuardian.h"
|
|
|
// Image test
|
|
|
#include "../ImageTest/ImageTest.h"
|
|
|
#include <stdexcept>
|
|
@@ -11,9 +12,21 @@ void RKNNManager::addRknnTask(std::string modelData)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- m_threads.emplace_back(&RKNNManager::taskThread, this, modelData);
|
|
|
- m_threads.emplace_back(&RKNNManager::taskThread, this, modelData);
|
|
|
- m_threads.emplace_back(&RKNNManager::taskThread, this, modelData);
|
|
|
+ m_vThreadSwitch.resize(3);
|
|
|
+ m_threads.clear();
|
|
|
+ m_bThreadSwitch = true;
|
|
|
+
|
|
|
+ m_threads.emplace_back(&RKNNManager::taskThread, this, 0, modelData);
|
|
|
+ int index = m_threads.size() - 1;
|
|
|
+ ThreadGuardian::getInstance().registerAIThread(index, std::bind(&RKNNManager::restartRknnThread, this, 0));
|
|
|
+
|
|
|
+ m_threads.emplace_back(&RKNNManager::taskThread, this, 1, modelData);
|
|
|
+ index = m_threads.size() - 1;
|
|
|
+ ThreadGuardian::getInstance().registerAIThread(index, std::bind(&RKNNManager::restartRknnThread, this, 1));
|
|
|
+
|
|
|
+ m_threads.emplace_back(&RKNNManager::taskThread, this, 2, modelData);
|
|
|
+ index = m_threads.size() - 1;
|
|
|
+ ThreadGuardian::getInstance().registerAIThread(index, std::bind(&RKNNManager::restartRknnThread, this, 2));
|
|
|
}
|
|
|
catch (const std::exception &e)
|
|
|
{
|
|
@@ -25,7 +38,41 @@ void RKNNManager::addRknnTask(std::string modelData)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void RKNNManager::taskThread(std::string modelpath)
|
|
|
+void RKNNManager::restartRknnThread(int threadIndex)
|
|
|
+{
|
|
|
+ // 先停止当前线程
|
|
|
+ // if (m_threads[threadIndex].joinable())
|
|
|
+ // {
|
|
|
+ // m_vThreadSwitch[threadIndex] = false;
|
|
|
+ // m_threads[threadIndex].join();
|
|
|
+ // }
|
|
|
+
|
|
|
+ if (threadIndex >= 0 && threadIndex < m_threads.size())
|
|
|
+ {
|
|
|
+ // // 1. 检查旧线程是否可合并(仍在运行)
|
|
|
+ // if (m_threads[threadIndex].joinable())
|
|
|
+ // {
|
|
|
+ // // 2. 选择等待旧线程结束或分离它(二选一)
|
|
|
+ // m_threads[threadIndex].join(); // 等待旧线程完成
|
|
|
+ // // 或者
|
|
|
+ // // m_threads[threadIndex].detach(); // 让旧线程独立运行(有风险!)
|
|
|
+ // }
|
|
|
+
|
|
|
+ // 重新启动线程
|
|
|
+ if (threadIndex >= 0 && threadIndex < m_threads.size())
|
|
|
+ {
|
|
|
+ m_threads[threadIndex] = std::thread(&RKNNManager::taskThread, this, threadIndex, m_modelData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void RKNNManager::updateThreadStatus(int threadIndex)
|
|
|
+{
|
|
|
+ std::string threadName = "rknnThread_" + std::to_string(threadIndex);
|
|
|
+ ThreadGuardian::getInstance().updateThreadHeartbeat(threadName);
|
|
|
+}
|
|
|
+
|
|
|
+void RKNNManager::taskThread(int index, std::string modelpath)
|
|
|
{
|
|
|
grpc::ClientContext *context;
|
|
|
R360::Empty response;
|
|
@@ -35,6 +82,10 @@ void RKNNManager::taskThread(std::string modelpath)
|
|
|
channel_args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, 100 * 1024 * 1024); // 设置最大发送消息大小为100MB
|
|
|
channel_args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, 100 * 1024 * 1024); // 设置最大接收消息大小为100MB
|
|
|
channel_args.SetInt(GRPC_ARG_HTTP2_WRITE_BUFFER_SIZE, 4 * 1024 * 1024);
|
|
|
+ channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, 10000); // 10秒
|
|
|
+ channel_args.SetInt(GRPC_ARG_MIN_RECONNECT_BACKOFF_MS, 1000); // 1秒
|
|
|
+ channel_args.SetInt(GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS, 1000); // 1
|
|
|
+ channel_args.SetInt(GRPC_ARG_ENABLE_RETRIES, 1); // 启用重试机制
|
|
|
std::unique_ptr<MessageService::Stub> stub_ = MessageService::NewStub(grpc::CreateCustomChannel(target_str, grpc::InsecureChannelCredentials(), channel_args));
|
|
|
std::unique_ptr<grpc::ClientWriter<DataList>> writer;
|
|
|
|
|
@@ -50,8 +101,6 @@ void RKNNManager::taskThread(std::string modelpath)
|
|
|
LOG_INFO("The model initialize is failed!!!\n");
|
|
|
return;
|
|
|
}
|
|
|
- m_threadSwitch = true;
|
|
|
- int index = infer.get_index();
|
|
|
DataPackagePtr dataPackage;
|
|
|
std::string pipeName = "rknn" + std::to_string(index);
|
|
|
|
|
@@ -68,10 +117,11 @@ void RKNNManager::taskThread(std::string modelpath)
|
|
|
// time test
|
|
|
UsbTest::HighResolutionTimer timer;
|
|
|
|
|
|
- while (m_threadSwitch)
|
|
|
+ while (m_bThreadSwitch)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ updateThreadStatus(index);
|
|
|
if (DataManager::getInstance().popData("resized" + std::to_string(index), dataPackage))
|
|
|
{
|
|
|
try
|