Skip to content

Commit

Permalink
Enable hot static code mapping onto huge pages
Browse files Browse the repository at this point in the history
This diff enables mapping of hot static code onto huge pages for open
source builds. It defines __hot_start and __hot_end at link time using
the first and last functions from the linker script. This feature can be
turned off by setting the CMake option MAP_TEXT_HUGE_PAGES to 'Off'.

hugifyText was aligned to 2MB so that we maximize the amount of hot code
we can map if __hot_end is placed just before it.

Solves facebook#5269
  • Loading branch information
octmoraru committed Jun 29, 2017
1 parent bf58f31 commit acd630d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CMake/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ option(ENABLE_PROXYGEN_SERVER "Build the Proxygen HTTP server" ON)

option(ENABLE_SPLIT_DWARF "Reduce linker memory usage by putting debugging information into .dwo files" OFF)

IF (LINUX)
option(MAP_TEXT_HUGE_PAGES "Remap hot static code onto huge pages" ON)
ENDIF()

IF (NOT DEFAULT_CONFIG_DIR)
set(DEFAULT_CONFIG_DIR "/etc/hhvm/" CACHE STRING
"Default directory to find php.ini")
Expand Down
15 changes: 13 additions & 2 deletions hphp/hhvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ endif()

option(ENABLE_LD_GOLD "Enable Hot Linker script using ld-gold" On)
set(SECTION_ORDERING_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../tools/oss_hot_section_ordering CACHE FILEPATH "File used by ld-gold for the relocation of sections")

if (MAP_TEXT_HUGE_PAGES)
file(STRINGS "${SECTION_ORDERING_FILE}" SECTION_ORDERING_CONTENTS)
list(GET SECTION_ORDERING_CONTENTS 0 HOT_START)
list(GET SECTION_ORDERING_CONTENTS -1 HOT_END)
string(REGEX REPLACE "\\.text\\.(.*)\\*" "\\1" HOT_START ${HOT_START})
string(REGEX REPLACE "\\.text\\.(.*)\\*" "\\1" HOT_END ${HOT_END})
set(DEFSYM_HOT_START "--defsym=__hot_start=${HOT_START},")
set(DEFSYM_HOT_END "--defsym=__hot_end=${HOT_END},")
endif()

if (ENABLE_LD_GOLD)
find_package(Gold)
endif()
Expand All @@ -21,9 +32,9 @@ target_link_libraries(hhvm ${HHVM_LINK_LIBRARIES} ${EZC_LINK_LIBRARIES} ${HRE_LI
link_object_libraries(hhvm ${HHVM_WHOLE_ARCHIVE_LIBRARIES})
if (GOLD_FOUND AND ENABLE_LD_GOLD)
if (CMAKE_CONFIGURATION_TYPES)
SET(LINKER_SCRIPT -fuse-ld=gold;-Wl,--icf=all,--gc-sections;release;-Wl,--section-ordering-file,${SECTION_ORDERING_FILE};default)
SET(LINKER_SCRIPT -fuse-ld=gold;-Wl,--icf=all,--gc-sections;release;-Wl,${DEFSYM_HOT_START}${DEFSYM_HOT_END}--section-ordering-file,${SECTION_ORDERING_FILE};default)
elseif(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
SET(LINKER_SCRIPT -fuse-ld=gold -Wl,--section-ordering-file,${SECTION_ORDERING_FILE},--icf=all,--gc-sections)
SET(LINKER_SCRIPT -fuse-ld=gold -Wl,${DEFSYM_HOT_START}${DEFSYM_HOT_END}--section-ordering-file,${SECTION_ORDERING_FILE},--icf=all,--gc-sections)
else()
SET(LINKER_SCRIPT -fuse-ld=gold -Wl,--icf=all,--gc-sections)
endif()
Expand Down
16 changes: 8 additions & 8 deletions hphp/runtime/base/program-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,12 @@ void execute_command_line_end(int xhprof, bool coverage, const char *program) {
const void* __hot_start = nullptr;
const void* __hot_end = nullptr;
#endif

#if FACEBOOK
# define AT_END_OF_TEXT __attribute__((__section__(".stub")))
#else
# define AT_END_OF_TEXT
#endif
# define ALIGN_HUGE_PAGE __attribute__((aligned(2 * 1024 * 1024)))

static void NEVER_INLINE AT_END_OF_TEXT __attribute__((__optimize__("2")))
static void NEVER_INLINE AT_END_OF_TEXT ALIGN_HUGE_PAGE __attribute__((__optimize__("2")))
hugifyText(char* from, char* to) {
#if FACEBOOK && !defined FOLLY_SANITIZE_ADDRESS && defined MADV_HUGEPAGE
#if !defined FOLLY_SANITIZE_ADDRESS && defined MADV_HUGEPAGE
if (from > to || (to - from) < sizeof(uint64_t)) {
// This shouldn't happen if HHVM is behaving correctly (I think),
// but if it does then there is nothing to do and we should bail
Expand Down Expand Up @@ -796,6 +792,10 @@ hugifyText(char* from, char* to) {
free(mem);
mlock(from, to - from);
Debug::DebugInfo::setPidMapOverlay(from, to);
std::stringstream ss;
ss << "Mapped text section onto huge pages from " <<
std::hex << (uint64_t*)from << " to " << (uint64_t*)to;
Logger::Info(ss.str());
#endif
}

Expand Down Expand Up @@ -854,7 +854,7 @@ static void pagein_self(void) {
if (to - from > maxHugeHotTextBytes) {
to = from + maxHugeHotTextBytes;
}
if (to < (void*)hugifyText) {
if (to <= (void*)hugifyText) {
hugifyText(from, to);
}
}
Expand Down

0 comments on commit acd630d

Please sign in to comment.