From daad7bbd3447f04f50af24dd487563c60bc1a9dd Mon Sep 17 00:00:00 2001 From: James Addison Date: Thu, 28 Jan 2021 19:40:48 +0000 Subject: [PATCH] src: adjust THP sysfs config token retrieval and file closure PR-URL: https://github.com/nodejs/node/pull/37187 Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- src/large_pages/node_large_page.cc | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/large_pages/node_large_page.cc b/src/large_pages/node_large_page.cc index 22810ef08fb878..eb546c581a1652 100644 --- a/src/large_pages/node_large_page.cc +++ b/src/large_pages/node_large_page.cc @@ -258,23 +258,21 @@ struct text_region FindNodeTextRegion() { #if defined(__linux__) bool IsTransparentHugePagesEnabled() { - std::ifstream ifs; - // File format reference: // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/huge_memory.c?id=13391c60da3308ed9980de0168f74cce6c62ac1d#n163 - ifs.open("/sys/kernel/mm/transparent_hugepage/enabled"); - if (!ifs) { + const char* filename = "/sys/kernel/mm/transparent_hugepage/enabled"; + std::ifstream config_stream(filename, std::ios::in); + if (!config_stream.good()) { PrintWarning("could not open /sys/kernel/mm/transparent_hugepage/enabled"); return false; } - std::string always, madvise; - if (ifs.is_open()) { - ifs >> always >> madvise; - } - ifs.close(); - - return always == "[always]" || madvise == "[madvise]"; + std::string token; + config_stream >> token; + if ("[always]" == token) return true; + config_stream >> token; + if ("[madvise]" == token) return true; + return false; } #elif defined(__FreeBSD__) bool IsSuperPagesEnabled() {