ccls/src/message_queue.cc

418 lines
13 KiB
C++
Raw Normal View History

2017-03-19 22:06:22 +00:00
#include "message_queue.h"
#include <cassert>
2017-03-28 01:04:37 +00:00
#include <cstring>
2017-03-20 05:43:48 +00:00
#include <functional>
#include <iostream>
#include <thread>
2017-03-25 20:24:42 +00:00
#include <doctest/doctest.h>
2017-03-20 05:43:48 +00:00
#include "platform.h"
2017-03-21 05:40:14 +00:00
#include "resizable_buffer.h"
2017-03-25 20:15:00 +00:00
#include "utils.h"
2017-03-21 05:40:14 +00:00
// TODO: figure out a logging solution
//#define MESSAGE_QUEUE_LOG
2017-03-20 05:43:48 +00:00
namespace {
const int kMinimumPartialPayloadSize = 128;
2017-03-21 05:40:14 +00:00
struct MessageHeader {
MessageHeader(uint32_t partial_id, bool has_more_chunks, size_t size)
2017-03-21 05:51:16 +00:00
: partial_id(partial_id), has_more_chunks(has_more_chunks), size(size) {}
2017-03-21 05:40:14 +00:00
uint32_t partial_id;
bool has_more_chunks;
size_t size;
};
2017-03-20 05:43:48 +00:00
struct BufferMessageIterator {
static BufferMessageIterator Begin(void* buffer, size_t bytes_used) {
if (bytes_used == 0)
return End();
return BufferMessageIterator(buffer, bytes_used);
}
static BufferMessageIterator End() {
return BufferMessageIterator(nullptr, 0);
}
// Start of buffer to iterate.
2017-03-21 05:40:14 +00:00
uint8_t* buffer;
2017-03-20 05:43:48 +00:00
// Number of bytes left in buffer to parse.
size_t remaining_bytes;
BufferMessageIterator(void* buffer, size_t remaining_bytes)
2017-03-21 05:51:16 +00:00
: buffer(reinterpret_cast<uint8_t*>(buffer)),
remaining_bytes(remaining_bytes) {}
2017-03-20 05:43:48 +00:00
2017-03-21 05:40:14 +00:00
MessageHeader* get() const {
return reinterpret_cast<MessageHeader*>(buffer);
2017-03-20 05:43:48 +00:00
}
2017-03-21 05:40:14 +00:00
MessageHeader* operator*() const { return get(); }
MessageHeader* operator->() const { return get(); }
2017-03-20 05:43:48 +00:00
void operator++() {
2017-03-21 05:40:14 +00:00
size_t next_message_offset = sizeof(MessageHeader) + get()->size;
2017-03-20 05:43:48 +00:00
if (next_message_offset >= remaining_bytes) {
assert(next_message_offset == remaining_bytes);
buffer = nullptr;
remaining_bytes = 0;
return;
}
2017-03-21 05:40:14 +00:00
buffer = buffer + next_message_offset;
2017-03-20 05:43:48 +00:00
remaining_bytes -= next_message_offset;
}
2017-03-21 05:40:14 +00:00
void* message_data() const {
return reinterpret_cast<void*>(buffer + sizeof(MessageHeader));
}
2017-03-20 05:43:48 +00:00
bool operator==(const BufferMessageIterator& other) const {
return buffer == other.buffer && remaining_bytes == other.remaining_bytes;
}
bool operator!=(const BufferMessageIterator& other) const {
return !(*this == other);
}
};
2017-03-21 05:51:16 +00:00
enum class RepeatResult { RunAgain, Break };
2017-03-20 05:43:48 +00:00
// Run |action| an arbitrary number of times.
void Repeat(std::function<RepeatResult()> action) {
bool first = true;
2017-03-22 17:16:09 +00:00
#if defined(MESSAGE_QUEUE_LOG)
2017-03-20 05:43:48 +00:00
int log_iteration_count = 0;
int log_count = 0;
2017-03-22 17:16:09 +00:00
#endif
2017-03-20 05:43:48 +00:00
while (true) {
if (!first) {
2017-03-22 17:16:09 +00:00
#if defined(MESSAGE_QUEUE_LOG)
2017-03-20 05:43:48 +00:00
if (log_iteration_count > 1000) {
log_iteration_count = 0;
2017-03-21 05:51:16 +00:00
std::cerr << "[info]: Buffer full, waiting (" << log_count++ << ")"
<< std::endl;
2017-03-20 05:43:48 +00:00
}
++log_iteration_count;
2017-03-22 17:16:09 +00:00
#endif
2017-03-21 05:51:16 +00:00
// TODO: See if we can figure out a way to use condition variables
// cross-process.
2017-03-20 05:43:48 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(0));
}
first = false;
if (action() == RepeatResult::RunAgain)
continue;
break;
}
}
2017-03-21 05:51:16 +00:00
ResizableBuffer* CreateOrFindResizableBuffer(
2017-03-22 17:16:09 +00:00
std::unordered_map<uint32_t, std::unique_ptr<ResizableBuffer>>&
2017-03-21 05:51:16 +00:00
resizable_buffers,
uint32_t id) {
2017-03-21 05:40:14 +00:00
auto it = resizable_buffers.find(id);
if (it != resizable_buffers.end())
return it->second.get();
return (resizable_buffers[id] = MakeUnique<ResizableBuffer>()).get();
}
std::unique_ptr<Buffer> MakeBuffer(void* content, size_t size) {
auto buffer = Buffer::Create(size);
memcpy(buffer->data, content, size);
2017-03-22 17:16:09 +00:00
return buffer;
2017-03-21 05:40:14 +00:00
}
2017-03-20 05:43:48 +00:00
} // namespace
2017-03-19 22:06:22 +00:00
2017-03-21 05:40:14 +00:00
Message::Message(void* data, size_t size) : data(data), size(size) {}
2017-03-19 22:06:22 +00:00
struct MessageQueue::BufferMetadata {
2017-03-21 05:51:16 +00:00
// Reset buffer.
void reset() { total_message_bytes_ = 0; }
2017-03-21 05:40:14 +00:00
// Total number of used bytes excluding the sizeof this metadata object.
2017-03-21 05:51:16 +00:00
void add_used_bytes(size_t used_bytes) { total_message_bytes_ += used_bytes; }
2017-03-19 22:06:22 +00:00
// The total number of bytes in use.
size_t total_bytes_used_including_metadata() {
return total_message_bytes_ + sizeof(BufferMetadata);
}
// The total number of bytes currently used for messages. This does not
// include the sizeof the buffer metadata.
2017-03-19 22:08:36 +00:00
size_t total_message_bytes() { return total_message_bytes_; }
2017-03-19 22:06:22 +00:00
2017-03-20 05:43:48 +00:00
int next_partial_message_id = 0;
2017-03-21 05:51:16 +00:00
private:
2017-03-19 22:06:22 +00:00
size_t total_message_bytes_ = 0;
};
2017-03-19 22:08:36 +00:00
MessageQueue::MessageQueue(std::unique_ptr<Buffer> buffer, bool buffer_has_data)
2017-03-21 05:51:16 +00:00
: buffer_(std::move(buffer)) {
assert(buffer_->capacity >=
(sizeof(BufferMetadata) + kMinimumPartialPayloadSize));
2017-03-21 05:40:14 +00:00
2017-03-19 22:06:22 +00:00
if (!buffer_has_data)
2017-03-19 22:08:36 +00:00
new (buffer_->data) BufferMetadata();
2017-03-21 05:40:14 +00:00
local_buffer_ = Buffer::Create(buffer_->capacity - sizeof(BufferMetadata));
memset(local_buffer_->data, 0, local_buffer_->capacity);
2017-03-19 22:06:22 +00:00
}
2017-03-20 05:43:48 +00:00
void MessageQueue::Enqueue(const Message& message) {
2017-03-21 05:40:14 +00:00
#if defined(MESSAGE_QUEUE_LOG)
int count = 0;
#endif
uint32_t partial_id = 0;
uint8_t* payload_data = reinterpret_cast<uint8_t*>(message.data);
size_t payload_size = message.size;
2017-03-20 05:43:48 +00:00
Repeat([&]() {
2017-03-21 05:40:14 +00:00
#if defined(MESSAGE_QUEUE_LOG)
if (count++ > 500) {
2017-03-21 05:51:16 +00:00
std::cerr << "x500 Sending partial message payload_size=" << payload_size
<< std::endl;
2017-03-21 05:40:14 +00:00
count = 0;
}
#endif
2017-03-20 05:43:48 +00:00
auto lock = buffer_->WaitForExclusiveAccess();
// We cannot find the entire payload in the buffer. We have to send chunks
// of it over time.
2017-03-21 05:40:14 +00:00
if (payload_size >= BytesAvailableInBuffer()) {
2017-03-21 05:51:16 +00:00
// There's not enough room for our minimum payload size, so try again
// later.
if ((sizeof(MessageHeader) + kMinimumPartialPayloadSize) >
BytesAvailableInBuffer())
2017-03-20 05:43:48 +00:00
return RepeatResult::RunAgain;
2017-03-21 05:40:14 +00:00
if (partial_id == 0) {
// note: pre-increment so we use 1 as the initial value
partial_id = ++metadata()->next_partial_message_id;
}
2017-03-20 05:43:48 +00:00
2017-03-21 05:51:16 +00:00
size_t sent_payload_size =
BytesAvailableInBuffer() - sizeof(MessageHeader);
// |sent_payload_size| must always be smaller than |payload_size|. If it
// is equal to |payload_size|, than we could have sent it as a normal,
// non-partial message. It's also an error if it is larger than
// payload_size (we're sending garbage data).
2017-03-21 05:40:14 +00:00
assert(sent_payload_size < payload_size);
2017-03-20 05:43:48 +00:00
2017-03-21 05:51:16 +00:00
CopyPayloadToBuffer(partial_id, payload_data, sent_payload_size,
true /*has_more_chunks*/);
2017-03-21 05:40:14 +00:00
payload_data += sent_payload_size;
payload_size -= sent_payload_size;
2017-03-20 05:43:48 +00:00
// Prepare for next time.
return RepeatResult::RunAgain;
}
2017-03-21 05:40:14 +00:00
// The entire payload fits. Send it all now.
else {
2017-03-21 05:51:16 +00:00
// Include partial message id, as there could have been previous parts of
// this payload.
CopyPayloadToBuffer(partial_id, payload_data, payload_size,
false /*has_more_chunks*/);
2017-03-20 05:43:48 +00:00
2017-03-21 05:40:14 +00:00
#if defined(MESSAGE_QUEUE_LOG)
2017-03-21 05:51:16 +00:00
std::cerr << "Sending full message with payload_size=" << payload_size
<< std::endl;
2017-03-21 05:40:14 +00:00
#endif
return RepeatResult::Break;
}
});
}
2017-03-20 05:43:48 +00:00
2017-03-21 05:40:14 +00:00
std::vector<std::unique_ptr<Buffer>> MessageQueue::DequeueAll() {
2017-03-22 17:16:09 +00:00
std::unordered_map<uint32_t, std::unique_ptr<ResizableBuffer>>
resizable_buffers;
2017-03-19 22:06:22 +00:00
2017-03-21 05:40:14 +00:00
std::vector<std::unique_ptr<Buffer>> result;
2017-03-20 05:43:48 +00:00
2017-03-21 05:51:16 +00:00
while (true) {
2017-03-21 05:40:14 +00:00
size_t local_buffer_size = 0;
2017-03-20 05:43:48 +00:00
2017-03-21 05:40:14 +00:00
// Move data from shared memory into a local buffer. Do this
// before parsing the blocks so that other processes can begin
// posting data as soon as possible.
{
std::unique_ptr<ScopedLock> lock = buffer_->WaitForExclusiveAccess();
assert(BytesAvailableInBuffer() >= 0);
2017-03-20 05:43:48 +00:00
2017-03-21 05:40:14 +00:00
// note: Do not copy over buffer_ metadata.
local_buffer_size = metadata()->total_message_bytes();
2017-03-21 05:51:16 +00:00
memcpy(local_buffer_->data, first_message_in_buffer(), local_buffer_size);
2017-03-20 05:43:48 +00:00
2017-03-21 05:40:14 +00:00
metadata()->reset();
2017-03-20 05:43:48 +00:00
}
2017-03-21 05:40:14 +00:00
// Parse blocks from shared memory.
2017-03-21 05:51:16 +00:00
for (auto it = BufferMessageIterator::Begin(local_buffer_->data,
local_buffer_size);
it != BufferMessageIterator::End(); ++it) {
2017-03-21 05:40:14 +00:00
#if defined(MESSAGE_QUEUE_LOG)
2017-03-21 05:51:16 +00:00
std::cerr << "Got message with partial_id=" << it->partial_id
<< ", payload_size=" << it->size
<< ", has_more_chunks=" << it->has_more_chunks << std::endl;
2017-03-20 05:43:48 +00:00
#endif
2017-03-21 05:40:14 +00:00
if (it->partial_id != 0) {
2017-03-21 05:51:16 +00:00
auto* buf =
CreateOrFindResizableBuffer(resizable_buffers, it->partial_id);
2017-03-21 05:40:14 +00:00
buf->Append(it.message_data(), it->size);
if (!it->has_more_chunks) {
result.push_back(MakeBuffer(buf->buffer, buf->size));
resizable_buffers.erase(it->partial_id);
}
2017-03-21 05:51:16 +00:00
} else {
2017-03-21 05:40:14 +00:00
// Note: we can't just return pointers to |local_buffer_| because if we
// read a partial message we will invalidate all of the existing
// pointers. We could jump through hoops to make it work (ie, if no
// partial messages return pointers to local_buffer_) but it is not
// worth the effort.
assert(!it->has_more_chunks);
result.push_back(MakeBuffer(it.message_data(), it->size));
}
}
2017-03-21 05:51:16 +00:00
// We're waiting for data to be posted to result. Delay a little so we
// don't push the CPU so hard.
if (!resizable_buffers.empty())
std::this_thread::sleep_for(std::chrono::microseconds(0));
else
break;
}
2017-03-21 05:40:14 +00:00
return result;
}
2017-03-21 05:51:16 +00:00
void MessageQueue::CopyPayloadToBuffer(uint32_t partial_id,
void* payload,
size_t payload_size,
bool has_more_chunks) {
2017-03-21 05:40:14 +00:00
assert(BytesAvailableInBuffer() >= (sizeof(MessageHeader) + payload_size));
// Copy header.
MessageHeader header(partial_id, has_more_chunks, payload_size);
memcpy(first_free_address_in_buffer(), &header, sizeof(MessageHeader));
metadata()->add_used_bytes(sizeof(MessageHeader));
// Copy payload.
memcpy(first_free_address_in_buffer(), payload, payload_size);
metadata()->add_used_bytes(payload_size);
2017-03-20 05:43:48 +00:00
}
MessageQueue::BufferMetadata* MessageQueue::metadata() const {
2017-03-19 22:06:22 +00:00
return reinterpret_cast<BufferMetadata*>(buffer_->data);
2017-03-20 05:43:48 +00:00
}
size_t MessageQueue::BytesAvailableInBuffer() const {
return buffer_->capacity - metadata()->total_bytes_used_including_metadata();
}
Message* MessageQueue::first_message_in_buffer() const {
2017-03-21 05:51:16 +00:00
return reinterpret_cast<Message*>(reinterpret_cast<uint8_t*>(buffer_->data) +
sizeof(BufferMetadata));
2017-03-20 05:43:48 +00:00
}
2017-03-21 05:40:14 +00:00
void* MessageQueue::first_free_address_in_buffer() const {
if (metadata()->total_bytes_used_including_metadata() >= buffer_->capacity)
2017-03-20 05:43:48 +00:00
return nullptr;
2017-03-21 05:40:14 +00:00
return reinterpret_cast<void*>(
2017-03-21 05:51:16 +00:00
reinterpret_cast<uint8_t*>(buffer_->data) +
metadata()->total_bytes_used_including_metadata());
2017-03-21 05:40:14 +00:00
}
TEST_SUITE("MessageQueue");
TEST_CASE("simple") {
2017-03-21 05:51:16 +00:00
MessageQueue queue(Buffer::Create(kMinimumPartialPayloadSize * 5),
false /*buffer_has_data*/);
2017-03-21 05:40:14 +00:00
int data = 0;
data = 1;
queue.Enqueue(Message(&data, sizeof(data)));
data = 2;
queue.Enqueue(Message(&data, sizeof(data)));
int expected = 0;
for (std::unique_ptr<Buffer>& m : queue.DequeueAll()) {
++expected;
REQUIRE(m->capacity == sizeof(data));
int* value = reinterpret_cast<int*>(m->data);
REQUIRE(expected == *value);
}
}
TEST_CASE("large payload") {
2017-03-21 05:51:16 +00:00
MessageQueue queue(Buffer::Create(kMinimumPartialPayloadSize * 5),
false /*buffer_has_data*/);
2017-03-21 05:40:14 +00:00
// Allocate big buffer.
size_t num_ints = kMinimumPartialPayloadSize * 100;
int* sent_ints = reinterpret_cast<int*>(malloc(sizeof(int) * num_ints));
for (int i = 0; i < num_ints; ++i)
sent_ints[i] = i;
2017-03-21 05:51:16 +00:00
// Queue big buffer. Add surrounding messages to make sure they get sent
// correctly.
2017-03-21 05:40:14 +00:00
// Run in a separate thread because Enqueue will block.
volatile bool done_sending = false;
std::thread sender([&]() {
int small = 5;
queue.Enqueue(Message(&small, sizeof(small)));
queue.Enqueue(Message(sent_ints, sizeof(int) * num_ints));
queue.Enqueue(Message(&small, sizeof(small)));
done_sending = true;
});
// Receive sent messages.
{
// Keep dequeuing messages until we have three.
std::vector<std::unique_ptr<Buffer>> messages;
while (messages.size() != 3) {
for (auto& message : queue.DequeueAll())
messages.emplace_back(std::move(message));
}
sender.join();
// Small
{
REQUIRE(sizeof(int) == messages[0]->capacity);
int* value = reinterpret_cast<int*>(messages[0]->data);
REQUIRE(*value == 5);
}
// Big
{
int* received_ints = reinterpret_cast<int*>(messages[1]->data);
REQUIRE(received_ints != sent_ints);
REQUIRE(messages[1]->capacity == (sizeof(int) * num_ints));
for (int i = 0; i < num_ints; ++i) {
REQUIRE(received_ints[i] == i);
REQUIRE(received_ints[i] == sent_ints[i]);
}
}
// Small
{
REQUIRE(sizeof(int) == messages[2]->capacity);
int* value = reinterpret_cast<int*>(messages[2]->data);
REQUIRE(*value == 5);
}
}
free(sent_ints);
}
2017-03-22 17:16:09 +00:00
TEST_SUITE_END();