F´ Flight Software - C/C++ Documentation  devel
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 <Os/Linux/Cpu.hpp>
6 #include <Fw/Types/Assert.hpp>
8 #include <Os/File.hpp>
9 #include <unistd.h>
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
27  USER = 1,
28  NICE = 2,
29  SYSTEM = 3,
30  IDLE = 4,
32 };
33 
34 
36 constexpr FwSizeType LINE_SIZE = 255; // log10(max(U64)) * 11 fields (kernel 2.6.33) = 220. Round to 256 - 1 (\0)
37 
39  Os::File file;
40  // Open the procfs file
41  constexpr char PROC_STAT_PATH[] = "/proc/stat";
42  if (file.open(PROC_STAT_PATH, Os::File::Mode::OPEN_READ) != Os::File::Status::OP_OK) {
43 
45  }
46  char proc_stat_line[LINE_SIZE + 1];
47  // File starts with cpu line, then individual CPUs.
48  for (FwSizeType i = 0; i < cpu_index + 2; i++) {
49  FwSignedSizeType read_size = sizeof proc_stat_line - 1;
50  Os::File::Status file_status = file.readline(reinterpret_cast<U8*>(proc_stat_line), read_size,
51  Os::File::WaitType::NO_WAIT);
52  proc_stat_line[read_size + 1] = '\0'; // Null terminate
53  if (file_status != Os::File::Status::OP_OK) {
55  }
56  // Make sure we've not strayed passed the cpu section
57  if (::strncmp(proc_stat_line, "cpu", 3) != 0) {
59  }
60  }
61  char* token_start = proc_stat_line + 3; // Length of "cpu"
62  for (FwSizeType i = 0; i < ProcCpuMeasures::MAX_CPU_TICK_TYPES; i++) {
63  FwSizeType token = 0;
65  token_start, static_cast<FwSizeType>(sizeof proc_stat_line) - static_cast<FwSizeType>(token_start - proc_stat_line), token, &token_start);
66  // Check conversion success
69  }
70  data[i] = token;
71  // Check cpu index is correct
72  if (i == ProcCpuMeasures::CPU_NUMBER and data[i] != cpu_index) {
74  }
75  }
77 }
78 
80  long cpus = sysconf(_SC_NPROCESSORS_ONLN);
81  if ((cpus > 0) && (static_cast<FwSizeType>(cpus) < std::numeric_limits<FwSizeType>::max())) {
82  cpu_count = static_cast<FwSizeType>(cpus);
83  return Status::OP_OK;
84  }
85  cpu_count = 0;
86  return Status::ERROR;
87 }
88 
90  FwSizeType count = 0;
91  CpuInterface::Status status = this->_getCount(count);
92  if (status != CpuInterface::Status::OP_OK) {
93  return status;
94  } else if (cpu_index >= count) {
96  }
97  ProcCpuData cpu_data = {0, 0, 0, 0, 0, 0, 0, 0};
98  status = getCpuData(cpu_index, cpu_data);
99  ticks.used = cpu_data[ProcCpuMeasures::USER] + cpu_data[ProcCpuMeasures::NICE] + cpu_data[ProcCpuMeasures::SYSTEM];
100  ticks.total = ticks.used + cpu_data[ProcCpuMeasures::IDLE];
101 
102  return status;
103 }
104 
106  return &this->m_handle;
107 }
108 
109 } // namespace Cpu
110 } // namespace Linux
111 } // namespace Os
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
Cpu variable handle parent.
Definition: Cpu.hpp:13
cpu implementation
Definition: Cpu.hpp:60
Status readline(U8 *buffer, FwSignedSizeType &size, WaitType wait)
read a line from the file using \n as the delimiter
Definition: File.cpp:237
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:45
Status _getCount(FwSizeType &cpu_count) override
Request the count of the CPUs detected by the system.
Definition: Cpu.cpp:79
CpuHandle * getHandle() override
returns the raw console handle
Definition: Cpu.cpp:105
Status _getTicks(Os::Cpu::Ticks &ticks, FwSizeType cpu_index) override
Get the CPU tick information for a given CPU.
Definition: Cpu.cpp:89
@ SUCCESSFUL_CONVERSION
Output should be valid.
Definition: StringUtils.hpp:48
StringToNumberStatus string_to_number(const CHAR *input, FwSizeType buffer_size, U8 &output, char **next, U8 base=0)
converts a string to a U8
Status
Generic OK/ERROR status.
Definition: Os.hpp:25
@ OP_OK
Operation succeeded.
Definition: Os.hpp:26
@ ERROR
Operation failed.
Definition: Os.hpp:27
CpuInterface::Status getCpuData(FwSizeType cpu_index, ProcCpuData data)
Definition: Cpu.cpp:38
@ CPU_NUMBER
Definition: Cpu.cpp:26
@ MAX_CPU_TICK_TYPES
Definition: Cpu.cpp:31
FwSizeType[ProcCpuMeasures::MAX_CPU_TICK_TYPES] ProcCpuData
Definition: Cpu.cpp:35
constexpr FwSizeType LINE_SIZE
Definition: Cpu.cpp:36
Generic used/total struct.
Definition: Os.hpp:31
FwSizeType total
Total amount.
Definition: Os.hpp:33
FwSizeType used
Used amount.
Definition: Os.hpp:32