mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
6.7 KiB
C
243 lines
6.7 KiB
C
4 years ago
|
/**
|
||
8 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
8 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
8 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_BUFFER_POOL_H
|
||
|
#define SRSRAN_BUFFER_POOL_H
|
||
8 years ago
|
|
||
4 years ago
|
#include "byte_buffer.h"
|
||
5 years ago
|
#include <algorithm>
|
||
|
#include <map>
|
||
8 years ago
|
#include <pthread.h>
|
||
|
#include <stack>
|
||
7 years ago
|
#include <string>
|
||
5 years ago
|
#include <vector>
|
||
8 years ago
|
|
||
|
/*******************************************************************************
|
||
|
INCLUDES
|
||
|
*******************************************************************************/
|
||
|
|
||
4 years ago
|
#include "srsran/common/common.h"
|
||
|
#include "srsran/srslog/srslog.h"
|
||
8 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
8 years ago
|
|
||
|
/******************************************************************************
|
||
|
* Buffer pool
|
||
|
*
|
||
|
* Preallocates a large number of buffer_t and provides allocate and
|
||
|
* deallocate functions. Provides quick object creation and deletion as well
|
||
5 years ago
|
* as object reuse.
|
||
8 years ago
|
* Singleton class of byte_buffer_t (but other pools of different type can be created)
|
||
|
*****************************************************************************/
|
||
|
|
||
|
template <class buffer_t>
|
||
5 years ago
|
class buffer_pool
|
||
|
{
|
||
8 years ago
|
public:
|
||
|
// non-static methods
|
||
6 years ago
|
buffer_pool(int capacity_ = -1)
|
||
8 years ago
|
{
|
||
6 years ago
|
uint32_t nof_buffers = POOL_SIZE;
|
||
6 years ago
|
if (capacity_ > 0) {
|
||
5 years ago
|
nof_buffers = (uint32_t)capacity_;
|
||
6 years ago
|
}
|
||
4 years ago
|
used.reserve(nof_buffers);
|
||
4 years ago
|
pthread_mutex_init(&mutex, nullptr);
|
||
|
pthread_cond_init(&cv_not_empty, nullptr);
|
||
5 years ago
|
for (uint32_t i = 0; i < nof_buffers; i++) {
|
||
4 years ago
|
buffer_t* b = new (std::nothrow) buffer_t;
|
||
|
if (!b) {
|
||
|
perror("Error allocating memory. Exiting...\n");
|
||
|
exit(-1);
|
||
|
}
|
||
8 years ago
|
available.push(b);
|
||
|
}
|
||
5 years ago
|
capacity = nof_buffers;
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
~buffer_pool()
|
||
|
{
|
||
|
// this destructor assumes all buffers have been properly deallocated
|
||
|
while (available.size()) {
|
||
8 years ago
|
delete available.top();
|
||
|
available.pop();
|
||
|
}
|
||
7 years ago
|
|
||
|
for (uint32_t i = 0; i < used.size(); i++) {
|
||
|
delete used[i];
|
||
|
}
|
||
6 years ago
|
pthread_cond_destroy(&cv_not_empty);
|
||
|
pthread_mutex_destroy(&mutex);
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
8 years ago
|
void print_all_buffers()
|
||
|
{
|
||
5 years ago
|
printf("%d buffers in queue\n", (int)used.size());
|
||
4 years ago
|
#ifdef SRSRAN_BUFFER_POOL_LOG_ENABLED
|
||
7 years ago
|
std::map<std::string, uint32_t> buffer_cnt;
|
||
5 years ago
|
for (uint32_t i = 0; i < used.size(); i++) {
|
||
|
buffer_cnt[strlen(used[i]->debug_name) ? used[i]->debug_name : "Undefined"]++;
|
||
7 years ago
|
}
|
||
|
std::map<std::string, uint32_t>::iterator it;
|
||
|
for (it = buffer_cnt.begin(); it != buffer_cnt.end(); it++) {
|
||
|
printf(" - %dx %s\n", it->second, it->first.c_str());
|
||
8 years ago
|
}
|
||
7 years ago
|
#endif
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
5 years ago
|
uint32_t nof_available_pdus() { return available.size(); }
|
||
7 years ago
|
|
||
5 years ago
|
bool is_almost_empty() { return available.size() < capacity / 20; }
|
||
8 years ago
|
|
||
4 years ago
|
buffer_t* allocate(const char* debug_name = nullptr, bool blocking = false)
|
||
5 years ago
|
{
|
||
8 years ago
|
pthread_mutex_lock(&mutex);
|
||
4 years ago
|
buffer_t* b = nullptr;
|
||
8 years ago
|
|
||
6 years ago
|
if (available.size() > 0) {
|
||
8 years ago
|
b = available.top();
|
||
|
used.push_back(b);
|
||
|
available.pop();
|
||
6 years ago
|
|
||
7 years ago
|
if (is_almost_empty()) {
|
||
5 years ago
|
printf("Warning buffer pool capacity is %f %%\n", (float)100 * available.size() / capacity);
|
||
8 years ago
|
}
|
||
4 years ago
|
#ifdef SRSRAN_BUFFER_POOL_LOG_ENABLED
|
||
6 years ago
|
if (debug_name) {
|
||
4 years ago
|
strncpy(b->debug_name, debug_name, SRSRAN_BUFFER_POOL_LOG_NAME_LEN);
|
||
|
b->debug_name[SRSRAN_BUFFER_POOL_LOG_NAME_LEN - 1] = 0;
|
||
6 years ago
|
}
|
||
8 years ago
|
#endif
|
||
6 years ago
|
} else if (blocking) {
|
||
|
// blocking allocation
|
||
5 years ago
|
while (available.size() == 0) {
|
||
6 years ago
|
pthread_cond_wait(&cv_not_empty, &mutex);
|
||
|
}
|
||
|
|
||
|
// retrieve the new buffer
|
||
|
b = available.top();
|
||
|
used.push_back(b);
|
||
|
available.pop();
|
||
|
|
||
|
// do not print any warning
|
||
5 years ago
|
} else {
|
||
8 years ago
|
printf("Error - buffer pool is empty\n");
|
||
5 years ago
|
|
||
4 years ago
|
#ifdef SRSRAN_BUFFER_POOL_LOG_ENABLED
|
||
8 years ago
|
print_all_buffers();
|
||
|
#endif
|
||
8 years ago
|
}
|
||
|
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
return b;
|
||
|
}
|
||
5 years ago
|
|
||
|
bool deallocate(buffer_t* b)
|
||
8 years ago
|
{
|
||
5 years ago
|
bool ret = false;
|
||
8 years ago
|
pthread_mutex_lock(&mutex);
|
||
|
typename std::vector<buffer_t*>::iterator elem = std::find(used.begin(), used.end(), b);
|
||
|
if (elem != used.end()) {
|
||
5 years ago
|
used.erase(elem);
|
||
8 years ago
|
available.push(b);
|
||
5 years ago
|
ret = true;
|
||
8 years ago
|
}
|
||
6 years ago
|
pthread_cond_signal(&cv_not_empty);
|
||
8 years ago
|
pthread_mutex_unlock(&mutex);
|
||
5 years ago
|
return ret;
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
private:
|
||
6 years ago
|
static const int POOL_SIZE = 4096;
|
||
8 years ago
|
std::stack<buffer_t*> available;
|
||
5 years ago
|
std::vector<buffer_t*> used;
|
||
6 years ago
|
pthread_mutex_t mutex;
|
||
|
pthread_cond_t cv_not_empty;
|
||
5 years ago
|
uint32_t capacity;
|
||
8 years ago
|
};
|
||
|
|
||
5 years ago
|
class byte_buffer_pool
|
||
|
{
|
||
4 years ago
|
using mem_chunk = typename std::aligned_storage<sizeof(byte_buffer_t), alignof(byte_buffer_t)>::type;
|
||
|
|
||
5 years ago
|
public:
|
||
8 years ago
|
// Singleton static methods
|
||
4 years ago
|
static byte_buffer_pool* get_instance(int capacity = -1)
|
||
|
{
|
||
|
static std::unique_ptr<byte_buffer_pool> instance(new byte_buffer_pool(capacity));
|
||
|
return instance.get();
|
||
|
}
|
||
4 years ago
|
byte_buffer_pool(int capacity = -1) : pool(capacity) {}
|
||
6 years ago
|
byte_buffer_pool(const byte_buffer_pool& other) = delete;
|
||
4 years ago
|
byte_buffer_pool(byte_buffer_pool&& other) = delete;
|
||
6 years ago
|
byte_buffer_pool& operator=(const byte_buffer_pool& other) = delete;
|
||
4 years ago
|
byte_buffer_pool& operator=(byte_buffer_pool&& other) = delete;
|
||
|
void* allocate(const char* debug_name = nullptr, bool blocking = false)
|
||
5 years ago
|
{
|
||
4 years ago
|
return pool.allocate(debug_name, blocking);
|
||
8 years ago
|
}
|
||
4 years ago
|
void enable_logger(bool enabled) { print_to_log = enabled; }
|
||
4 years ago
|
void deallocate(void* b)
|
||
5 years ago
|
{
|
||
|
if (!b) {
|
||
7 years ago
|
return;
|
||
|
}
|
||
4 years ago
|
if (!pool.deallocate(static_cast<mem_chunk*>(b))) {
|
||
4 years ago
|
#ifdef SRSRAN_BUFFER_POOL_LOG_ENABLED
|
||
4 years ago
|
print_error("Error deallocating PDU: Addr=0x%p, name=%s not found in pool", (void*)b, b->debug_name);
|
||
6 years ago
|
#else
|
||
4 years ago
|
print_error("Error deallocating PDU: Addr=0x%p", (void*)b);
|
||
6 years ago
|
#endif
|
||
6 years ago
|
}
|
||
8 years ago
|
}
|
||
4 years ago
|
void print_all_buffers() { pool.print_all_buffers(); }
|
||
5 years ago
|
|
||
8 years ago
|
private:
|
||
4 years ago
|
/// Formats and prints the input string and arguments into the configured output stream.
|
||
|
template <typename... Args>
|
||
|
void print_error(const char* str, Args&&... args)
|
||
|
{
|
||
|
if (print_to_log) {
|
||
|
srslog::fetch_basic_logger("POOL", false).error(str, std::forward<Args>(args)...);
|
||
|
} else {
|
||
|
fmt::printf(std::string(str) + "\n", std::forward<Args>(args)...);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private:
|
||
4 years ago
|
bool print_to_log = false;
|
||
|
buffer_pool<mem_chunk> pool;
|
||
8 years ago
|
};
|
||
|
|
||
4 years ago
|
inline unique_byte_buffer_t make_byte_buffer() noexcept
|
||
|
{
|
||
|
return std::unique_ptr<byte_buffer_t>(new (std::nothrow) byte_buffer_t());
|
||
|
}
|
||
|
|
||
4 years ago
|
inline unique_byte_buffer_t make_byte_buffer(uint32_t size, uint8_t value) noexcept
|
||
|
{
|
||
|
return std::unique_ptr<byte_buffer_t>(new (std::nothrow) byte_buffer_t(size, value));
|
||
|
}
|
||
|
|
||
4 years ago
|
inline unique_byte_buffer_t make_byte_buffer(const char* debug_ctxt) noexcept
|
||
|
{
|
||
|
std::unique_ptr<byte_buffer_t> buffer(new (std::nothrow) byte_buffer_t());
|
||
|
if (buffer == nullptr) {
|
||
|
srslog::fetch_basic_logger("POOL").error("Failed to allocate byte buffer in %s", debug_ctxt);
|
||
|
}
|
||
|
return buffer;
|
||
6 years ago
|
}
|
||
8 years ago
|
|
||
4 years ago
|
} // namespace srsran
|
||
8 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_BUFFER_POOL_H
|