src/process.cpp
// TODO: Return this process's CPU utilization
Return this process's CPU utilization
Return this process ' s CPU utilization
float Process::CpuUtilization() {
    std::stringstream path;
    path << kProcDirectory << "/" << pid << kStatFilename;
    std::ifstream is(path.str());
    string line;
    std::unordered_map<int ,int> data = {
            {13,0},
            {14,0},
            {15,0},
            {16,0},
            {21,0}};
    if (is.is_open()) {
        getline(is, line);
        std::istringstream ss(line);
        string val;
        for (int i = 1, count = 0; count < data.size(); ++i) {
            ss >> val;
            if (data.find(i) != data.end()) {
                int val_int = std::atoi(val.c_str());
                data[i] = val_int;
                count++;
            }
        }
    }
    long long uptime = LinuxParser::UpTime();
    long int hertz = sysconf(_SC_CLK_TCK);
    float  total_time = data[13] + data[14] +
                        data[15] + data[16];
    long int seconds = uptime -  (data[21] / hertz);
    cpuUtilization = ((total_time / hertz) / seconds);
    return cpuUtilization;
  }
float Process :: CpuUtilization ( ) { std :: stringstream path ; path << kProcDirectory << " " << pid << kStatFilename ; std :: ifstream is ( path . str ( ) ) ; string line ; std :: unordered_map < int , int > data = { { 13 , 0 } , { 14 , 0 } , { 15 , 0 } , { 16 , 0 } , { 21 , 0 } } ; if ( is . is_open ( ) ) { getline ( is , line ) ; std :: istringstream ss ( line ) ; string val ; for ( int i = 1 , count = 0 ; count < data . size ( ) ; ++ i ) { ss >> val ; if ( data . find ( i ) != data . end ( ) ) { int val_int = std :: atoi ( val . c_str ( ) ) ; data [ i ] = val_int ; count ++ ; } } } long long uptime = LinuxParser :: UpTime ( ) ; long int hertz = sysconf ( _SC_CLK_TCK ) ; float total_time = data [ 13 ] + data [ 14 ] + data [ 15 ] + data [ 16 ] ; long int seconds = uptime - ( data [ 21 ] / hertz ) ; cpuUtilization = ( ( total_time / hertz ) / seconds ) ; return cpuUtilization ; }
TODO: Return this process's CPU utilization
TODO : Return this process ' s CPU utilization