src/linux_parser.cpp
// Todo: Read and return the system memory utilization
Read and return the system memory utilization
Read and return the system memory utilization
float LinuxParser::MemoryUtilization() { 
    string mem_file = kProcDirectory + kMeminfoFilename;
    std::ifstream file = GetStream(mem_file);
    unsigned long total_mem_KB;
    unsigned long avail_mem_KB;
    bool read_memTotal = false;
    bool read_memAvailable = false;
    string token;
    while(file >> token) {
        if(token == "MemTotal:") {
            if(file >> total_mem_KB) {
                read_memTotal = true;
            } else {
                file.close();
                throw std::runtime_error("Couldn't read total memory!!");
            }
        }
        if(token == "MemAvailable:") {
            if(file >> avail_mem_KB) {
                read_memAvailable = true;
            } else {
                file.close();
                throw std::runtime_error("Couldn't read available memory!!");
                return 0.0;       
            }
        }
        if (read_memAvailable && read_memTotal) {
            break;
        }
        file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    file.close();
    float mem_util_percent = ((total_mem_KB - avail_mem_KB)*100.0)/total_mem_KB;
    return mem_util_percent; 
}
float LinuxParser :: MemoryUtilization ( ) { string mem_file = kProcDirectory + kMeminfoFilename ; std :: ifstream file = GetStream ( mem_file ) ; unsigned long total_mem_KB ; unsigned long avail_mem_KB ; bool read_memTotal = false ; bool read_memAvailable = false ; string token ; while ( file >> token ) { if ( token == " " ) { if ( file >> total_mem_KB ) { read_memTotal = true ; } else { file . close ( ) ; throw std :: runtime_error ( " " ) ; } } if ( token == " " ) { if ( file >> avail_mem_KB ) { read_memAvailable = true ; } else { file . close ( ) ; throw std :: runtime_error ( " " ) ; return 0.0 ; } } if ( read_memAvailable && read_memTotal ) { break ; } file . ignore ( std :: numeric_limits < std :: streamsize > :: max ( ) , ' \n ' ) ; } file . close ( ) ; float mem_util_percent = ( ( total_mem_KB - avail_mem_KB ) * 100.0 ) / total_mem_KB ; return mem_util_percent ; }
Todo: Read and return the system memory utilization
Todo : Read and return the system memory utilization
// ignore rest of the line