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

Even more portability and performance of your code #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion runtime/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace NanoLogConfig {
// Controls in what mode the compressed log file will be opened
static const int FILE_PARAMS = O_APPEND|O_RDWR|O_CREAT|O_NOATIME|O_DSYNC;
static const char FILE_PARAMS[] = "a+";

// Location of the initial log file
static const char DEFAULT_LOG_FILE[] = "./compressedLog";
Expand Down
99 changes: 99 additions & 0 deletions runtime/DoubleBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

#pragma once
#include <condition_variable>
#include <cstdio>
#include <memory>
#include <mutex>
#include <new>

#include <unistd.h>

#include "Config.h"
#include "Portability.h"

class DoubleBuffer
{
using MemPtrT = std::unique_ptr<char[]>;

static MemPtrT allocateMemory()
{
return MemPtrT{new char[NanoLogConfig::OUTPUT_BUFFER_SIZE]};
};

static char *accessOnce(const MemPtrT &ptr)
{
NANOLOG_READ_WRITE_BARRIER;
return ptr.get();
}

MemPtrT freeBuffer;
MemPtrT compressingBuffer;
MemPtrT writeBuffer;
unsigned size;
int errorCode;

std::condition_variable condition;
std::mutex mutex;

public:
DoubleBuffer()
: freeBuffer(allocateMemory()),
compressingBuffer(allocateMemory()),
writeBuffer(nullptr),
size(0),
errorCode(0),
condition(),
mutex(){};

char *getCompressingBuffer() noexcept { return compressingBuffer.get(); }

bool writeInProgress() const { return accessOnce(freeBuffer) == nullptr; }

int swapBuffer(unsigned count) noexcept
{
while (accessOnce(writeBuffer) != nullptr) {}

{
std::unique_lock<std::mutex> lock(mutex);
size = count;
std::swap(writeBuffer, compressingBuffer);
if (freeBuffer == nullptr) {
condition.wait(lock, [this]() { return freeBuffer != nullptr; });
} else {
condition.notify_one();
}
std::swap(freeBuffer, compressingBuffer);
return errorCode;
}
}

void writeToFile(FILE* file) noexcept
{
unsigned tmp_size = 0;
MemPtrT tmp_ptr = nullptr;

{
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [this]() { return writeBuffer != nullptr; });
tmp_size = size;
std::swap(writeBuffer, tmp_ptr);
}

int res = 0;
if (tmp_size != 0) {
size_t wsize = fwrite(tmp_ptr.get(), 1u, tmp_size, file);
res = (wsize != tmp_size) ? errno : 0;
}

while (accessOnce(freeBuffer) != nullptr) {}

{
std::unique_lock<std::mutex> lock(mutex);
errorCode = res;
std::swap(freeBuffer, tmp_ptr);
if (compressingBuffer == nullptr) {
condition.notify_one();
}
}
}
};
27 changes: 27 additions & 0 deletions runtime/FSyncPortable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#ifndef _WIN32

#include <unistd.h>

#else

#include <io.h>
#include <Windows.h>

inline int fsync(int fd) {
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
return -1;
}
if (!FlushFileBuffers(h)) {
return -1;
}
return 0;
}

inline int fdatasync(int fd) {
return fsync(fd);
}

#endif
8 changes: 8 additions & 0 deletions runtime/Portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@
#define NANOLOG_PRINTF_FORMAT_ATTR(string_index, first_to_check)
#endif

#if defined(_MSC_VER)
extern "C" void _ReadWriteBarrier(void);
#pragma intrinsic(_ReadWriteBarrier)
#define NANOLOG_READ_WRITE_BARRIER _ReadWriteBarrier()
#elif defined(__GNUC__)
#define NANOLOG_READ_WRITE_BARRIER __asm__ __volatile__("" ::: "memory")
#endif

#endif /* NANOLOG_PORTABILITY_H */
Loading