Skip to content

Commit

Permalink
Dont parse same file twice
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Isaev <[email protected]>
  • Loading branch information
isaevil committed Oct 11, 2021
1 parent 8702780 commit 5cbc115
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/tbbmalloc/shared_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ T min ( const T& val1, const T& val2 ) {
// TODO: add a constructor to remove warnings suppression
struct parseFileItem {
const char* format;
unsigned long long& value;
long long& value;
};

#if defined(_MSC_VER) && (_MSC_VER<1900) && !defined(__INTEL_COMPILER)
Expand Down
20 changes: 9 additions & 11 deletions src/tbbmalloc/tbbmalloc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,33 +447,31 @@ class HugePagesStatus {
void parseSystemMemInfo() {
bool hpAvailable = false;
bool thpAvailable = false;
unsigned long long hugePageSize = 0;
unsigned long long hugePageSizePresent = 0;
long long hugePageSize = -1;

#if __unix__
// Check huge pages existence
unsigned long long meminfoHugePagesTotal = 0;
long long meminfoHugePagesTotal = 0;

parseFileItem meminfoItems[] = {
// Parse system huge page size
{ "Hugepagesize: %llu kB", hugePageSize },
{ "Hugepagesiz%c:", hugePageSizePresent },
{ "Hugepagesize: %lld kB", hugePageSize },
// Check if there are preallocated huge pages on the system
// https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
{ "HugePages_Total: %llu", meminfoHugePagesTotal } };
{ "HugePages_Total: %lld", meminfoHugePagesTotal } };

parseFile</*BUFF_SIZE=*/100>("/proc/meminfo", meminfoItems);

// Double check another system information regarding preallocated
// huge pages if there are no information in /proc/meminfo
unsigned long long vmHugePagesTotal = 0;
long long vmHugePagesTotal = 0;

parseFileItem vmItem[] = { { "%llu", vmHugePagesTotal } };
parseFileItem vmItem[] = { { "%lld", vmHugePagesTotal } };

// We parse a counter number, it can't be huge
parseFile</*BUFF_SIZE=*/100>("/proc/sys/vm/nr_hugepages", vmItem);

if (hugePageSizePresent == 'e' && (meminfoHugePagesTotal > 0 || vmHugePagesTotal > 0)) {
if (hugePageSize > -1 && (meminfoHugePagesTotal > 0 || vmHugePagesTotal > 0)) {
MALLOC_ASSERT(hugePageSize != 0, "Huge Page size can't be zero if we found preallocated.");

// Any non zero value clearly states that there are preallocated
Expand All @@ -482,11 +480,11 @@ class HugePagesStatus {
}

// Check if there is transparent huge pages support on the system
unsigned long long thpPresent = 'n';
long long thpPresent = 'n';
parseFileItem thpItem[] = { { "[alwa%cs] madvise never\n", thpPresent } };
parseFile</*BUFF_SIZE=*/100>("/sys/kernel/mm/transparent_hugepage/enabled", thpItem);

if (thpPresent == 'y' && hugePageSizePresent == 'e') {
if (hugePageSize > -1 && thpPresent == 'y') {
MALLOC_ASSERT(hugePageSize != 0, "Huge Page size can't be zero if we found thp existence.");
thpAvailable = true;
}
Expand Down
18 changes: 9 additions & 9 deletions test/common/memory_usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,30 @@ namespace utils {
#if __unix__

inline bool isTHPEnabledOnMachine() {
unsigned long long thpPresent = 'n';
unsigned long long hugePageSizePresent = 0;
long long thpPresent = 'n';
long long hugePageSize = -1;

parseFileItem thpItem[] = { { "[alwa%cs] madvise never\n", thpPresent } };
parseFileItem hpSizeItem[] = { { "Hugepagesiz%c:", hugePageSizePresent } };
parseFileItem hugePageItem[] = { { "Hugepagesize: %lld kB", hugePageSize } };

parseFile</*BUFF_SIZE=*/100>("/sys/kernel/mm/transparent_hugepage/enabled", thpItem);
parseFile</*BUFF_SIZE=*/100>("/proc/meminfo", hpSizeItem);
parseFile</*BUFF_SIZE=*/100>("/proc/meminfo", hugePageItem);

if (hugePageSizePresent == 'e' && thpPresent == 'y') {
if (hugePageSize > -1 && thpPresent == 'y') {
return true;
} else {
return false;
}
}
inline unsigned long long getSystemTHPAllocatedSize() {
unsigned long long anonHugePagesSize = 0;
inline long long getSystemTHPAllocatedSize() {
long long anonHugePagesSize = 0;
parseFileItem meminfoItems[] = {
{ "AnonHugePages: %llu kB", anonHugePagesSize } };
parseFile</*BUFF_SIZE=*/100>("/proc/meminfo", meminfoItems);
return anonHugePagesSize;
}
inline unsigned long long getSystemTHPCount() {
unsigned long long anonHugePages = 0;
inline long long getSystemTHPCount() {
long long anonHugePages = 0;
parseFileItem vmstatItems[] = {
{ "nr_anon_transparent_hugepages %llu", anonHugePages } };
parseFile</*BUFF_SIZE=*/100>("/proc/vmstat", vmstatItems);
Expand Down

0 comments on commit 5cbc115

Please sign in to comment.