|
mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
|
#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. | |
| 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.
| 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.
| start | Timestamp captured at the start of the batch. |
| end | Timestamp captured at the end of the batch. |
| num | Total number of tiles/metatiles processed during the interval. |
| 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.
| xmlname | Logical map style name (duplicated onto the heap). |
| x | Top-left tile X coordinate of the metatile. |
| y | Top-left tile Y coordinate of the metatile. |
| z | Tile zoom level. |
|
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.
| 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.
| 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:
| spath | Socket path (starting with '/') or TCP host[:port] specification. |
| 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.
|
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.
| cmd | Pointer to the protocol command to send. |
| fd | Connected socket file descriptor to renderd. |
| 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().
| num | Number of worker threads to spawn. |
| spath | Socket path or host[:port] string passed to each worker thread. |
| max_load | Maximum system 1-minute load average allowed before throttling workers. |
| 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.
| arg | const char * socket path or host[:port] string forwarded to make_connection(). |
| 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.
|
static |
System 1-minute load average threshold above which workers pause submitting requests.
|
static |
Number of active worker threads spawned by spawn_workers().
| struct speed_stats performance_stats |
Global per-zoom-level render count and latency statistics.
|
static |
Condition variable signaled when an item is enqueued or work_complete is set.
|
static |
Condition variable signaled whenever a worker dequeues an item (reducing qLen).
|
static |
Head (dequeue end) and tail (enqueue end) pointers of the FIFO work queue.
|
static |
Mutex protecting the submission queue (qHead, qTail, qLen, and work_complete).
|
static |
Maximum capacity of the bounded submission queue (initialized to no_workers).
|
static |
Mutex protecting aggregate per-zoom render timing statistics (performance_stats).
|
static |
|
static |
Shutdown flag set by finish_workers() to wake idle workers and terminate them once drained.
|
static |
Dynamically allocated array of worker pthread_t handles.