Home | History | Annotate | Download | only in base
      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 
     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 
     39 namespace 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 
     56 class 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 
     88 class 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 
    124 // Returns the output of "uname".
    125 std::string ReadLinuxUname();
    126 
    127 // Returns the content (int) of
    128 // /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
    129 // Returns -1 on error.
    130 int ReadCpuMaxFreq();
    131 
    132 }  // namespace talk_base
    133 
    134 #endif  // defined(LINUX) || defined(ANDROID)
    135 #endif  // TALK_BASE_LINUX_H_
    136