-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconn_buffer.h
118 lines (93 loc) · 4.29 KB
/
conn_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "generic.h"
#include <pthread.h>
#if !defined(_conn_buffer_h_)
#define _conn_buffer_h_
#if defined(CONN_BUFFER_TESTS)
#define CB_STATIC
#if defined(CONN_BUFFER_MODULE)
#define CB_STATIC_DECL(decl) decl
#else
#define CB_STATIC_DECL(decl) extern decl
#endif /* #if defined(CONN_BUFFER_MODULE) */
#else
#define CB_STATIC static
#if defined(CONN_BUFFER_MODULE)
#define CB_STATIC_DECL(decl) static decl
#else
#define CB_STATIC_DECL(decl)
#endif /* #if defined(CONN_BUFFER_MODULE) */
#endif /* #if defined(CONN_BUFFER_TESTS) */
#define CONN_BUFFER_INITIAL_BUFFER_COUNT_DEFAULT (8)
#define CONN_BUFFER_RSIZE_LIMIT_DEFAULT (128 * 1024)
#define CONN_BUFFER_TOTAL_RSIZE_RANGE_BOTTOM_DEFAULT (8 * 1024 * 1024)
#define CONN_BUFFER_TOTAL_RSIZE_RANGE_TOP_DEFAULT (16 * 1024 * 1024)
#define CONN_BUFFER_SIZE (16 * 1024 * 1024)
#define CONN_BUFFER_SIGNATURE (0xbeadbeef)
#define CONN_BUFFER_HEADER_CONTENTS \
uint32_t signature; \
uint32_t prev_rusage; \
uint32_t max_rusage; \
uint8_t unused[3]; \
unsigned char :5; \
unsigned char rusage_updated:1; \
unsigned char in_freelist:1; \
unsigned char used:1;
#define CONN_BUFFER_HEADER_SZ sizeof(struct { CONN_BUFFER_HEADER_CONTENTS })
#define CONN_BUFFER_DATA_SZ (CONN_BUFFER_SIZE - CONN_BUFFER_HEADER_SZ)
typedef struct conn_buffer_s conn_buffer_t;
struct conn_buffer_s {
CONN_BUFFER_HEADER_CONTENTS;
unsigned char data[CONN_BUFFER_DATA_SZ];
};
typedef struct conn_buffer_stats_s conn_buffer_stats_t;
struct conn_buffer_stats_s {
uint64_t allocs;
uint64_t frees;
uint64_t destroys;
uint64_t reclamations_started;
uint64_t allocs_failed;
};
typedef struct conn_buffer_group_s conn_buffer_group_t;
struct conn_buffer_group_s {
conn_buffer_t** free_buffers;
size_t free_buffer_list_capacity;
size_t num_free_buffers;
size_t total_rsize;
size_t total_rsize_in_freelist;
bool reclamation_in_progress;
bool initialized;
struct {
pthread_t tid; /* associated thread id */
size_t initial_buffer_count; /* initial buffers set up */
size_t buffer_rsize_limit; /* if the reported usage of a block is
* greater or equal to this limit, the
* block is immediately returned to the
* OS. otherwise, the block is
* recycled. */
size_t total_rsize_range_top; /* the total reported usage of the
* conn_buffer system is not to exceed
* this limit. */
size_t total_rsize_range_bottom;/* when the total_rsize has hit
* total_rsize_range_top, buffers are
* returned to the OS until we reach
* this value *or* there's nothing else
* on the freelist to return. */
size_t page_size; /* page size on the OS. */
} settings;
conn_buffer_stats_t stats;
pthread_mutex_t lock; /* lock for this connection buffer group. */
};
extern void* alloc_conn_buffer(conn_buffer_group_t* cbg, size_t max_rusage_hint);
extern void free_conn_buffer(conn_buffer_group_t* cbg, void* ptr, ssize_t max_rusage);
extern void report_max_rusage(conn_buffer_group_t* cbg, void* ptr, size_t max_rusage);
extern char* conn_buffer_stats(size_t* result_size);
extern void conn_buffer_init(unsigned threads,
size_t initial_buffer_count,
size_t buffer_rsize_limit,
size_t total_rsize_range_bottom,
size_t total_rsize_range_top);
extern conn_buffer_group_t* get_conn_buffer_group(unsigned thread);
extern bool assign_thread_id_to_conn_buffer_group(unsigned group, pthread_t tid);
CB_STATIC_DECL(int cb_freelist_check(conn_buffer_group_t* cbg));
#endif /* #if !defined(_conn_buffer_h_) */