mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
Loading...
Searching...
No Matches
render_submit_queue.c File Reference
#include <assert.h>
#include <errno.h>
#include <glib.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "g_logger.h"
#include "protocol.h"
#include "protocol_helper.h"
#include "render_config.h"
#include "render_submit_queue.h"
#include "sys_utils.h"

Functions

static struct protocol * fetch (void)
 Pop the next tile render request from the bounded queue and build a protocol command.
static int process (struct protocol *cmd, int fd)
 Send a single render command to renderd and wait synchronously for its reply.
int make_connection (const char *spath)
 Open a stream connection to renderd over a Unix domain socket or TCP/IP socket.
void check_load (void)
 Throttle worker submission while the system 1-minute load average meets or exceeds maxLoad.
void display_rate (struct timeval start, struct timeval end, int num)
 Log the elapsed time and average throughput (tiles/sec) for a batch of rendered tiles.
void enqueue (const char *xmlname, int x, int y, int z)
 Enqueue a metatile render request onto the bounded worker submission queue.
void finish_workers (void)
 Signal all worker threads to finish remaining queued items and join them.
void print_statistics (void)
 Log per-zoom-level minimum, average, maximum, and cumulative render times.
void spawn_workers (int num, const char *spath, int max_load)
 Initialize synchronization primitives and spawn the pool of render-client worker threads.
void * thread_main (void *arg)
 Worker thread main loop that fetches queued requests and submits them to renderd.
void wait_for_empty_queue ()
 Block the calling thread until all currently queued items have been popped by workers.

Variables

static pthread_mutex_t qLock
 Mutex protecting the submission queue (qHead, qTail, qLen, and work_complete).
static pthread_mutex_t qStatsLock
 Mutex protecting aggregate per-zoom render timing statistics (performance_stats).
static pthread_cond_t qCondNotEmpty
 Condition variable signaled when an item is enqueued or work_complete is set.
static pthread_cond_t qCondNotFull
 Condition variable signaled whenever a worker dequeues an item (reducing qLen).
static int maxLoad = 0
 System 1-minute load average threshold above which workers pause submitting requests.
static unsigned int qMaxLen
 Maximum capacity of the bounded submission queue (initialized to no_workers).
static unsigned int qLen
 Current number of queued items waiting between qHead and qTail.
struct speed_stats performance_stats
 Global per-zoom-level render count and latency statistics.
static struct qItem * qHead
 Head (dequeue end) and tail (enqueue end) pointers of the FIFO work queue.
static struct qItem * qTail
static int no_workers
 Number of active worker threads spawned by spawn_workers().
static pthread_t * workers
 Dynamically allocated array of worker pthread_t handles.
static int work_complete
 Shutdown flag set by finish_workers() to wake idle workers and terminate them once drained.

Function Documentation

◆ check_load()

void check_load ( void )

Throttle worker submission while the system 1-minute load average meets or exceeds maxLoad.

Polls get_load_avg() in a loop and sleeps 5 seconds per iteration until system load drops below maxLoad.

◆ display_rate()

void display_rate ( struct timeval start,
struct timeval end,
int num )

Log the elapsed time and average throughput (tiles/sec) for a batch of rendered tiles.

Parameters
startTimestamp captured at the start of the batch.
endTimestamp captured at the end of the batch.
numTotal number of tiles/metatiles processed during the interval.

◆ enqueue()

void enqueue ( const char * xmlname,
int x,
int y,
int z )

Enqueue a metatile render request onto the bounded worker submission queue.

Allocates a qItem node, blocks on qCondNotFull if the queue has reached qMaxLen, appends the node to the FIFO list, and signals qCondNotEmpty to wake a waiting worker.

Parameters
xmlnameLogical map style name (duplicated onto the heap).
xTop-left tile X coordinate of the metatile.
yTop-left tile Y coordinate of the metatile.
zTile zoom level.

◆ fetch()

struct protocol * fetch ( void )
static

Pop the next tile render request from the bounded queue and build a protocol command.

Blocks on qCondNotEmpty while the queue is empty. Returns NULL once work_complete is signaled and no items remain. Otherwise dequeues the head qItem, signals qCondNotFull, frees the queue node, and returns a heap-allocated protocol v2 cmdRenderBulk message.

Returns
Heap-allocated struct protocol command (caller owns memory), or NULL when shutting down.

◆ finish_workers()

void finish_workers ( void )

Signal all worker threads to finish remaining queued items and join them.

Sets work_complete = 1, broadcasts qCondNotEmpty so idle workers exit fetch(), joins every worker thread via pthread_join(), and frees the workers handle array.

◆ make_connection()

int make_connection ( const char * spath)

Open a stream connection to renderd over a Unix domain socket or TCP/IP socket.

Interprets spath based on its leading character:

  • Leading '/': Connects to an AF_UNIX stream socket at filesystem path spath. Returns FD_INVALID if connect() fails.
  • Otherwise: Parses <host>[:<port>] (defaulting host to RENDERD_HOST and port to RENDERD_PORT), resolves addresses via getaddrinfo(), and connects over TCP. Terminates the process with exit(2) on unrecoverable socket/DNS/connect errors.
Parameters
spathSocket path (starting with '/') or TCP host[:port] specification.
Returns
Connected socket file descriptor, or FD_INVALID if a Unix socket connection failed.

◆ print_statistics()

void print_statistics ( void )

Log per-zoom-level minimum, average, maximum, and cumulative render times.

Iterates from zoom 0 through MAX_ZOOM, skipping any zoom levels with zero rendered requests, and formats millisecond timings from performance_stats as seconds.

◆ process()

int process ( struct protocol * cmd,
int fd )
static

Send a single render command to renderd and wait synchronously for its reply.

Measures round-trip wall-clock latency in milliseconds. If renderd replies with cmdDone, updates performance_stats for cmd->z under qStatsLock. If renderd replies with any non-cmdDone status (e.g., busy/retry), sleeps 1 second before returning.

Parameters
cmdPointer to the protocol command to send.
fdConnected socket file descriptor to renderd.
Returns
Positive byte count returned by recv_cmd() if a response was received, or 0 if reading the response failed (indicating a lost connection).

◆ spawn_workers()

void spawn_workers ( int num,
const char * spath,
int max_load )

Initialize synchronization primitives and spawn the pool of render-client worker threads.

Configures the bounded queue capacity (qMaxLen = num) and system load ceiling (maxLoad), initializes qLock, qStatsLock, qCondNotEmpty, and qCondNotFull, and launches num threads running thread_main().

Parameters
numNumber of worker threads to spawn.
spathSocket path or host[:port] string passed to each worker thread.
max_loadMaximum system 1-minute load average allowed before throttling workers.

◆ thread_main()

void * thread_main ( void * arg)

Worker thread main loop that fetches queued requests and submits them to renderd.

Opens a dedicated connection to renderd via make_connection(), then repeatedly checks system load, pops the next command via fetch(), and submits it via process(). If the connection drops mid-flight, retries connecting every 30 seconds and re-submits the interrupted command until it succeeds.

Parameters
argconst char * socket path or host[:port] string forwarded to make_connection().
Returns
Always returns NULL once the queue is drained and work_complete is set.

◆ wait_for_empty_queue()

void wait_for_empty_queue ( void )

Block the calling thread until all currently queued items have been popped by workers.

Waits on qCondNotFull (which is signaled every time fetch() decrements qLen) until qLen reaches 0. Note that the final popped items may still be in flight inside process() when this function returns; call finish_workers() to wait for completion.

Variable Documentation

◆ maxLoad

int maxLoad = 0
static

System 1-minute load average threshold above which workers pause submitting requests.

◆ no_workers

int no_workers
static

Number of active worker threads spawned by spawn_workers().

◆ performance_stats

struct speed_stats performance_stats

Global per-zoom-level render count and latency statistics.

◆ qCondNotEmpty

pthread_cond_t qCondNotEmpty
static

Condition variable signaled when an item is enqueued or work_complete is set.

◆ qCondNotFull

pthread_cond_t qCondNotFull
static

Condition variable signaled whenever a worker dequeues an item (reducing qLen).

◆ qHead

struct qItem* qHead
static

Head (dequeue end) and tail (enqueue end) pointers of the FIFO work queue.

◆ qLen

unsigned int qLen
static

Current number of queued items waiting between qHead and qTail.

◆ qLock

pthread_mutex_t qLock
static

Mutex protecting the submission queue (qHead, qTail, qLen, and work_complete).

◆ qMaxLen

unsigned int qMaxLen
static

Maximum capacity of the bounded submission queue (initialized to no_workers).

◆ qStatsLock

pthread_mutex_t qStatsLock
static

Mutex protecting aggregate per-zoom render timing statistics (performance_stats).

◆ qTail

struct qItem * qTail
static

◆ work_complete

int work_complete
static

Shutdown flag set by finish_workers() to wake idle workers and terminate them once drained.

◆ workers

pthread_t* workers
static

Dynamically allocated array of worker pthread_t handles.