blob: 46fa5ed6b2771b4015711230400ab1b57cf90235 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
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
28#ifndef TALK_BASE_LINUX_H_
29#define TALK_BASE_LINUX_H_
30
31#if defined(LINUX) || defined(ANDROID)
32#include <string>
33#include <map>
34#include <vector>
35
36#include "talk/base/scoped_ptr.h"
37#include "talk/base/stream.h"
38
39namespace talk_base {
40
41//////////////////////////////////////////////////////////////////////////////
42// ConfigParser parses a FileStream of an ".ini."-type format into a map.
43//////////////////////////////////////////////////////////////////////////////
44
45// Sample Usage:
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;
53// }
54// }
55
56class ConfigParser {
57 public:
58 typedef std::map<std::string, std::string> SimpleMap;
59 typedef std::vector<SimpleMap> MapVector;
60
61 ConfigParser();
62 virtual ~ConfigParser();
63
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);
69
70 private:
71 scoped_ptr<StreamInterface> instream_;
72};
73
74//////////////////////////////////////////////////////////////////////////////
75// ProcCpuInfo reads CPU info from the /proc subsystem on any *NIX platform.
76//////////////////////////////////////////////////////////////////////////////
77
78// Sample Usage:
79// ProcCpuInfo proc_info;
80// int no_of_cpu;
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);
85// }
86// }
87
88class ProcCpuInfo {
89 public:
90 ProcCpuInfo();
91 virtual ~ProcCpuInfo();
92
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();
96
97 // Obtains the number of logical CPU threads and places the value num.
98 virtual bool GetNumCpus(int* num);
99
100 // Obtains the number of physical CPU cores and places the value num.
101 virtual bool GetNumPhysicalCpus(int* num);
102
103 // Obtains the CPU family id.
104 virtual bool GetCpuFamily(int* id);
105
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);
109
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);
114
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,
118 int* result);
119
120 private:
121 ConfigParser::MapVector sections_;
122};
123
mallinath@webrtc.orgb881d272014-02-03 16:57:16 +0000124#if !defined(GOOGLE_CHROME_BUILD) && !defined(CHROMIUM_BUILD)
125// Builds a string containing the info from lsb_release on a single line.
126std::string ReadLinuxLsbRelease();
127#endif
128
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000129// Returns the output of "uname".
130std::string ReadLinuxUname();
131
132// Returns the content (int) of
133// /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
134// Returns -1 on error.
135int ReadCpuMaxFreq();
136
137} // namespace talk_base
138
139#endif // defined(LINUX) || defined(ANDROID)
140#endif // TALK_BASE_LINUX_H_