source: mainline/uspace/srv/bd/hr/fge.c@ eb31781

Last change on this file since eb31781 was 8137d36, checked in by Miroslav Cimerman <mc@…>, 8 months ago

hr: fge: style

  • Property mode set to 100644
File size: 11.2 KB
Line 
1/*
2 * Copyright (c) 2024 Miroslav Cimerman
3 * Copyright (c) 2024 Vojtech Horky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup hr
31 * @{
32 */
33/**
34 * @file
35 * @brief Fibril group executor
36 *
37 * Fibril pool with pre-allocated storage allowing
38 * execution of groups consisting of multiple work
39 * units.
40 */
41
42#include <adt/bitmap.h>
43#include <adt/circ_buf.h>
44#include <assert.h>
45#include <errno.h>
46#include <fibril_synch.h>
47#include <stdatomic.h>
48#include <stdbool.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <types/common.h>
52
53#include "fge.h"
54
55struct fge_fibril_data;
56typedef struct fge_fibril_data fge_fibril_data_t;
57struct wu_queue;
58typedef struct wu_queue wu_queue_t;
59
60static void *hr_fpool_make_storage(hr_fpool_t *, ssize_t *);
61static void hr_fpool_group_epilogue(hr_fpool_t *);
62static errno_t fge_fibril(void *);
63static errno_t wu_queue_init(wu_queue_t *, size_t);
64static void wu_queue_push(wu_queue_t *, fge_fibril_data_t *);
65static void wu_queue_pop(wu_queue_t *, fge_fibril_data_t *);
66static ssize_t hr_fpool_get_free_slot(hr_fpool_t *);
67
68typedef struct fge_fibril_data {
69 hr_wu_t wu; /* user-provided work unit fcn pointer */
70 void *arg;
71 hr_fgroup_t *group;
72 ssize_t memslot; /* index to pool bitmap slot */
73} fge_fibril_data_t;
74
75typedef struct wu_queue {
76 fibril_mutex_t lock;
77 fibril_condvar_t not_empty;
78 fibril_condvar_t not_full;
79 fge_fibril_data_t *fexecs;
80 circ_buf_t cbuf;
81} wu_queue_t;
82
83struct hr_fpool {
84 fibril_mutex_t lock;
85 fibril_condvar_t all_wus_done;
86 bitmap_t bitmap;
87 wu_queue_t queue;
88 fid_t *fibrils;
89 uint8_t *wu_storage;
90 size_t fibril_cnt;
91 size_t max_wus;
92 size_t active_groups;
93 bool stop;
94 size_t wu_size;
95 size_t wu_storage_free_count;
96};
97
98struct hr_fgroup {
99 hr_fpool_t *pool;
100 size_t wu_cnt; /* total wu count */
101 size_t submitted;
102 size_t reserved_cnt; /* no. of reserved wu storage slots */
103 size_t reserved_avail;
104 size_t *memslots; /* indices to pool bitmap */
105 void *own_mem;
106 size_t own_used;
107 errno_t final_errno;
108 atomic_size_t finished_okay;
109 atomic_size_t finished_fail;
110 atomic_size_t wus_started;
111 fibril_mutex_t lock;
112 fibril_condvar_t all_done;
113};
114
115hr_fpool_t *hr_fpool_create(size_t fibril_cnt, size_t max_wus,
116 size_t wu_storage_size)
117{
118 void *bitmap_data = NULL;
119
120 hr_fpool_t *result = calloc(1, sizeof(hr_fpool_t));
121 if (result == NULL)
122 return NULL;
123
124 result->fibrils = malloc(sizeof(fid_t) * fibril_cnt);
125 if (result->fibrils == NULL)
126 goto bad;
127
128 result->wu_storage = malloc(wu_storage_size * max_wus);
129 if (result->wu_storage == NULL)
130 goto bad;
131
132 bitmap_data = calloc(1, bitmap_size(max_wus));
133 if (bitmap_data == NULL)
134 goto bad;
135 bitmap_initialize(&result->bitmap, max_wus, bitmap_data);
136
137 if (wu_queue_init(&result->queue, max_wus) != EOK)
138 goto bad;
139
140 fibril_mutex_initialize(&result->lock);
141 fibril_condvar_initialize(&result->all_wus_done);
142
143 result->max_wus = max_wus;
144 result->fibril_cnt = fibril_cnt;
145 result->wu_size = wu_storage_size;
146 result->wu_storage_free_count = max_wus;
147 result->stop = false;
148 result->active_groups = 0;
149
150 for (size_t i = 0; i < fibril_cnt; i++) {
151 result->fibrils[i] = fibril_create(fge_fibril, result);
152 fibril_start(result->fibrils[i]);
153 }
154
155 return result;
156bad:
157 if (result->queue.fexecs)
158 free(result->queue.fexecs);
159 if (bitmap_data)
160 free(bitmap_data);
161 if (result->wu_storage)
162 free(result->wu_storage);
163 if (result->fibrils)
164 free(result->fibrils);
165 free(result);
166
167 return NULL;
168}
169
170void hr_fpool_destroy(hr_fpool_t *pool)
171{
172 fibril_mutex_lock(&pool->lock);
173 pool->stop = true;
174 while (pool->active_groups > 0)
175 fibril_condvar_wait(&pool->all_wus_done, &pool->lock);
176
177 fibril_mutex_unlock(&pool->lock);
178
179 free(pool->bitmap.bits);
180 free(pool->queue.fexecs);
181 free(pool->wu_storage);
182 free(pool->fibrils);
183 free(pool);
184}
185
186hr_fgroup_t *hr_fgroup_create(hr_fpool_t *parent, size_t wu_cnt)
187{
188 hr_fgroup_t *result = malloc(sizeof(hr_fgroup_t));
189 if (result == NULL)
190 return NULL;
191
192 result->reserved_cnt = 0;
193 result->own_mem = NULL;
194 result->memslots = NULL;
195
196 fibril_mutex_lock(&parent->lock);
197
198 parent->active_groups++;
199
200 if (parent->wu_storage_free_count >= wu_cnt) {
201 parent->wu_storage_free_count -= wu_cnt;
202 result->reserved_cnt = wu_cnt;
203 } else {
204 /*
205 * Could be more conservative with memory here and
206 * allocate space only for one work unit and execute
207 * work units sequentially like it was first intended with
208 * the fallback storage.
209 */
210 size_t taking = parent->wu_storage_free_count;
211 result->own_mem = malloc(parent->wu_size * (wu_cnt - taking));
212 if (result->own_mem == NULL)
213 goto bad;
214 result->reserved_cnt = taking;
215 parent->wu_storage_free_count = 0;
216 }
217
218 if (result->reserved_cnt > 0) {
219 result->memslots =
220 malloc(sizeof(size_t) * result->reserved_cnt);
221 if (result->memslots == NULL)
222 goto bad;
223 }
224
225 fibril_mutex_unlock(&parent->lock);
226
227 result->pool = parent;
228 result->wu_cnt = wu_cnt;
229 result->submitted = 0;
230 result->reserved_avail = result->reserved_cnt;
231 result->own_used = 0;
232 result->final_errno = EOK;
233 result->finished_okay = 0;
234 result->finished_fail = 0;
235 result->wus_started = 0;
236
237 fibril_mutex_initialize(&result->lock);
238 fibril_condvar_initialize(&result->all_done);
239
240 return result;
241
242bad:
243 fibril_mutex_lock(&parent->lock);
244 parent->wu_storage_free_count += result->reserved_cnt;
245 fibril_mutex_unlock(&parent->lock);
246
247 if (result->memslots)
248 free(result->memslots);
249 if (result->own_mem)
250 free(result->own_mem);
251 free(result);
252
253 return NULL;
254}
255
256void *hr_fgroup_alloc(hr_fgroup_t *group)
257{
258 void *storage;
259
260 fibril_mutex_lock(&group->lock);
261
262 if (group->reserved_avail > 0) {
263 ssize_t memslot;
264 storage = hr_fpool_make_storage(group->pool, &memslot);
265 assert(storage != NULL);
266 group->reserved_avail--;
267 group->memslots[group->submitted] = memslot;
268 } else {
269 storage =
270 group->own_mem + group->pool->wu_size * group->own_used;
271 group->own_used++;
272 }
273
274 fibril_mutex_unlock(&group->lock);
275
276 return storage;
277}
278
279void hr_fgroup_submit(hr_fgroup_t *group, hr_wu_t wu, void *arg)
280{
281 fibril_mutex_lock(&group->lock);
282 assert(group->submitted + 1 <= group->wu_cnt);
283
284 fge_fibril_data_t executor;
285 executor.wu = wu;
286 executor.arg = arg;
287 executor.group = group;
288
289 if (group->submitted < group->reserved_cnt)
290 executor.memslot = group->memslots[group->submitted];
291 else
292 executor.memslot = -1;
293
294 group->submitted++;
295 fibril_mutex_unlock(&group->lock);
296
297 wu_queue_push(&group->pool->queue, &executor);
298}
299
300errno_t hr_fgroup_wait(hr_fgroup_t *group, size_t *rokay, size_t *rfailed)
301{
302 fibril_mutex_lock(&group->lock);
303 while (true) {
304 size_t finished = group->finished_fail + group->finished_okay;
305 if (group->wus_started != 0 && group->wus_started == finished)
306 break;
307
308 fibril_condvar_wait(&group->all_done, &group->lock);
309 }
310
311 if (rokay)
312 *rokay = group->finished_okay;
313 if (rfailed)
314 *rfailed = group->finished_fail;
315
316 errno_t rc = EOK;
317 if (group->finished_okay != group->wus_started)
318 rc = EIO;
319
320 fibril_mutex_unlock(&group->lock);
321
322 hr_fpool_group_epilogue(group->pool);
323
324 if (group->memslots)
325 free(group->memslots);
326 if (group->own_mem)
327 free(group->own_mem);
328 free(group);
329
330 return rc;
331}
332
333static void *hr_fpool_make_storage(hr_fpool_t *pool, ssize_t *rmemslot)
334{
335 fibril_mutex_lock(&pool->lock);
336 ssize_t memslot = hr_fpool_get_free_slot(pool);
337 assert(memslot != -1);
338
339 bitmap_set(&pool->bitmap, memslot, 1);
340
341 fibril_mutex_unlock(&pool->lock);
342
343 if (rmemslot)
344 *rmemslot = memslot;
345
346 return pool->wu_storage + pool->wu_size * memslot;
347}
348
349static void hr_fpool_group_epilogue(hr_fpool_t *pool)
350{
351 fibril_mutex_lock(&pool->lock);
352
353 pool->active_groups--;
354 if (pool->active_groups == 0)
355 fibril_condvar_signal(&pool->all_wus_done);
356
357 fibril_mutex_unlock(&pool->lock);
358}
359
360static errno_t fge_fibril(void *arg)
361{
362 hr_fpool_t *pool = arg;
363 while (true) {
364 fge_fibril_data_t executor;
365 fibril_mutex_lock(&pool->lock);
366
367 while (circ_buf_nused(&pool->queue.cbuf) == 0 && !pool->stop) {
368 fibril_condvar_wait(&pool->queue.not_empty,
369 &pool->lock);
370 }
371
372 if (pool->stop && circ_buf_nused(&pool->queue.cbuf) == 0) {
373 fibril_mutex_unlock(&pool->lock);
374 break;
375 }
376
377 wu_queue_pop(&pool->queue, &executor);
378
379 fibril_mutex_unlock(&pool->lock);
380
381 hr_fgroup_t *group = executor.group;
382
383 atomic_fetch_add_explicit(&group->wus_started, 1,
384 memory_order_relaxed);
385
386 errno_t rc = executor.wu(executor.arg);
387
388 if (rc == EOK)
389 atomic_fetch_add_explicit(&group->finished_okay, 1,
390 memory_order_relaxed);
391 else
392 atomic_fetch_add_explicit(&group->finished_fail, 1,
393 memory_order_relaxed);
394
395 fibril_mutex_lock(&pool->lock);
396 if (executor.memslot > -1) {
397 bitmap_set(&pool->bitmap, executor.memslot, 0);
398 pool->wu_storage_free_count++;
399 }
400
401 size_t group_total_done = group->finished_fail +
402 group->finished_okay;
403 if (group->wus_started == group_total_done)
404 fibril_condvar_signal(&group->all_done);
405
406 fibril_mutex_unlock(&pool->lock);
407 }
408 return EOK;
409}
410
411static errno_t wu_queue_init(wu_queue_t *queue, size_t nmemb)
412{
413 queue->fexecs = malloc(sizeof(fge_fibril_data_t) * nmemb);
414 if (queue->fexecs == NULL)
415 return ENOMEM;
416
417 circ_buf_init(&queue->cbuf, queue->fexecs, nmemb,
418 sizeof(fge_fibril_data_t));
419
420 fibril_mutex_initialize(&queue->lock);
421 fibril_condvar_initialize(&queue->not_empty);
422 fibril_condvar_initialize(&queue->not_full);
423
424 return EOK;
425}
426
427static void wu_queue_push(wu_queue_t *queue, fge_fibril_data_t *executor)
428{
429 fibril_mutex_lock(&queue->lock);
430
431 while (circ_buf_push(&queue->cbuf, executor) == EAGAIN)
432 fibril_condvar_wait(&queue->not_full, &queue->lock);
433
434 fibril_condvar_signal(&queue->not_empty);
435
436 fibril_mutex_unlock(&queue->lock);
437}
438
439static void wu_queue_pop(wu_queue_t *queue, fge_fibril_data_t *executor)
440{
441 fibril_mutex_lock(&queue->lock);
442
443 while (circ_buf_pop(&queue->cbuf, executor) == EAGAIN)
444 fibril_condvar_wait(&queue->not_empty, &queue->lock);
445
446 fibril_condvar_signal(&queue->not_full);
447
448 fibril_mutex_unlock(&queue->lock);
449}
450
451static ssize_t hr_fpool_get_free_slot(hr_fpool_t *pool)
452{
453 bitmap_t *bitmap = &pool->bitmap;
454 for (size_t i = 0; i < pool->max_wus; i++)
455 if (!bitmap_get(bitmap, i))
456 return i;
457 return -1;
458}
459
460/** @}
461 */
Note: See TracBrowser for help on using the repository browser.