xref: /openbmc/qemu/event-loop-base.c (revision 019fbfa4bcd2d3a835c241295e22ab2b5b56129b)
1 /*
2  * QEMU event-loop base
3  *
4  * Copyright (C) 2022 Red Hat Inc
5  *
6  * Authors:
7  *  Stefan Hajnoczi <stefanha@redhat.com>
8  *  Nicolas Saenz Julienne <nsaenzju@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qom/object_interfaces.h"
16 #include "qapi/error.h"
17 #include "block/thread-pool.h"
18 #include "system/event-loop-base.h"
19 
20 typedef struct {
21     const char *name;
22     ptrdiff_t offset; /* field's byte offset in EventLoopBase struct */
23 } EventLoopBaseParamInfo;
24 
25 static void event_loop_base_instance_init(Object *obj)
26 {
27     EventLoopBase *base = EVENT_LOOP_BASE(obj);
28 
29     base->thread_pool_max = THREAD_POOL_MAX_THREADS_DEFAULT;
30 }
31 
32 static EventLoopBaseParamInfo aio_max_batch_info = {
33     "aio-max-batch", offsetof(EventLoopBase, aio_max_batch),
34 };
35 static EventLoopBaseParamInfo thread_pool_min_info = {
36     "thread-pool-min", offsetof(EventLoopBase, thread_pool_min),
37 };
38 static EventLoopBaseParamInfo thread_pool_max_info = {
39     "thread-pool-max", offsetof(EventLoopBase, thread_pool_max),
40 };
41 
42 static void event_loop_base_get_param(Object *obj, Visitor *v,
43         const char *name, void *opaque, Error **errp)
44 {
45     EventLoopBase *event_loop_base = EVENT_LOOP_BASE(obj);
46     EventLoopBaseParamInfo *info = opaque;
47     int64_t *field = (void *)event_loop_base + info->offset;
48 
49     visit_type_int64(v, name, field, errp);
50 }
51 
52 static void event_loop_base_set_param(Object *obj, Visitor *v,
53         const char *name, void *opaque, Error **errp)
54 {
55     EventLoopBaseClass *bc = EVENT_LOOP_BASE_GET_CLASS(obj);
56     EventLoopBase *base = EVENT_LOOP_BASE(obj);
57     EventLoopBaseParamInfo *info = opaque;
58     int64_t *field = (void *)base + info->offset;
59     int64_t value;
60 
61     if (!visit_type_int64(v, name, &value, errp)) {
62         return;
63     }
64 
65     if (value < 0) {
66         error_setg(errp, "%s value must be in range [0, %" PRId64 "]",
67                    info->name, INT64_MAX);
68         return;
69     }
70 
71     *field = value;
72 
73     if (bc->update_params) {
74         bc->update_params(base, errp);
75     }
76 }
77 
78 static void event_loop_base_complete(UserCreatable *uc, Error **errp)
79 {
80     EventLoopBaseClass *bc = EVENT_LOOP_BASE_GET_CLASS(uc);
81     EventLoopBase *base = EVENT_LOOP_BASE(uc);
82 
83     if (bc->init) {
84         bc->init(base, errp);
85     }
86 }
87 
88 static bool event_loop_base_can_be_deleted(UserCreatable *uc)
89 {
90     EventLoopBaseClass *bc = EVENT_LOOP_BASE_GET_CLASS(uc);
91     EventLoopBase *backend = EVENT_LOOP_BASE(uc);
92 
93     if (bc->can_be_deleted) {
94         return bc->can_be_deleted(backend);
95     }
96 
97     return true;
98 }
99 
100 static void event_loop_base_class_init(ObjectClass *klass, void *class_data)
101 {
102     UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
103     ucc->complete = event_loop_base_complete;
104     ucc->can_be_deleted = event_loop_base_can_be_deleted;
105 
106     object_class_property_add(klass, "aio-max-batch", "int",
107                               event_loop_base_get_param,
108                               event_loop_base_set_param,
109                               NULL, &aio_max_batch_info);
110     object_class_property_add(klass, "thread-pool-min", "int",
111                               event_loop_base_get_param,
112                               event_loop_base_set_param,
113                               NULL, &thread_pool_min_info);
114     object_class_property_add(klass, "thread-pool-max", "int",
115                               event_loop_base_get_param,
116                               event_loop_base_set_param,
117                               NULL, &thread_pool_max_info);
118 }
119 
120 static const TypeInfo event_loop_base_info = {
121     .name = TYPE_EVENT_LOOP_BASE,
122     .parent = TYPE_OBJECT,
123     .instance_size = sizeof(EventLoopBase),
124     .instance_init = event_loop_base_instance_init,
125     .class_size = sizeof(EventLoopBaseClass),
126     .class_init = event_loop_base_class_init,
127     .abstract = true,
128     .interfaces = (InterfaceInfo[]) {
129         { TYPE_USER_CREATABLE },
130         { }
131     }
132 };
133 
134 static void register_types(void)
135 {
136     type_register_static(&event_loop_base_info);
137 }
138 type_init(register_types);
139