float cpu_percentage( unsigned int cpu_usage_delay ) { // Get the load times from the XNU kernel host_cpu_load_info_data_t load1 = _get_cpu_percentage(); usleep(cpu_usage_delay); host_cpu_load_info_data_t load2 = _get_cpu_percentage(); // Current load times unsigned long long current_user = load1.cpu_ticks[CPU_STATE_USER]; unsigned long long current_system = load1.cpu_ticks[CPU_STATE_SYSTEM]; unsigned long long current_nice = load1.cpu_ticks[CPU_STATE_NICE]; unsigned long long current_idle = load1.cpu_ticks[CPU_STATE_IDLE]; // Next load times unsigned long long next_user = load2.cpu_ticks[CPU_STATE_USER]; unsigned long long next_system = load2.cpu_ticks[CPU_STATE_SYSTEM]; unsigned long long next_nice = load2.cpu_ticks[CPU_STATE_NICE]; unsigned long long next_idle = load2.cpu_ticks[CPU_STATE_IDLE]; // Difference between the two unsigned long long diff_user = next_user - current_user; unsigned long long diff_system = next_system - current_system; unsigned long long diff_nice = next_nice - current_nice; unsigned long long diff_idle = next_idle - current_idle; return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0; }
float cpu_percentage( unsigned int cpu_usage_delay ) { // Get the load times from the XNU kernel host_cpu_load_info_data_t load1 = _get_cpu_percentage(); usleep(cpu_usage_delay); host_cpu_load_info_data_t load2 = _get_cpu_percentage(); // Current load times unsigned long long current_user = load1.cpu_ticks[CPU_STATE_USER]; unsigned long long current_system = load1.cpu_ticks[CPU_STATE_SYSTEM]; unsigned long long current_nice = load1.cpu_ticks[CPU_STATE_NICE]; unsigned long long current_idle = load1.cpu_ticks[CPU_STATE_IDLE]; // Next load times unsigned long long next_user = load2.cpu_ticks[CPU_STATE_USER]; unsigned long long next_system = load2.cpu_ticks[CPU_STATE_SYSTEM]; unsigned long long next_nice = load2.cpu_ticks[CPU_STATE_NICE]; unsigned long long next_idle = load2.cpu_ticks[CPU_STATE_IDLE]; // Difference between the two unsigned long long diff_user = next_user - current_user; unsigned long long diff_system = next_system - current_system; unsigned long long diff_nice = next_nice - current_nice; unsigned long long diff_idle = next_idle - current_idle; #else // Linux float cpu_percentage( unsigned int cpu_usage_delay ) { std::string stat_line; size_t line_start_pos; size_t line_end_pos; unsigned long long current_user; unsigned long long current_system; unsigned long long current_nice; unsigned long long current_idle; unsigned long long next_user; unsigned long long next_system; unsigned long long next_nice; unsigned long long next_idle; unsigned long long diff_user; unsigned long long diff_system; unsigned long long diff_nice; unsigned long long diff_idle; std::istringstream iss; std::ifstream stat_file("/proc/stat"); getline(stat_file, stat_line); stat_file.close(); // skip "cpu" line_start_pos = stat_line.find_first_not_of(" ", 3); line_end_pos = stat_line.find_first_of(' ', line_start_pos); line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1); line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1); line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1); iss.str( stat_line.substr( line_start_pos, line_end_pos - line_start_pos ) ); iss >> current_user >> current_nice >> current_system >> current_idle; iss.clear(); usleep( cpu_usage_delay ); stat_file.open("/proc/stat"); getline(stat_file, stat_line); stat_file.close(); // skip "cpu" line_start_pos = stat_line.find_first_not_of(" ", 3); line_end_pos = stat_line.find_first_of(' ', line_start_pos); line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1); line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1); line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1); iss.str( stat_line.substr( line_start_pos, line_end_pos - line_start_pos ) ); iss >> next_user >> next_nice >> next_system >> next_idle; iss.clear(); diff_user = next_user - current_user; diff_system = next_system - current_system; diff_nice = next_nice - current_nice; diff_idle = next_idle - current_idle; #endif // platform return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0; }