JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
SysInfo.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #include <jevois/Debug/SysInfo.H>
19 #include <jevois/Debug/Log.H>
20 #include <fstream>
21 #include <algorithm>
22 
23 namespace
24 {
25  std::string getFileString(char const * fname, int skip = 0)
26  {
27  std::ifstream ifs(fname);
28  if (ifs.is_open() == false) LFATAL("Cannot read file: " << fname);
29  std::string str;
30  while (skip-- >= 0) std::getline(ifs, str);
31  ifs.close();
32 
33  return str;
34  }
35 }
36 
37 // ####################################################################################################
38 std::string jevois::getSysInfoCPU()
39 {
40  int freq = std::stoi(getFileString("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")) / 1000;
41  int temp = std::stoi(getFileString("/sys/class/thermal/thermal_zone0/temp"));
42 
43  // On some hosts, temp is in millidegrees:
44  if (temp > 200) temp /= 1000;
45 
46  std::string load = getFileString("/proc/loadavg");
47  return "CPU: " + std::to_string(freq) + "MHz, " + std::to_string(temp) + "C, load: " + load;
48 }
49 
50 // ####################################################################################################
51 namespace
52 {
53  // from http://stackoverflow.com/questions/8362094/replace-multiple-spaces-with-one-space-in-a-string
54  bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
55 
56  void cleanSpaces(std::string & str)
57  {
58  std::string::iterator new_end = std::unique(str.begin(), str.end(), BothAreSpaces);
59  str.erase(new_end, str.end());
60  }
61 }
62 
63 // ####################################################################################################
64 std::string jevois::getSysInfoMem()
65 {
66  std::string memtotal = getFileString("/proc/meminfo"); cleanSpaces(memtotal);
67  std::string memfree = getFileString("/proc/meminfo", 1); cleanSpaces(memfree);
68  return memtotal + ", " + memfree;
69 }
70 
71 // ####################################################################################################
73 {
74  std::string ver = getFileString("/proc/version");
75 
76  // Truncate at "Linux Version XXX":
77  size_t pos = ver.find(' ');
78  pos = ver.find(' ', pos + 1);
79  pos = ver.find(' ', pos + 1);
80 
81  return ver.substr(0, pos);
82 }
std::string getSysInfoVersion()
Get O.S. version info.
Definition: SysInfo.C:72
std::string getSysInfoCPU()
Get CPU info: frequency, thermal, load.
Definition: SysInfo.C:38
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:212
std::string to_string(T const &val)
Convert from type to string.
Definition: UtilsImpl.H:51
std::string getSysInfoMem()
Get memory info.
Definition: SysInfo.C:64