ARPDAS_QNX6 1.0
|
00001 #include "cpu_usage.h" 00002 00003 cpu_usage::cpu_usage() { 00004 last_sutime = 0; 00005 fd = -1; 00006 } 00007 00008 cpu_usage::~cpu_usage() { 00009 if (fd >= 0) close(fd); 00010 } 00011 00012 /** 00013 * Established the connection with the idle process. 00014 * This is separate from the constructor to facilitate 00015 * error reporting. 00016 */ 00017 void cpu_usage::init() { 00018 fd = open( "/proc/1/as", O_RDONLY ); 00019 if ( fd < 0 ) nl_error( 3, "Unable to open /proc/1/as" ); 00020 } 00021 00022 /** 00023 * @param rate The rate at which this function is called in Hz. 00024 * @return Percent CPU utilization since the last call. 00025 */ 00026 unsigned char cpu_usage::report( double rate ) { 00027 procfs_status my_status; 00028 _Uint64t this_sutime; 00029 unsigned char rv = 0; 00030 00031 my_status.tid = 1; 00032 devctl( fd, DCMD_PROC_TIDSTATUS, &my_status, sizeof(my_status), NULL ); 00033 if (last_sutime) { 00034 double pct; 00035 _Uint64t dt = my_status.sutime - last_sutime; 00036 pct = 100. - rate * dt * 100e-9; 00037 rv = (pct < 0) ? 0 : ((unsigned char) pct); 00038 } 00039 last_sutime = my_status.sutime; 00040 return rv; 00041 }