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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 18 | #include <haproxy/dynbuf.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | #include <haproxy/global.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 20 | #include <haproxy/list.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 21 | #include <haproxy/pool.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 25 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 26 | int init_buffer() |
| 27 | { |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 28 | void *buffer; |
Willy Tarreau | e8e5091 | 2021-02-20 11:38:56 +0100 | [diff] [blame] | 29 | int thr; |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 30 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 31 | 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] | 32 | if (!pool_head_buffer) |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 33 | return 0; |
| 34 | |
Willy Tarreau | e8e5091 | 2021-02-20 11:38:56 +0100 | [diff] [blame] | 35 | for (thr = 0; thr < MAX_THREADS; thr++) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 36 | LIST_INIT(&ha_thread_info[thr].buffer_wq); |
Willy Tarreau | e8e5091 | 2021-02-20 11:38:56 +0100 | [diff] [blame] | 37 | |
| 38 | |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 39 | /* The reserved buffer is what we leave behind us. Thus we always need |
| 40 | * at least one extra buffer in minavail otherwise we'll end up waking |
| 41 | * up tasks with no memory available, causing a lot of useless wakeups. |
| 42 | * That means that we always want to have at least 3 buffers available |
| 43 | * (2 for current session, one for next session that might be needed to |
| 44 | * release a server connection). |
| 45 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 46 | pool_head_buffer->minavail = MAX(global.tune.reserved_bufs, 3); |
Willy Tarreau | 33cb065 | 2014-12-23 22:52:37 +0100 | [diff] [blame] | 47 | if (global.tune.buf_limit) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 48 | pool_head_buffer->limit = global.tune.buf_limit; |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 49 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 50 | buffer = pool_refill_alloc(pool_head_buffer, pool_head_buffer->minavail - 1); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 51 | if (!buffer) |
| 52 | return 0; |
| 53 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 54 | pool_free(pool_head_buffer, buffer); |
Willy Tarreau | a24adf0 | 2014-11-27 01:11:56 +0100 | [diff] [blame] | 55 | return 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 58 | /* |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 59 | * Dumps part or all of a buffer. |
| 60 | */ |
| 61 | void buffer_dump(FILE *o, struct buffer *b, int from, int to) |
| 62 | { |
| 63 | fprintf(o, "Dumping buffer %p\n", b); |
Willy Tarreau | 81521ed | 2018-06-19 07:48:13 +0200 | [diff] [blame] | 64 | fprintf(o, " orig=%p size=%u head=%u tail=%u data=%u\n", |
| 65 | 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] | 66 | |
| 67 | 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] | 68 | fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
| 69 | /* dump hexa */ |
| 70 | while (from < to) { |
| 71 | int i; |
| 72 | |
| 73 | fprintf(o, " %04x: ", from); |
| 74 | for (i = 0; ((from + i) < to) && (i < 16) ; i++) { |
Willy Tarreau | 81521ed | 2018-06-19 07:48:13 +0200 | [diff] [blame] | 75 | fprintf(o, "%02x ", (unsigned char)b_orig(b)[from + i]); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 76 | if (((from + i) & 15) == 7) |
| 77 | fprintf(o, "- "); |
| 78 | } |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 79 | if (to - from < 16) { |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 80 | int j = 0; |
| 81 | |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 82 | for (j = 0; j < from + 16 - to; j++) |
| 83 | fprintf(o, " "); |
Godbach | c3916a7 | 2013-11-21 10:21:22 +0800 | [diff] [blame] | 84 | if (j > 8) |
| 85 | fprintf(o, " "); |
Godbach | c08057c | 2013-11-14 10:15:20 +0800 | [diff] [blame] | 86 | } |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 87 | fprintf(o, " "); |
| 88 | for (i = 0; (from + i < to) && (i < 16) ; i++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 89 | 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] | 90 | if ((((from + i) & 15) == 15) && ((from + i) != to-1)) |
| 91 | fprintf(o, "\n"); |
| 92 | } |
| 93 | from += i; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 94 | } |
| 95 | fprintf(o, "\n--\n"); |
William Lallemand | be0efd8 | 2012-11-22 18:01:40 +0100 | [diff] [blame] | 96 | fflush(o); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 99 | /* see offer_buffers() for details */ |
| 100 | void __offer_buffers(void *from, unsigned int count) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 101 | { |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 102 | struct buffer_wait *wait, *wait_back; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 103 | |
| 104 | /* For now, we consider that all objects need 1 buffer, so we can stop |
| 105 | * waking up them once we have enough of them to eat all the available |
| 106 | * buffers. Note that we don't really know if they are streams or just |
| 107 | * other tasks, but that's a rough estimate. Similarly, for each cached |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 108 | * event we'll need 1 buffer. |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 109 | */ |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 110 | list_for_each_entry_safe(wait, wait_back, &ti->buffer_wq, list) { |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 111 | if (!count) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 112 | break; |
| 113 | |
| 114 | if (wait->target == from || !wait->wakeup_cb(wait->target)) |
| 115 | continue; |
| 116 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 117 | LIST_DEL_INIT(&wait->list); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 118 | count--; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * Local variables: |
| 124 | * c-indent-level: 8 |
| 125 | * c-basic-offset: 8 |
| 126 | * End: |
| 127 | */ |