Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Memory management #4443

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2a08909
Move aligned malloc to MemoryManager.h, port to stl allocator
lukas-w Nov 30, 2017
3c73270
Rename MemoryManager.h to Memory.h
lukas-w Nov 30, 2017
1340c27
Memory.h fixes
lukas-w Apr 15, 2018
1cd8e15
Replace LocklessAllocator with new MemoryPool class
lukas-w Apr 15, 2018
3e1a966
Replace NotePlayHandleManager implementation with MemoryPool
lukas-w Apr 17, 2018
178888a
BufferManager: Use MemoryPool, rename to BufferPool
lukas-w Apr 17, 2018
2cb4455
Replace AllignedAllocator implementation with rpmalloc calls
lukas-w Apr 18, 2018
ef7b8c6
Add naive benchmarks
lukas-w Apr 18, 2018
8b122d5
Fix libcds on MinGW
lukas-w Apr 29, 2018
2f8e231
Fix macOS linking problem
lukas-w Jun 19, 2018
119efee
Fix tests crash due to incomplete cleanup
lukas-w Jun 21, 2018
71e9b45
Memory: Fix wrong rebind
lukas-w Jul 8, 2018
5349be6
NiftyCounter: Fix decrement
lukas-w Aug 3, 2018
d35df8e
Move BufferPool::clear to MixHelpers and rewrite
lukas-w Aug 25, 2019
bd1ee29
Silence int conversion warning
lukas-w Aug 25, 2019
e21c00e
Apply suggestions by @PhysSong
lukas-w May 4, 2020
68d7157
Merge branch 'master' into refac/memory
lukas-w May 4, 2020
62606b6
Try to fix CircleCI linux build
lukas-w May 6, 2020
1cd8b7a
Fix libcds nifty counter typo
lukas-w May 11, 2020
29df871
Add nifty counter instance to MmAllocaator
lukas-w May 6, 2020
78c92e8
Tests: Allow specifying test suit name
lukas-w May 6, 2020
5ae42ca
Try to fix libcds counters issues
lukas-w May 11, 2020
a64f83e
Fix libcds MSVC compilation
lukas-w May 11, 2020
74ee635
Merge remote-tracking branch 'origin/master' into refac/memory
lukas-w May 12, 2020
ac0081d
Fix crash on exit with MSVC
lukas-w May 14, 2020
846ca17
CMake: Build lmms shared library instead of object library
lukas-w May 22, 2020
9df27e9
Merge branch 'master' into refac/memory
lukas-w Dec 11, 2020
ac5b3f0
Revert irrelevant changes
PhysSong Sep 21, 2024
47e6a30
Merge branch 'master' into refac/memory
PhysSong Oct 3, 2024
7ce62fe
Always build libcds as a static library
PhysSong Oct 3, 2024
7782954
Fix namespace
PhysSong Oct 3, 2024
f54ac45
Remove leftover of mingw-std-threads
PhysSong Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
[submodule "src/3rdparty/mingw-std-threads"]
path = src/3rdparty/mingw-std-threads
url = https://github.com/meganz/mingw-std-threads.git
[submodule "src/3rdparty/libcds"]
path = src/3rdparty/libcds
url = https://github.com/khizmax/libcds.git
[submodule "doc/wiki"]
path = doc/wiki
url = https://github.com/lmms/lmms.wiki.git
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ CHECK_INCLUDE_FILES(locale.h LMMS_HAVE_LOCALE_H)

LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_INSTALL_PREFIX}")


FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED)
FIND_PACKAGE(Qt5 COMPONENTS LinguistTools QUIET)

Expand All @@ -150,6 +151,8 @@ INCLUDE_DIRECTORIES(
${Qt5Xml_INCLUDE_DIRS}
)

INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/src/3rdparty/mingw-std-threads")

SET(QT_LIBRARIES
Qt5::Core
Qt5::Gui
Expand Down Expand Up @@ -561,6 +564,7 @@ include(CompileCache)
ADD_SUBDIRECTORY(cmake)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(plugins)
ADD_SUBDIRECTORY(benchmarks)
ADD_SUBDIRECTORY(tests)
ADD_SUBDIRECTORY(data)
ADD_SUBDIRECTORY(doc)
Expand Down
18 changes: 18 additions & 0 deletions LICENSE.MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}")
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include")
INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}")

SET(CMAKE_CXX_STANDARD 11)

SET(CMAKE_AUTOMOC ON)

ADD_EXECUTABLE(benchmarks
EXCLUDE_FROM_ALL
benchmark.cpp
$<TARGET_OBJECTS:lmmsobjs>
)
TARGET_LINK_LIBRARIES(benchmarks ${QT_LIBRARIES} ${LMMS_REQUIRED_LIBS})
130 changes: 130 additions & 0 deletions benchmarks/benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <thread>

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#include "libcds.h"
#include <cds/container/vyukov_mpmc_cycle_queue.h>

#include "Engine.h"
#include "PerfLog.h"

#include "LocklessList.h"
#include "MemoryPool.h"

#include "NotePlayHandle.h"

template<typename T>
using LocklessQueue = cds::container::VyukovMPMCCycleQueue<T>;

template<typename Alloc>
void benchmark_allocator(QString name, Alloc&& alloc, size_t n, size_t I)
{
using T = typename Alloc::value_type;
constexpr size_t S = sizeof(T);

std::vector<T*> ptrs{n};
PerfLogTimer timer(QString("Allocate: %1 x %2 x %3 bytes, %4")
.arg(I).arg(n).arg(S).arg(name));

for (size_t i=0; i < I; i++)
{
for (size_t j=0; j < n; j++) {
ptrs[j] = alloc.allocate(1);
}
for (size_t j=0; j < n; j++) {
alloc.deallocate(ptrs[j], 1);
}
}
}


template<typename Alloc>
void benchmark_allocator_threaded(QString name, Alloc&& alloc, size_t n, size_t t)
{
using T = typename Alloc::value_type;
constexpr size_t S = sizeof(T);

LocklessQueue<T*> ptrs{n};

PerfLogTimer timer(QString("Allocate multi-threaded: %1 x %2 bytes using %3 threads, %4")
.arg(n).arg(S).arg(t).arg(name));

std::vector<std::thread> threads; threads.reserve(t*2);

std::atomic_uint_fast64_t allocated{0};
std::atomic_uint_fast64_t deallocated{0};

for (size_t i=0; i < t; i++) {
threads.emplace_back([&]() {
while(allocated++ < n) {
auto ptr = alloc.allocate(1);
ptrs.push(ptr);
}
});
}
for (size_t i=0; i < t; i++) {
threads.emplace_back([&]() {
while(deallocated++ < n) {
T* ptr;
while (! ptrs.pop(ptr));
alloc.deallocate(ptr, 1);
}
});
}

for (std::thread& thread : threads) {
thread.join();
}
}

int main(int argc, char* argv[])
{
new QCoreApplication(argc, argv);

using Stack = LocklessList<size_t>;
{
size_t n = 100 * 1000 * 1000;
Copy link
Member

Choose a reason for hiding this comment

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

I think you should make it as constexpr.

Copy link
Member Author

Choose a reason for hiding this comment

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

That wouldn't make any difference, would it?

Copy link
Contributor

Choose a reason for hiding this comment

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

It evaluates at compile-time rather than run-time.

Stack stack{n};
PerfLogTimer timer("LocklessList: Insert 100m entries, single-threaded, pre-allocated");
for (size_t i=0; i < n; i++) {
stack.push(i);
}
}
{
size_t n = 50 * 1000 * 1000;
size_t t = 5;
Stack stack{n};
std::vector<std::thread> threads; threads.reserve(t);
PerfLogTimer timer("LocklessList: Push 50m entries, multi-threaded, pre-allocated");

for (int i=0; i < 5; i++) {
threads.emplace_back([&]() {
for (size_t j=0; j < n / t; j++) {
stack.push(j);
}
});
}

for (int i=0; i < 5; i++) {
threads.at(i).join();
}
}

{
size_t n = 10 * 1000 * 1000;
constexpr size_t S = 256;
using T = std::array<char, S>;
benchmark_allocator("std::allocator", std::allocator<T>{}, n, 1);
benchmark_allocator("MmAllocator", MmAllocator<T>{}, n, 1);
benchmark_allocator("MemoryPool", MemoryPool<T>{n}, n, 1);
}
{
size_t n = 10 * 1000 * 1000;
constexpr size_t S = 256;
using T = std::array<char, S>;
benchmark_allocator_threaded("std::allocator", std::allocator<T>{}, n, 4);
benchmark_allocator_threaded("MmAllocator", MmAllocator<T>{}, n, 4);
benchmark_allocator_threaded("MemoryPool", MemoryPool<T>{n}, n, 4);
}
}
30 changes: 12 additions & 18 deletions cmake/modules/BuildPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

INCLUDE(GenQrc)

MACRO(BUILD_PLUGIN PLUGIN_NAME)
CMAKE_PARSE_ARGUMENTS(PLUGIN "" "LINK;EXPORT_BASE_NAME" "MOCFILES;EMBEDDED_RESOURCES;UICFILES" ${ARGN})
FUNCTION(BUILD_PLUGIN PLUGIN_NAME)
CMAKE_PARSE_ARGUMENTS(PLUGIN "" "DESTINATION;LINK;EXPORT_BASE_NAME" "MOCFILES;EMBEDDED_RESOURCES;UICFILES" ${ARGN})
SET(PLUGIN_SOURCES ${PLUGIN_UNPARSED_ARGUMENTS})

IF(NOT PLUGIN_DESTINATION)
SET(PLUGIN_DESTINATION "${PLUGIN_DIR}")
ENDIF()

INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_BINARY_DIR}" "${CMAKE_SOURCE_DIR}/include")

ADD_DEFINITIONS(-DPLUGIN_NAME=${PLUGIN_NAME})
Expand Down Expand Up @@ -56,22 +60,13 @@ MACRO(BUILD_PLUGIN PLUGIN_NAME)

ADD_LIBRARY(${PLUGIN_NAME} ${PLUGIN_LINK} ${PLUGIN_SOURCES} ${plugin_MOC_out} ${RCC_OUT})

TARGET_LINK_LIBRARIES(${PLUGIN_NAME} Qt5::Widgets Qt5::Xml)

IF(LMMS_BUILD_WIN32)
TARGET_LINK_LIBRARIES(${PLUGIN_NAME} lmms)
ENDIF(LMMS_BUILD_WIN32)
TARGET_LINK_LIBRARIES(${PLUGIN_NAME} Qt5::Widgets Qt5::Xml lmms)

INSTALL(TARGETS ${PLUGIN_NAME} DESTINATION "${PLUGIN_DIR}")
INSTALL(TARGETS ${PLUGIN_NAME} DESTINATION "${PLUGIN_DESTINATION}")

IF(LMMS_BUILD_APPLE)
IF ("${PLUGIN_LINK}" STREQUAL "SHARED")
SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
ELSE()
SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES LINK_FLAGS "-bundle_loader \"${CMAKE_BINARY_DIR}/lmms\"")
ENDIF()
ADD_DEPENDENCIES(${PLUGIN_NAME} lmms)
ENDIF(LMMS_BUILD_APPLE)
IF(LMMS_BUILD_APPLE AND "${PLUGIN_LINK}" STREQUAL "SHARED")
SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
ENDIF()
IF(LMMS_BUILD_WIN32 AND STRIP)
SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES PREFIX "")
ADD_CUSTOM_COMMAND(TARGET ${PLUGIN_NAME} POST_BUILD COMMAND ${STRIP} "$<TARGET_FILE:${PLUGIN_NAME}>")
Expand All @@ -89,5 +84,4 @@ MACRO(BUILD_PLUGIN PLUGIN_NAME)
TARGET_INCLUDE_DIRECTORIES(${PLUGIN_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)
ENDMACRO(BUILD_PLUGIN)

ENDFUNCTION(BUILD_PLUGIN)
4 changes: 2 additions & 2 deletions cmake/modules/CheckSubmodules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ SET(ENV{LC_ALL} "C")
SET(ENV{LANG} "en_US")

# Assume alpha-numeric paths
STRING(REGEX MATCHALL "path = [-0-9A-Za-z/]+" SUBMODULE_LIST ${SUBMODULE_DATA})
STRING(REGEX MATCHALL "url = [.:%-0-9A-Za-z/]+" SUBMODULE_URL_LIST ${SUBMODULE_DATA})
STRING(REGEX MATCHALL "path = [-0-9A-Za-z/_]+" SUBMODULE_LIST ${SUBMODULE_DATA})
STRING(REGEX MATCHALL "url = [.:%-0-9A-Za-z/_]+" SUBMODULE_URL_LIST ${SUBMODULE_DATA})

FOREACH(_part ${SUBMODULE_LIST})
STRING(REPLACE "path = " "" SUBMODULE_PATH ${_part})
Expand Down
26 changes: 26 additions & 0 deletions cmake/modules/DetectMachine.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
INCLUDE(CheckCXXSourceCompiles)

IF(WIN32)
SET(LMMS_BUILD_WIN32 1)
ELSEIF(APPLE)
Expand Down Expand Up @@ -86,3 +88,27 @@ IF(LMMS_BUILD_APPLE)
STRING(REGEX REPLACE "\\.[0-9]*$" "" APPLE_OS_VER "${APPLE_OS_VER}")
SET(CMAKE_MACOSX_RPATH 1)
ENDIF()

# Detect MinGW
IF(WIN32 AND CMAKE_COMPILER_IS_GNUCXX)
SET(MINGW TRUE)
ENDIF()

# Detect MINGW thread support
IF(MINGW)
CHECK_CXX_SOURCE_COMPILES("
#include <mutex>
#include <thread>
int main(int argc, const char* argv[]) {
std::mutex m;
std::this_thread::yield();
return 0;
}
" HAS_STD_THREADS)
IF(NOT HAS_STD_THREADS)
SET(NEED_MINGW_THREADS_REPLACEMENT TRUE)
ELSE()
SET(NEED_MINGW_THREADS_REPLACEMENT FALSE)
ENDIF()
MESSAGE(NEED_MINGW_THREADS_REPLACEMENT ${NEED_MINGW_THREADS_REPLACEMENT})
ENDIF()
5 changes: 4 additions & 1 deletion cmake/toolchains/common/Ubuntu-MinGW-W64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
set(MINGW_PREFIX /usr/${TOOLCHAIN_PREFIX})

set(CMAKE_FIND_ROOT_PATH ${MINGW_PREFIX})
SET(ENV{PKG_CONFIG} /usr/bin/${TOOLCHAIN_PREFIX}-pkg-config)

IF(WIN64)
SET(TOOLCHAIN_PREFIX32 ${CMAKE_SYSTEM_PROCESSOR32}-w64-mingw32)
SET(CMAKE_C_COMPILER32 ${TOOLCHAIN_PREFIX32}-gcc)
SET(CMAKE_CXX_COMPILER32 ${TOOLCHAIN_PREFIX32}-g++)
set(MINGW_PREFIX32 /usr/${TOOLCHAIN_PREFIX32})
ENDIF()

INCLUDE(${CMAKE_CURRENT_LIST_DIR}/WinCrossCompile.cmake)
2 changes: 1 addition & 1 deletion include/AudioPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>

#include "MemoryManager.h"
#include "Memory.h"
#include "PlayHandle.h"

class EffectChain;
Expand Down
2 changes: 1 addition & 1 deletion include/AutomatableModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "Model.h"
#include "MidiTime.h"
#include "ValueBuffer.h"
#include "MemoryManager.h"
#include "Memory.h"
#include "ModelVisitor.h"

// simple way to map a property of a view to a model
Expand Down
2 changes: 1 addition & 1 deletion include/BasicFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "lmms_basics.h"
#include "lmms_constants.h"
#include "interpolation.h"
#include "MemoryManager.h"
#include "Memory.h"

template<ch_cnt_t CHANNELS=DEFAULT_CHANNELS> class BasicFilters;

Expand Down
17 changes: 4 additions & 13 deletions include/BufferManager.h → include/BufferPool.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* BufferManager.h - A buffer caching/memory management system
* BufferPool.h
*
* Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
* Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
Expand All @@ -23,25 +23,16 @@
*
*/

#ifndef BUFFER_MANAGER_H
#define BUFFER_MANAGER_H
#pragma once
Copy link
Member

Choose a reason for hiding this comment

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

I saw you're using #pragma once instead of macro guards.
I know this has some advantages and most major compilers support this. However, Do we need to change existing files? Also, do we need to consider some compilers which don't support this directive?

Copy link
Member Author

Choose a reason for hiding this comment

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

No need to change existing files. Personally I don't know of any relevant compilers that don't support this and have never had #pragma once become a problem on any platform.


#include "lmms_export.h"
#include "lmms_basics.h"

class LMMS_EXPORT BufferManager
/// Legacy interface for buffer re-use. Uses MemoryPool internally now.
class LMMS_EXPORT BufferPool
{
public:
static void init( fpp_t framesPerPeriod );
static sampleFrame * acquire();
// audio-buffer-mgm
static void clear( sampleFrame * ab, const f_cnt_t frames,
const f_cnt_t offset = 0 );
#ifndef LMMS_DISABLE_SURROUND
static void clear( surroundSampleFrame * ab, const f_cnt_t frames,
const f_cnt_t offset = 0 );
#endif
static void release( sampleFrame * buf );
};

#endif
2 changes: 1 addition & 1 deletion include/DataFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <QDomDocument>

#include "lmms_export.h"
#include "MemoryManager.h"
#include "Memory.h"

class QTextStream;

Expand Down
2 changes: 1 addition & 1 deletion include/Delay.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "lmms_basics.h"
#include "lmms_math.h"
#include "interpolation.h"
#include "MemoryManager.h"
#include "Memory.h"

// brief usage

Expand Down
Loading