Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
utility: refactor tls memory allocator (#25)
Browse files Browse the repository at this point in the history
remove c interface of tls memory allocator, and move
related code to utility module
  • Loading branch information
shengofsun authored Apr 23, 2018
1 parent 76733c1 commit a1cb64b
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 188 deletions.
16 changes: 0 additions & 16 deletions include/dsn/c/api_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,4 @@ extern DSN_API void dsn_coredump();
return; \
} \
} while (0)

/*!
@defgroup memory Memory Management
@ingroup service-api-utilities
Memory Management
@{
*/

/*! high-performance malloc for transient objects, i.e., their life-time is short */
extern DSN_API void *dsn_transient_malloc(uint32_t size);

/*! high-performance free for transient objects, paired with \ref dsn_transient_malloc */
extern DSN_API void dsn_transient_free(void *ptr);

/*@}*/
99 changes: 0 additions & 99 deletions include/dsn/cpp/callocator.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/dsn/cpp/task_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <dsn/cpp/rpc_stream.h>
#include <dsn/cpp/serialization.h>
#include <dsn/cpp/zlocks.h>
#include <dsn/cpp/callocator.h>
#include <dsn/utility/callocator.h>
#include <dsn/utility/utils.h>
#include <dsn/utility/autoref_ptr.h>
#include <dsn/utility/synchronize.h>
Expand Down
3 changes: 2 additions & 1 deletion include/dsn/tool-api/rpc_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
#include <dsn/utility/dlib.h>
#include <dsn/utility/blob.h>
#include <dsn/utility/link.h>
#include <dsn/cpp/callocator.h>
#include <dsn/utility/callocator.h>

#include <dsn/tool-api/auto_codes.h>
#include <dsn/tool-api/rpc_address.h>
#include <dsn/tool-api/global_config.h>
Expand Down
6 changes: 3 additions & 3 deletions include/dsn/tool-api/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@

#include <dsn/utility/ports.h>
#include <dsn/utility/extensible_object.h>
#include <dsn/utility/callocator.h>
#include <dsn/utility/utils.h>
#include <dsn/utility/binary_writer.h>
#include <dsn/tool-api/task_spec.h>
#include <dsn/tool-api/task_tracker.h>
#include <dsn/tool-api/rpc_message.h>
#include <dsn/cpp/callocator.h>
#include <dsn/tool-api/auto_codes.h>
#include <dsn/utility/utils.h>
#include <dsn/utility/binary_writer.h>

namespace dsn {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,33 @@
* THE SOFTWARE.
*/

/*
* Description:
* What is this file about?
*
* Revision history:
* xxxx-xx-xx, author, first version
* xxxx-xx-xx, author, fix bug about xxx
*/

#pragma once

#include <dsn/utility/ports.h>
#include <dsn/utility/utils.h>
#include <dsn/utility/blob.h>
#include <dsn/service_api_c.h>
#include <dsn/utility/transient_memory.h>

namespace dsn {
typedef struct tls_transient_memory_t

typedef void *(*t_allocate)(size_t);
typedef void (*t_deallocate)(void *);

/// callocate_object overrides the operator "new" and "delete", which may be useful for objects
/// who want to use custom new/delete to boost performance
template <t_allocate a, t_deallocate d>
class callocator_object
{
unsigned int magic;
size_t remain_bytes;
char block_ptr_buffer[sizeof(std::shared_ptr<char>)];
std::shared_ptr<char> *block;
char *next;
bool committed;
} tls_transient_memory_t;
public:
void *operator new(size_t size) { return a(size); }

extern __thread tls_transient_memory_t tls_trans_memory;
extern void tls_trans_mem_init(size_t default_per_block_bytes);
extern void tls_trans_mem_alloc(size_t min_size);
void operator delete(void *p) { d(p); }

extern void tls_trans_mem_next(void **ptr, size_t *sz, size_t min_size);
extern void tls_trans_mem_commit(size_t use_size);
void *operator new[](size_t size) { return a(size); }

extern blob tls_trans_mem_alloc_blob(size_t sz);
void operator delete[](void *p) { d(p); }
};

extern void *tls_trans_malloc(size_t sz);
extern void tls_trans_free(void *ptr);
/// transient_object uses tls_trans_malloc/tls_trans_free as custom memory allocate.
/// in rdsn, serveral frequenctly allocated objects(task, rpc_message, etc.)
/// are derived from transient_objects,
/// so that their memory can be mamanged by trans_memory_allocator
typedef callocator_object<tls_trans_malloc, tls_trans_free> transient_object;
}
85 changes: 85 additions & 0 deletions include/dsn/utility/transient_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Microsoft Corporation
*
* -=- Robust Distributed System Nucleus (rDSN) -=-
*
* 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.
*/

#pragma once

#include <dsn/utility/ports.h>
#include <dsn/utility/blob.h>

namespace dsn {

/// transient memory is mainly used for allocating & freeing memory in block,
/// so as to avoid the memory fragmentation problems when server is running for a long time
///
/// the key design is allocating a block with "malloc" each time, and keep it in
/// the structure "tls_trans_memory". memory allocation of other modules should try to
/// get memory from the pre-allocated block in "tls_trans_memory":
///
/// |--------------- pre-allocated block --------------------| <- tls_trans_memory.block
/// |--memory piece 1--|--memory piece 2--|--memory piece 3--|
///
/// the tls_trans_memory->block is a shared_ptr pointing to the pre-allocated block,
/// for each memory piece allocated from the block, there is also a shared-ptr pointing the whole
/// block. please refer to @tls_trans_mem_next for details
///
/// so the pre-allocated block will be free until all the shared-ptrs are destructed.
/// tls_trans_memory.block will release an old memory_block if the remaining size is
/// too small, and other shared-ptr will release the block when "tls_trans_free" calls

typedef struct tls_transient_memory_t
{
unsigned int magic;
size_t remain_bytes;
char block_ptr_buffer[sizeof(std::shared_ptr<char>)];
std::shared_ptr<char> *block;
char *next;
bool committed;
} tls_transient_memory_t;

extern __thread tls_transient_memory_t tls_trans_memory;

// initialize the default block size, should call this at the beginning of the process
void tls_trans_mem_init(size_t default_per_block_bytes);

// acquire a memory piece from current block
// "min_size" is the minimal size of the memory piece user needs,
// "*sz" stores the actual size the allocator gives, which is no less than min_size
// *ptr stores the starting position of the memory piece
// both ptr & sz shouldn't be null
//
// "tls_trans_mem_next" should be used together with "tls_trans_mem_commit"
void tls_trans_mem_next(void **ptr, size_t *sz, size_t min_size);
void tls_trans_mem_commit(size_t use_size);

// allocate a blob, the size is "sz"
blob tls_trans_mem_alloc_blob(size_t sz);

// allocate memory
void *tls_trans_malloc(size_t sz);

// free memory, ptr shouldn't be null
void tls_trans_free(void *ptr);
}
2 changes: 1 addition & 1 deletion src/core/core/disk_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <dsn/tool-api/perf_counter.h>
#include <dsn/tool-api/aio_provider.h>
#include <dsn/utility/utils.h>
#include "transient_memory.h"
#include <dsn/utility/transient_memory.h>

using namespace dsn::utils;

Expand Down
6 changes: 3 additions & 3 deletions src/core/core/rpc_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@

#include <dsn/utility/ports.h>
#include <dsn/utility/crc.h>
#include <dsn/utility/transient_memory.h>
#include <dsn/tool-api/rpc_message.h>
#include <dsn/tool-api/network.h>
#include <dsn/tool-api/message_parser.h>
#include <cctype>

#include "task_engine.h"
#include "transient_memory.h"

using namespace dsn::utils;

Expand Down Expand Up @@ -287,8 +287,8 @@ message_ex *message_ex::create_receive_message_with_standalone_header(const blob
{
message_ex *msg = new message_ex();
std::shared_ptr<char> header_holder(
static_cast<char *>(dsn_transient_malloc(sizeof(message_header))),
[](char *c) { dsn_transient_free(c); });
static_cast<char *>(dsn::tls_trans_malloc(sizeof(message_header))),
[](char *c) { dsn::tls_trans_free(c); });
msg->header = reinterpret_cast<message_header *>(header_holder.get());
memset(msg->header, 0, sizeof(message_header));
msg->buffers.emplace_back(blob(std::move(header_holder), sizeof(message_header)));
Expand Down
2 changes: 1 addition & 1 deletion src/core/core/service_api_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@

#include <dsn/utility/configuration.h>
#include <dsn/utility/filesystem.h>
#include <dsn/utility/transient_memory.h>
#include <dsn/tool-api/command_manager.h>
#include "service_engine.h"
#include "rpc_engine.h"
#include "disk_engine.h"
#include "task_engine.h"
#include "coredump.h"
#include "transient_memory.h"
#include <fstream>

#ifndef _WIN32
Expand Down
Loading

0 comments on commit a1cb64b

Please sign in to comment.