aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/outgoing_stream.h
blob: 980bd401f094edd8fc1ff46d2735ae9059d75eb6 (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma once

#include <library/cpp/actors/core/event_load.h>
#include <library/cpp/actors/util/rc_buf.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <deque>

namespace NInterconnect {

    template<size_t TotalSize>
    class TOutgoingStreamT {
        static constexpr size_t BufferSize = TotalSize - sizeof(ui32) * 2;

        struct TBuffer {
            char Data[BufferSize];
            ui32 RefCount;
            ui32 Index;

            struct TDeleter {
                void operator ()(TBuffer *buffer) const {
                    free(buffer);
                }
            };
        };

        static_assert(sizeof(TBuffer) == TotalSize);

        struct TSendChunk {
            TContiguousSpan Span;
            TBuffer *Buffer;
        };

        std::vector<std::unique_ptr<TBuffer, typename TBuffer::TDeleter>> Buffers;
        TBuffer *AppendBuffer = nullptr;
        size_t AppendOffset = BufferSize; // into the last buffer
        std::deque<TSendChunk> SendQueue;
        size_t SendQueuePos = 0;
        size_t SendOffset = 0;
        size_t UnsentBytes = 0;

    public:
        operator bool() const {
            return SendQueuePos != SendQueue.size();
        }

        size_t CalculateOutgoingSize() const {
            size_t res = 0;
            for (const TSendChunk& chunk : SendQueue) {
                res += chunk.Span.size();
            }
            return res;
        }

        size_t CalculateUnsentSize() const {
#ifndef NDEBUG
            size_t res = 0;
            for (auto it = SendQueue.begin() + SendQueuePos; it != SendQueue.end(); ++it) {
                res += it->Span.size();
            }
            Y_ABORT_UNLESS(UnsentBytes == res - SendOffset);
#endif
            return UnsentBytes;
        }

        size_t GetSendQueueSize() const {
            return SendQueue.size();
        }

        TMutableContiguousSpan AcquireSpanForWriting(size_t maxLen) {
            if (maxLen && AppendOffset == BufferSize) { // we have no free buffer, allocate one
                Buffers.emplace_back(static_cast<TBuffer*>(malloc(sizeof(TBuffer))));
                AppendBuffer = Buffers.back().get();
                Y_ABORT_UNLESS(AppendBuffer);
                AppendBuffer->RefCount = 1; // through AppendBuffer pointer
                AppendBuffer->Index = Buffers.size() - 1;
                AppendOffset = 0;
            }
            return {AppendBuffer->Data + AppendOffset, Min(maxLen, BufferSize - AppendOffset)};
        }

        void Align() {
            if (AppendOffset != BufferSize) {
                AppendOffset += -(reinterpret_cast<uintptr_t>(AppendBuffer->Data) + AppendOffset) & 63;
                if (AppendOffset > BufferSize) {
                    AppendOffset = BufferSize;
                    DropBufferReference(std::exchange(AppendBuffer, nullptr));
                }
            }
        }

        void Append(TContiguousSpan span) {
            if (AppendBuffer && span.data() == AppendBuffer->Data + AppendOffset) { // the only valid case to use previously acquired span
                AppendAcquiredSpan(span);
            } else {
#ifndef NDEBUG
                // ensure this span does not point into any existing buffer part
                const char *begin = span.data();
                const char *end = span.data() + span.size();
                for (const auto& buffer : Buffers) {
                    const char *bufferBegin = buffer->Data;
                    const char *bufferEnd = bufferBegin + BufferSize;
                    if (bufferBegin < end && begin < bufferEnd) {
                        Y_ABORT();
                    }
                }
#endif
                AppendSpanWithGlueing(span, nullptr);
            }
        }

        void Write(TContiguousSpan in) {
            while (in.size()) {
                auto outChunk = AcquireSpanForWriting(in.size());
                memcpy(outChunk.data(), in.data(), outChunk.size());
                AppendAcquiredSpan(outChunk);
                in = in.SubSpan(outChunk.size(), Max<size_t>());
            }
        }

        using TBookmark = TStackVec<TMutableContiguousSpan, 2>;

        TBookmark Bookmark(size_t len) {
            TBookmark bookmark;

            while (len) {
                const auto span = AcquireSpanForWriting(len);
                AppendAcquiredSpan(span);
                bookmark.push_back(span);
                len -= span.size();
            }

            return bookmark;
        }

        void WriteBookmark(TBookmark&& bookmark, TContiguousSpan in) {
            for (auto& outChunk : bookmark) {
                Y_DEBUG_ABORT_UNLESS(outChunk.size() <= in.size());
                memcpy(outChunk.data(), in.data(), outChunk.size());
                in = in.SubSpan(outChunk.size(), Max<size_t>());
            }
        }

        void Rewind() {
            SendQueuePos = 0;
            SendOffset = 0;
            UnsentBytes = 0;
            for (const auto& item : SendQueue) {
                UnsentBytes += item.Span.size();
            }
        }

        void RewindToEnd() {
            SendQueuePos = SendQueue.size();
            SendOffset = 0;
            UnsentBytes = 0;
        }

        template<typename T>
        void ProduceIoVec(T& container, size_t maxItems, size_t maxBytes) {
            size_t offset = SendOffset;
            for (auto it = SendQueue.begin() + SendQueuePos; it != SendQueue.end() && std::size(container) < maxItems && maxBytes; ++it) {
                const TContiguousSpan span = it->Span.SubSpan(offset, maxBytes);
                container.push_back(NActors::TConstIoVec{span.data(), span.size()});
                offset = 0;
                maxBytes -= span.size();
            }
        }

        void Advance(size_t numBytes) { // called when numBytes portion of data has been sent
            Y_DEBUG_ABORT_UNLESS(numBytes == 0 || SendQueuePos != SendQueue.size());
            Y_DEBUG_ABORT_UNLESS(numBytes <= UnsentBytes);
            SendOffset += numBytes;
            UnsentBytes -= numBytes;
            for (auto it = SendQueue.begin() + SendQueuePos; SendOffset && it->Span.size() <= SendOffset; ++SendQueuePos, ++it) {
                SendOffset -= it->Span.size();
                Y_DEBUG_ABORT_UNLESS(SendOffset == 0 || SendQueuePos != SendQueue.size() - 1);
            }
        }

        void DropFront(size_t numBytes) { // drops first numBytes from the queue, freeing buffers when necessary
            while (numBytes) {
                Y_DEBUG_ABORT_UNLESS(!SendQueue.empty());
                auto& front = SendQueue.front();
                if (numBytes < front.Span.size()) {
                    front.Span = front.Span.SubSpan(numBytes, Max<size_t>());
                    if (SendQueuePos == 0) {
                        Y_DEBUG_ABORT_UNLESS(numBytes <= SendOffset, "numBytes# %zu SendOffset# %zu SendQueuePos# %zu"
                            " SendQueue.size# %zu CalculateUnsentSize# %zu", numBytes, SendOffset, SendQueuePos,
                            SendQueue.size(), CalculateUnsentSize());
                        SendOffset -= numBytes;
                    }
                    break;
                } else {
                    numBytes -= front.Span.size();
                }
                Y_DEBUG_ABORT_UNLESS(!front.Buffer || (front.Span.data() >= front.Buffer->Data &&
                    front.Span.data() + front.Span.size() <= front.Buffer->Data + BufferSize));
                DropBufferReference(front.Buffer);
                SendQueue.pop_front();
                if (SendQueuePos) {
                    --SendQueuePos;
                } else {
                    SendOffset = 0;
                }
            }
        }

        template<typename T>
        void ScanLastBytes(size_t numBytes, T&& callback) const {
            auto it = SendQueue.end();
            ssize_t offset = -numBytes;
            while (offset < 0) {
                Y_DEBUG_ABORT_UNLESS(it != SendQueue.begin());
                const TSendChunk& chunk = *--it;
                offset += chunk.Span.size();
            }
            for (; it != SendQueue.end(); ++it, offset = 0) {
                callback(it->Span.SubSpan(offset, Max<size_t>()));
            }
        }

    private:
        void AppendAcquiredSpan(TContiguousSpan span) {
            TBuffer *buffer = AppendBuffer;
            Y_DEBUG_ABORT_UNLESS(buffer);
            Y_DEBUG_ABORT_UNLESS(span.data() == AppendBuffer->Data + AppendOffset);
            AppendOffset += span.size();
            Y_DEBUG_ABORT_UNLESS(AppendOffset <= BufferSize);
            if (AppendOffset == BufferSize) {
                AppendBuffer = nullptr;
            } else {
                ++buffer->RefCount;
            }
            AppendSpanWithGlueing(span, buffer);
        }

        void AppendSpanWithGlueing(TContiguousSpan span, TBuffer *buffer) {
            UnsentBytes += span.size();
            if (!SendQueue.empty()) {
                auto& back = SendQueue.back();
                if (back.Span.data() + back.Span.size() == span.data()) { // check if it is possible just to extend the last span
                    Y_DEBUG_ABORT_UNLESS(buffer == back.Buffer);
                    if (SendQueuePos == SendQueue.size()) {
                        --SendQueuePos;
                        SendOffset = back.Span.size();
                    }
                    back.Span = {back.Span.data(), back.Span.size() + span.size()};
                    DropBufferReference(buffer);
                    return;
                }
            }
            SendQueue.push_back(TSendChunk{span, buffer});
        }

        void DropBufferReference(TBuffer *buffer) {
            if (buffer && !--buffer->RefCount) {
                const size_t index = buffer->Index;
                auto& cell = Buffers[index];
                Y_DEBUG_ABORT_UNLESS(cell.get() == buffer);
                std::swap(cell, Buffers.back());
                cell->Index = index;
                Buffers.pop_back();
            }
        }
    };

    using TOutgoingStream = TOutgoingStreamT<262144>;

} // NInterconnect