Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2008 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_LINUX_H_
     12 #define WEBRTC_BASE_LINUX_H_
     13 
     14 #if defined(WEBRTC_LINUX)
     15 #include <string>
     16 #include <map>
     17 #include <vector>
     18 
     19 #include "webrtc/base/scoped_ptr.h"
     20 #include "webrtc/base/stream.h"
     21 
     22 namespace rtc {
     23 
     24 //////////////////////////////////////////////////////////////////////////////
     25 // ConfigParser parses a FileStream of an ".ini."-type format into a map.
     26 //////////////////////////////////////////////////////////////////////////////
     27 
     28 // Sample Usage:
     29 //   ConfigParser parser;
     30 //   ConfigParser::MapVector key_val_pairs;
     31 //   if (parser.Open(inifile) && parser.Parse(&key_val_pairs)) {
     32 //     for (int section_num=0; i < key_val_pairs.size(); ++section_num) {
     33 //       std::string val1 = key_val_pairs[section_num][key1];
     34 //       std::string val2 = key_val_pairs[section_num][key2];
     35 //       // Do something with valn;
     36 //     }
     37 //   }
     38 
     39 class ConfigParser {
     40  public:
     41   typedef std::map<std::string, std::string> SimpleMap;
     42   typedef std::vector<SimpleMap> MapVector;
     43 
     44   ConfigParser();
     45   virtual ~ConfigParser();
     46 
     47   virtual bool Open(const std::string& filename);
     48   virtual void Attach(StreamInterface* stream);
     49   virtual bool Parse(MapVector* key_val_pairs);
     50   virtual bool ParseSection(SimpleMap* key_val_pair);
     51   virtual bool ParseLine(std::string* key, std::string* value);
     52 
     53  private:
     54   scoped_ptr<StreamInterface> instream_;
     55 };
     56 
     57 //////////////////////////////////////////////////////////////////////////////
     58 // ProcCpuInfo reads CPU info from the /proc subsystem on any *NIX platform.
     59 //////////////////////////////////////////////////////////////////////////////
     60 
     61 // Sample Usage:
     62 //   ProcCpuInfo proc_info;
     63 //   int no_of_cpu;
     64 //   if (proc_info.LoadFromSystem()) {
     65 //      std::string out_str;
     66 //      proc_info.GetNumCpus(&no_of_cpu);
     67 //      proc_info.GetCpuStringValue(0, "vendor_id", &out_str);
     68 //      }
     69 //   }
     70 
     71 class ProcCpuInfo {
     72  public:
     73   ProcCpuInfo();
     74   virtual ~ProcCpuInfo();
     75 
     76   // Reads the proc subsystem's cpu info into memory. If this fails, this
     77   // returns false; if it succeeds, it returns true.
     78   virtual bool LoadFromSystem();
     79 
     80   // Obtains the number of logical CPU threads and places the value num.
     81   virtual bool GetNumCpus(int* num);
     82 
     83   // Obtains the number of physical CPU cores and places the value num.
     84   virtual bool GetNumPhysicalCpus(int* num);
     85 
     86   // Obtains the CPU family id.
     87   virtual bool GetCpuFamily(int* id);
     88 
     89   // Obtains the number of sections in /proc/cpuinfo, which may be greater
     90   // than the number of CPUs (e.g. on ARM)
     91   virtual bool GetSectionCount(size_t* count);
     92 
     93   // Looks for the CPU proc item with the given name for the given section
     94   // number and places the string value in result.
     95   virtual bool GetSectionStringValue(size_t section_num, const std::string& key,
     96                                      std::string* result);
     97 
     98   // Looks for the CPU proc item with the given name for the given section
     99   // number and places the int value in result.
    100   virtual bool GetSectionIntValue(size_t section_num, const std::string& key,
    101                                   int* result);
    102 
    103  private:
    104   ConfigParser::MapVector sections_;
    105 };
    106 
    107 // Returns the output of "uname".
    108 std::string ReadLinuxUname();
    109 
    110 // Returns the content (int) of
    111 // /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
    112 // Returns -1 on error.
    113 int ReadCpuMaxFreq();
    114 
    115 }  // namespace rtc
    116 
    117 #endif  // defined(WEBRTC_LINUX)
    118 #endif  // WEBRTC_BASE_LINUX_H_
    119