[linux|freebsd] Fix condition to calculate CPU usage
This commit is contained in:
@@ -87,8 +87,12 @@ get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system)
|
||||
cp_system = cpu_state[CP_SYS] + cpu_state[CP_INTR];
|
||||
cp_total = cpu_state[CP_IDLE] + cp_user + cp_system;
|
||||
|
||||
*cpu_user = (cp_user > cp_user_old) ? (cp_user - cp_user_old) * 100 / (gdouble)(cp_total - cp_total_old) : 0;
|
||||
*cpu_system = (cp_system > cp_system_old) ? (cp_system - cp_system_old) * 100 / (gdouble)(cp_total - cp_total_old) : 0;
|
||||
*cpu_user = *cpu_system = 0.0;
|
||||
if (cp_total > cp_total_old)
|
||||
{
|
||||
*cpu_user = (cp_user - cp_user_old) * 100 / (gdouble)(cp_total - cp_total_old);
|
||||
*cpu_system = (cp_system - cp_system_old) * 100 / (gdouble)(cp_total - cp_total_old);
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
|
||||
@@ -93,8 +93,12 @@ get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system)
|
||||
cur_jiffies_system = system;
|
||||
cur_jiffies = cur_jiffies_user + cur_jiffies_system + idle;
|
||||
|
||||
*cpu_user = (old_jiffies > 0) ? (cur_jiffies_user - old_jiffies_user) * 100 / (gdouble)(cur_jiffies - old_jiffies) : 0;
|
||||
*cpu_system = (old_jiffies > 0) ? (cur_jiffies_system - old_jiffies_system) * 100 / (gdouble)(cur_jiffies - old_jiffies) : 0;
|
||||
*cpu_user = *cpu_system = 0.0;
|
||||
if (cur_jiffies > old_jiffies)
|
||||
{
|
||||
*cpu_user = (cur_jiffies_user - old_jiffies_user) * 100 / (gdouble)(cur_jiffies - old_jiffies);
|
||||
*cpu_system = (cur_jiffies_system - old_jiffies_system) * 100 / (gdouble)(cur_jiffies - old_jiffies);
|
||||
}
|
||||
*cpu_count = _cpu_count;
|
||||
|
||||
return TRUE;
|
||||
|
||||
Reference in New Issue
Block a user