Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Ring buffer management |
| 3 | * |
| 4 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <common/buf.h> |
| 23 | #include <common/compat.h> |
| 24 | #include <common/config.h> |
| 25 | #include <common/hathreads.h> |
| 26 | #include <proto/ring.h> |
| 27 | |
| 28 | /* Creates and returns a ring buffer of size <size> bytes. Returns NULL on |
| 29 | * allocation failure. |
| 30 | */ |
| 31 | struct ring *ring_new(size_t size) |
| 32 | { |
| 33 | struct ring *ring = NULL; |
| 34 | void *area = NULL; |
| 35 | |
| 36 | if (size < 2) |
| 37 | goto fail; |
| 38 | |
| 39 | ring = malloc(sizeof(*ring)); |
| 40 | if (!ring) |
| 41 | goto fail; |
| 42 | |
| 43 | area = malloc(size); |
| 44 | if (!area) |
| 45 | goto fail; |
| 46 | |
| 47 | HA_RWLOCK_INIT(&ring->lock); |
| 48 | ring->readers_count = 0; |
| 49 | ring->ofs = 0; |
| 50 | ring->buf = b_make(area, size, 0, 0); |
| 51 | /* write the initial RC byte */ |
| 52 | b_putchr(&ring->buf, 0); |
| 53 | return ring; |
| 54 | fail: |
| 55 | free(area); |
| 56 | free(ring); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | /* Resizes existing ring <ring> to <size> which must be larger, without losing |
| 61 | * its contents. The new size must be at least as large as the previous one or |
| 62 | * no change will be performed. The pointer to the ring is returned on success, |
| 63 | * or NULL on allocation failure. This will lock the ring for writes. |
| 64 | */ |
| 65 | struct ring *ring_resize(struct ring *ring, size_t size) |
| 66 | { |
| 67 | void *area; |
| 68 | |
| 69 | if (b_size(&ring->buf) >= size) |
| 70 | return ring; |
| 71 | |
| 72 | area = malloc(size); |
| 73 | if (!area) |
| 74 | return NULL; |
| 75 | |
| 76 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 77 | |
| 78 | /* recheck the buffer's size, it may have changed during the malloc */ |
| 79 | if (b_size(&ring->buf) < size) { |
| 80 | /* copy old contents */ |
| 81 | b_getblk(&ring->buf, area, ring->buf.data, 0); |
| 82 | area = HA_ATOMIC_XCHG(&ring->buf.area, area); |
| 83 | ring->buf.size = size; |
| 84 | ring->buf.head = 0; |
| 85 | } |
| 86 | |
| 87 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 88 | |
| 89 | free(area); |
| 90 | return ring; |
| 91 | } |
| 92 | |
| 93 | /* destroys and frees ring <ring> */ |
| 94 | void ring_free(struct ring *ring) |
| 95 | { |
| 96 | if (!ring) |
| 97 | return; |
| 98 | free(ring->buf.area); |
| 99 | free(ring); |
| 100 | } |
| 101 | |
Willy Tarreau | be97853 | 2019-08-27 11:44:13 +0200 | [diff] [blame^] | 102 | /* Tries to send <npfx> parts from <prefix> followed by <nmsg> parts from <msg> |
| 103 | * to ring <ring>. The message is sent atomically. It may be truncated to |
| 104 | * <maxlen> bytes if <maxlen> is non-null. There is no distinction between the |
| 105 | * two lists, it's just a convenience to help the caller prepend some prefixes |
| 106 | * when necessary. It takes the ring's write lock to make sure no other thread |
| 107 | * will touch the buffer during the update. Returns the number of bytes sent, |
| 108 | * or <=0 on failure. |
| 109 | */ |
| 110 | ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg) |
| 111 | { |
| 112 | struct buffer *buf = &ring->buf; |
| 113 | size_t totlen = 0; |
| 114 | size_t lenlen; |
| 115 | size_t dellen; |
| 116 | int dellenlen; |
| 117 | ssize_t sent = 0; |
| 118 | int i; |
| 119 | |
| 120 | /* we have to find some room to add our message (the buffer is |
| 121 | * never empty and at least contains the previous counter) and |
| 122 | * to update both the buffer contents and heads at the same |
| 123 | * time (it's doable using atomic ops but not worth the |
| 124 | * trouble, let's just lock). For this we first need to know |
| 125 | * the total message's length. We cannot measure it while |
| 126 | * copying due to the varint encoding of the length. |
| 127 | */ |
| 128 | for (i = 0; i < npfx; i++) |
| 129 | totlen += pfx[i].len; |
| 130 | for (i = 0; i < nmsg; i++) |
| 131 | totlen += msg[i].len; |
| 132 | |
| 133 | if (totlen > maxlen) |
| 134 | totlen = maxlen; |
| 135 | |
| 136 | lenlen = varint_bytes(totlen); |
| 137 | |
| 138 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 139 | if (lenlen + totlen + 1 + 1 > b_size(buf)) |
| 140 | goto done_buf; |
| 141 | |
| 142 | while (b_room(buf) < lenlen + totlen + 1) { |
| 143 | /* we need to delete the oldest message (from the end), |
| 144 | * and we have to stop if there's a reader stuck there. |
| 145 | * Unless there's corruption in the buffer it's guaranteed |
| 146 | * that we have enough data to find 1 counter byte, a |
| 147 | * varint-encoded length (1 byte min) and the message |
| 148 | * payload (0 bytes min). |
| 149 | */ |
| 150 | if (*b_head(buf)) |
| 151 | goto done_buf; |
| 152 | dellenlen = b_peek_varint(buf, 1, &dellen); |
| 153 | if (!dellenlen) |
| 154 | goto done_buf; |
| 155 | BUG_ON(b_data(buf) < 1 + dellenlen + dellen); |
| 156 | |
| 157 | b_del(buf, 1 + dellenlen + dellen); |
| 158 | ring->ofs += 1 + dellenlen + dellen; |
| 159 | } |
| 160 | |
| 161 | /* OK now we do have room */ |
| 162 | __b_put_varint(buf, totlen); |
| 163 | |
| 164 | totlen = 0; |
| 165 | for (i = 0; i < npfx; i++) { |
| 166 | size_t len = pfx[i].len; |
| 167 | |
| 168 | if (len + totlen > maxlen) |
| 169 | len = maxlen - totlen; |
| 170 | if (len) |
| 171 | __b_putblk(buf, pfx[i].ptr, len); |
| 172 | totlen += len; |
| 173 | } |
| 174 | |
| 175 | for (i = 0; i < nmsg; i++) { |
| 176 | size_t len = msg[i].len; |
| 177 | |
| 178 | if (len + totlen > maxlen) |
| 179 | len = maxlen - totlen; |
| 180 | if (len) |
| 181 | __b_putblk(buf, msg[i].ptr, len); |
| 182 | totlen += len; |
| 183 | } |
| 184 | |
| 185 | *b_tail(buf) = 0; buf->data++;; // new read counter |
| 186 | sent = lenlen + totlen + 1; |
| 187 | done_buf: |
| 188 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 189 | return sent; |
| 190 | } |
Willy Tarreau | 172945f | 2019-08-08 15:28:52 +0200 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * Local variables: |
| 194 | * c-indent-level: 8 |
| 195 | * c-basic-offset: 8 |
| 196 | * End: |
| 197 | */ |