统计任务轮询的平均时间和最大时间

This commit is contained in:
nnhy 2015-07-20 01:54:40 +00:00
parent 0b082b370a
commit 1b4f3f00e4
2 changed files with 17 additions and 2 deletions

View File

@ -83,6 +83,9 @@ TaskScheduler::TaskScheduler(string name)
Running = false; Running = false;
Current = NULL; Current = NULL;
Count = 0; Count = 0;
Cost = 0;
MaxCost = 0;
} }
TaskScheduler::~TaskScheduler() TaskScheduler::~TaskScheduler()
@ -171,6 +174,8 @@ void TaskScheduler::Execute(uint usMax)
now -= Sys.StartTime; // 当前时间。减去系统启动时间,避免修改系统时间后导致调度停摆 now -= Sys.StartTime; // 当前时间。减去系统启动时间,避免修改系统时间后导致调度停摆
ulong min = UInt64_Max; // 最小时间,这个时间就会有任务到来 ulong min = UInt64_Max; // 最小时间,这个时间就会有任务到来
TimeCost tc;
int i = -1; int i = -1;
while(_Tasks.MoveNext(i)) while(_Tasks.MoveNext(i))
{ {
@ -188,6 +193,13 @@ void TaskScheduler::Execute(uint usMax)
} }
} }
int cost = tc.Elapsed();
if(Cost > 0)
Cost = (Cost + cost) / 2;
else
Cost = cost;
if(cost > MaxCost) MaxCost = cost;
// 如果有最小时间,睡一会吧 // 如果有最小时间,睡一会吧
now = Time.Current(); // 当前时间 now = Time.Current(); // 当前时间
if(min != UInt64_Max && min > now) if(min != UInt64_Max && min > now)
@ -202,7 +214,7 @@ void TaskScheduler::ShowStatus(void* param)
{ {
TaskScheduler* ts = (TaskScheduler*)param; TaskScheduler* ts = (TaskScheduler*)param;
debug_printf("Task::ShowStatus 系统启动 %s \r\n", Time.Now().ToString()); debug_printf("Task::ShowStatus 平均 %dus 最大 %dus 系统启动 %s \r\n", ts->Cost, ts->MaxCost, Time.Now().ToString());
int i = -1; int i = -1;
while(ts->_Tasks.MoveNext(i)) while(ts->_Tasks.MoveNext(i))

3
Task.h
View File

@ -60,6 +60,9 @@ public:
bool Running; // 是否正在运行 bool Running; // 是否正在运行
byte Reversed[3];// 保留,避免对齐问题 byte Reversed[3];// 保留,避免对齐问题
int Cost; // 平均执行时间
int MaxCost; // 最大执行时间
TaskScheduler(string name = NULL); TaskScheduler(string name = NULL);
~TaskScheduler(); ~TaskScheduler();