Skip to content

Commit

Permalink
Merge pull request #1116 from sfregoso/system_resources
Browse files Browse the repository at this point in the history
System resources
  • Loading branch information
kevin-f-ortega authored Nov 19, 2021
2 parents b844e63 + b36a66c commit 5b18ddf
Show file tree
Hide file tree
Showing 22 changed files with 1,277 additions and 4 deletions.
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;
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

0 comments on commit 5b18ddf

Please sign in to comment.