You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #if defined(LINUX) || defined(ANDROID)
  28. #include "talk/base/linux.h"
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <sys/utsname.h>
  32. #include <sys/wait.h>
  33. #include <cstdio>
  34. #include <set>
  35. #include "talk/base/stringencode.h"
  36. namespace talk_base {
  37. static const char kCpuInfoFile[] = "/proc/cpuinfo";
  38. static const char kCpuMaxFreqFile[] =
  39. "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
  40. ProcCpuInfo::ProcCpuInfo() {
  41. }
  42. ProcCpuInfo::~ProcCpuInfo() {
  43. }
  44. bool ProcCpuInfo::LoadFromSystem() {
  45. ConfigParser procfs;
  46. if (!procfs.Open(kCpuInfoFile)) {
  47. return false;
  48. }
  49. return procfs.Parse(&sections_);
  50. };
  51. bool ProcCpuInfo::GetSectionCount(size_t* count) {
  52. if (sections_.empty()) {
  53. return false;
  54. }
  55. if (count) {
  56. *count = sections_.size();
  57. }
  58. return true;
  59. }
  60. bool ProcCpuInfo::GetNumCpus(int* num) {
  61. if (sections_.empty()) {
  62. return false;
  63. }
  64. int total_cpus = 0;
  65. #if defined(__arm__)
  66. // Count the number of blocks that have a "processor" key defined. On ARM,
  67. // there may be extra blocks of information that aren't per-processor.
  68. size_t section_count = sections_.size();
  69. for (size_t i = 0; i < section_count; ++i) {
  70. int processor_id;
  71. if (GetSectionIntValue(i, "processor", &processor_id)) {
  72. ++total_cpus;
  73. }
  74. }
  75. // Single core ARM systems don't include "processor" keys at all, so return
  76. // that we have a single core if we didn't find any explicitly above.
  77. if (total_cpus == 0) {
  78. total_cpus = 1;
  79. }
  80. #else
  81. // On X86, there is exactly one info section per processor.
  82. total_cpus = static_cast<int>(sections_.size());
  83. #endif
  84. if (num) {
  85. *num = total_cpus;
  86. }
  87. return true;
  88. }
  89. bool ProcCpuInfo::GetNumPhysicalCpus(int* num) {
  90. if (sections_.empty()) {
  91. return false;
  92. }
  93. // TODO: /proc/cpuinfo only reports cores that are currently
  94. // _online_, so this may underreport the number of physical cores.
  95. #if defined(__arm__)
  96. // ARM (currently) has no hyperthreading, so just return the same value
  97. // as GetNumCpus.
  98. return GetNumCpus(num);
  99. #else
  100. int total_cores = 0;
  101. std::set<int> physical_ids;
  102. size_t section_count = sections_.size();
  103. for (size_t i = 0; i < section_count; ++i) {
  104. int physical_id;
  105. int cores;
  106. // Count the cores for the physical id only if we have not counted the id.
  107. if (GetSectionIntValue(i, "physical id", &physical_id) &&
  108. GetSectionIntValue(i, "cpu cores", &cores) &&
  109. physical_ids.find(physical_id) == physical_ids.end()) {
  110. physical_ids.insert(physical_id);
  111. total_cores += cores;
  112. }
  113. }
  114. if (num) {
  115. *num = total_cores;
  116. }
  117. return true;
  118. #endif
  119. }
  120. bool ProcCpuInfo::GetCpuFamily(int* id) {
  121. int cpu_family = 0;
  122. #if defined(__arm__)
  123. // On some ARM platforms, there is no 'cpu family' in '/proc/cpuinfo'. But
  124. // there is 'CPU Architecture' which can be used as 'cpu family'.
  125. // See http://en.wikipedia.org/wiki/ARM_architecture for a good list of
  126. // ARM cpu families, architectures, and their mappings.
  127. // There may be multiple sessions that aren't per-processor. We need to scan
  128. // through each session until we find the first 'CPU architecture'.
  129. size_t section_count = sections_.size();
  130. for (size_t i = 0; i < section_count; ++i) {
  131. if (GetSectionIntValue(i, "CPU architecture", &cpu_family)) {
  132. // We returns the first one (if there are multiple entries).
  133. break;
  134. };
  135. }
  136. #else
  137. GetSectionIntValue(0, "cpu family", &cpu_family);
  138. #endif
  139. if (id) {
  140. *id = cpu_family;
  141. }
  142. return true;
  143. }
  144. bool ProcCpuInfo::GetSectionStringValue(size_t section_num,
  145. const std::string& key,
  146. std::string* result) {
  147. if (section_num >= sections_.size()) {
  148. return false;
  149. }
  150. ConfigParser::SimpleMap::iterator iter = sections_[section_num].find(key);
  151. if (iter == sections_[section_num].end()) {
  152. return false;
  153. }
  154. *result = iter->second;
  155. return true;
  156. }
  157. bool ProcCpuInfo::GetSectionIntValue(size_t section_num,
  158. const std::string& key,
  159. int* result) {
  160. if (section_num >= sections_.size()) {
  161. return false;
  162. }
  163. ConfigParser::SimpleMap::iterator iter = sections_[section_num].find(key);
  164. if (iter == sections_[section_num].end()) {
  165. return false;
  166. }
  167. return FromString(iter->second, result);
  168. }
  169. ConfigParser::ConfigParser() {}
  170. ConfigParser::~ConfigParser() {}
  171. bool ConfigParser::Open(const std::string& filename) {
  172. FileStream* fs = new FileStream();
  173. if (!fs->Open(filename, "r", NULL)) {
  174. return false;
  175. }
  176. instream_.reset(fs);
  177. return true;
  178. }
  179. void ConfigParser::Attach(StreamInterface* stream) {
  180. instream_.reset(stream);
  181. }
  182. bool ConfigParser::Parse(MapVector* key_val_pairs) {
  183. // Parses the file and places the found key-value pairs into key_val_pairs.
  184. SimpleMap section;
  185. while (ParseSection(&section)) {
  186. key_val_pairs->push_back(section);
  187. section.clear();
  188. }
  189. return (!key_val_pairs->empty());
  190. }
  191. bool ConfigParser::ParseSection(SimpleMap* key_val_pair) {
  192. // Parses the next section in the filestream and places the found key-value
  193. // pairs into key_val_pair.
  194. std::string key, value;
  195. while (ParseLine(&key, &value)) {
  196. (*key_val_pair)[key] = value;
  197. }
  198. return (!key_val_pair->empty());
  199. }
  200. bool ConfigParser::ParseLine(std::string* key, std::string* value) {
  201. // Parses the next line in the filestream and places the found key-value
  202. // pair into key and val.
  203. std::string line;
  204. if ((instream_->ReadLine(&line)) == SR_EOS) {
  205. return false;
  206. }
  207. std::vector<std::string> tokens;
  208. if (2 != split(line, ':', &tokens)) {
  209. return false;
  210. }
  211. // Removes whitespace at the end of Key name
  212. size_t pos = tokens[0].length() - 1;
  213. while ((pos > 0) && isspace(tokens[0][pos])) {
  214. pos--;
  215. }
  216. tokens[0].erase(pos + 1);
  217. // Removes whitespace at the start of value
  218. pos = 0;
  219. while (pos < tokens[1].length() && isspace(tokens[1][pos])) {
  220. pos++;
  221. }
  222. tokens[1].erase(0, pos);
  223. *key = tokens[0];
  224. *value = tokens[1];
  225. return true;
  226. }
  227. static bool ExpectLineFromStream(FileStream* stream,
  228. std::string* out) {
  229. StreamResult res = stream->ReadLine(out);
  230. if (res != SR_SUCCESS) {
  231. if (res != SR_EOS) {
  232. LOG(LS_ERROR) << "Error when reading from stream";
  233. } else {
  234. LOG(LS_ERROR) << "Incorrect number of lines in stream";
  235. }
  236. return false;
  237. }
  238. return true;
  239. }
  240. static void ExpectEofFromStream(FileStream* stream) {
  241. std::string unused;
  242. StreamResult res = stream->ReadLine(&unused);
  243. if (res == SR_SUCCESS) {
  244. LOG(LS_WARNING) << "Ignoring unexpected extra lines from stream";
  245. } else if (res != SR_EOS) {
  246. LOG(LS_WARNING) << "Error when checking for extra lines from stream";
  247. }
  248. }
  249. // For caching the lsb_release output (reading it invokes a sub-process and
  250. // hence is somewhat expensive).
  251. static std::string lsb_release_string;
  252. static CriticalSection lsb_release_string_critsec;
  253. std::string ReadLinuxLsbRelease() {
  254. CritScope cs(&lsb_release_string_critsec);
  255. if (!lsb_release_string.empty()) {
  256. // Have cached result from previous call.
  257. return lsb_release_string;
  258. }
  259. // No cached result. Run lsb_release and parse output.
  260. POpenStream lsb_release_output;
  261. if (!lsb_release_output.Open("lsb_release -idrcs", "r", NULL)) {
  262. LOG_ERR(LS_ERROR) << "Can't run lsb_release";
  263. return lsb_release_string; // empty
  264. }
  265. // Read in the command's output and build the string.
  266. std::ostringstream sstr;
  267. std::string line;
  268. int wait_status;
  269. if (!ExpectLineFromStream(&lsb_release_output, &line)) {
  270. return lsb_release_string; // empty
  271. }
  272. sstr << "DISTRIB_ID=" << line;
  273. if (!ExpectLineFromStream(&lsb_release_output, &line)) {
  274. return lsb_release_string; // empty
  275. }
  276. sstr << " DISTRIB_DESCRIPTION=\"" << line << '"';
  277. if (!ExpectLineFromStream(&lsb_release_output, &line)) {
  278. return lsb_release_string; // empty
  279. }
  280. sstr << " DISTRIB_RELEASE=" << line;
  281. if (!ExpectLineFromStream(&lsb_release_output, &line)) {
  282. return lsb_release_string; // empty
  283. }
  284. sstr << " DISTRIB_CODENAME=" << line;
  285. // Should not be anything left.
  286. ExpectEofFromStream(&lsb_release_output);
  287. lsb_release_output.Close();
  288. wait_status = lsb_release_output.GetWaitStatus();
  289. if (wait_status == -1 ||
  290. !WIFEXITED(wait_status) ||
  291. WEXITSTATUS(wait_status) != 0) {
  292. LOG(LS_WARNING) << "Unexpected exit status from lsb_release";
  293. }
  294. lsb_release_string = sstr.str();
  295. return lsb_release_string;
  296. }
  297. std::string ReadLinuxUname() {
  298. struct utsname buf;
  299. if (uname(&buf) < 0) {
  300. LOG_ERR(LS_ERROR) << "Can't call uname()";
  301. return std::string();
  302. }
  303. std::ostringstream sstr;
  304. sstr << buf.sysname << " "
  305. << buf.release << " "
  306. << buf.version << " "
  307. << buf.machine;
  308. return sstr.str();
  309. }
  310. int ReadCpuMaxFreq() {
  311. FileStream fs;
  312. std::string str;
  313. int freq = -1;
  314. if (!fs.Open(kCpuMaxFreqFile, "r", NULL) ||
  315. SR_SUCCESS != fs.ReadLine(&str) ||
  316. !FromString(str, &freq)) {
  317. return -1;
  318. }
  319. return freq;
  320. }
  321. } // namespace talk_base
  322. #endif // defined(LINUX) || defined(ANDROID)