90 lines
2.6 KiB
C++
90 lines
2.6 KiB
C++
#include "res_mgr.h"
|
|
|
|
resource_mgr::resource_mgr()
|
|
{
|
|
#ifdef USE_MONITOR_THREAD
|
|
thread_.reset(new std::thread(&resource_mgr::thread_monitor, this));
|
|
#endif
|
|
}
|
|
resource_mgr::~resource_mgr()
|
|
{
|
|
run_ = monitor_ = false;
|
|
if(thread_.get() && thread_->joinable())
|
|
thread_->join();
|
|
}
|
|
|
|
void resource_mgr::thread_monitor(void)
|
|
{
|
|
while(run_)
|
|
{
|
|
if(monitor_)
|
|
{
|
|
do
|
|
{
|
|
uint64_t now = 0;
|
|
if(utils::get_memory_usage(nullptr, &now, nullptr) == 0)
|
|
mem_now_ = now;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
} while (monitor_);
|
|
|
|
}
|
|
else
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string resource_mgr::task_type(_task task)
|
|
{
|
|
RETURN_ENUM_STR(task, TASK_NONE);
|
|
RETURN_ENUM_STR(task, TASK_EP0);
|
|
RETURN_ENUM_STR(task, TASK_BULK_IN);
|
|
RETURN_ENUM_STR(task, TASK_BULK_OUT);
|
|
RETURN_ENUM_STR(task, TASK_CAPTURER);
|
|
RETURN_ENUM_STR(task, TASK_IMG_PROCESSOR);
|
|
|
|
return std::move("Unk-task(" + std::to_string((int)task) + ")");
|
|
}
|
|
|
|
void resource_mgr::start(void)
|
|
{
|
|
printf("\tprocess %llu start resource monitor with mem limit = %llu\n", GetCurrentProcessId(), mem_limit_);
|
|
monitor_ = true;
|
|
}
|
|
void resource_mgr::stop(void)
|
|
{
|
|
monitor_ = false;
|
|
}
|
|
void resource_mgr::set_memory_limit(uint64_t max_size)
|
|
{
|
|
mem_limit_ = max_size;
|
|
}
|
|
bool resource_mgr::is_resource_enable(_task task, bool wait, int to_ms)
|
|
{
|
|
#ifndef USE_MONITOR_THREAD
|
|
if(task == TASK_CAPTURER)
|
|
utils::get_memory_usage(nullptr, nullptr, (uint64_t*)&mem_now_);
|
|
#endif
|
|
|
|
if(wait && mem_now_ > mem_limit_)
|
|
{
|
|
chronograph watch;
|
|
utils::to_log(LOG_LEVEL_WARNING, "Resources(task '%s') have reached their maximum limit(%lld): %lld, wait now ...\n", resource_mgr::task_type(task).c_str(), mem_limit_, mem_now_);
|
|
printf("Resources(task '%s') have reached their maximum limit(%lld): %lld, wait now ...\n", resource_mgr::task_type(task).c_str(), mem_limit_, mem_now_);
|
|
while(monitor_ && watch.elapse_ms() < to_ms)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
#ifndef USE_MONITOR_THREAD
|
|
if(task == TASK_CAPTURER)
|
|
utils::get_memory_usage(nullptr, nullptr, (uint64_t*)&mem_now_);
|
|
#endif
|
|
if(mem_now_ < mem_limit_)
|
|
break;
|
|
}
|
|
utils::to_log(LOG_LEVEL_WARNING, "Resources(task '%s') wait result: %lld\n", resource_mgr::task_type(task).c_str(), mem_now_);
|
|
}
|
|
|
|
return mem_now_ < mem_limit_;
|
|
}
|