Skip to content

Commit

Permalink
Replace OSAllocator implementation with memory-extra private library
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=274221

Reviewed by Don Olmstead.

To reserve large address space, our platform requires precise control of virtual address
space and commiti/decommit management is not enough for this purpose. In this patch, I've
introduced new memory-extra private library for that purpose and add platform specific
OSAllocator implementation.

Also replace showmap library with embedded one inside memory-extra and use clever way to
collect memory information.

* Source/JavaScriptCore/PlatformPlayStation.cmake:
* Source/JavaScriptCore/shell/PlatformPlayStation.cmake:
* Source/JavaScriptCore/testmem/PlatformPlayStation.cmake:
* Source/JavaScriptCore/testmem/testmem.cpp:
(Footprint::now):
* Source/WTF/wtf/PlatformPlayStation.cmake:
* Source/WTF/wtf/playstation/OSAllocatorPlayStation.cpp: Added.
(WTF::OSAllocator::tryReserveAndCommit):
(WTF::OSAllocator::tryReserveUncommitted):
(WTF::OSAllocator::reserveUncommitted):
(WTF::OSAllocator::tryReserveUncommittedAligned):
(WTF::OSAllocator::reserveAndCommit):
(WTF::OSAllocator::commit):
(WTF::OSAllocator::decommit):
(WTF::OSAllocator::hintMemoryNotNeededSoon):
(WTF::OSAllocator::releaseDecommitted):
(WTF::OSAllocator::protect):
* Source/WebCore/PlatformPlayStation.cmake:
* Source/WebCore/page/playstation/ResourceUsageThreadPlayStation.cpp:
(WebCore::ResourceUsageThread::platformCollectMemoryData):
* Source/cmake/OptionsPlayStation.cmake:
* Tools/TestWebKitAPI/PlatformPlayStation.cmake:

Canonical link: https://commits.webkit.org/278920@main
  • Loading branch information
basuke committed May 17, 2024
1 parent 5dc8c9e commit 3042eb6
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 38 deletions.
5 changes: 5 additions & 0 deletions Source/JavaScriptCore/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ list(APPEND JavaScriptCore_PRIVATE_DEFINITIONS
FIXED_EXECUTABLE_MEMORY_POOL_SIZE_IN_MB=64
)

list(APPEND JavaScriptCore_PRIVATE_INCLUDE_DIRECTORIES ${MEMORY_EXTRA_INCLUDE_DIR})

target_link_libraries(LLIntSettingsExtractor PRIVATE ${MEMORY_EXTRA_LIB})
target_link_libraries(LLIntOffsetsExtractor PRIVATE ${MEMORY_EXTRA_LIB})

if (DEVELOPER_MODE)
add_subdirectory(testmem)
endif ()
11 changes: 11 additions & 0 deletions Source/JavaScriptCore/shell/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ if (${CMAKE_GENERATOR} MATCHES "Visual Studio")
endif ()
endif ()

list(APPEND jsc_LIBRARIES ${MEMORY_EXTRA_LIB})

if (DEVELOPER_MODE)
list(APPEND testapi_LIBRARIES ${MEMORY_EXTRA_LIB})
list(APPEND testmasm_LIBRARIES ${MEMORY_EXTRA_LIB})
list(APPEND testRegExp_LIBRARIES ${MEMORY_EXTRA_LIB})
list(APPEND testb3_LIBRARIES ${MEMORY_EXTRA_LIB})
list(APPEND testair_LIBRARIES ${MEMORY_EXTRA_LIB})
list(APPEND testdfg_LIBRARIES ${MEMORY_EXTRA_LIB})
endif ()

if (${CMAKE_GENERATOR} MATCHES "Visual Studio")
# With the VisualStudio generator, the compiler complains about -std=c++* for C sources.
set_source_files_properties(
Expand Down
14 changes: 2 additions & 12 deletions Source/JavaScriptCore/testmem/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,5 @@ if (${CMAKE_GENERATOR} MATCHES "Visual Studio")
set_target_properties(testmem PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
endif ()

# For the simple version, we could use find_library in
# Source/cmake/OptionsPlayStation.cmake, make a target with
# something like add_library(showmem static IMPORTED) and
# then set the location with set_target_properties on the
# IMPORTED_LOCATION property, and then in the playstation
# config for testmem adding showmem to the libraries list.

find_library(SHOWMAP_LIB showmap)
list(APPEND testmem_LIBRARIES ${SHOWMAP_LIB})

find_path(SHOWMAP_INCLUDE_DIR NAMES showmap.h)
list(APPEND testmem_INCLUDE_DIRECTORIES ${SHOWMAP_INCLUDE_DIR})
list(APPEND testmem_PRIVATE_INCLUDE_DIRECTORIES ${MEMORY_EXTRA_INCLUDE_DIR})
list(APPEND testmem_LIBRARIES ${MEMORY_EXTRA_LIB})
16 changes: 7 additions & 9 deletions Source/JavaScriptCore/testmem/testmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <wtf/MonotonicTime.h>

#if PLATFORM(PLAYSTATION)
#include <showmap.h>
#include <memory-extra/showmap.h>
#endif

static void description()
Expand All @@ -51,18 +51,16 @@ struct Footprint {
static std::optional<Footprint> now()
{
#if PLATFORM(PLAYSTATION)
showmap::Result result;
memory_extra::showmap::Result<4> result;
auto* entry = result.entry("SceNKFastMalloc");
result.collect();
if (auto* entry = result.entry("SceNKFastMalloc")) {
return Footprint {
static_cast<uint64_t>(entry->effectiveRss()),
static_cast<uint64_t>(entry->vss)
};
}
return Footprint {
static_cast<uint64_t>(entry->rss),
static_cast<uint64_t>(entry->vss)
};
#else
#error "No testmem implementation for this platform."
#endif
return std::nullopt;
}
};

Expand Down
6 changes: 5 additions & 1 deletion Source/WTF/wtf/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ list(APPEND WTF_SOURCES

playstation/FileSystemPlayStation.cpp
playstation/LanguagePlayStation.cpp
playstation/OSAllocatorPlayStation.cpp
playstation/UniStdExtrasPlayStation.cpp

posix/CPUTimePOSIX.cpp
posix/FileSystemPOSIX.cpp
posix/OSAllocatorPOSIX.cpp
posix/ThreadingPOSIX.cpp

text/unix/TextBreakIteratorInternalICUUnix.cpp
Expand All @@ -23,6 +23,10 @@ list(APPEND WTF_PUBLIC_HEADERS
unix/UnixFileDescriptor.h
)

list(APPEND WTF_PRIVATE_INCLUDE_DIRECTORIES
${MEMORY_EXTRA_INCLUDE_DIR}
)

list(APPEND WTF_LIBRARIES
${KERNEL_LIBRARY}
Threads::Threads
Expand Down
132 changes: 132 additions & 0 deletions Source/WTF/wtf/playstation/OSAllocatorPlayStation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (C) 2010-2022 Apple Inc. All rights reserved.
* Copyright (C) 2024 SONY Interactive Entertainment Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include <wtf/OSAllocator.h>

#include <errno.h>
#include <memory-extra/vss.h>
#include <wtf/Assertions.h>
#include <wtf/MathExtras.h>
#include <wtf/PageBlock.h>

namespace WTF {

void* OSAllocator::tryReserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable, bool jitCageEnabled, bool includesGuardPages)
{
ASSERT_UNUSED(includesGuardPages, !includesGuardPages);
ASSERT_UNUSED(jitCageEnabled, !jitCageEnabled);
ASSERT_UNUSED(executable, !executable);

void* result = memory_extra::vss::reserve(bytes);
if (!result)
return nullptr;

bool success = memory_extra::vss::commit(result, bytes, writable, usage);
if (!success) {
memory_extra::vss::release(result, bytes);
return nullptr;
}

return result;
}

void* OSAllocator::tryReserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable, bool jitCageEnabled, bool includesGuardPages)
{
UNUSED_PARAM(usage);
UNUSED_PARAM(writable);
ASSERT_UNUSED(includesGuardPages, !includesGuardPages);
ASSERT_UNUSED(jitCageEnabled, !jitCageEnabled);
ASSERT_UNUSED(executable, !executable);

void* result = memory_extra::vss::reserve(bytes);
if (!result)
return nullptr;

return result;
}

void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable, bool jitCageEnabled, bool includesGuardPages)
{
void* result = tryReserveUncommitted(bytes, usage, writable, executable, jitCageEnabled, includesGuardPages);
RELEASE_ASSERT(result);
return result;
}

void* OSAllocator::tryReserveUncommittedAligned(size_t bytes, size_t alignment, Usage usage, bool writable, bool executable, bool jitCageEnabled, bool includesGuardPages)
{
ASSERT_UNUSED(includesGuardPages, !includesGuardPages);
ASSERT_UNUSED(jitCageEnabled, !jitCageEnabled);
ASSERT_UNUSED(executable, !executable);
UNUSED_PARAM(usage);
UNUSED_PARAM(writable);
ASSERT(hasOneBitSet(alignment) && alignment >= pageSize());

void* result = memory_extra::vss::reserve(bytes, alignment);
if (!result)
return nullptr;

return result;
}

void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable, bool jitCageEnabled, bool includesGuardPages)
{
void* result = tryReserveAndCommit(bytes, usage, writable, executable, jitCageEnabled, includesGuardPages);
RELEASE_ASSERT(result);
return result;
}

void OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable)
{
ASSERT_UNUSED(executable, !executable);
bool success = memory_extra::vss::commit(address, bytes, writable, -1);
RELEASE_ASSERT(success);
}

void OSAllocator::decommit(void* address, size_t bytes)
{
bool success = memory_extra::vss::decommit(address, bytes);
RELEASE_ASSERT(success);
}

void OSAllocator::hintMemoryNotNeededSoon(void* address, size_t bytes)
{
UNUSED_PARAM(address);
UNUSED_PARAM(bytes);
}

void OSAllocator::releaseDecommitted(void* address, size_t bytes)
{
bool success = memory_extra::vss::release(address, bytes);
RELEASE_ASSERT(success);
}

bool OSAllocator::protect(void* address, size_t bytes, bool readable, bool writable)
{
return memory_extra::vss::protect(address, bytes, readable, writable, -1);
}

} // namespace WTF
6 changes: 1 addition & 5 deletions Source/WebCore/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ if (USE_WPE_BACKEND_PLAYSTATION)
list(APPEND WebCore_MODULES WPE)
endif ()

find_library(SHOWMAP_LIB showmap)
list(APPEND WebCore_LIBRARIES ${SHOWMAP_LIB})

find_path(SHOWMAP_INCLUDE_DIR NAMES showmap.h)
list(APPEND WebCore_INCLUDE_DIRECTORIES ${SHOWMAP_INCLUDE_DIR})
list(APPEND WebCore_PRIVATE_INCLUDE_DIRECTORIES ${MEMORY_EXTRA_INCLUDE_DIR})

PLAYSTATION_COPY_MODULES(WebCore TARGETS ${WebCore_MODULES})
20 changes: 9 additions & 11 deletions Source/WebCore/page/playstation/ResourceUsageThreadPlayStation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include <JavaScriptCore/GCActivityCallback.h>
#include <JavaScriptCore/VM.h>
#include <showmap.h>
#include <memory-extra/showmap.h>

namespace WebCore {

Expand Down Expand Up @@ -71,17 +71,15 @@ void ResourceUsageThread::platformCollectMemoryData(JSC::VM* vm, ResourceUsageDa

// TODO: collect dirty size of "MemoryCategory::Images" and "MemoryCategory::Layers"

showmap::Result<256> result;
memory_extra::showmap::Result<4> result;
auto entry = result.reserve("SceNKFastMalloc");
result.collect();
data.totalDirtySize = result.effectiveRss();

if (auto* entry = result.entry("SceNKFastMalloc")) {
auto rss = entry->effectiveRss();
RELEASE_ASSERT(data.totalDirtySize > rss);
categories[MemoryCategory::Other].dirtySize = data.totalDirtySize - rss;
categories[MemoryCategory::bmalloc].dirtySize = rss - std::min(rss, currentGCDirtySize);
} else
categories[MemoryCategory::Other] = data.totalDirtySize - std::min(data.totalDirtySize, currentGCDirtySize);
data.totalDirtySize = result.rss;

auto rss = entry->rss;
RELEASE_ASSERT(data.totalDirtySize > rss);
categories[MemoryCategory::Other].dirtySize = data.totalDirtySize - rss;
categories[MemoryCategory::bmalloc].dirtySize = rss - std::min(rss, currentGCDirtySize);

data.totalExternalSize = currentGCOwnedExternal;

Expand Down
3 changes: 3 additions & 0 deletions Source/cmake/OptionsPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ set(PAL_LIBRARY_TYPE OBJECT)
set(WebCore_LIBRARY_TYPE OBJECT)
set(WebKit_LIBRARY_TYPE SHARED)

find_library(MEMORY_EXTRA_LIB MemoryExtra)
find_path(MEMORY_EXTRA_INCLUDE_DIR NAMES memory-extra)

# Enable multi process builds for Visual Studio
if (NOT ${CMAKE_GENERATOR} MATCHES "Ninja")
add_definitions(/MP)
Expand Down
8 changes: 8 additions & 0 deletions Tools/TestWebKitAPI/PlatformPlayStation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ list(APPEND TestWTF_PRIVATE_INCLUDE_DIRECTORIES
${WEBKIT_LIBRARIES_DIR}/include
)

list(APPEND TestWTF_LIBRARIES
${MEMORY_EXTRA_LIB}
)

WEBKIT_ADD_TARGET_CXX_FLAGS(TestWTF -Wno-unused-function)

list(APPEND TestJavaScriptCore_SOURCES
Expand All @@ -20,6 +24,10 @@ list(APPEND TestJavaScriptCore_PRIVATE_INCLUDE_DIRECTORIES
${WEBKIT_LIBRARIES_DIR}/include
)

list(APPEND TestJavaScriptCore_LIBRARIES
${MEMORY_EXTRA_LIB}
)

WEBKIT_ADD_TARGET_CXX_FLAGS(TestJavaScriptCore -Wno-unused-function)

list(APPEND TestWebCore_SOURCES
Expand Down

0 comments on commit 3042eb6

Please sign in to comment.