Skip to content

Commit 5b18ddf

Browse files
Merge pull request #1116 from sfregoso/system_resources
System resources
2 parents b844e63 + b36a66c commit 5b18ddf

22 files changed

+1277
-4
lines changed

.github/actions/spelling/expect.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ basestring
109109
bashcompinit
110110
Bassic
111111
batchmode
112+
bavail
112113
bbd
113114
bc
114115
bcm
@@ -497,6 +498,7 @@ FADV
497498
fadvise
498499
FAKELOGGER
499500
fallocate
501+
fallthrough
500502
fcheck
501503
fclose
502504
fcntl
@@ -555,6 +557,7 @@ fprintf
555557
fprofile
556558
fptr
557559
fputil
560+
Fregoso
558561
frontend
559562
frox
560563
frsize
@@ -777,6 +780,7 @@ isfxmlwriter
777780
isgenerator
778781
ishii
779782
isinstance
783+
isnan
780784
isoschematron
781785
isr
782786
ISREG
@@ -936,6 +940,7 @@ membername
936940
memcheck
937941
memcmp
938942
memcpy
943+
meminfo
939944
memname
940945
memoizing
941946
memset
@@ -1330,6 +1335,7 @@ saxutils
13301335
sbb
13311336
SBF
13321337
sbin
1338+
scanf
13331339
sched
13341340
schem
13351341
schematron
@@ -1382,6 +1388,7 @@ setval
13821388
setw
13831389
sev
13841390
sface
1391+
sfregoso
13851392
sgl
13861393
SGN
13871394
SHELLCOMMAND
@@ -1453,6 +1460,7 @@ startswith
14531460
startuml
14541461
startword
14551462
staticmethod
1463+
statfs
14561464
statvfs
14571465
stdarg
14581466
STDC
@@ -1719,6 +1727,7 @@ VERSIONED
17191727
versioning
17201728
vfd
17211729
VFILE
1730+
vfs
17221731
vhd
17231732
vhdl
17241733
viewforum
@@ -1729,6 +1738,8 @@ virtualization
17291738
virtualized
17301739
vlist
17311740
vm
1741+
vmstat
1742+
vmsize
17321743
VMIN
17331744
vsnprintf
17341745
VTIME

Os/Baremetal/SystemResources.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ======================================================================
2+
// \title Baremetal/SystemResources.cpp
3+
// \author mstarch
4+
// \brief hpp file for SystemResources component implementation class
5+
//
6+
// \copyright
7+
// Copyright 2021, by the California Institute of Technology.
8+
// ALL RIGHTS RESERVED. United States Government Sponsorship
9+
// acknowledged.
10+
//
11+
// ======================================================================
12+
13+
namespace Os {
14+
15+
SystemResources::SystemResourcesStatus SystemResources::getCpuCount(U32& cpuCount) {
16+
// Assumes 1 CPU
17+
cpuCount = 1;
18+
return SYSTEM_RESOURCES_OK;
19+
}
20+
21+
SystemResources::SystemResourcesStatus SystemResources::getCpuTicks(CpuTicks& cpu_ticks, U32 cpu_index) {
22+
// Always 100 percent
23+
cpu_ticks.used = 1;
24+
cpu_ticks.total = 1;
25+
return SYSTEM_RESOURCES_OK;
26+
}
27+
28+
29+
SystemResources::SystemResourcesStatus SystemResources::getMemUtil(MemUtil& memory_util) {
30+
U8 stack_allocation = 0;
31+
U8* heap_allocation = new U8;
32+
if (heap_allocation != NULL) {
33+
// Crude way to estimate memory usage: stack grows down, heap grows up. Stack - Heap = FREE bytes before collision
34+
U64 free = static_cast<U64>(&stack_allocation - heap_allocation);
35+
memory_util.total = free;
36+
memory_util.util = 0;
37+
delete heap_allocation;
38+
}
39+
return SYSTEM_RESOURCES_OK;
40+
}
41+
} // namespace Os

Os/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ endif()
5757
if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
5858
list(APPEND SOURCE_FILES
5959
"${CMAKE_CURRENT_LIST_DIR}/MacOs/IPCQueueStub.cpp"
60+
"${CMAKE_CURRENT_LIST_DIR}/MacOs/SystemResources.cpp"
6061
)
6162
# Linux IPC queues implementation
6263
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
6364
list(APPEND SOURCE_FILES
6465
"${CMAKE_CURRENT_LIST_DIR}/Posix/IPCQueue.cpp"
6566
"${CMAKE_CURRENT_LIST_DIR}/Posix/LocklessQueue.cpp"
67+
"${CMAKE_CURRENT_LIST_DIR}/Linux/SystemResources.cpp"
6668
)
6769
# Shared libraries need an -rt dependency for mq libs
6870
if (BUILD_SHARED_LIBS)
@@ -75,12 +77,13 @@ endif()
7577
if (FPRIME_USE_BAREMETAL_SCHEDULER)
7678
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/Baremetal/TaskRunner/")
7779
foreach (ITER_ITEM IN LISTS SOURCE_FILES)
78-
if (ITER_ITEM MATCHES "Task\.cpp$")
80+
if (ITER_ITEM MATCHES "Task\\.cpp$")
7981
list(REMOVE_ITEM SOURCE_FILES "${ITER_ITEM}")
8082
endif()
8183
endforeach()
8284
list(APPEND SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Baremetal/Task.cpp")
8385
list(APPEND SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Baremetal/Mutex.cpp")
86+
list(APPEND SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Baremetal/SystemResources.cpp")
8487
endif()
8588
register_fprime_module()
8689

@@ -97,6 +100,7 @@ set(UT_SOURCE_FILES
97100
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsValidateFileTest.cpp"
98101
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsTaskTest.cpp"
99102
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsFileSystemTest.cpp"
103+
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsSystemResourcesTest.cpp"
100104
)
101105
register_fprime_ut()
102106

Os/Linux/SystemResources.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// ======================================================================
2+
// \title Linux/SystemResources.cpp
3+
// \author sfregoso
4+
// \brief hpp file for SystemResources component implementation class
5+
//
6+
// \copyright
7+
// Copyright 2021, by the California Institute of Technology.
8+
// ALL RIGHTS RESERVED. United States Government Sponsorship
9+
// acknowledged.
10+
//
11+
// ======================================================================
12+
#include <stdio.h> /* fopen() */
13+
#include <stdlib.h> /* scanf */
14+
#include <sys/vfs.h> /* statfs() */
15+
#include <string.h>
16+
#include <Os/SystemResources.hpp>
17+
#include <Fw/Types/Assert.hpp>
18+
19+
namespace Os {
20+
static const U32 LINUX_CPU_LINE_LIMIT = 1024; // Maximum lines to read before bailing
21+
22+
SystemResources::SystemResourcesStatus SystemResources::getCpuCount(U32 &cpuCount) {
23+
char line[512] = {0};
24+
FILE *fp = NULL;
25+
U32 cpu_count = 0;
26+
27+
if ((fp = fopen("/proc/stat", "r")) == NULL) {
28+
return SYSTEM_RESOURCES_ERROR;
29+
}
30+
31+
if (fgets(line, sizeof(line), fp) == NULL) { //1st line. Aggregate cpu line. Skip
32+
fclose(fp);
33+
return SYSTEM_RESOURCES_ERROR;
34+
}
35+
36+
for (U32 i = 0; i < LINUX_CPU_LINE_LIMIT; i++) {
37+
if (fgets(line, sizeof(line), fp) == NULL) { //cpu# line
38+
break;
39+
}
40+
41+
if (!(line[0] == 'c' && line[1] == 'p' && line[2] == 'u')) {
42+
break;
43+
}
44+
cpu_count++;
45+
}
46+
fclose(fp);
47+
cpuCount = cpu_count;
48+
49+
return SYSTEM_RESOURCES_OK;
50+
}
51+
52+
SystemResources::SystemResourcesStatus SystemResources::getCpuTicks(CpuTicks &cpu_ticks, U32 cpu_index) {
53+
char line[512] = {0};
54+
FILE *fp = NULL;
55+
U32 cpu_data[4] = {0};
56+
U32 cpuCount = 0;
57+
SystemResources::SystemResourcesStatus status = SYSTEM_RESOURCES_ERROR;
58+
U64 cpuUsed = 0;
59+
U64 cpuTotal = 0;
60+
61+
if ((status = getCpuCount(cpuCount)) != SYSTEM_RESOURCES_OK) {
62+
return status;
63+
}
64+
65+
if (cpu_index >= cpuCount) {
66+
return SYSTEM_RESOURCES_ERROR;
67+
}
68+
69+
if ((fp = fopen("/proc/stat", "r")) == NULL) {
70+
71+
return SYSTEM_RESOURCES_ERROR;
72+
73+
}
74+
75+
if (fgets(line, sizeof(line), fp) == NULL) { //1st line. Aggregate cpu line.
76+
fclose(fp);
77+
return SYSTEM_RESOURCES_ERROR;
78+
}
79+
80+
for (U32 i = 0; i < cpu_index + 1; i++) {
81+
if (fgets(line, sizeof(line), fp) == NULL) { //cpu# line
82+
fclose(fp);
83+
return SYSTEM_RESOURCES_ERROR;
84+
}
85+
if (i != cpu_index) { continue; }
86+
87+
if (!(line[0] == 'c' && line[1] == 'p' && line[2] == 'u')) {
88+
fclose(fp);
89+
return SYSTEM_RESOURCES_ERROR;
90+
}
91+
92+
sscanf(line, "%*s %d %d %d %d", &cpu_data[0],
93+
&cpu_data[1],
94+
&cpu_data[2],
95+
&cpu_data[3]); //cpu#: 4 numbers: usr, nice, sys, idle
96+
97+
cpuUsed = cpu_data[0] + cpu_data[1] + cpu_data[2];
98+
cpuTotal = cpu_data[0] + cpu_data[1] + cpu_data[2] + cpu_data[3];
99+
100+
break;
101+
}
102+
fclose(fp);
103+
104+
cpu_ticks.used = cpuUsed;
105+
cpu_ticks.total = cpuTotal;
106+
return SYSTEM_RESOURCES_OK;
107+
}
108+
109+
SystemResources::SystemResourcesStatus SystemResources::getMemUtil(MemUtil &memory_util) {
110+
FILE *fp = NULL;
111+
NATIVE_INT_TYPE total = 0;
112+
NATIVE_INT_TYPE free = 0;
113+
// Fallbacks
114+
memory_util.total = 1;
115+
memory_util.used = 1;
116+
117+
fp = fopen("/proc/meminfo", "r");
118+
if (fp == NULL) {
119+
return SYSTEM_RESOURCES_ERROR;
120+
}
121+
122+
if (fscanf(fp, "%*s %d %*s", &total) != 1 || /* 1st line is MemTotal */
123+
fscanf(fp, "%*s %d", &free) != 1) { /* 2nd line is MemFree */
124+
fclose(fp);
125+
return SYSTEM_RESOURCES_ERROR;
126+
}
127+
fclose(fp);
128+
129+
// Check results
130+
if (total < 0 or free < 0 or total < free) {
131+
return SYSTEM_RESOURCES_ERROR;
132+
}
133+
memory_util.total = static_cast<U64>(total) * 1024; // KB to Bytes
134+
memory_util.used = static_cast<U64>(total - free) * 1024;
135+
return SYSTEM_RESOURCES_OK;
136+
}
137+
}

0 commit comments

Comments
 (0)