Skip to content

Commit

Permalink
8310233: Fix THP detection on Linux
Browse files Browse the repository at this point in the history
Reviewed-by: jsjolen, dholmes
  • Loading branch information
tstuefe committed Jul 17, 2023
1 parent 81c4e8f commit 37ca902
Show file tree
Hide file tree
Showing 7 changed files with 716 additions and 131 deletions.
228 changes: 228 additions & 0 deletions src/hotspot/os/linux/hugepages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023, Red Hat Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "hugepages.hpp"

#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "runtime/os.hpp"
#include "utilities/debug.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"

#include <dirent.h>

StaticHugePageSupport::StaticHugePageSupport() :
_initialized(false), _pagesizes(), _default_hugepage_size(SIZE_MAX) {}

os::PageSizes StaticHugePageSupport::pagesizes() const {
assert(_initialized, "Not initialized");
return _pagesizes;
}

size_t StaticHugePageSupport::default_hugepage_size() const {
assert(_initialized, "Not initialized");
return _default_hugepage_size;
}

// Scan /proc/meminfo and return value of Hugepagesize
static size_t scan_default_hugepagesize() {
size_t pagesize = 0;

// large_page_size on Linux is used to round up heap size. x86 uses either
// 2M or 4M page, depending on whether PAE (Physical Address Extensions)
// mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
// page as large as 1G.
//
// Here we try to figure out page size by parsing /proc/meminfo and looking
// for a line with the following format:
// Hugepagesize: 2048 kB
//
// If we can't determine the value (e.g. /proc is not mounted, or the text
// format has been changed), we'll set largest page size to 0

FILE *fp = os::fopen("/proc/meminfo", "r");
if (fp) {
while (!feof(fp)) {
int x = 0;
char buf[16];
if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
pagesize = x * K;
break;
}
} else {
// skip to next line
for (;;) {
int ch = fgetc(fp);
if (ch == EOF || ch == (int)'\n') break;
}
}
}
fclose(fp);
}

return pagesize;
}

// Given a file that contains a single (integral) number, return that number in (*out) and true;
// in case of an error, return false.
static bool read_number_file(const char* file, size_t* out) {
FILE* f = ::fopen(file, "r");
bool rc = false;
if (f != nullptr) {
uint64_t i = 0;
if (::fscanf(f, SIZE_FORMAT, out) == 1) {
rc = true;
}
::fclose(f);
}
return rc;
}

static const char* const sys_hugepages = "/sys/kernel/mm/hugepages";

// Scan all directories in /sys/kernel/mm/hugepages/hugepages-xxxx
// to discover the available page sizes
static os::PageSizes scan_hugepages() {

os::PageSizes pagesizes;

DIR *dir = opendir(sys_hugepages);

struct dirent *entry;
size_t pagesize;
while ((entry = readdir(dir)) != nullptr) {
if (entry->d_type == DT_DIR &&
sscanf(entry->d_name, "hugepages-%zukB", &pagesize) == 1) {
// The kernel is using kB, hotspot uses bytes
// Add each found Large Page Size to page_sizes
pagesize *= K;
pagesizes.add(pagesize);
}
}
closedir(dir);

return pagesizes;
}

void StaticHugePageSupport::print_on(outputStream* os) {
if (_initialized) {
os->print_cr("Static hugepage support:");
for (size_t s = _pagesizes.smallest(); s != 0; s = _pagesizes.next_larger(s)) {
os->print_cr(" hugepage size: " EXACTFMT, EXACTFMTARGS(s));
}
os->print_cr(" default hugepage size: " EXACTFMT, EXACTFMTARGS(_default_hugepage_size));
} else {
os->print_cr(" unknown.");
}
}

void StaticHugePageSupport::scan_os() {
_pagesizes = scan_hugepages();
_default_hugepage_size = scan_default_hugepagesize();
assert(_pagesizes.contains(_default_hugepage_size),
"Unexpected configuration: default pagesize (" SIZE_FORMAT ") "
"has no associated directory in /sys/kernel/mm/hugepages..", _default_hugepage_size);
_initialized = true;
LogTarget(Info, pagesize) lt;
if (lt.is_enabled()) {
LogStream ls(lt);
print_on(&ls);
}
}

THPSupport::THPSupport() :
_initialized(false), _mode(THPMode::never), _pagesize(SIZE_MAX) {}


THPMode THPSupport::mode() const {
assert(_initialized, "Not initialized");
return _mode;
}

size_t THPSupport::pagesize() const {
assert(_initialized, "Not initialized");
return _pagesize;
}

void THPSupport::scan_os() {
// Scan /sys/kernel/mm/transparent_hugepage/enabled
// see mm/huge_memory.c
_mode = THPMode::never;
const char* filename = "/sys/kernel/mm/transparent_hugepage/enabled";
FILE* f = ::fopen(filename, "r");
if (f != nullptr) {
char buf[64];
char* s = fgets(buf, sizeof(buf), f);
assert(s == buf, "Should have worked");
if (::strstr(buf, "[madvise]") != nullptr) {
_mode = THPMode::madvise;
} else if (::strstr(buf, "[always]") != nullptr) {
_mode = THPMode::always;
} else {
assert(::strstr(buf, "[never]") != nullptr, "Weird content of %s: %s", filename, buf);
}
fclose(f);
}

// Scan large page size for THP from hpage_pmd_size
_pagesize = 0;
if (read_number_file("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", &_pagesize)) {
assert(_pagesize > 0, "Expected");
}
_initialized = true;

LogTarget(Info, pagesize) lt;
if (lt.is_enabled()) {
LogStream ls(lt);
print_on(&ls);
}
}

void THPSupport::print_on(outputStream* os) {
if (_initialized) {
os->print_cr("Transparent hugepage (THP) support:");
os->print_cr(" THP mode: %s",
(_mode == THPMode::always ? "always" : (_mode == THPMode::never ? "never" : "madvise")));
os->print_cr(" THP pagesize: " EXACTFMT, EXACTFMTARGS(_pagesize));
} else {
os->print_cr(" unknown.");
}
}

StaticHugePageSupport HugePages::_static_hugepage_support;
THPSupport HugePages::_thp_support;

void HugePages::initialize() {
_static_hugepage_support.scan_os();
_thp_support.scan_os();
}

void HugePages::print_on(outputStream* os) {
_static_hugepage_support.print_on(os);
_thp_support.print_on(os);
}
110 changes: 110 additions & 0 deletions src/hotspot/os/linux/hugepages.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023, Red Hat Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef OS_LINUX_HUGEPAGES_HPP
#define OS_LINUX_HUGEPAGES_HPP

#include "memory/allStatic.hpp"
#include "runtime/os.hpp" // for os::PageSizes
#include "utilities/globalDefinitions.hpp"

class outputStream;

// Header contains the interface that reads OS information about
// available hugepage support:
// - class StaticHugePageSupport - about static (non-THP) hugepages
// - class THPSupport - about transparent huge pages
// and:
// - class HugePages - a static umbrella wrapper

// Information about static (non-thp) hugepages
class StaticHugePageSupport {
bool _initialized;

// All supported hugepage sizes (sizes for which entries exist
// in /sys/kernel/mm/hugepages/hugepage-xxx)
os::PageSizes _pagesizes;

// Contains the default hugepage. The "default hugepage size" is the one that
// - is marked in /proc/meminfo as "Hugepagesize"
// - is the size one gets when using mmap(MAP_HUGETLB) when omitting size specifiers like MAP_HUGE_SHIFT)
size_t _default_hugepage_size;

public:
StaticHugePageSupport();

void scan_os();

os::PageSizes pagesizes() const;
size_t default_hugepage_size() const;
void print_on(outputStream* os);
};

enum class THPMode { always, never, madvise };

// 2) for transparent hugepages
class THPSupport {
bool _initialized;

// See /sys/kernel/mm/transparent_hugepages/enabled
THPMode _mode;

// Contains the THP page size
size_t _pagesize;

public:

THPSupport();

// Queries the OS, fills in object
void scan_os();

THPMode mode() const;
size_t pagesize() const;
void print_on(outputStream* os);
};

// Umbrella static interface
class HugePages : public AllStatic {

static StaticHugePageSupport _static_hugepage_support;
static THPSupport _thp_support;

public:

static const StaticHugePageSupport& static_info() { return _static_hugepage_support; }
static const THPSupport& thp_info() { return _thp_support; }

static size_t default_static_hugepage_size() { return _static_hugepage_support.default_hugepage_size(); }
static bool supports_static_hugepages() { return default_static_hugepage_size() > 0; }
static THPMode thp_mode() { return _thp_support.mode(); }
static bool supports_thp() { return thp_mode() == THPMode::madvise || thp_mode() == THPMode::always; }
static size_t thp_pagesize() { return _thp_support.pagesize(); }

static void initialize();
static void print_on(outputStream* os);
};

#endif // OS_LINUX_HUGEPAGES_HPP

9 comments on commit 37ca902

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tstuefe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk-21u

@openjdk
Copy link

@openjdk openjdk bot commented on 37ca902 Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tstuefe The target repository jdk-21u is not a valid target for backports.
List of valid target repositories: openjdk/jdk, openjdk/jdk11u, openjdk/jdk11u-dev, openjdk/jdk17u, openjdk/jdk17u-dev, openjdk/jdk19u, openjdk/jdk20u, openjdk/jdk21, openjdk/jdk21u, openjdk/jdk7u, openjdk/jdk8u, openjdk/jdk8u-dev, openjdk/jfx, openjdk/jfx20u, openjdk/jfx21u, openjdk/shenandoah-jdk8u, openjdk/shenandoah-jdk8u-dev.
Supplying the organization/group prefix is optional.

@tstuefe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u

@openjdk
Copy link

@openjdk openjdk bot commented on 37ca902 Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tstuefe the backport was successfully created on the branch tstuefe-backport-37ca9024 in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 37ca9024 from the openjdk/jdk repository.

The commit being backported was authored by Thomas Stuefe on 17 Jul 2023 and was reviewed by Johan Sjölen and David Holmes.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:

$ git fetch https://github.com/openjdk-bots/jdk21u.git tstuefe-backport-37ca9024:tstuefe-backport-37ca9024
$ git checkout tstuefe-backport-37ca9024
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git tstuefe-backport-37ca9024

@tstuefe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u

@openjdk
Copy link

@openjdk openjdk bot commented on 37ca902 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tstuefe Could not automatically backport 37ca9024 to openjdk/jdk17u due to conflicts in the following files:

  • src/hotspot/os/linux/os_linux.cpp
  • src/hotspot/os/linux/os_linux.hpp

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk17u. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk17u.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b tstuefe-backport-37ca9024

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97

# Backport the commit
$ git cherry-pick --no-commit 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u with the title Backport 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97.

@tstuefe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u

@openjdk
Copy link

@openjdk openjdk bot commented on 37ca902 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tstuefe Could not automatically backport 37ca9024 to openjdk/jdk11u due to conflicts in the following files:

  • src/hotspot/os/linux/os_linux.cpp
  • src/hotspot/os/linux/os_linux.hpp

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk11u. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk11u.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b tstuefe-backport-37ca9024

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97

# Backport the commit
$ git cherry-pick --no-commit 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk11u with the title Backport 37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97.

Please sign in to comment.