Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2011, 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 #ifdef LINUX
     32 #include <string>
     33 #include <map>
     34 #include <vector>
     35 #include "talk/base/stream.h"
     36 
     37 namespace talk_base {
     38 
     39 //////////////////////////////////////////////////////////////////////////////
     40 // ConfigParser parses a FileStream of an ".ini."-type format into a map.
     41 //////////////////////////////////////////////////////////////////////////////
     42 
     43 // Sample Usage:
     44 //   ConfigParser parser;
     45 //   ConfigParser::MapVector key_val_pairs;
     46 //   if (parser.Open(inifile) && parser.Parse(&key_val_pairs)) {
     47 //     for (int section_num=0; i < key_val_pairs.size(); ++section_num) {
     48 //       std::string val1 = key_val_pairs[section_num][key1];
     49 //       std::string val2 = key_val_pairs[section_num][key2];
     50 //       // Do something with valn;
     51 //     }
     52 //   }
     53 
     54 class ConfigParser {
     55  public:
     56   typedef std::map<std::string, std::string> SimpleMap;
     57   typedef std::vector<SimpleMap> MapVector;
     58 
     59   ConfigParser();
     60   virtual ~ConfigParser();
     61 
     62   virtual bool Open(const std::string& filename);
     63   virtual void Attach(StreamInterface* stream);
     64   virtual bool Parse(MapVector *key_val_pairs);
     65   virtual bool ParseSection(SimpleMap *key_val_pair);
     66   virtual bool ParseLine(std::string *key, std::string *value);
     67 
     68  private:
     69   scoped_ptr<StreamInterface> instream_;
     70 };
     71 
     72 //////////////////////////////////////////////////////////////////////////////
     73 // ProcCpuInfo reads CPU info from the /proc subsystem on any *NIX platform.
     74 //////////////////////////////////////////////////////////////////////////////
     75 
     76 // Sample Usage:
     77 //   ProcCpuInfo proc_info;
     78 //   int no_of_cpu;
     79 //   if (proc_info.LoadFromSystem()) {
     80 //      std::string out_str;
     81 //      proc_info.GetNumCpus(&no_of_cpu);
     82 //      proc_info.GetCpuStringValue(0, "vendor_id", &out_str);
     83 //      }
     84 //   }
     85 
     86 class ProcCpuInfo {
     87  public:
     88   ProcCpuInfo();
     89   virtual ~ProcCpuInfo();
     90 
     91   // Reads the proc subsystem's cpu info into memory. If this fails, this
     92   // returns false; if it succeeds, it returns true.
     93   virtual bool LoadFromSystem();
     94 
     95   // Obtains the number of logical CPU threads and places the value num.
     96   virtual bool GetNumCpus(int *num);
     97 
     98   // Obtains the number of physical CPU cores and places the value num.
     99   virtual bool GetNumPhysicalCpus(int *num);
    100 
    101   // Looks for the CPU proc item with the given name for the given CPU number
    102   // and places the string value in result.
    103   virtual bool GetCpuStringValue(int cpu_id, const std::string& key,
    104                                  std::string *result);
    105 
    106   // Looks for the CPU proc item with the given name for the given CPU number
    107   // and places the int value in result.
    108   virtual bool GetCpuIntValue(int cpu_id, const std::string& key,
    109                               int *result);
    110 
    111  private:
    112   ConfigParser::MapVector cpu_info_;
    113 };
    114 
    115 // Builds a string containing the info from lsb_release on a single line.
    116 std::string ReadLinuxLsbRelease();
    117 
    118 // Returns the output of "uname".
    119 std::string ReadLinuxUname();
    120 
    121 // Returns the content (int) of
    122 // /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
    123 // Returns -1 on error.
    124 int ReadCpuMaxFreq();
    125 
    126 }  // namespace talk_base
    127 
    128 #endif  // LINUX
    129 #endif  // TALK_BASE_LINUX_H_
    130