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

Proper disable pool allocations. + Config oatpp compiler options from… #49

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,52 @@ option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(OATPP_INSTALL "Create installation target for oat++" ON)
option(OATPP_BUILD_TESTS "Create test target for oat++" ON)

###################################################################################################
## COMPILATION CONFIG #############################################################################
###################################################################################################

option(OATPP_DISABLE_ENV_OBJECT_COUNTERS "Disable object counting for Release builds for better performance" OFF)
option(OATPP_DISABLE_POOL_ALLOCATIONS "This will make oatpp::base::memory::MemoryPool, method obtain and free call new and delete directly" OFF)

set(OATPP_THREAD_HARDWARE_CONCURRENCY "AUTO" CACHE STRING "Predefined value for function oatpp::concurrency::Thread::getHardwareConcurrency()")
set(OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT "10" CACHE STRING "Number of shards of ThreadDistributedMemoryPool")
set(OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT "2" CACHE STRING "oatpp::async::Executor default number of threads")

## Print config ##################################################################################

message("\n############################################################################")
message("## oatpp module compilation config:\n")

message("OATPP_DISABLE_ENV_OBJECT_COUNTERS=${OATPP_DISABLE_ENV_OBJECT_COUNTERS}")
message("OATPP_DISABLE_POOL_ALLOCATIONS=${OATPP_DISABLE_POOL_ALLOCATIONS}")
message("OATPP_THREAD_HARDWARE_CONCURRENCY=${OATPP_THREAD_HARDWARE_CONCURRENCY}")
message("OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT=${OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT}")
message("OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT=${OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT}")

## Set definitions ###############################################################################

if(OATPP_DISABLE_ENV_OBJECT_COUNTERS)
add_definitions(-DOATPP_DISABLE_ENV_OBJECT_COUNTERS)
endif()

if(OATPP_DISABLE_POOL_ALLOCATIONS)
add_definitions (-DOATPP_DISABLE_POOL_ALLOCATIONS)
endif()

set(AUTO_VALUE AUTO)
if(NOT OATPP_THREAD_HARDWARE_CONCURRENCY STREQUAL AUTO_VALUE)
add_definitions (-DOATPP_THREAD_HARDWARE_CONCURRENCY=${OATPP_THREAD_HARDWARE_CONCURRENCY})
endif()

add_definitions (
-DOATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT=${OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT}
-DOATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT=${OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT}
)

message("\n############################################################################\n")

###################################################################################################

message("oatpp version: '${OATPP_THIS_MODULE_VERSION}'")

add_subdirectory(src)
Expand Down
2 changes: 1 addition & 1 deletion src/oatpp/core/async/Executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Executor {
std::atomic<v_word32> m_balancer;
public:

Executor(v_int32 threadsCount)
Executor(v_int32 threadsCount = OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT)
: m_threadsCount(threadsCount)
, m_threads(new std::shared_ptr<oatpp::concurrency::Thread>[m_threadsCount])
, m_processors(new std::shared_ptr<SubmissionProcessor>[m_threadsCount])
Expand Down
6 changes: 3 additions & 3 deletions src/oatpp/core/base/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
#endif

/**
* AsyncHttpConnectionHandler default number of threads
* oatpp::async::Executor default number of threads
*/
#ifndef OATPP_ASYNC_HTTP_CONNECTION_HANDLER_THREAD_NUM_DEFAULT
#define OATPP_ASYNC_HTTP_CONNECTION_HANDLER_THREAD_NUM_DEFAULT 2
#ifndef OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT
#define OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT 2
#endif

/**
Expand Down
19 changes: 19 additions & 0 deletions src/oatpp/core/base/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ void Environment::setLogger(Logger* logger){
m_logger = logger;
}

void Environment::printCompilationConfig() {

#ifdef OATPP_DISABLE_ENV_OBJECT_COUNTERS
OATPP_LOGD("oatpp/Config", "OATPP_DISABLE_ENV_OBJECT_COUNTERS");
#endif

#ifdef OATPP_DISABLE_POOL_ALLOCATIONS
OATPP_LOGD("oatpp/Config", "OATPP_DISABLE_POOL_ALLOCATIONS");
#endif

#ifdef OATPP_THREAD_HARDWARE_CONCURRENCY
OATPP_LOGD("oatpp/Config", "OATPP_THREAD_HARDWARE_CONCURRENCY=%d", OATPP_THREAD_HARDWARE_CONCURRENCY);
#endif

OATPP_LOGD("oatpp/Config", "OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT=%d", OATPP_THREAD_DISTRIBUTED_MEM_POOL_SHARDS_COUNT);
OATPP_LOGD("oatpp/Config", "OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT=%d\n", OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT);

}

void Environment::log(v_int32 priority, const std::string& tag, const std::string& message) {
if(m_logger != nullptr) {
m_logger->log(priority, tag, message);
Expand Down
2 changes: 2 additions & 0 deletions src/oatpp/core/base/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Environment{
static v_counter getThreadLocalObjectsCreated();

static void setLogger(Logger* logger);

static void printCompilationConfig();

static void log(v_int32 priority, const std::string& tag, const std::string& message);
static void logFormatted(v_int32 priority, const std::string& tag, const char* message, ...);
Expand Down
4 changes: 4 additions & 0 deletions src/oatpp/core/base/memory/MemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
namespace oatpp { namespace base { namespace memory {

void MemoryPool::allocChunk() {
#ifdef OATPP_DISABLE_POOL_ALLOCATIONS
// DO NOTHING
#else
v_int32 entryBlockSize = sizeof(EntryHeader) + m_entrySize;
v_int32 chunkMemSize = entryBlockSize * m_chunkSize;
p_char8 mem = new v_char8[chunkMemSize];
Expand All @@ -37,6 +40,7 @@ void MemoryPool::allocChunk() {
EntryHeader* entry = new (mem + i * entryBlockSize) EntryHeader(this, m_id, m_rootEntry);
m_rootEntry = entry;
}
#endif
}

void* MemoryPool::obtain() {
Expand Down
4 changes: 2 additions & 2 deletions src/oatpp/web/server/AsyncHttpConnectionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AsyncHttpConnectionHandler : public base::Controllable, public network::se
public:

AsyncHttpConnectionHandler(const std::shared_ptr<HttpRouter>& router,
v_int32 threadCount = OATPP_ASYNC_HTTP_CONNECTION_HANDLER_THREAD_NUM_DEFAULT)
v_int32 threadCount = OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT)
: m_executor(std::make_shared<oatpp::async::Executor>(threadCount))
, m_router(router)
, m_errorHandler(handler::DefaultErrorHandler::createShared())
Expand All @@ -72,7 +72,7 @@ class AsyncHttpConnectionHandler : public base::Controllable, public network::se
public:

static std::shared_ptr<AsyncHttpConnectionHandler> createShared(const std::shared_ptr<HttpRouter>& router,
v_int32 threadCount = OATPP_ASYNC_HTTP_CONNECTION_HANDLER_THREAD_NUM_DEFAULT){
v_int32 threadCount = OATPP_ASYNC_EXECUTOR_THREAD_NUM_DEFAULT){
return std::make_shared<AsyncHttpConnectionHandler>(router, threadCount);
}

Expand Down
2 changes: 2 additions & 0 deletions test/oatpp/AllTestsMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Logger : public oatpp::base::Logger {

void runTests() {

oatpp::base::Environment::printCompilationConfig();

OATPP_RUN_TEST(oatpp::test::base::RegRuleTest);
OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);

Expand Down
6 changes: 4 additions & 2 deletions test/oatpp/core/base/memory/MemoryPoolTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ void testPool(v_int32 objectsNumber, v_int32 garbageNumber, v_int32 chunkSize){
}

doGarbageAllocs(pool, garbageNumber);
#ifndef OATPP_DISABLE_POOL_ALLOCATIONS
for(v_int32 i = 0; i < objectsNumber; i++){
OATPP_ASSERT(objects[i]->a == -100);
}

#else
OATPP_LOGV("TEST[base::memory::MemoryPoolTest]", "\033[35mWARNING. 'OATPP_DISABLE_POOL_ALLOCATIONS' flag is ON. Assertions disabled.\033[0m");
#endif
delete [] objects;

OATPP_ASSERT(pool.getObjectsCount() == 0);
Expand Down