few tweaks for library performance improvement

This commit is contained in:
Mohamed Boussaffa
2016-03-05 04:53:03 +08:00
parent 245e152854
commit 59e2acb636
2 changed files with 28 additions and 11 deletions

View File

@@ -4,6 +4,7 @@
#include <pthread.h> #include <pthread.h>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <condition_variable>
#include <atomic> #include <atomic>
#include <memory> #include <memory>
#include <thread> #include <thread>
@@ -19,12 +20,16 @@ extern Process * unknownip;
static std::shared_ptr<std::thread> monitor_thread_ptr; static std::shared_ptr<std::thread> monitor_thread_ptr;
static std::atomic_bool monitor_thread_run_flag(false); static std::atomic_bool monitor_thread_run_flag(false);
std::mutex monitor_exit_event_mutex;
std::condition_variable monitor_exit_event;
static NethogsMonitor::Callback monitor_udpate_callback; static NethogsMonitor::Callback monitor_udpate_callback;
typedef std::map<int, NethogsAppUpdate> NethogsAppUpdateMap; typedef std::map<int, NethogsAppUpdate> NethogsAppUpdateMap;
static NethogsAppUpdateMap monitor_update_data; static NethogsAppUpdateMap monitor_update_data;
static int monitor_refresh_delay = 1; static int monitor_refresh_delay = 1;
static int monitor_pc_dispatch_delay_ms = 50;
static time_t monitor_last_refresh_time = 0; static time_t monitor_last_refresh_time = 0;
void NethogsMonitor::threadProc() void NethogsMonitor::threadProc()
@@ -117,9 +122,8 @@ void NethogsMonitor::threadProc()
if (!packets_read) if (!packets_read)
{ {
// If no packets were read at all this iteration, pause to prevent 100% std::unique_lock<std::mutex> lk(monitor_exit_event_mutex);
// Pause 10 milliseconds monitor_exit_event.wait_for(lk, std::chrono::milliseconds(monitor_pc_dispatch_delay_ms));
usleep(10000);
} }
} }
} }
@@ -186,20 +190,25 @@ void NethogsMonitor::handleUpdate()
u_int32_t recv_bytes; u_int32_t recv_bytes;
float sent_kbs; float sent_kbs;
float recv_kbs; float recv_kbs;
curproc->getVal()->getkbps (&sent_kbs, &recv_kbs); curproc->getVal()->getkbps (&recv_kbs, &sent_kbs);
curproc->getVal()->gettotal (&recv_bytes, &sent_bytes); curproc->getVal()->gettotal (&recv_bytes, &sent_bytes);
if( monitor_udpate_callback ) if( monitor_udpate_callback )
{ {
//notify update //notify update
bool const new_process = (monitor_update_data.find(pid) == monitor_update_data.end());
NethogsAppUpdate &data = monitor_update_data[pid]; NethogsAppUpdate &data = monitor_update_data[pid];
bool data_change = false; bool data_change = false;
#define NHM_UPDATE_ONE_FIELD(TO,FROM) if((TO)!=(FROM)) { TO = FROM; data_change = true; } #define NHM_UPDATE_ONE_FIELD(TO,FROM) if((TO)!=(FROM)) { TO = FROM; data_change = true; }
NHM_UPDATE_ONE_FIELD( data.pid, pid ) if( new_process )
{
NHM_UPDATE_ONE_FIELD( data.pid, pid )
NHM_UPDATE_ONE_FIELD( data.app_name, curproc->getVal()->name )
}
NHM_UPDATE_ONE_FIELD( data.uid, curproc->getVal()->getUid() ) NHM_UPDATE_ONE_FIELD( data.uid, curproc->getVal()->getUid() )
NHM_UPDATE_ONE_FIELD( data.app_name, curproc->getVal()->name )
NHM_UPDATE_ONE_FIELD( data.device_name, curproc->getVal()->devicename ) NHM_UPDATE_ONE_FIELD( data.device_name, curproc->getVal()->devicename )
NHM_UPDATE_ONE_FIELD( data.sent_bytes, sent_bytes ) NHM_UPDATE_ONE_FIELD( data.sent_bytes, sent_bytes )
NHM_UPDATE_ONE_FIELD( data.recv_bytes, recv_bytes ) NHM_UPDATE_ONE_FIELD( data.recv_bytes, recv_bytes )
@@ -234,6 +243,11 @@ void NethogsMonitor::setRefreshDelay(int seconds)
monitor_refresh_delay = seconds; monitor_refresh_delay = seconds;
} }
void NethogsMonitor::setPcapDispatchDelay(int milliseconds)
{
monitor_pc_dispatch_delay_ms = milliseconds;
}
void NethogsMonitor::start() void NethogsMonitor::start()
{ {
bool expected = false; bool expected = false;
@@ -248,6 +262,7 @@ void NethogsMonitor::stop()
bool expected = true; bool expected = true;
if( monitor_thread_run_flag.compare_exchange_strong(expected, false) ) if( monitor_thread_run_flag.compare_exchange_strong(expected, false) )
{ {
monitor_exit_event.notify_one();
monitor_thread_ptr->join(); monitor_thread_ptr->join();
monitor_udpate_callback = nullptr; monitor_udpate_callback = nullptr;
} }

View File

@@ -36,15 +36,17 @@ public:
//register async callback to receive updates //register async callback to receive updates
//have to be called before start //have to be called before start
static void registerUpdateCallback(Callback const& cb); static void registerUpdateCallback(Callback const& cb);
static void setRefreshDelay(int seconds);
//start the monitor //start the monitor
static void start(); static void start();
//stop the monitor //stop the monitor
static void stop(); static void stop();
//tuning functions
static void setRefreshDelay(int seconds);
static void setPcapDispatchDelay(int milliseconds);
private: private:
static void threadProc(); static void threadProc();
static void handleUpdate(); static void handleUpdate();