cpuInfo.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2017 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "cpuInfo.H"
29 #include "IOstreams.H"
30 
31 #include <fstream>
32 
33 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
34 
35 // file-scope function
36 // split things like "a key word\t: value information"
37 // into ("a_key_word", "value information")
38 //
39 static bool split(const std::string& line, std::string& key, std::string& val)
40 {
41  key.clear();
42  val.clear();
43 
44  const auto keyLen = line.find_first_of("\t:");
45  const auto sep = line.find(':');
46 
47  if (keyLen == std::string::npos || sep == std::string::npos)
48  {
49  return false;
50  }
51 
52  const auto begVal = line.find_first_not_of(" :", sep);
53 
54  if (begVal == std::string::npos)
55  {
56  return false;
57  }
58 
59  key = line.substr(0, keyLen);
60  val = line.substr(begVal);
61 
62  // Avoid spaces in key - replace with '_'
63  for (auto iter = key.begin(); iter < key.end(); ++iter)
64  {
65  if (*iter == ' ')
66  {
67  *iter = '_';
68  }
69  }
70 
71  // std::cerr<<"key=<" << key << "> val=<" << val << ">\n";
72 
73  return true;
74 }
75 
76 
77 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
78 
79 // Parse the following type of content.
80 // A TAB separates the keyword from content. Eg,
81 //
82 // "cpu cores\t: 6"
83 //
84 // ===========================
85 // processor : 0
86 // vendor_id : GenuineIntel
87 // cpu family : 6
88 // model : 63
89 // model name : Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz
90 // stepping : 2
91 // microcode : 0x35
92 // cpu MHz : 1200.000
93 // cache size : 15360 KB
94 // physical id : 0
95 // siblings : 12
96 // core id : 0
97 // cpu cores : 6
98 // apicid : 0
99 // initial apicid : 0
100 // fpu : yes
101 // fpu_exception : yes
102 // cpuid level : 15
103 // wp : yes
104 // flags : fpu vme ...
105 // bugs :
106 // bogomips : 4789.15
107 // clflush size : 64
108 // cache_alignment : 64
109 // address sizes : 46 bits physical, 48 bits virtual
110 // power management:
111 
112 void Foam::cpuInfo::parse()
113 {
114  int ncpu = 0;
115  std::string line, key, val;
116 
117  std::ifstream is("/proc/cpuinfo");
118  while (is.good() && std::getline(is, line))
119  {
120  if (!split(line, key, val))
121  {
122  continue;
123  }
124 
125  if (key == "processor")
126  {
127  if (ncpu++)
128  {
129  break; // stop after the first cpu
130  }
131  }
132  else if (key == "vendor_id") { vendor_id = val; }
133  else if (key == "model_name") { model_name = val; }
134  else if (key == "cpu_family") { cpu_family = std::stoi(val); }
135  else if (key == "model") { model = std::stoi(val); }
136  else if (key == "cpu_MHz") { cpu_MHz = std::stof(val); }
137  else if (key == "cpu_cores") { cpu_cores = std::stoi(val); }
138  else if (key == "siblings") { siblings = std::stoi(val); }
139  }
140 }
141 
142 
143 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
144 
146 :
147  vendor_id(),
148  model_name(),
149  cpu_family(-1),
150  model(-1),
151  cpu_MHz(0),
152  siblings(0),
153  cpu_cores(0)
154 {
155  parse();
156 }
157 
158 
159 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
160 
161 void Foam::cpuInfo::write(Ostream& os) const
162 {
163  if (!vendor_id.empty())
164  {
165  os.writeEntry("vendor_id", vendor_id);
166  }
167  if (!model_name.empty())
168  {
169  os.writeEntry("model_name", model_name);
170  }
171 
172  os.writeEntryIfDifferent<int>("cpu_family", -1, cpu_family);
173  os.writeEntryIfDifferent<int>("model", -1, model);
174  os.writeEntryIfDifferent<float>("cpu_MHz", 0, cpu_MHz);
175  os.writeEntryIfDifferent<int>("cpu_cores", 0, cpu_cores);
176  os.writeEntryIfDifferent<int>("siblings", 0, siblings);
177 }
178 
179 
180 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
cpuInfo.H
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
os
OBJstream os(runTime.globalPath()/outputName)
Foam::cpuInfo::cpuInfo
cpuInfo()
Construct and populate with information.
Definition: cpuInfo.C:37
split
static bool split(const std::string &line, std::string &key, std::string &val)
Definition: cpuInfo.C:39
Foam::cpuInfo::write
void write(Ostream &os) const
Write content as dictionary entries.
Definition: cpuInfo.C:51