Skip to content
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ test: \
test_geom \
test_helpers \
test_options_parser \
test_ordered_batches \
test_pbf_reader \
test_pooled_string \
test_relation_roles \
Expand Down Expand Up @@ -223,6 +224,10 @@ test_options_parser: \
test/options_parser.test.o
$(CXX) $(CXXFLAGS) -o test.options_parser $^ $(INC) $(LIB) $(LDFLAGS) && ./test.options_parser

test_ordered_batches: \
test/ordered_batches.test.o
$(CXX) $(CXXFLAGS) -o test.ordered_batches $^ $(INC) $(LIB) $(LDFLAGS) && ./test.ordered_batches

test_osm_store: \
test/osm_store.test.o
$(CXX) $(CXXFLAGS) -o test.osm_store $^ $(INC) $(LIB) $(LDFLAGS) && ./test.osm_store
Expand Down
6 changes: 6 additions & 0 deletions include/mbtiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#define _MBTILES_H

#include <string>
#include <atomic>
#include <mutex>
#include <vector>
#include "external/sqlite_modern_cpp.h"
#include "ordered_batches.h"

struct PendingStatement {
int zoom;
Expand All @@ -27,6 +29,7 @@ class MBTiles {

std::shared_ptr<std::vector<PendingStatement>> pendingStatements1, pendingStatements2;
std::mutex pendingStatementsMutex;
std::atomic<size_t> pendingBytes;

void insertOrReplace(int zoom, int x, int y, const std::string& data, bool isMerge);
void flushPendingStatements();
Expand All @@ -39,6 +42,9 @@ class MBTiles {
void saveTile(int zoom, int x, int y, std::string *data, bool isMerge);
void closeForWriting();

// Tiles are written in batch order, so the file doesn't depend on thread scheduling
OrderedBatches<PendingStatement> batches;

void openForReading(std::string &filename);
void readBoundingBox(double &minLon, double &maxLon, double &minLat, double &maxLat);
void readTileList(std::vector<std::tuple<int,int,int>> &tileList);
Expand Down
157 changes: 157 additions & 0 deletions include/ordered_batches.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*! \file */
#ifndef _ORDERED_BATCHES_H
#define _ORDERED_BATCHES_H

#include <condition_variable>
#include <deque>
#include <cstdint>
#include <functional>
#include <map>
#include <mutex>
#include <utility>
#include <vector>

// Writes batches of output in the order they were handed out, not the order threads
// finish them, so the output file doesn't depend on thread scheduling.
//
// A thread collects a batch's items between begin() and end(). end() writes the batch
// if it's the next one, followed by any batches that were waiting for it; otherwise it
// holds the batch. Batches must be numbered 0, 1, 2, ... in the order they're handed out,
// and a thread calls waitForRoom() before it takes its next batch: that keeps what is
// held below the limit set with setMaxHeldBytes(). The batch everyone waits for is always
// being worked on by a thread that isn't waiting, so waitForRoom() always returns.
//
// writeItem is called by one thread at a time, with writeMutex() held, in batch order.
// If flushItem is given, a batch then goes through flushItem as well: also one thread at a time
// and in batch order, but without holding up writeItem. Whichever thread is free does it, so the
// two steps run side by side. A batch counts as held until it has been flushed.
template <typename T>
class OrderedBatches {
public:
OrderedBatches(std::function<void(T&)> writeItem, std::function<uint64_t(const T&)> itemBytes,
std::function<void(T&)> flushItem = nullptr)
: writeItem(writeItem), itemBytes(itemBytes), flushItem(flushItem) { }

void setMaxHeldBytes(uint64_t bytes) {
std::lock_guard<std::mutex> lock(m);
maxHeldBytes = bytes;
}

bool inBatch() const { return current().active; }

void begin(uint64_t seq) {
Current& c = current();
c.active = true;
c.seq = seq;
c.items.clear();
}

void add(T&& item) { current().items.emplace_back(std::move(item)); }

// Hand the batch over. If it's the next one to be written and no other thread is
// writing, this thread writes it and then any held batches that follow it; meanwhile
// other threads just hand their batches over and carry on.
void end() {
Current& c = current();
c.active = false;
uint64_t bytes = 0;
for (const auto& item : c.items) bytes += itemBytes(item);
std::unique_lock<std::mutex> lock(m);
heldBytes += bytes;
held.emplace(c.seq, std::make_pair(bytes, std::move(c.items)));
c.items = std::vector<T>();
if (draining) {
flushQueued(lock);
return;
}
draining = true;
while (!held.empty() && held.begin()->first == nextSeq) {
auto batch = std::move(held.begin()->second);
held.erase(held.begin());
lock.unlock();
{
std::lock_guard<std::mutex> writeLock(w);
for (auto& item : batch.second) writeItem(item);
}
lock.lock();
nextSeq++;
if (flushItem) {
toFlush.emplace_back(std::move(batch));
heldShrunk.notify_one(); // a waiting thread can flush it
continue;
}
heldBytes -= batch.first;
// Wake the waiting threads only once there is room. Woken for every batch
// written, they all found no room and went back to sleep, again and again:
// most of the system time of a 96-thread planet run.
if (heldBytes <= maxHeldBytes) heldShrunk.notify_all();
}
draining = false;
flushQueued(lock);
}

void waitForRoom() {
std::unique_lock<std::mutex> lock(m);
while (heldBytes > maxHeldBytes) {
if (!toFlush.empty() && !flushing) flushQueued(lock);
else heldShrunk.wait(lock);
}
}

bool allWritten() {
std::lock_guard<std::mutex> lock(m);
return held.empty() && toFlush.empty() && !flushing;
}

// Held while items are written; lock it to write anything outside a batch
std::mutex& writeMutex() { return w; }

private:
struct Current {
bool active = false;
uint64_t seq = 0;
std::vector<T> items;
};
// Flush the batches waiting for flushItem, unless another thread already does; called and
// returns with m held
void flushQueued(std::unique_lock<std::mutex> &lock) {
if (!flushItem || flushing) return;
flushing = true;
while (!toFlush.empty()) {
auto batch = std::move(toFlush.front());
toFlush.pop_front();
lock.unlock();
for (auto& item : batch.second) flushItem(item);
batch.second = std::vector<T>();
lock.lock();
heldBytes -= batch.first;
if (heldBytes <= maxHeldBytes) heldShrunk.notify_all();
}
flushing = false;
// batches queued while finishing the last one are taken by the next thread that comes by;
// wake one in case all others wait for room
heldShrunk.notify_one();
}

// The batch this thread is working on (one per thread and item type)
static Current& current() {
static thread_local Current c;
return c;
}

std::function<void(T&)> writeItem;
std::function<uint64_t(const T&)> itemBytes;
std::function<void(T&)> flushItem;
uint64_t maxHeldBytes = 1ull << 30;
std::mutex m; // guards the fields below
std::mutex w; // held while writing
std::condition_variable heldShrunk;
bool draining = false;
uint64_t nextSeq = 0;
uint64_t heldBytes = 0;
std::map<uint64_t, std::pair<uint64_t, std::vector<T>>> held; // seq -> (bytes, items)
std::deque<std::pair<uint64_t, std::vector<T>>> toFlush; // written in order, waiting for flushItem
bool flushing = false; // a thread is flushing
};

#endif //_ORDERED_BATCHES_H
23 changes: 21 additions & 2 deletions include/pmtiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
#define _PMTILES_H

#include <fstream>
#include <map>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "external/pmtiles.hpp"
#include "ordered_batches.h"

struct TileOffset {
uint64_t offset : 40;
Expand All @@ -28,6 +33,14 @@ struct TileOffset {
// PMTiles v3 tile type for MapLibre Tiles (not yet in external/pmtiles.hpp)
#define TILETYPE_MLT 0x6

// A tile waiting to be written: tiny tiles keep their raw data, so that a repeat
// can reuse the earlier copy, and are only compressed if they're written
struct PendingTile {
uint64_t tileId;
bool isTiny;
std::string data; // after writeTile: the bytes flushTile appends to the file (empty if a repeat)
};

class PMTiles {

public:
Expand All @@ -42,10 +55,14 @@ class PMTiles {
void saveTile(int zoom, int x, int y, std::string &data);
void close(std::string &metadata);

// Tiles are written in batch order, so the file doesn't depend on thread scheduling
OrderedBatches<PendingTile> batches;

private:
std::ofstream outputStream;
std::mutex fileMutex; // guards file writes, numTilesWritten
std::mutex indexMutex; // guards access to sparseIndex, denseIndex, tinyCache, numTilesAddressed
std::vector<char> outputBuffer; // a large stream buffer: one thread writes all tiles
uint64_t tileDataEnd = 0; // bytes of tile data written so far (tellp() costs a flush and a seek per tile)
std::mutex fileMutex; // guards leaf directory writes in close()
uint64_t leafStart = 0;
uint64_t numTilesWritten = 0;
uint64_t numTilesAddressed = 0;
Expand All @@ -54,6 +71,8 @@ class PMTiles {
std::vector<TileOffset> denseIndex;
std::unordered_map<std::string, TileOffset> tinyCache;

void writeTile(PendingTile &tile); // with batches.writeMutex() held: places the tile
void flushTile(PendingTile &tile); // in the same order, by one thread at a time: writes its bytes
void appendWithRLE(std::vector<pmtiles::entryv3> &entries, pmtiles::entryv3 &entry);
void appendTileEntry(uint64_t tileId, TileOffset offset, std::vector<pmtiles::entryv3> &rootEntries, std::vector<pmtiles::entryv3> &entries);
void flushEntries(std::vector<pmtiles::entryv3> &rootEntries, std::vector<pmtiles::entryv3> &entries);
Expand Down
40 changes: 37 additions & 3 deletions src/mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@
#include "helpers.h"
#include <iostream>
#include <cmath>
#include <stdexcept>

using namespace sqlite;
using namespace std;

MBTiles::MBTiles():
pendingStatements1(std::make_shared<std::vector<PendingStatement>>()),
pendingStatements2(std::make_shared<std::vector<PendingStatement>>())
pendingStatements2(std::make_shared<std::vector<PendingStatement>>()),
pendingBytes(0),
batches([this](PendingStatement& stmt) {
const std::lock_guard<std::mutex> lock(m);
insertOrReplace(stmt.zoom, stmt.x, stmt.y, stmt.data, stmt.isMerge);
},
[](const PendingStatement& stmt) { return sizeof(PendingStatement) + stmt.data.size(); })
{}

// At most this much is buffered while SQLite is busy; beyond it, workers wait
// for the writer instead (a planet run otherwise buffered ~15M tiles, over 3 GB).
static const size_t MAX_PENDING_BYTES = 256 << 20;

static size_t pendingCost(const std::string& data) {
return sizeof(PendingStatement) + data.size();
}

MBTiles::~MBTiles() {
if (db && inTransaction) db << "COMMIT;"; // commit all the changes if open
}
Expand Down Expand Up @@ -71,6 +86,7 @@ void MBTiles::flushPendingStatements() {
while(!pendingStatements2->empty()) {
const PendingStatement& stmt = pendingStatements2->back();
insertOrReplace(stmt.zoom, stmt.x, stmt.y, stmt.data, stmt.isMerge);
pendingBytes -= pendingCost(stmt.data);
pendingStatements2->pop_back();
}

Expand All @@ -80,19 +96,37 @@ void MBTiles::flushPendingStatements() {
}

void MBTiles::saveTile(int zoom, int x, int y, string *data, bool isMerge) {
if (batches.inBatch()) {
batches.add({zoom, x, y, *data, isMerge});
return;
}

// If the lock is available, write directly to SQLite.
if (m.try_lock()) {
insertOrReplace(zoom, x, y, *data, isMerge);
flushPendingStatements();
m.unlock();
} else {
return;
}

{
// Else buffer the write for later, copying its binary blob.
const std::lock_guard<std::mutex> lock(pendingStatementsMutex);
pendingStatements1->push_back({zoom, x, y, *data, isMerge});
if (pendingBytes < MAX_PENDING_BYTES) {
pendingBytes += pendingCost(*data);
pendingStatements1->push_back({zoom, x, y, *data, isMerge});
return;
}
}

// The buffer is full: wait for SQLite, then help drain the buffer.
const std::lock_guard<std::mutex> lock(m);
insertOrReplace(zoom, x, y, *data, isMerge);
flushPendingStatements();
}

void MBTiles::closeForWriting() {
if (!batches.allWritten()) throw std::runtime_error("MBTiles: a batch of tiles was never written - please file an issue");
flushPendingStatements();
preparedStatements[0].used(true);
preparedStatements[1].used(true);
Expand Down
Loading
Loading