F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Cpu.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Linux/Cpu.cpp
3 // \brief Linux implementation for Os::Cpu
4 // ======================================================================
5 #include <unistd.h>
6 #include <Fw/Types/Assert.hpp>
8 #include <Os/File.hpp>
9 #include <Os/Linux/Cpu.hpp>
10 #include <cstring>
11 namespace Os {
12 namespace Linux {
13 namespace Cpu {
14 
15 // Proc FS /proc/stat file format example:
16 //
17 // cpu 270288 1598 88660 8470416 15185 9775 2990 867 0 0
18 // cpu0 67462 108 22104 2118172 3779 2540 648 287 0 0
19 // cpu1 74237 650 24059 2109680 2995 2447 667 93 0 0
20 // cpu2 69388 630 22033 2115177 4428 2424 649 378 0 0
21 // cpu3 59199 209 20462 2127387 3981 2363 1024 108 0 0
22 // ...
23 
24 // Needed format, and compliant with kernel < 2.5
25 enum ProcCpuMeasures { CPU_NUMBER = 0, USER = 1, NICE = 2, SYSTEM = 3, IDLE = 4, MAX_CPU_TICK_TYPES = 8 };
26 
28 constexpr FwSizeType LINE_SIZE = 255; // log10(max(U64)) * 11 fields (kernel 2.6.33) = 220. Round to 256 - 1 (\0)
29 
31  Os::File file;
32  // Open the procfs file
33  constexpr char PROC_STAT_PATH[] = "/proc/stat";
34  if (file.open(PROC_STAT_PATH, Os::File::Mode::OPEN_READ) != Os::File::Status::OP_OK) {
36  }
37  char proc_stat_line[LINE_SIZE + 1];
38  // File starts with cpu line, then individual CPUs.
39  for (FwSizeType i = 0; i < cpu_index + 2; i++) {
40  FwSizeType read_size = sizeof proc_stat_line - 1;
41  Os::File::Status file_status =
42  file.readline(reinterpret_cast<U8*>(proc_stat_line), read_size, Os::File::WaitType::NO_WAIT);
43  proc_stat_line[read_size + 1] = '\0'; // Null terminate
44  if (file_status != Os::File::Status::OP_OK) {
46  }
47  // Make sure we've not strayed passed the cpu section
48  if (::strncmp(proc_stat_line, "cpu", 3) != 0) {
50  }
51  }
52  char* token_start = proc_stat_line + 3; // Length of "cpu"
53  for (FwSizeType i = 0; i < ProcCpuMeasures::MAX_CPU_TICK_TYPES; i++) {
54  FwSizeType token = 0;
56  token_start,
57  static_cast<FwSizeType>(sizeof proc_stat_line) - static_cast<FwSizeType>(token_start - proc_stat_line),
58  token, &token_start);
59  // Check conversion success
62  }
63  data[i] = token;
64  // Check cpu index is correct
65  if (i == ProcCpuMeasures::CPU_NUMBER and data[i] != cpu_index) {
67  }
68  }
70 }
71 
73  long cpus = sysconf(_SC_NPROCESSORS_ONLN);
74  if ((cpus > 0) && (static_cast<FwSizeType>(cpus) < std::numeric_limits<FwSizeType>::max())) {
75  cpu_count = static_cast<FwSizeType>(cpus);
76  return Status::OP_OK;
77  }
78  cpu_count = 0;
79  return Status::ERROR;
80 }
81 
83  FwSizeType count = 0;
84  CpuInterface::Status status = this->_getCount(count);
85  if (status != CpuInterface::Status::OP_OK) {
86  return status;
87  } else if (cpu_index >= count) {
89  }
90  ProcCpuData cpu_data = {0, 0, 0, 0, 0, 0, 0, 0};
91  status = getCpuData(cpu_index, cpu_data);
92  ticks.used = cpu_data[ProcCpuMeasures::USER] + cpu_data[ProcCpuMeasures::NICE] + cpu_data[ProcCpuMeasures::SYSTEM];
93  ticks.total = ticks.used + cpu_data[ProcCpuMeasures::IDLE];
94 
95  return status;
96 }
97 
99  return &this->m_handle;
100 }
101 
102 } // namespace Cpu
103 } // namespace Linux
104 } // namespace Os
Operation succeeded.
Definition: Os.hpp:26
PlatformSizeType FwSizeType
cpu implementation
Definition: Cpu.hpp:60
Operation failed.
Definition: Os.hpp:27
Status _getCount(FwSizeType &cpu_count) override
Request the count of the CPUs detected by the system.
Definition: Cpu.cpp:72
FwSizeType[ProcCpuMeasures::MAX_CPU_TICK_TYPES] ProcCpuData
Definition: Cpu.cpp:27
StringToNumberStatus string_to_number(const CHAR *input, FwSizeType buffer_size, U8 &output, char **next, U8 base=0)
converts a string to a U8
Cpu variable handle parent.
Definition: Cpu.hpp:13
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
CpuHandle * getHandle() override
returns the raw console handle
Definition: Cpu.cpp:98
Status
Generic OK/ERROR status.
Definition: Os.hpp:25
constexpr FwSizeType LINE_SIZE
Definition: Cpu.cpp:28
Status _getTicks(Os::Cpu::Ticks &ticks, FwSizeType cpu_index) override
Get the CPU tick information for a given CPU.
Definition: Cpu.cpp:82
CpuInterface::Status getCpuData(FwSizeType cpu_index, ProcCpuData data)
Definition: Cpu.cpp:30
Status readline(U8 *buffer, FwSizeType &size, WaitType wait)
read a line from the file using \n as the delimiter
Definition: File.cpp:255
FwSizeType total
Total amount.
Definition: Os.hpp:33
FwSizeType used
Used amount.
Definition: Os.hpp:32
Generic used/total struct.
Definition: Os.hpp:31