Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System resources #1116

Merged
merged 25 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2430c1c
Added function for getting system resources for Linux
Oct 22, 2021
54d161d
Change data types; added functions per Mike's request
Oct 25, 2021
8e23903
Add some works into spelling list so recognized by spelling bot
Oct 25, 2021
0d7a639
handled return value sfor gets and scanf
Oct 25, 2021
127e312
Added SystemResources component to Svc
Nov 8, 2021
0a36e1a
Updated UT and added some member variables
Nov 8, 2021
c0a72b9
Added version, enable cmd logit and updated UT
Nov 9, 2021
6e0260f
Added version.hpp creation and dependecies
Nov 9, 2021
c63a723
Added stubs of SystemResources for MacOs and Baremetals
Nov 10, 2021
ecce067
Rid unused member variables and documented member variables
Nov 10, 2021
f7f3646
Corrected spelling and added words to expect.txt
Nov 10, 2021
cb2f221
Fixed more spelling and added to allow list. Fixed format issues with…
Nov 10, 2021
04c9d9e
Renamed generate version script and fixed a few format issues
Nov 10, 2021
5df2e03
Another format change to please CI
Nov 10, 2021
9bf4737
Another format change to please CI
Nov 10, 2021
f6ffd3c
Changed git query command format
Nov 10, 2021
8ac0b3b
Changed git query command format
Nov 10, 2021
538a0ed
Added --always to git tag request; Added format info to percent telem…
Nov 15, 2021
e05718a
lestarch: adding macOS implementation of system resources; cleaning A…
LeStarch Nov 18, 2021
df27c92
lestarch: fixing up resources for Linux
LeStarch Nov 19, 2021
b58c014
lestarch: allowing fallthrough in UT
LeStarch Nov 19, 2021
edd2597
lestarch: more build system appeasment
LeStarch Nov 19, 2021
ea8ae3d
lestarch: code review fixes
LeStarch Nov 19, 2021
771d67b
lestarch: nullptr -> NULL for v2
LeStarch Nov 19, 2021
b36a66c
lestarch: switching to NULL for v2
LeStarch Nov 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ basestring
bashcompinit
Bassic
batchmode
bavail
bbd
bc
bcm
Expand Down Expand Up @@ -497,6 +498,7 @@ FADV
fadvise
FAKELOGGER
fallocate
fallthrough
fcheck
fclose
fcntl
Expand Down Expand Up @@ -555,6 +557,7 @@ fprintf
fprofile
fptr
fputil
Fregoso
frontend
frox
frsize
Expand Down Expand Up @@ -777,6 +780,7 @@ isfxmlwriter
isgenerator
ishii
isinstance
isnan
isoschematron
isr
ISREG
Expand Down Expand Up @@ -936,6 +940,7 @@ membername
memcheck
memcmp
memcpy
meminfo
memname
memoizing
memset
Expand Down Expand Up @@ -1330,6 +1335,7 @@ saxutils
sbb
SBF
sbin
scanf
sched
schem
schematron
Expand Down Expand Up @@ -1382,6 +1388,7 @@ setval
setw
sev
sface
sfregoso
sgl
SGN
SHELLCOMMAND
Expand Down Expand Up @@ -1453,6 +1460,7 @@ startswith
startuml
startword
staticmethod
statfs
statvfs
stdarg
STDC
Expand Down Expand Up @@ -1719,6 +1727,7 @@ VERSIONED
versioning
vfd
VFILE
vfs
vhd
vhdl
viewforum
Expand All @@ -1729,6 +1738,8 @@ virtualization
virtualized
vlist
vm
vmstat
vmsize
VMIN
vsnprintf
VTIME
Expand Down
41 changes: 41 additions & 0 deletions Os/Baremetal/SystemResources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// ======================================================================
// \title Baremetal/SystemResources.cpp
// \author mstarch
// \brief hpp file for SystemResources component implementation class
//
// \copyright
// Copyright 2021, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================

namespace Os {

SystemResources::SystemResourcesStatus SystemResources::getCpuCount(U32& cpuCount) {
// Assumes 1 CPU
cpuCount = 1;
return SYSTEM_RESOURCES_OK;
}

SystemResources::SystemResourcesStatus SystemResources::getCpuTicks(CpuTicks& cpu_ticks, U32 cpu_index) {
// Always 100 percent
cpu_ticks.used = 1;
cpu_ticks.total = 1;
return SYSTEM_RESOURCES_OK;
}


SystemResources::SystemResourcesStatus SystemResources::getMemUtil(MemUtil& memory_util) {
U8 stack_allocation = 0;
U8* heap_allocation = new U8;
LeStarch marked this conversation as resolved.
Show resolved Hide resolved
if (heap_allocation != NULL) {
// Crude way to estimate memory usage: stack grows down, heap grows up. Stack - Heap = FREE bytes before collision
U64 free = static_cast<U64>(&stack_allocation - heap_allocation);
memory_util.total = free;
memory_util.util = 0;
delete heap_allocation;
}
return SYSTEM_RESOURCES_OK;
}
} // namespace Os
6 changes: 5 additions & 1 deletion Os/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
list(APPEND SOURCE_FILES
"${CMAKE_CURRENT_LIST_DIR}/MacOs/IPCQueueStub.cpp"
"${CMAKE_CURRENT_LIST_DIR}/MacOs/SystemResources.cpp"
)
# Linux IPC queues implementation
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
list(APPEND SOURCE_FILES
"${CMAKE_CURRENT_LIST_DIR}/Posix/IPCQueue.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Posix/LocklessQueue.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Linux/SystemResources.cpp"
)
# Shared libraries need an -rt dependency for mq libs
if (BUILD_SHARED_LIBS)
Expand All @@ -75,12 +77,13 @@ endif()
if (FPRIME_USE_BAREMETAL_SCHEDULER)
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/Baremetal/TaskRunner/")
foreach (ITER_ITEM IN LISTS SOURCE_FILES)
if (ITER_ITEM MATCHES "Task\.cpp$")
if (ITER_ITEM MATCHES "Task\\.cpp$")
list(REMOVE_ITEM SOURCE_FILES "${ITER_ITEM}")
endif()
endforeach()
list(APPEND SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Baremetal/Task.cpp")
list(APPEND SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Baremetal/Mutex.cpp")
list(APPEND SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Baremetal/SystemResources.cpp")
endif()
register_fprime_module()

Expand All @@ -97,6 +100,7 @@ set(UT_SOURCE_FILES
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsValidateFileTest.cpp"
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsTaskTest.cpp"
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsFileSystemTest.cpp"
"${CMAKE_CURRENT_LIST_DIR}/test/ut/OsSystemResourcesTest.cpp"
)
register_fprime_ut()

Expand Down
137 changes: 137 additions & 0 deletions Os/Linux/SystemResources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// ======================================================================
// \title Linux/SystemResources.cpp
// \author sfregoso
// \brief hpp file for SystemResources component implementation class
//
// \copyright
// Copyright 2021, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================
#include <stdio.h> /* fopen() */
#include <stdlib.h> /* scanf */
#include <sys/vfs.h> /* statfs() */
#include <string.h>
#include <Os/SystemResources.hpp>
#include <Fw/Types/Assert.hpp>

namespace Os {
static const U32 LINUX_CPU_LINE_LIMIT = 1024; // Maximum lines to read before bailing

SystemResources::SystemResourcesStatus SystemResources::getCpuCount(U32 &cpuCount) {
char line[512] = {0};
FILE *fp = NULL;
U32 cpu_count = 0;

if ((fp = fopen("/proc/stat", "r")) == NULL) {
return SYSTEM_RESOURCES_ERROR;
}

if (fgets(line, sizeof(line), fp) == NULL) { //1st line. Aggregate cpu line. Skip
fclose(fp);
return SYSTEM_RESOURCES_ERROR;
}

for (U32 i = 0; i < LINUX_CPU_LINE_LIMIT; i++) {
if (fgets(line, sizeof(line), fp) == NULL) { //cpu# line
break;
}

if (!(line[0] == 'c' && line[1] == 'p' && line[2] == 'u')) {
break;
}
cpu_count++;
}
fclose(fp);
cpuCount = cpu_count;

return SYSTEM_RESOURCES_OK;
}

SystemResources::SystemResourcesStatus SystemResources::getCpuTicks(CpuTicks &cpu_ticks, U32 cpu_index) {
char line[512] = {0};
FILE *fp = NULL;
U32 cpu_data[4] = {0};
U32 cpuCount = 0;
SystemResources::SystemResourcesStatus status = SYSTEM_RESOURCES_ERROR;
U64 cpuUsed = 0;
U64 cpuTotal = 0;

if ((status = getCpuCount(cpuCount)) != SYSTEM_RESOURCES_OK) {
return status;
}

if (cpu_index >= cpuCount) {
return SYSTEM_RESOURCES_ERROR;
}

if ((fp = fopen("/proc/stat", "r")) == NULL) {

return SYSTEM_RESOURCES_ERROR;

}

if (fgets(line, sizeof(line), fp) == NULL) { //1st line. Aggregate cpu line.
fclose(fp);
return SYSTEM_RESOURCES_ERROR;
}

for (U32 i = 0; i < cpu_index + 1; i++) {
if (fgets(line, sizeof(line), fp) == NULL) { //cpu# line
fclose(fp);
return SYSTEM_RESOURCES_ERROR;
}
if (i != cpu_index) { continue; }

if (!(line[0] == 'c' && line[1] == 'p' && line[2] == 'u')) {
fclose(fp);
return SYSTEM_RESOURCES_ERROR;
}

sscanf(line, "%*s %d %d %d %d", &cpu_data[0],
&cpu_data[1],
&cpu_data[2],
&cpu_data[3]); //cpu#: 4 numbers: usr, nice, sys, idle

cpuUsed = cpu_data[0] + cpu_data[1] + cpu_data[2];
cpuTotal = cpu_data[0] + cpu_data[1] + cpu_data[2] + cpu_data[3];

break;
}
fclose(fp);

cpu_ticks.used = cpuUsed;
cpu_ticks.total = cpuTotal;
return SYSTEM_RESOURCES_OK;
}

SystemResources::SystemResourcesStatus SystemResources::getMemUtil(MemUtil &memory_util) {
FILE *fp = NULL;
NATIVE_INT_TYPE total = 0;
NATIVE_INT_TYPE free = 0;
// Fallbacks
memory_util.total = 1;
memory_util.used = 1;

fp = fopen("/proc/meminfo", "r");
if (fp == NULL) {
return SYSTEM_RESOURCES_ERROR;
}

if (fscanf(fp, "%*s %d %*s", &total) != 1 || /* 1st line is MemTotal */
fscanf(fp, "%*s %d", &free) != 1) { /* 2nd line is MemFree */
fclose(fp);
return SYSTEM_RESOURCES_ERROR;
}
fclose(fp);

// Check results
if (total < 0 or free < 0 or total < free) {
return SYSTEM_RESOURCES_ERROR;
}
memory_util.total = static_cast<U64>(total) * 1024; // KB to Bytes
memory_util.used = static_cast<U64>(total - free) * 1024;
return SYSTEM_RESOURCES_OK;
}
}
Loading