Distributed engine: Send binary stats updates to slaves
[pachi/derm.git] / distributed / protocol.h
blobfc0004bac75b8e00fe04b7291764d6d06bd912b1
1 #ifndef ZZGO_DISTRIBUTED_PROTOCOL_H
2 #define ZZGO_DISTRIBUTED_PROTOCOL_H
4 #include <sys/socket.h>
5 #include <arpa/inet.h>
7 #include "board.h"
10 /* Each slave thread maintains a ring of 32 buffers holding
11 * incremental stats received from the slave. The oldest
12 * buffer is recycled to hold stats sent to the slave and
13 * received the next reply. */
14 #define BUFFERS_PER_SLAVE_BITS 5
15 #define BUFFERS_PER_SLAVE (1 << BUFFERS_PER_SLAVE_BITS)
17 struct slave_state;
18 typedef void (*buffer_hook)(void *buf, int size);
19 typedef void (*state_alloc_hook)(struct slave_state *sstate);
20 typedef int (*getargs_hook)(void *buf, struct slave_state *sstate, int cmd_id);
22 struct slave_state {
23 int max_buf_size;
24 int thread_id;
25 struct in_addr client; // for debugging only
26 state_alloc_hook alloc_hook;
27 buffer_hook insert_hook;
28 getargs_hook args_hook;
30 /* Index in received_queue of most recent processed
31 * buffer, -1 if none processed yet. */
32 int last_processed;
34 /* --- PRIVATE DATA for protocol.c --- */
36 struct {
37 void *buf;
38 int size;
39 /* Index in received_queue, -1 if not there. */
40 int queue_index;
41 } b[BUFFERS_PER_SLAVE];
43 int newest_buf;
44 int slave_sock;
46 /* Id of gtp command at time of last_processed. */
47 int last_cmd_id;
49 /* --- PRIVATE DATA for merge.c --- */
51 /* Hash table of incremental stats. */
52 struct incr_stats *stats_htable;
53 int stats_hbits;
54 int stats_id;
56 /* Hash indices updated by stats merge. */
57 int *merged;
58 int max_merged_nodes;
60 extern struct slave_state default_sstate;
62 void protocol_lock(void);
63 void protocol_unlock(void);
65 void logline(struct in_addr *client, char *prefix, char *s);
67 void clear_receive_queue(void);
68 void update_cmd(struct board *b, char *cmd, char *args, bool new_id);
69 void new_cmd(struct board *b, char *cmd, char *args);
70 void get_replies(double time_limit, int min_replies);
71 void protocol_init(char *slave_port, char *proxy_port, int max_slaves);
73 extern int reply_count;
74 extern char **gtp_replies;
75 extern int active_slaves;
77 /* All binary buffers received from all slaves in current move are in
78 * receive_queue[0..queue_length-1] */
79 struct receive_buf {
80 volatile void *buf;
81 /* All buffers have the same physical size.
82 * size is the number of valid bytes. */
83 int size;
84 /* id of the thread that received the buffer. */
85 int thread_id;
87 extern struct receive_buf *receive_queue;
88 extern int queue_length;
90 /* Max size of all gtp commands for one game.
91 * 60 chars for the first line of genmoves plus 100 lines
92 * of 30 chars each for the stats at last move. */
93 #define CMDS_SIZE (60*MAX_GAMELEN + 30*100)
95 /* Max size for one line of reply or slave log. */
96 #define BSIZE 4096
98 #endif