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

GetCpuCount: using get_nprocs() #1322

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,7 @@ synchronicity
synopsys
sys
SYSFS
sysinfo
systemd
tabbedwidths
tabccitt
Expand Down
29 changes: 2 additions & 27 deletions Os/Linux/SystemResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,15 @@
#include <cstdio> /* fopen() */
#include <cstdlib> /* scanf */
#include <sys/vfs.h> /* statfs() */
#include <sys/sysinfo.h> /* get_nprocs() */
#include <cstring>
#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 = nullptr;
U32 cpu_count = 0;

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

if (fgets(line, sizeof(line), fp) == nullptr) { //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) == nullptr) { //cpu# line
break;
}

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

cpuCount = get_nprocs();
return SYSTEM_RESOURCES_OK;
}

Expand Down