blob: d6bd242d489dbe79b708af97506605f8e037aa47 [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * Buffer management functions.
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
William Lallemandbe0efd82012-11-22 18:01:40 +010013#include <ctype.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/buffer.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020019#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020020
21#include <types/global.h>
22
Willy Tarreaubafbe012017-11-24 17:34:44 +010023struct pool_head *pool_head_buffer;
Willy Tarreau9b28e032012-10-12 23:49:43 +020024
Christopher Fauleta73e59b2016-12-09 17:30:18 +010025/* list of objects waiting for at least one buffer */
26struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
Willy Tarreaub6a2f582017-11-26 11:08:14 +010027__decl_hathreads(HA_SPINLOCK_T __attribute__((aligned(64))) buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010028
Willy Tarreau9b28e032012-10-12 23:49:43 +020029/* perform minimal intializations, report 0 in case of error, 1 if OK. */
30int init_buffer()
31{
Willy Tarreaua24adf02014-11-27 01:11:56 +010032 void *buffer;
33
Willy Tarreauc9fa0482018-07-10 17:43:27 +020034 pool_head_buffer = create_pool("buffer", global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT);
Willy Tarreaubafbe012017-11-24 17:34:44 +010035 if (!pool_head_buffer)
Willy Tarreaua24adf02014-11-27 01:11:56 +010036 return 0;
37
38 /* The reserved buffer is what we leave behind us. Thus we always need
39 * at least one extra buffer in minavail otherwise we'll end up waking
40 * up tasks with no memory available, causing a lot of useless wakeups.
41 * That means that we always want to have at least 3 buffers available
42 * (2 for current session, one for next session that might be needed to
43 * release a server connection).
44 */
Willy Tarreaubafbe012017-11-24 17:34:44 +010045 pool_head_buffer->minavail = MAX(global.tune.reserved_bufs, 3);
Willy Tarreau33cb0652014-12-23 22:52:37 +010046 if (global.tune.buf_limit)
Willy Tarreaubafbe012017-11-24 17:34:44 +010047 pool_head_buffer->limit = global.tune.buf_limit;
Willy Tarreaua24adf02014-11-27 01:11:56 +010048
Christopher Faulet2a944ee2017-11-07 10:42:54 +010049 HA_SPIN_INIT(&buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +020050
Willy Tarreaubafbe012017-11-24 17:34:44 +010051 buffer = pool_refill_alloc(pool_head_buffer, pool_head_buffer->minavail - 1);
Willy Tarreaua24adf02014-11-27 01:11:56 +010052 if (!buffer)
53 return 0;
54
Willy Tarreaubafbe012017-11-24 17:34:44 +010055 pool_free(pool_head_buffer, buffer);
Willy Tarreaua24adf02014-11-27 01:11:56 +010056 return 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +020057}
58
Christopher Fauletad405f12017-08-29 15:30:11 +020059void deinit_buffer()
60{
Willy Tarreaubafbe012017-11-24 17:34:44 +010061 pool_destroy(pool_head_buffer);
Christopher Fauletad405f12017-08-29 15:30:11 +020062}
63
Willy Tarreauaf819352012-08-27 22:08:00 +020064/*
Willy Tarreauc7e42382012-08-24 19:22:53 +020065 * Dumps part or all of a buffer.
66 */
67void buffer_dump(FILE *o, struct buffer *b, int from, int to)
68{
69 fprintf(o, "Dumping buffer %p\n", b);
Willy Tarreau81521ed2018-06-19 07:48:13 +020070 fprintf(o, " orig=%p size=%u head=%u tail=%u data=%u\n",
71 b_orig(b), (unsigned int)b_size(b), (unsigned int)b_head_ofs(b), (unsigned int)b_tail_ofs(b), (unsigned int)b_data(b));
Willy Tarreauc7e42382012-08-24 19:22:53 +020072
73 fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
William Lallemandbe0efd82012-11-22 18:01:40 +010074 fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
75 /* dump hexa */
76 while (from < to) {
77 int i;
78
79 fprintf(o, " %04x: ", from);
80 for (i = 0; ((from + i) < to) && (i < 16) ; i++) {
Willy Tarreau81521ed2018-06-19 07:48:13 +020081 fprintf(o, "%02x ", (unsigned char)b_orig(b)[from + i]);
William Lallemandbe0efd82012-11-22 18:01:40 +010082 if (((from + i) & 15) == 7)
83 fprintf(o, "- ");
84 }
Godbachc08057c2013-11-14 10:15:20 +080085 if (to - from < 16) {
Godbachc3916a72013-11-21 10:21:22 +080086 int j = 0;
87
Godbachc08057c2013-11-14 10:15:20 +080088 for (j = 0; j < from + 16 - to; j++)
89 fprintf(o, " ");
Godbachc3916a72013-11-21 10:21:22 +080090 if (j > 8)
91 fprintf(o, " ");
Godbachc08057c2013-11-14 10:15:20 +080092 }
William Lallemandbe0efd82012-11-22 18:01:40 +010093 fprintf(o, " ");
94 for (i = 0; (from + i < to) && (i < 16) ; i++) {
Willy Tarreau81521ed2018-06-19 07:48:13 +020095 fprintf(o, "%c", isprint((int)b_orig(b)[from + i]) ? b_orig(b)[from + i] : '.') ;
William Lallemandbe0efd82012-11-22 18:01:40 +010096 if ((((from + i) & 15) == 15) && ((from + i) != to-1))
97 fprintf(o, "\n");
98 }
99 from += i;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200100 }
101 fprintf(o, "\n--\n");
William Lallemandbe0efd82012-11-22 18:01:40 +0100102 fflush(o);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200103}
104
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100105/* see offer_buffer() for details */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100106void __offer_buffer(void *from, unsigned int threshold)
107{
108 struct buffer_wait *wait, *bak;
109 int avail;
110
111 /* For now, we consider that all objects need 1 buffer, so we can stop
112 * waking up them once we have enough of them to eat all the available
113 * buffers. Note that we don't really know if they are streams or just
114 * other tasks, but that's a rough estimate. Similarly, for each cached
115 * event we'll need 1 buffer. If no buffer is currently used, always
116 * wake up the number of tasks we can offer a buffer based on what is
117 * allocated, and in any case at least one task per two reserved
118 * buffers.
119 */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100120 avail = pool_head_buffer->allocated - pool_head_buffer->used - global.tune.reserved_bufs / 2;
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100121
122 list_for_each_entry_safe(wait, bak, &buffer_wq, list) {
123 if (avail <= threshold)
124 break;
125
126 if (wait->target == from || !wait->wakeup_cb(wait->target))
127 continue;
128
129 LIST_DEL(&wait->list);
130 LIST_INIT(&wait->list);
131
132 avail--;
133 }
134}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200135
136/*
137 * Local variables:
138 * c-indent-level: 8
139 * c-basic-offset: 8
140 * End:
141 */