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 all 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 @@ -25,6 +25,9 @@
[submodule "src/3rdparty/weakjack/weakjack"]
path = src/3rdparty/weakjack/weakjack
url = https://github.com/x42/weakjack.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
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,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.
13 changes: 13 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SET(CMAKE_AUTOMOC ON)

ADD_EXECUTABLE(benchmarks
EXCLUDE_FROM_ALL
benchmark.cpp
)

# TODO replace usages of include_directories in src/CMakeLists.txt with target_include_directories
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this todo can be done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or are you referring to #7255 ?

# and remove this line (also in tests/CMakeLists.txt)
target_include_directories(benchmarks PRIVATE $<TARGET_PROPERTY:lmmsobjs,INCLUDE_DIRECTORIES>)

target_static_libraries(benchmarks PRIVATE lmmsobjs)
target_compile_features(benchmarks PRIVATE cxx_std_17)
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"

using namespace lmms;

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("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("MemoryPool", MemoryPool<T>{n}, n, 4);
}
}
4 changes: 2 additions & 2 deletions cmake/modules/CheckSubmodules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ SET(ENV{LC_ALL} "C")
SET(ENV{LANG} "en_US")

# Submodule list pairs, unparsed (WARNING: Assumes alpha-numeric paths)
STRING(REGEX MATCHALL "path = [-0-9A-Za-z/]+" SUBMODULE_LIST_RAW ${SUBMODULE_DATA})
STRING(REGEX MATCHALL "url = [.:%-0-9A-Za-z/]+" SUBMODULE_URL_RAW ${SUBMODULE_DATA})
STRING(REGEX MATCHALL "path = [-0-9A-Za-z/_]+" SUBMODULE_LIST_RAW ${SUBMODULE_DATA})
STRING(REGEX MATCHALL "url = [.:%-0-9A-Za-z/_]+" SUBMODULE_URL_RAW ${SUBMODULE_DATA})

# Submodule list pairs, parsed
SET(SUBMODULE_LIST "")
Expand Down
10 changes: 4 additions & 6 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,8 +23,7 @@
*
*/

#ifndef LMMS_BUFFER_MANAGER_H
#define LMMS_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"
Expand All @@ -34,7 +33,8 @@ namespace lmms

class SampleFrame;

class LMMS_EXPORT BufferManager
/// Legacy interface for buffer re-use. Uses MemoryPool internally now.
class LMMS_EXPORT BufferPool
{
public:
static void init( fpp_t fpp );
Expand All @@ -47,5 +47,3 @@ class LMMS_EXPORT BufferManager


} // namespace lmms

#endif // LMMS_BUFFER_MANAGER_H
87 changes: 0 additions & 87 deletions include/LocklessAllocator.h

This file was deleted.

11 changes: 5 additions & 6 deletions include/LocklessList.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef LMMS_LOCKLESS_LIST_H
#define LMMS_LOCKLESS_LIST_H

#include "LocklessAllocator.h"
#include "MemoryPool.h"

#include <atomic>

Expand All @@ -44,7 +44,7 @@ class LocklessList

LocklessList( size_t size ) :
m_first(nullptr),
m_allocator(new LocklessAllocatorT<Element>(size))
m_allocator(new MemoryPool<Element>(size))
{
}

Expand All @@ -55,7 +55,7 @@ class LocklessList

void push( T value )
{
Element * e = m_allocator->alloc();
Element * e = m_allocator->allocate();
e->value = value;
e->next = m_first.load(std::memory_order_relaxed);

Expand Down Expand Up @@ -84,14 +84,13 @@ class LocklessList

void free( Element * e )
{
m_allocator->free( e );
m_allocator->destroy( e );
}


private:
std::atomic<Element*> m_first;
LocklessAllocatorT<Element> * m_allocator;

MemoryPool<Element> * m_allocator;
} ;


Expand Down
Loading
Loading