Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "sys/times.h" 7 : #include <cstdlib> 8 : #include <base/cpuinfo.h> 9 : 10 : #include <fstream> 11 : #include <iostream> 12 : 13 : #include <boost/algorithm/string/find.hpp> 14 : #include <boost/algorithm/string/find_iterator.hpp> 15 : #include <boost/algorithm/string/split.hpp> 16 : 17 : using namespace boost; 18 : 19 0 : uint32_t NumCpus() { 20 : static uint32_t count = 0; 21 : 22 0 : if (count != 0) { 23 0 : return count; 24 : } 25 : 26 0 : std::ifstream file("/proc/cpuinfo"); 27 : std::string content((std::istreambuf_iterator<char>(file)), 28 0 : std::istreambuf_iterator<char>()); 29 : // Create a find_iterator 30 : typedef find_iterator<std::string::iterator> string_find_iterator; 31 : 32 0 : for (string_find_iterator it = 33 0 : make_find_iterator(content, first_finder("model name", is_iequal())); 34 0 : it != string_find_iterator(); ++it, count++); 35 0 : return count; 36 0 : } 37 : 38 0 : void LoadAvg(CpuLoad &load) { 39 : double averages[3]; 40 0 : uint32_t num_cpus = NumCpus(); 41 0 : getloadavg(averages, 3); 42 0 : if (num_cpus > 0) { 43 0 : load.one_min_avg = averages[0]/num_cpus; 44 0 : load.five_min_avg = averages[1]/num_cpus; 45 0 : load.fifteen_min_avg = averages[2]/num_cpus; 46 : } 47 0 : } 48 : 49 : 50 : 51 0 : void ProcessMemInfo(ProcessMemInfo &info) { 52 0 : std::ifstream file("/proc/self/status"); 53 0 : bool vmsize = false; 54 0 : bool peak = false; 55 0 : bool rss = false; 56 0 : std::string line; 57 0 : while (std::getline(file, line)) { 58 0 : if (line.find("VmSize") != std::string::npos) { 59 0 : std::stringstream vm(line); 60 0 : std::string tmp; vm >> tmp; vm >> info.virt; 61 0 : vmsize = true; 62 0 : } 63 0 : if (line.find("VmRSS") != std::string::npos) { 64 0 : std::stringstream vm(line); 65 0 : std::string tmp; vm >> tmp; vm >> info.res; 66 0 : rss = true; 67 0 : } 68 0 : if (line.find("VmPeak") != std::string::npos) { 69 0 : std::stringstream vm(line); 70 0 : std::string tmp; vm >> tmp; vm >> info.peakvirt; 71 0 : peak = true; 72 0 : } 73 0 : if (rss && vmsize && peak) 74 0 : break; 75 : } 76 0 : } 77 : 78 0 : void SystemMemInfo(SystemMemInfo &info) { 79 0 : std::ifstream file("/proc/meminfo"); 80 0 : std::string tmp; 81 : // MemTotal: 132010576 kB 82 0 : file >> tmp; file >> info.total; file >> tmp; 83 : // MemFree: 90333184 kB 84 0 : file >> tmp; file >> info.free; file >> tmp; 85 : // Buffers: 1029924 kB 86 0 : file >> tmp; file >> info.buffers; file >> tmp; 87 : // Cached: 10290012 kB 88 0 : file >> tmp; file >> info.cached; 89 : // Used = Total - Free 90 0 : info.used = info.total - info.free; 91 0 : }