| 1 | /* |
| 2 | * Event loop thread |
| 3 | * |
| 4 | * Copyright Red Hat Inc., 2013 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Stefan Hajnoczi <stefanha@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #ifndef IOTHREAD_H |
| 15 | #define IOTHREAD_H |
| 16 | |
| 17 | #include "qemu/aio.h" |
| 18 | #include "qemu/thread.h" |
| 19 | #include "qom/object.h" |
| 20 | #include "system/event-loop-base.h" |
| 21 | |
| 22 | #define TYPE_IOTHREAD "iothread" |
| 23 | |
| 24 | #ifdef CONFIG_POSIX |
| 25 | /* |
| 26 | * Benchmark results from 2016 on NVMe SSD drives show max polling times around |
| 27 | * 16-32 microseconds yield IOPS improvements for both iodepth=1 and iodepth=32 |
| 28 | * workloads. |
| 29 | */ |
| 30 | #define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL |
| 31 | #define IOTHREAD_POLL_GROW_DEFAULT 2ULL |
| 32 | #define IOTHREAD_POLL_SHRINK_DEFAULT 2ULL |
| 33 | #define IOTHREAD_POLL_WEIGHT_DEFAULT 3ULL |
| 34 | #else |
| 35 | #define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL |
| 36 | #define IOTHREAD_POLL_GROW_DEFAULT 0ULL |
| 37 | #define IOTHREAD_POLL_SHRINK_DEFAULT 0ULL |
| 38 | #define IOTHREAD_POLL_WEIGHT_DEFAULT 0ULL |
| 39 | #endif |
| 40 | |
| 41 | struct IOThread { |
| 42 | EventLoopBase parent_obj; |
| 43 | |
| 44 | QemuThread thread; |
| 45 | AioContext *ctx; |
| 46 | bool run_gcontext; /* whether we should run gcontext */ |
| 47 | GMainContext *worker_context; |
| 48 | GMainLoop *main_loop; |
| 49 | QemuSemaphore init_done_sem; /* is thread init done? */ |
| 50 | bool stopping; /* has iothread_stop() been called? */ |
| 51 | bool running; /* should iothread_run() continue? */ |
| 52 | int thread_id; |
| 53 | |
| 54 | /* AioContext poll parameters */ |
| 55 | int64_t poll_max_ns; |
| 56 | int64_t poll_grow; |
| 57 | int64_t poll_shrink; |
| 58 | int64_t poll_weight; |
| 59 | }; |
| 60 | typedef struct IOThread IOThread; |
| 61 | |
| 62 | DECLARE_INSTANCE_CHECKER(IOThread, IOTHREAD, |
| 63 | TYPE_IOTHREAD) |
| 64 | |
| 65 | char *iothread_get_id(IOThread *iothread); |
| 66 | IOThread *iothread_by_id(const char *id); |
| 67 | AioContext *iothread_get_aio_context(IOThread *iothread); |
| 68 | GMainContext *iothread_get_g_main_context(IOThread *iothread); |
| 69 | |
| 70 | /* |
| 71 | * Helpers used to allocate iothreads for internal use. These |
| 72 | * iothreads will not be seen by monitor clients when query using |
| 73 | * "query-iothreads". |
| 74 | */ |
| 75 | IOThread *iothread_create(const char *id, Error **errp); |
| 76 | void iothread_stop(IOThread *iothread); |
| 77 | void iothread_destroy(IOThread *iothread); |
| 78 | |
| 79 | /* |
| 80 | * Returns true if executing within IOThread context, |
| 81 | * false otherwise. |
| 82 | */ |
| 83 | bool qemu_in_iothread(void); |
| 84 | |
| 85 | #endif /* IOTHREAD_H */ |