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.

138 lines
4.9 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. #ifndef TALK_BASE_LINUX_H_
  28. #define TALK_BASE_LINUX_H_
  29. #if defined(LINUX) || defined(ANDROID)
  30. #include <string>
  31. #include <map>
  32. #include <vector>
  33. #include "talk/base/scoped_ptr.h"
  34. #include "talk/base/stream.h"
  35. namespace talk_base {
  36. //////////////////////////////////////////////////////////////////////////////
  37. // ConfigParser parses a FileStream of an ".ini."-type format into a map.
  38. //////////////////////////////////////////////////////////////////////////////
  39. // Sample Usage:
  40. // ConfigParser parser;
  41. // ConfigParser::MapVector key_val_pairs;
  42. // if (parser.Open(inifile) && parser.Parse(&key_val_pairs)) {
  43. // for (int section_num=0; i < key_val_pairs.size(); ++section_num) {
  44. // std::string val1 = key_val_pairs[section_num][key1];
  45. // std::string val2 = key_val_pairs[section_num][key2];
  46. // // Do something with valn;
  47. // }
  48. // }
  49. class ConfigParser {
  50. public:
  51. typedef std::map<std::string, std::string> SimpleMap;
  52. typedef std::vector<SimpleMap> MapVector;
  53. ConfigParser();
  54. virtual ~ConfigParser();
  55. virtual bool Open(const std::string& filename);
  56. virtual void Attach(StreamInterface* stream);
  57. virtual bool Parse(MapVector* key_val_pairs);
  58. virtual bool ParseSection(SimpleMap* key_val_pair);
  59. virtual bool ParseLine(std::string* key, std::string* value);
  60. private:
  61. scoped_ptr<StreamInterface> instream_;
  62. };
  63. //////////////////////////////////////////////////////////////////////////////
  64. // ProcCpuInfo reads CPU info from the /proc subsystem on any *NIX platform.
  65. //////////////////////////////////////////////////////////////////////////////
  66. // Sample Usage:
  67. // ProcCpuInfo proc_info;
  68. // int no_of_cpu;
  69. // if (proc_info.LoadFromSystem()) {
  70. // std::string out_str;
  71. // proc_info.GetNumCpus(&no_of_cpu);
  72. // proc_info.GetCpuStringValue(0, "vendor_id", &out_str);
  73. // }
  74. // }
  75. class ProcCpuInfo {
  76. public:
  77. ProcCpuInfo();
  78. virtual ~ProcCpuInfo();
  79. // Reads the proc subsystem's cpu info into memory. If this fails, this
  80. // returns false; if it succeeds, it returns true.
  81. virtual bool LoadFromSystem();
  82. // Obtains the number of logical CPU threads and places the value num.
  83. virtual bool GetNumCpus(int* num);
  84. // Obtains the number of physical CPU cores and places the value num.
  85. virtual bool GetNumPhysicalCpus(int* num);
  86. // Obtains the CPU family id.
  87. virtual bool GetCpuFamily(int* id);
  88. // Obtains the number of sections in /proc/cpuinfo, which may be greater
  89. // than the number of CPUs (e.g. on ARM)
  90. virtual bool GetSectionCount(size_t* count);
  91. // Looks for the CPU proc item with the given name for the given section
  92. // number and places the string value in result.
  93. virtual bool GetSectionStringValue(size_t section_num, const std::string& key,
  94. std::string* result);
  95. // Looks for the CPU proc item with the given name for the given section
  96. // number and places the int value in result.
  97. virtual bool GetSectionIntValue(size_t section_num, const std::string& key,
  98. int* result);
  99. private:
  100. ConfigParser::MapVector sections_;
  101. };
  102. // Builds a string containing the info from lsb_release on a single line.
  103. std::string ReadLinuxLsbRelease();
  104. // Returns the output of "uname".
  105. std::string ReadLinuxUname();
  106. // Returns the content (int) of
  107. // /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
  108. // Returns -1 on error.
  109. int ReadCpuMaxFreq();
  110. } // namespace talk_base
  111. #endif // defined(LINUX) || defined(ANDROID)
  112. #endif // TALK_BASE_LINUX_H_