3 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_LINUX_H_
29 #define TALK_BASE_LINUX_H_
31 #if defined(LINUX) || defined(ANDROID)
36 #include "talk/base/scoped_ptr.h"
37 #include "talk/base/stream.h"
41 //////////////////////////////////////////////////////////////////////////////
42 // ConfigParser parses a FileStream of an ".ini."-type format into a map.
43 //////////////////////////////////////////////////////////////////////////////
46 // ConfigParser parser;
47 // ConfigParser::MapVector key_val_pairs;
48 // if (parser.Open(inifile) && parser.Parse(&key_val_pairs)) {
49 // for (int section_num=0; i < key_val_pairs.size(); ++section_num) {
50 // std::string val1 = key_val_pairs[section_num][key1];
51 // std::string val2 = key_val_pairs[section_num][key2];
52 // // Do something with valn;
58 typedef std::map<std::string, std::string> SimpleMap;
59 typedef std::vector<SimpleMap> MapVector;
62 virtual ~ConfigParser();
64 virtual bool Open(const std::string& filename);
65 virtual void Attach(StreamInterface* stream);
66 virtual bool Parse(MapVector* key_val_pairs);
67 virtual bool ParseSection(SimpleMap* key_val_pair);
68 virtual bool ParseLine(std::string* key, std::string* value);
71 scoped_ptr<StreamInterface> instream_;
74 //////////////////////////////////////////////////////////////////////////////
75 // ProcCpuInfo reads CPU info from the /proc subsystem on any *NIX platform.
76 //////////////////////////////////////////////////////////////////////////////
79 // ProcCpuInfo proc_info;
81 // if (proc_info.LoadFromSystem()) {
82 // std::string out_str;
83 // proc_info.GetNumCpus(&no_of_cpu);
84 // proc_info.GetCpuStringValue(0, "vendor_id", &out_str);
91 virtual ~ProcCpuInfo();
93 // Reads the proc subsystem's cpu info into memory. If this fails, this
94 // returns false; if it succeeds, it returns true.
95 virtual bool LoadFromSystem();
97 // Obtains the number of logical CPU threads and places the value num.
98 virtual bool GetNumCpus(int* num);
100 // Obtains the number of physical CPU cores and places the value num.
101 virtual bool GetNumPhysicalCpus(int* num);
103 // Obtains the CPU family id.
104 virtual bool GetCpuFamily(int* id);
106 // Obtains the number of sections in /proc/cpuinfo, which may be greater
107 // than the number of CPUs (e.g. on ARM)
108 virtual bool GetSectionCount(size_t* count);
110 // Looks for the CPU proc item with the given name for the given section
111 // number and places the string value in result.
112 virtual bool GetSectionStringValue(size_t section_num, const std::string& key,
113 std::string* result);
115 // Looks for the CPU proc item with the given name for the given section
116 // number and places the int value in result.
117 virtual bool GetSectionIntValue(size_t section_num, const std::string& key,
121 ConfigParser::MapVector sections_;
124 // Returns the output of "uname".
125 std::string ReadLinuxUname();
127 // Returns the content (int) of
128 // /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
129 // Returns -1 on error.
130 int ReadCpuMaxFreq();
132 } // namespace talk_base
134 #endif // defined(LINUX) || defined(ANDROID)
135 #endif // TALK_BASE_LINUX_H_