Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | """Generate coroutine wrappers for block subsystem. |
| 3 | |
| 4 | The program parses one or several concatenated c files from stdin, |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 5 | searches for functions with the 'co_wrapper' specifier |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 6 | and generates corresponding wrappers on stdout. |
| 7 | |
| 8 | Usage: block-coroutine-wrapper.py generated-file.c FILE.[ch]... |
| 9 | |
| 10 | Copyright (c) 2020 Virtuozzo International GmbH. |
| 11 | |
| 12 | This program is free software; you can redistribute it and/or modify |
| 13 | it under the terms of the GNU General Public License as published by |
| 14 | the Free Software Foundation; either version 2 of the License, or |
| 15 | (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | """ |
| 25 | |
| 26 | import sys |
| 27 | import re |
| 28 | from typing import Iterator |
| 29 | |
| 30 | |
| 31 | def gen_header(): |
| 32 | copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL) |
| 33 | copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE) |
| 34 | copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE) |
| 35 | return f"""\ |
| 36 | /* |
| 37 | * File is generated by scripts/block-coroutine-wrapper.py |
| 38 | * |
| 39 | {copyright} |
| 40 | */ |
| 41 | |
| 42 | #include "qemu/osdep.h" |
| 43 | #include "block/coroutines.h" |
| 44 | #include "block/block-gen.h" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 45 | #include "block/block_int.h" |
| 46 | #include "block/dirty-bitmap.h" |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 47 | """ |
| 48 | |
| 49 | |
| 50 | class ParamDecl: |
| 51 | param_re = re.compile(r'(?P<decl>' |
| 52 | r'(?P<type>.*[ *])' |
| 53 | r'(?P<name>[a-z][a-z0-9_]*)' |
| 54 | r')') |
| 55 | |
| 56 | def __init__(self, param_decl: str) -> None: |
| 57 | m = self.param_re.match(param_decl.strip()) |
| 58 | if m is None: |
| 59 | raise ValueError(f'Wrong parameter declaration: "{param_decl}"') |
| 60 | self.decl = m.group('decl') |
| 61 | self.type = m.group('type') |
| 62 | self.name = m.group('name') |
| 63 | |
| 64 | |
| 65 | class FuncDecl: |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 66 | def __init__(self, wrapper_type: str, return_type: str, name: str, |
| 67 | args: str, variant: str) -> None: |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 68 | self.return_type = return_type.strip() |
| 69 | self.name = name.strip() |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 70 | self.struct_name = snake_to_camel(self.name) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 71 | self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 72 | self.create_only_co = 'mixed' not in variant |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 73 | self.graph_rdlock = 'bdrv_rdlock' in variant |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 74 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 75 | self.wrapper_type = wrapper_type |
| 76 | |
| 77 | if wrapper_type == 'co': |
| 78 | subsystem, subname = self.name.split('_', 1) |
| 79 | self.target_name = f'{subsystem}_co_{subname}' |
| 80 | else: |
| 81 | assert wrapper_type == 'no_co' |
| 82 | subsystem, co_infix, subname = self.name.split('_', 2) |
| 83 | if co_infix != 'co': |
| 84 | raise ValueError(f"Invalid no_co function name: {self.name}") |
| 85 | if not self.create_only_co: |
| 86 | raise ValueError(f"no_co function can't be mixed: {self.name}") |
| 87 | if self.graph_rdlock: |
| 88 | raise ValueError(f"no_co function can't be rdlock: {self.name}") |
| 89 | self.target_name = f'{subsystem}_{subname}' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 90 | |
| 91 | t = self.args[0].type |
| 92 | if t == 'BlockDriverState *': |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame] | 93 | ctx = 'bdrv_get_aio_context(bs)' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 94 | elif t == 'BdrvChild *': |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame] | 95 | ctx = 'bdrv_get_aio_context(child->bs)' |
| 96 | elif t == 'BlockBackend *': |
| 97 | ctx = 'blk_get_aio_context(blk)' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 98 | else: |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame] | 99 | ctx = 'qemu_get_aio_context()' |
| 100 | self.ctx = ctx |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 101 | |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 102 | self.get_result = 's->ret = ' |
| 103 | self.ret = 'return s.ret;' |
| 104 | self.co_ret = 'return ' |
| 105 | self.return_field = self.return_type + " ret;" |
| 106 | if self.return_type == 'void': |
| 107 | self.get_result = '' |
| 108 | self.ret = '' |
| 109 | self.co_ret = '' |
| 110 | self.return_field = '' |
| 111 | |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 112 | def gen_list(self, format: str) -> str: |
| 113 | return ', '.join(format.format_map(arg.__dict__) for arg in self.args) |
| 114 | |
| 115 | def gen_block(self, format: str) -> str: |
| 116 | return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) |
| 117 | |
| 118 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 119 | # Match wrappers declared with a co_wrapper mark |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 120 | func_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)' |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 121 | r'(\s*coroutine_fn)?' |
| 122 | r'\s*(?P<wrapper_type>(no_)?co)_wrapper' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 123 | r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 124 | r'(?P<wrapper_name>[a-z][a-z0-9_]*)' |
| 125 | r'\((?P<args>[^)]*)\);$', re.MULTILINE) |
| 126 | |
| 127 | |
| 128 | def func_decl_iter(text: str) -> Iterator: |
| 129 | for m in func_decl_re.finditer(text): |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 130 | yield FuncDecl(wrapper_type=m.group('wrapper_type'), |
| 131 | return_type=m.group('return_type'), |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 132 | name=m.group('wrapper_name'), |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 133 | args=m.group('args'), |
| 134 | variant=m.group('variant')) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def snake_to_camel(func_name: str) -> str: |
| 138 | """ |
| 139 | Convert underscore names like 'some_function_name' to camel-case like |
| 140 | 'SomeFunctionName' |
| 141 | """ |
| 142 | words = func_name.split('_') |
| 143 | words = [w[0].upper() + w[1:] for w in words] |
| 144 | return ''.join(words) |
| 145 | |
| 146 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 147 | def create_mixed_wrapper(func: FuncDecl) -> str: |
| 148 | """ |
| 149 | Checks if we are already in coroutine |
| 150 | """ |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 151 | name = func.target_name |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 152 | struct_name = func.struct_name |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 153 | graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else '' |
| 154 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 155 | return f"""\ |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 156 | {func.return_type} {func.name}({ func.gen_list('{decl}') }) |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 157 | {{ |
| 158 | if (qemu_in_coroutine()) {{ |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 159 | {graph_assume_lock} |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 160 | {func.co_ret}{name}({ func.gen_list('{name}') }); |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 161 | }} else {{ |
| 162 | {struct_name} s = {{ |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame] | 163 | .poll_state.ctx = {func.ctx}, |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 164 | .poll_state.in_progress = true, |
| 165 | |
| 166 | { func.gen_block(' .{name} = {name},') } |
| 167 | }}; |
| 168 | |
| 169 | s.poll_state.co = qemu_coroutine_create({name}_entry, &s); |
| 170 | |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 171 | bdrv_poll_co(&s.poll_state); |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 172 | {func.ret} |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 173 | }} |
| 174 | }}""" |
| 175 | |
| 176 | |
| 177 | def create_co_wrapper(func: FuncDecl) -> str: |
| 178 | """ |
| 179 | Assumes we are not in coroutine, and creates one |
| 180 | """ |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 181 | name = func.target_name |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 182 | struct_name = func.struct_name |
| 183 | return f"""\ |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 184 | {func.return_type} {func.name}({ func.gen_list('{decl}') }) |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 185 | {{ |
| 186 | {struct_name} s = {{ |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame] | 187 | .poll_state.ctx = {func.ctx}, |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 188 | .poll_state.in_progress = true, |
| 189 | |
| 190 | { func.gen_block(' .{name} = {name},') } |
| 191 | }}; |
| 192 | assert(!qemu_in_coroutine()); |
| 193 | |
| 194 | s.poll_state.co = qemu_coroutine_create({name}_entry, &s); |
| 195 | |
Emanuele Giuseppe Esposito | 6700dfb | 2022-11-28 09:23:35 -0500 | [diff] [blame] | 196 | bdrv_poll_co(&s.poll_state); |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 197 | {func.ret} |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 198 | }}""" |
| 199 | |
| 200 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 201 | def gen_co_wrapper(func: FuncDecl) -> str: |
Vladimir Sementsov-Ogievskiy | bb43694 | 2021-06-10 13:07:57 +0300 | [diff] [blame] | 202 | assert not '_co_' in func.name |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 203 | assert func.wrapper_type == 'co' |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 204 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 205 | name = func.target_name |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 206 | struct_name = func.struct_name |
Vladimir Sementsov-Ogievskiy | bb43694 | 2021-06-10 13:07:57 +0300 | [diff] [blame] | 207 | |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 208 | graph_lock='' |
| 209 | graph_unlock='' |
| 210 | if func.graph_rdlock: |
| 211 | graph_lock=' bdrv_graph_co_rdlock();' |
| 212 | graph_unlock=' bdrv_graph_co_rdunlock();' |
| 213 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 214 | creation_function = create_mixed_wrapper |
| 215 | if func.create_only_co: |
| 216 | creation_function = create_co_wrapper |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 217 | |
| 218 | return f"""\ |
| 219 | /* |
| 220 | * Wrappers for {name} |
| 221 | */ |
| 222 | |
| 223 | typedef struct {struct_name} {{ |
| 224 | BdrvPollCo poll_state; |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 225 | {func.return_field} |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 226 | { func.gen_block(' {decl};') } |
| 227 | }} {struct_name}; |
| 228 | |
| 229 | static void coroutine_fn {name}_entry(void *opaque) |
| 230 | {{ |
| 231 | {struct_name} *s = opaque; |
| 232 | |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 233 | {graph_lock} |
Emanuele Giuseppe Esposito | 5b317b8 | 2023-01-13 21:41:59 +0100 | [diff] [blame] | 234 | {func.get_result}{name}({ func.gen_list('s->{name}') }); |
Emanuele Giuseppe Esposito | e6d3f7a | 2022-12-07 14:18:36 +0100 | [diff] [blame] | 235 | {graph_unlock} |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 236 | s->poll_state.in_progress = false; |
| 237 | |
| 238 | aio_wait_kick(); |
| 239 | }} |
| 240 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 241 | {creation_function(func)}""" |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 242 | |
| 243 | |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 244 | def gen_no_co_wrapper(func: FuncDecl) -> str: |
| 245 | assert '_co_' in func.name |
| 246 | assert func.wrapper_type == 'no_co' |
| 247 | |
| 248 | name = func.target_name |
| 249 | struct_name = func.struct_name |
| 250 | |
| 251 | return f"""\ |
| 252 | /* |
| 253 | * Wrappers for {name} |
| 254 | */ |
| 255 | |
| 256 | typedef struct {struct_name} {{ |
| 257 | Coroutine *co; |
| 258 | {func.return_field} |
| 259 | { func.gen_block(' {decl};') } |
| 260 | }} {struct_name}; |
| 261 | |
| 262 | static void {name}_bh(void *opaque) |
| 263 | {{ |
| 264 | {struct_name} *s = opaque; |
| 265 | |
| 266 | {func.get_result}{name}({ func.gen_list('s->{name}') }); |
| 267 | |
| 268 | aio_co_wake(s->co); |
| 269 | }} |
| 270 | |
| 271 | {func.return_type} coroutine_fn {func.name}({ func.gen_list('{decl}') }) |
| 272 | {{ |
| 273 | {struct_name} s = {{ |
| 274 | .co = qemu_coroutine_self(), |
| 275 | { func.gen_block(' .{name} = {name},') } |
| 276 | }}; |
| 277 | assert(qemu_in_coroutine()); |
| 278 | |
| 279 | aio_bh_schedule_oneshot(qemu_get_aio_context(), {name}_bh, &s); |
| 280 | qemu_coroutine_yield(); |
| 281 | |
| 282 | {func.ret} |
| 283 | }}""" |
| 284 | |
| 285 | |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 286 | def gen_wrappers(input_code: str) -> str: |
| 287 | res = '' |
| 288 | for func in func_decl_iter(input_code): |
| 289 | res += '\n\n\n' |
Kevin Wolf | d6ee2e3 | 2023-01-26 18:24:20 +0100 | [diff] [blame^] | 290 | if func.wrapper_type == 'co': |
| 291 | res += gen_co_wrapper(func) |
| 292 | else: |
| 293 | res += gen_no_co_wrapper(func) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 294 | |
| 295 | return res |
| 296 | |
| 297 | |
| 298 | if __name__ == '__main__': |
| 299 | if len(sys.argv) < 3: |
| 300 | exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') |
| 301 | |
| 302 | with open(sys.argv[1], 'w', encoding='utf-8') as f_out: |
| 303 | f_out.write(gen_header()) |
| 304 | for fname in sys.argv[2:]: |
| 305 | with open(fname, encoding='utf-8') as f_in: |
| 306 | f_out.write(gen_wrappers(f_in.read())) |
| 307 | f_out.write('\n') |