Skip to content

Commit

Permalink
Using sysconf to calculate system memory
Browse files Browse the repository at this point in the history
  • Loading branch information
ypapadop-amd committed Dec 9, 2024
1 parent 93a0065 commit a59ea1a
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions runtime/hsa-runtime/core/driver/xdna/amd_xdna_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>

#include <cstdio>
#include <cstring>
#include <memory>
#include <string>

Expand Down Expand Up @@ -87,29 +86,9 @@ hsa_status_t XdnaDriver::DiscoverDriver() {
}

uint64_t XdnaDriver::GetSystemMemoryByteSize() {
static const std::string meminfo_node("/proc/meminfo");

FILE *file = std::fopen(meminfo_node.c_str(), "r");
if (file == nullptr)
throw AMD::hsa_exception(HSA_STATUS_ERROR_INVALID_FILE,
"Invalid memory info file.");

const int expected_args = 2;
int result = 0;
char property_name[256];
unsigned long long property_val = 0;
while ((result = std::fscanf(file, "%255s %llu\n", property_name,
&property_val)) == expected_args) {
if (std::strcmp(property_name, "MemTotal:") == 0)
break;
}
std::fclose(file);

if ((result == EOF) || (result != expected_args))
throw AMD::hsa_exception(HSA_STATUS_ERROR, "Could not parse file.");

// memory reported is in Kbytes
return property_val * 1024ull;
const long pagesize = sysconf(_SC_PAGESIZE);
const long page_count = sysconf(_SC_PHYS_PAGES);
return pagesize * page_count;
}

uint64_t XdnaDriver::GetDevHeapByteSize() {
Expand Down

0 comments on commit a59ea1a

Please sign in to comment.