Reduced memory consumption on network level.
This commit is contained in:
parent
bb96684daf
commit
1b875fdabb
17 changed files with 462 additions and 205 deletions
103
src/base/net/tools/LineReader.cpp
Normal file
103
src/base/net/tools/LineReader.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
/* XMRig
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "base/net/tools/LineReader.h"
|
||||
#include "base/net/tools/NetBuffer.h"
|
||||
#include "base/kernel/interfaces/ILineListener.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
xmrig::LineReader::~LineReader()
|
||||
{
|
||||
NetBuffer::release(m_buf);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::LineReader::parse(char *data, size_t size)
|
||||
{
|
||||
assert(m_listener != nullptr && size > 0);
|
||||
if (!m_listener || size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_buf) {
|
||||
return getline(data, size);
|
||||
}
|
||||
|
||||
add(data, size);
|
||||
getline(m_buf, m_pos);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::LineReader::reset()
|
||||
{
|
||||
if (m_buf) {
|
||||
NetBuffer::release(m_buf);
|
||||
m_buf = nullptr;
|
||||
m_pos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void xmrig::LineReader::add(const char *data, size_t size)
|
||||
{
|
||||
if (size > NetBuffer::kChunkSize - m_pos) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_buf) {
|
||||
m_buf = NetBuffer::allocate();
|
||||
m_pos = 0;
|
||||
}
|
||||
|
||||
memcpy(m_buf + m_pos, data, size);
|
||||
m_pos += size;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::LineReader::getline(char *data, size_t size)
|
||||
{
|
||||
char *end = nullptr;
|
||||
char *start = data;
|
||||
size_t remaining = size;
|
||||
|
||||
while ((end = static_cast<char*>(memchr(start, '\n', remaining))) != nullptr) {
|
||||
*end = '\0';
|
||||
|
||||
end++;
|
||||
|
||||
const auto len = static_cast<size_t>(end - start);
|
||||
if (len > 1) {
|
||||
m_listener->onLine(start, len - 1);
|
||||
}
|
||||
|
||||
remaining -= len;
|
||||
start = end;
|
||||
}
|
||||
|
||||
if (remaining == 0) {
|
||||
return reset();
|
||||
}
|
||||
|
||||
if (!m_buf || m_buf != data) {
|
||||
add(start, remaining);
|
||||
}
|
||||
}
|
62
src/base/net/tools/LineReader.h
Normal file
62
src/base/net/tools/LineReader.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
/* XMRig
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_LINEREADER_H
|
||||
#define XMRIG_LINEREADER_H
|
||||
|
||||
|
||||
#include "base/tools/Object.h"
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class ILineListener;
|
||||
|
||||
|
||||
class LineReader
|
||||
{
|
||||
public:
|
||||
XMRIG_DISABLE_COPY_MOVE(LineReader)
|
||||
|
||||
LineReader() = default;
|
||||
LineReader(ILineListener *listener) : m_listener(listener) {}
|
||||
~LineReader();
|
||||
|
||||
inline void setListener(ILineListener *listener) { m_listener = listener; }
|
||||
|
||||
void parse(char *data, size_t size);
|
||||
void reset();
|
||||
|
||||
private:
|
||||
void add(const char *data, size_t size);
|
||||
void getline(char *data, size_t size);
|
||||
|
||||
char *m_buf = nullptr;
|
||||
ILineListener *m_listener = nullptr;
|
||||
size_t m_pos = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_NETBUFFER_H */
|
96
src/base/net/tools/MemPool.h
Normal file
96
src/base/net/tools/MemPool.h
Normal file
|
@ -0,0 +1,96 @@
|
|||
/* XMRig
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_MEMPOOL_H
|
||||
#define XMRIG_MEMPOOL_H
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
template<size_t CHUNK_SIZE, size_t INIT_SIZE>
|
||||
class MemPool
|
||||
{
|
||||
public:
|
||||
MemPool() = default;
|
||||
|
||||
|
||||
constexpr size_t chunkSize() const { return CHUNK_SIZE; }
|
||||
inline size_t freeSize() const { return m_free.size() * CHUNK_SIZE; }
|
||||
inline size_t size() const { return m_data.size() * CHUNK_SIZE * INIT_SIZE; }
|
||||
|
||||
|
||||
inline char *allocate()
|
||||
{
|
||||
if (m_free.empty()) {
|
||||
resize();
|
||||
}
|
||||
|
||||
const size_t i = *m_free.begin();
|
||||
const size_t r = i / INIT_SIZE;
|
||||
|
||||
char *ptr = m_data[r].data() + (i - r * INIT_SIZE) * CHUNK_SIZE;
|
||||
|
||||
m_used.insert({ ptr, i });
|
||||
m_free.erase(i);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
inline void deallocate(const char *ptr)
|
||||
{
|
||||
if (ptr == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert(m_used.count(ptr));
|
||||
|
||||
m_free.emplace(m_used[ptr]);
|
||||
m_used.erase(ptr);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
inline void resize()
|
||||
{
|
||||
const size_t index = m_data.size();
|
||||
m_data[index];
|
||||
|
||||
for (size_t i = 0; i < INIT_SIZE; ++i) {
|
||||
m_free.emplace((index * INIT_SIZE) + i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::map<const char *, size_t> m_used;
|
||||
std::map<size_t, std::array<char, CHUNK_SIZE * INIT_SIZE> > m_data;
|
||||
std::set<size_t> m_free;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_MEMPOOL_H */
|
83
src/base/net/tools/NetBuffer.cpp
Normal file
83
src/base/net/tools/NetBuffer.cpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
/* XMRig
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "base/net/tools/MemPool.h"
|
||||
#include "base/net/tools/NetBuffer.h"
|
||||
|
||||
|
||||
#include <cassert>
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
static constexpr size_t kInitSize = 4;
|
||||
static MemPool<NetBuffer::kChunkSize, kInitSize> *pool = nullptr;
|
||||
|
||||
|
||||
inline MemPool<NetBuffer::kChunkSize, kInitSize> *getPool()
|
||||
{
|
||||
if (!pool) {
|
||||
pool = new MemPool<NetBuffer::kChunkSize, kInitSize>();
|
||||
}
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
|
||||
} // namespace xmrig
|
||||
|
||||
|
||||
char *xmrig::NetBuffer::allocate()
|
||||
{
|
||||
return getPool()->allocate();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::NetBuffer::destroy()
|
||||
{
|
||||
if (!pool) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert(pool->freeSize() == pool->size());
|
||||
|
||||
delete pool;
|
||||
pool = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::NetBuffer::onAlloc(uv_handle_t *, size_t, uv_buf_t *buf)
|
||||
{
|
||||
buf->base = getPool()->allocate();
|
||||
buf->len = kChunkSize;
|
||||
}
|
||||
|
||||
|
||||
void xmrig::NetBuffer::release(const char *buf)
|
||||
{
|
||||
getPool()->deallocate(buf);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::NetBuffer::release(const uv_buf_t *buf)
|
||||
{
|
||||
getPool()->deallocate(buf->base);
|
||||
}
|
49
src/base/net/tools/NetBuffer.h
Normal file
49
src/base/net/tools/NetBuffer.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
/* XMRig
|
||||
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_NETBUFFER_H
|
||||
#define XMRIG_NETBUFFER_H
|
||||
|
||||
|
||||
struct uv_buf_t;
|
||||
using uv_handle_t = struct uv_handle_s;
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class NetBuffer
|
||||
{
|
||||
public:
|
||||
static constexpr size_t kChunkSize = 16 * 1024;
|
||||
|
||||
static char *allocate();
|
||||
static void destroy();
|
||||
static void onAlloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
|
||||
static void release(const char *buf);
|
||||
static void release(const uv_buf_t *buf);
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_NETBUFFER_H */
|
|
@ -1,99 +0,0 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_RECVBUF_H
|
||||
#define XMRIG_RECVBUF_H
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#include "base/kernel/interfaces/ILineListener.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
template<size_t N>
|
||||
class RecvBuf
|
||||
{
|
||||
public:
|
||||
inline RecvBuf() :
|
||||
m_buf(),
|
||||
m_pos(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline char *base() { return m_buf; }
|
||||
inline char *current() { return m_buf + m_pos; }
|
||||
inline const char *base() const { return m_buf; }
|
||||
inline const char *current() const { return m_buf + m_pos; }
|
||||
inline size_t available() const { return N - m_pos; }
|
||||
inline size_t pos() const { return m_pos; }
|
||||
inline void nread(size_t size) { m_pos += size; }
|
||||
inline void reset() { m_pos = 0; }
|
||||
|
||||
constexpr inline size_t size() const { return N; }
|
||||
|
||||
inline void getline(ILineListener *listener)
|
||||
{
|
||||
char *end;
|
||||
char *start = m_buf;
|
||||
size_t remaining = m_pos;
|
||||
|
||||
while ((end = static_cast<char*>(memchr(start, '\n', remaining))) != nullptr) {
|
||||
*end = '\0';
|
||||
|
||||
end++;
|
||||
const size_t len = static_cast<size_t>(end - start);
|
||||
|
||||
listener->onLine(start, len - 1);
|
||||
|
||||
remaining -= len;
|
||||
start = end;
|
||||
}
|
||||
|
||||
if (remaining == 0) {
|
||||
m_pos = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (start == m_buf) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(m_buf, start, remaining);
|
||||
m_pos = remaining;
|
||||
}
|
||||
|
||||
private:
|
||||
char m_buf[N];
|
||||
size_t m_pos;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_RECVBUF_H */
|
Loading…
Add table
Add a link
Reference in a new issue