Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/buffer.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 20 | |
| 21 | #include <types/global.h> |
| 22 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 23 | struct pool_head *pool_head_buffer; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 24 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 25 | /* list of objects waiting for at least one buffer */ |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 26 | struct mt_list buffer_wq = LIST_HEAD_INIT(buffer_wq); |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 27 | __decl_aligned_spinlock(buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 29 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 30 | int init_buffer() |
| 31 | { |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 32 | void *buffer; |
| 33 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 34 | pool_head_buffer = create_pool("buffer", global.tune.bufsize, MEM_F_SHARED|MEM_F_EXACT); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 35 | if (!pool_head_buffer) |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 36 | 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 Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 45 | pool_head_buffer->minavail = MAX(global.tune.reserved_bufs, 3); |
Willy Tarreau | 33cb065 | 2014-12-23 22:52:37 +0100 | [diff] [blame] | 46 | if (global.tune.buf_limit) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 47 | pool_head_buffer->limit = global.tune.buf_limit; |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 48 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 49 | buffer = pool_refill_alloc(pool_head_buffer, pool_head_buffer->minavail - 1); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 50 | if (!buffer) |
| 51 | return 0; |
| 52 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 53 | pool_free(pool_head_buffer, buffer); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 54 | return 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 57 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 58 | * Dumps part or all of a buffer. |
| 59 | */ |
| 60 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 61 | { |
| 62 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | 81521ed | 2018-06-19 07:48:13 +0200 | [diff] [blame] | 63 | fprintf(o, " orig=%p size=%u head=%u tail=%u data=%u\n", |
| 64 | 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 Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 65 | |
| 66 | fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 67 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 68 | /* dump hexa */ |
| 69 | while (from < to) { |
| 70 | int i; |
| 71 | |
| 72 | fprintf(o, " %04x: ", from); |
| 73 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
Willy Tarreau | 81521ed | 2018-06-19 07:48:13 +0200 | [diff] [blame] | 74 | fprintf(o, "%02x ", (unsigned char)b_orig(b)[from + i]); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 75 | if (((from + i) & 15) == 7) |
| 76 | fprintf(o, "- "); |
| 77 | } |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 78 | if (to - from < 16) { |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 79 | int j = 0; |
| 80 | |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 81 | for (j = 0; j < from + 16 - to; j++) |
| 82 | fprintf(o, " "); |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 83 | if (j > 8) |
| 84 | fprintf(o, " "); |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 85 | } |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 86 | fprintf(o, " "); |
| 87 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 88 | fprintf(o, "%c", isprint((unsigned char)b_orig(b)[from + i]) ? b_orig(b)[from + i] : '.') ; |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 89 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 90 | fprintf(o, "\n"); |
| 91 | } |
| 92 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 93 | } |
| 94 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 95 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 98 | /* see offer_buffer() for details */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 99 | void __offer_buffer(void *from, unsigned int threshold) |
| 100 | { |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 101 | struct buffer_wait *wait; |
| 102 | struct mt_list *elt1, elt2; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 103 | int avail; |
| 104 | |
| 105 | /* For now, we consider that all objects need 1 buffer, so we can stop |
| 106 | * waking up them once we have enough of them to eat all the available |
| 107 | * buffers. Note that we don't really know if they are streams or just |
| 108 | * other tasks, but that's a rough estimate. Similarly, for each cached |
| 109 | * event we'll need 1 buffer. If no buffer is currently used, always |
| 110 | * wake up the number of tasks we can offer a buffer based on what is |
| 111 | * allocated, and in any case at least one task per two reserved |
| 112 | * buffers. |
| 113 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 114 | avail = pool_head_buffer->allocated - pool_head_buffer->used - global.tune.reserved_bufs / 2; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 116 | mt_list_for_each_entry_safe(wait, &buffer_wq, list, elt1, elt2) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 117 | if (avail <= threshold) |
| 118 | break; |
| 119 | |
| 120 | if (wait->target == from || !wait->wakeup_cb(wait->target)) |
| 121 | continue; |
| 122 | |
Olivier Houchard | 6c96fc1 | 2020-03-10 17:39:21 +0100 | [diff] [blame] | 123 | MT_LIST_DEL_SAFE(elt1); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 124 | avail--; |
| 125 | } |
| 126 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Local variables: |
| 130 | * c-indent-level: 8 |
| 131 | * c-basic-offset: 8 |
| 132 | * End: |
| 133 | */ |