Skip to content

Commit

Permalink
mod dump
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Zhao committed Jun 24, 2015
1 parent 74f878f commit fa0a08d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 76 deletions.
24 changes: 19 additions & 5 deletions example/perf/src/main.cpp
Expand Up @@ -81,15 +81,29 @@ class dumy_t: public obj_counter_t<dumy_t>

};

int main(int argc, char* argv[])
void test(int n = 100)
{
for (int i = 0; i < 100; ++i)
AUTO_PERF();
for (int i = 0; i < n; ++i)
{
foo_t* p = new foo_t();
foo2_t* p2 = new foo2_t();
dumy_t* p3 = new dumy_t();
}
singleton_t<obj_sum_mgr_t>::instance().dump("./dump.txt");
}
int main(int argc, char* argv[])
{
singleton_t<performance_daemon_t>::instance().start("perf", 5);

test();

for (int j = 0; j < 10; ++j)
{
sleep(2);
test(500);
}
sleep(600);
singleton_t<performance_daemon_t>::instance().stop();
return 0;
task_t t1(&xx, NULL);
task_t t2 = t1;
Expand All @@ -105,7 +119,7 @@ int main(int argc, char* argv[])
tmp.del(NULL);
tmp.timer_check();*/

//singleton_t<performance_daemon_t>::instance().start("perf.txt", 5);
//

timer_service_t ts(1);
ts.timer_callback(2000, task_t(&xx, (void*)2));
Expand All @@ -123,7 +137,7 @@ int main(int argc, char* argv[])
usleep(1000*100);
}
*/
sleep(600);


//singleton_t<performance_daemon_t>::instance().stop();
return 0;
Expand Down
26 changes: 17 additions & 9 deletions fflib/base/fftype.h
Expand Up @@ -141,29 +141,37 @@ class obj_sum_mgr_t

void dump(const string& path_)
{
ofstream tmp_fstream;
tmp_fstream.open(path_.c_str(), ios::app);
FILE* fp = ::fopen(path_.c_str(), "a+");
if (NULL == fp)
{
return;
}

char tmp_buff[256] = {0};
if(getc(fp) == EOF)
{
int n = snprintf(tmp_buff, sizeof(tmp_buff), "time,obj,num\n");
fwrite(tmp_buff, n, 1, fp);
}

map<string, long> ret = get_all_obj_num();
map<string, long>::iterator it = ret.begin();

time_t timep = time(NULL);
struct tm *tmp = localtime(&timep);

char tmp_buff[256];

sprintf(tmp_buff, "%04d%02d%02d-%02d:%02d:%02d",
tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
char buff[1024] = {0};

tmp_fstream << "obj,num,time\n";

for (; it != ret.end(); ++it)
{
snprintf(buff, sizeof(buff), "%s,%ld,%s\n", it->first.c_str(), it->second, tmp_buff);
tmp_fstream << buff;
int n = snprintf(buff, sizeof(buff), "%s,%s,%ld\n", tmp_buff, it->first.c_str(), it->second);
fwrite(buff, n, 1, fp);
}

tmp_fstream.flush();
fclose(fp);
}
protected:
mutex_t m_mutex;
Expand Down
116 changes: 69 additions & 47 deletions fflib/base/performance_daemon.cpp
@@ -1,5 +1,8 @@
#include "base/performance_daemon.h"

#include "base/fftype.h"
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace ff;

performance_daemon_t::performance_daemon_t():
Expand All @@ -19,6 +22,18 @@ int performance_daemon_t::start(const string& path_, int seconds_)
{
if (true == m_started) return -1;

m_path = path_;
int rc = ::access(m_path.c_str(), F_OK);
if (0 != rc)
{
rc = ::mkdir(m_path.c_str(), 0777);
if (rc != 0)
{
printf("performance_daemon_t::start mkdir<%s>failed error<%s>\n", m_path.c_str(), ::strerror(errno));
return -1;
}
}

m_timeout_sec = seconds_;

//! 启动定时器 1 times/seconds
Expand All @@ -33,8 +48,7 @@ int performance_daemon_t::start(const string& path_, int seconds_)
((performance_daemon_t*)p_)->run();
}
};

m_fstream.open(path_.c_str());

m_thread.create_thread(task_t(&lambda_t::run, this), 1);
return 0;
}
Expand All @@ -53,35 +67,14 @@ int performance_daemon_t::stop()

m_task_queue.close();
m_thread.join();
m_fstream.close();
return 0;
}

void performance_daemon_t::post(const string& mod_, long us_)
void performance_daemon_t::post(const string& mod_, long arg_, long us_)
{
struct lambda_perf_data_t
{
static void add_perf_data(void* p_)
{
lambda_perf_data_t* pd_ptr = (lambda_perf_data_t*)p_;
pd_ptr->pd->add_perf_data(pd_ptr->mod, pd_ptr->cost);

delete pd_ptr;
}
lambda_perf_data_t(performance_daemon_t* p_, const string& mod_, long cost_):
pd(p_),
mod(mod_),
cost(cost_)
{
}
performance_daemon_t* pd;
string mod;
long cost;
};

if (m_started)
{
m_task_queue.produce(task_t(&lambda_perf_data_t::add_perf_data, new lambda_perf_data_t(this, mod_, us_)));
m_task_queue.produce(task_binder_t::gen(&performance_daemon_t::add_perf_data, this, mod_, arg_, us_));
}
else
{
Expand All @@ -92,7 +85,7 @@ void performance_daemon_t::post(const string& mod_, long us_)
void performance_daemon_t::handle_timer()
{
flush();
m_perf_info.clear();
//m_perf_info.clear();
m_timer_service->timer_callback(m_timeout_sec * 1000, task_t(&timer_lambda_t::setup_timer, this));
}

Expand All @@ -103,28 +96,47 @@ void performance_daemon_t::flush()
time_t timep = time(NULL);
struct tm *tmp = localtime(&timep);

char tmp_buff[256];
char tmp_buff[256] = {0};
char buff[1024] = {0};

sprintf(tmp_buff, "/%04d-%02d-%02d.perf",
tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
string filename = m_path + tmp_buff;

FILE* fp = ::fopen(filename.c_str(), "a+");
if (NULL == fp)
{
return;
}

if(getc(fp) == EOF)
{
int n = snprintf(buff, sizeof(buff), "time,mod,rps,exe_times,max_cost[us],min_cost[us],per_cost[us]\n");
fwrite(buff, n, 1, fp);
}

sprintf(tmp_buff, "%04d%02d%02d-%02d:%02d:%02d",
tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
char buff[1024] = {0};

snprintf(buff, sizeof(buff), "time,mod,max_cost[us],min_cost[us],per_cost[us],request_per_second,exe_times\n");
m_fstream << buff;

for (; it != m_perf_info.end(); ++it)
{
perf_info_t& pinfo = it->second;
long per = pinfo.total / pinfo.times;
long rps = (per == 0? -1: 1000000 / per);
long per = (pinfo.times == 0? 0: (pinfo.total / pinfo.times));
long rps = (per == 0? 0: 1000000 / per);

//! -------------------------- time, mod, max, min, per, rps, times
snprintf(buff, sizeof(buff), "%s,%s,%ld,%ld,%ld,%ld,%ld\n",
tmp_buff, it->first.c_str(), pinfo.max, pinfo.min, per, rps, pinfo.times);
m_fstream << buff;
int n = snprintf(buff, sizeof(buff), "%s,%s,%ld,%ld,%ld,%ld,%ld\n",
tmp_buff, it->first.c_str(), rps, pinfo.times, pinfo.max, per == 0? 0: pinfo.min, per);
fwrite(buff, n, 1, fp);
pinfo.clear();
}
fclose(fp);


m_fstream.flush();
sprintf(tmp_buff, "/%04d-%02d-%02d.counter",
tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
string filecounter = m_path + tmp_buff;
singleton_t<obj_sum_mgr_t>::instance().dump(filecounter);
}


Expand All @@ -135,19 +147,29 @@ void performance_daemon_t::run()
m_task_queue.run();
}

void performance_daemon_t::add_perf_data(const string& mod_, long us_)
void performance_daemon_t::add_perf_data(const string& mod_, long arg_, long us_)
{
perf_info_t& pinfo = m_perf_info[mod_];
perf_info_t* pinfo = NULL;
if (arg_ >= 0)
{
char msg[32];
snprintf(msg, sizeof(msg), "%s[%ld]", mod_.c_str(), arg_);
pinfo = &(m_perf_info[msg]);
}
else
{
pinfo = &(m_perf_info[mod_]);
}

pinfo.total += us_;
pinfo.times += 1;
pinfo->total += us_;
pinfo->times += 1;

if (us_ > pinfo.max)
if (us_ > pinfo->max)
{
pinfo.max = us_;
pinfo->max = us_;
}
if (us_ < pinfo.min)
if (us_ < pinfo->min)
{
pinfo.min = us_;
pinfo->min = us_;
}
}
33 changes: 20 additions & 13 deletions fflib/base/performance_daemon.h
Expand Up @@ -17,15 +17,18 @@ namespace ff {

#define AUTO_PERF() performance_daemon_t::perf_tool_t __tmp__(__FUNCTION__)
#define PERF(m) performance_daemon_t::perf_tool_t __tmp__(m)
#define AUTO_CMD_PERF(X, Y) performance_daemon_t::perf_tool_t __tmp__(X, Y)
#define PERF_MONITOR singleton_t<performance_daemon_t>::instance()


//! 性能监控
class performance_daemon_t
{
public:
struct perf_tool_t
{
perf_tool_t(const char* mod_):
mod(mod_)
perf_tool_t(const char* mod_, long arg_ = -1):
mod(mod_),
arg(arg_)
{
gettimeofday(&tm, NULL);
}
Expand All @@ -34,9 +37,10 @@ class performance_daemon_t
struct timeval now;
gettimeofday(&now, NULL);
long cost = (now.tv_sec - tm.tv_sec)*1000000 + (now.tv_usec - tm.tv_usec);
singleton_t<performance_daemon_t>::instance().post(mod, cost);
singleton_t<performance_daemon_t>::instance().post(mod, arg, cost);
}
const char* mod;
long arg;
struct timeval tm;
};
struct perf_info_t
Expand All @@ -47,6 +51,13 @@ class performance_daemon_t
total(0),
times(0)
{}
void clear()
{
max = 0;
min = 2147483647;
total = 0;
times = 0;
}
long max;
long min;
long total;
Expand All @@ -69,23 +80,19 @@ class performance_daemon_t
performance_daemon_t();
~performance_daemon_t();

//! 启动线程,创建文件
int start(const string& path_, int seconds_);
//! 关闭线程
int start(const string& path_ = "./perf", int seconds_ = 30*60);

int stop();

//! 增加性能监控数据
void post(const string& mod_, long us);
void post(const string& mod_, long arg_, long us);

//! 更新性能日志
void flush();

//! 所有队列
void run();

task_queue_t& get_task_queue() { return m_task_queue; }
public:
void add_perf_data(const string& mod_, long us);
void add_perf_data(const string& mod_, long arg_, long us);

private:
void handle_timer();
Expand All @@ -96,7 +103,7 @@ class performance_daemon_t
map<string, perf_info_t> m_perf_info;
task_queue_t m_task_queue;
thread_t m_thread;
ofstream m_fstream;
string m_path;
timer_service_t* m_timer_service;
};

Expand Down
2 changes: 1 addition & 1 deletion fflib/base/timer_service.h
Expand Up @@ -55,7 +55,7 @@ class timer_service_t
typedef list<registered_info_t> registered_info_list_t;
typedef multimap<long, registered_info_t> registered_info_map_t;
public:
timer_service_t(long tick = 1):
timer_service_t(long tick = 1000):
m_runing(true),
m_efd(-1),
m_min_timeout(tick),
Expand Down
2 changes: 1 addition & 1 deletion fflib/rpc/broker_service.cpp
Expand Up @@ -551,7 +551,7 @@ int broker_service_t::service_obj_t::interface_callback(msg_i& msg_, const strin

long cost = now.tv_sec*1000000 + now.tv_usec - it->second.start_time;

singleton_t<performance_daemon_t>::instance().post(it->second.req_msg, cost);
singleton_t<performance_daemon_t>::instance().post(it->second.req_msg, 0, cost);
m_callback_map.erase(it);
return 0;
}
Expand Down

0 comments on commit fa0a08d

Please sign in to comment.