newtx/scanner/res_mgr.h

68 lines
2.2 KiB
C
Raw Normal View History

2024-02-25 05:51:52 +00:00
// resource manager
//
// Date: 2024-02-25
//
// Resources in monitoring: memory
#pragma once
#include <base/utils.h>
#include <base/packet.h>
class resource_mgr
{
volatile bool run_ = true;
volatile bool monitor_ = false;
uint64_t mem_limit_ = SIZE_GB(2);
uint64_t mem_now_ = 0;
std::unique_ptr<std::thread> thread_;
void thread_monitor(void);
public:
resource_mgr();
~resource_mgr();
static std::string task_type(_task task);
public:
void start(void);
void stop(void);
void set_memory_limit(uint64_t max_size = SIZE_GB(2));
bool is_resource_enable(_task task, bool wait = false, int to_ms = 0);
};
// watch.reset();
// bool first = true;
// while(scanning_)
// {
// if(res_(TASK_CAPTURER, true, 3))
// {
// if(!first)
// {
// uint64_t now = 0;
// utils::get_memory_usage(nullptr, &now, nullptr);
// utils::to_log(LOG_LEVEL_DEBUG, "Resources OK: %lld\n", now);
// }
// break;
// }
// if(first)
// {
// uint64_t now = 0;
// first = false;
// utils::get_memory_usage(nullptr, &now, nullptr);
// utils::to_log(LOG_LEVEL_WARNING, "Resources have reached their maximum limit: %lld\n", now);
// }
// if(watch.elapse_s() >= 3)
// {
// uint64_t now = 0;
// utils::get_memory_usage(nullptr, &now, nullptr);
// utils::to_log(LOG_LEVEL_FATAL, "Resources is not enough(memory = %lld) for continue scanning, exit scanning now.\n", now);
// auto_scan_ = scanning_ = false;
// err = SCANNER_ERR_INSUFFICIENT_MEMORY;
// break;
// }
// std::this_thread::sleep_for(std::chrono::milliseconds(3));
// }