2024-02-25 05:51:52 +00:00
# include "res_mgr.h"
resource_mgr : : resource_mgr ( )
{
2024-03-08 02:22:47 +00:00
# ifdef USE_MONITOR_THREAD
2024-02-25 05:51:52 +00:00
thread_ . reset ( new std : : thread ( & resource_mgr : : thread_monitor , this ) ) ;
2024-03-08 02:22:47 +00:00
# endif
2024-02-25 05:51:52 +00:00
}
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 )
{
2024-03-08 07:04:10 +00:00
printf ( " \t process %llu start resource monitor with mem limit = %llu \n " , GetCurrentProcessId ( ) , mem_limit_ ) ;
2024-02-25 05:51:52 +00:00
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 )
{
2024-03-08 02:22:47 +00:00
# ifndef USE_MONITOR_THREAD
if ( task = = TASK_CAPTURER )
2024-03-08 07:04:10 +00:00
utils : : get_memory_usage ( nullptr , nullptr , ( uint64_t * ) & mem_now_ ) ;
2024-03-08 02:22:47 +00:00
# endif
2024-02-25 05:51:52 +00:00
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 ) ) ;
2024-03-08 07:04:10 +00:00
# ifndef USE_MONITOR_THREAD
if ( task = = TASK_CAPTURER )
utils : : get_memory_usage ( nullptr , nullptr , ( uint64_t * ) & mem_now_ ) ;
# endif
2024-02-25 05:51:52 +00:00
if ( mem_now_ < mem_limit_ )
break ;
}
2024-03-08 07:04:10 +00:00
utils : : to_log ( LOG_LEVEL_WARNING , " Resources(task '%s') wait result: %lld \n " , resource_mgr : : task_type ( task ) . c_str ( ) , mem_now_ ) ;
2024-02-25 05:51:52 +00:00
}
return mem_now_ < mem_limit_ ;
}