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" |
| 45 | #include "block/block_int.h"\ |
| 46 | """ |
| 47 | |
| 48 | |
| 49 | class ParamDecl: |
| 50 | param_re = re.compile(r'(?P<decl>' |
| 51 | r'(?P<type>.*[ *])' |
| 52 | r'(?P<name>[a-z][a-z0-9_]*)' |
| 53 | r')') |
| 54 | |
| 55 | def __init__(self, param_decl: str) -> None: |
| 56 | m = self.param_re.match(param_decl.strip()) |
| 57 | if m is None: |
| 58 | raise ValueError(f'Wrong parameter declaration: "{param_decl}"') |
| 59 | self.decl = m.group('decl') |
| 60 | self.type = m.group('type') |
| 61 | self.name = m.group('name') |
| 62 | |
| 63 | |
| 64 | class FuncDecl: |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 65 | def __init__(self, return_type: str, name: str, args: str, |
| 66 | variant: str) -> None: |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 67 | self.return_type = return_type.strip() |
| 68 | self.name = name.strip() |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 69 | self.struct_name = snake_to_camel(self.name) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 70 | self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 71 | self.create_only_co = 'mixed' not in variant |
| 72 | |
| 73 | subsystem, subname = self.name.split('_', 1) |
| 74 | self.co_name = f'{subsystem}_co_{subname}' |
| 75 | |
| 76 | t = self.args[0].type |
| 77 | if t == 'BlockDriverState *': |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame^] | 78 | ctx = 'bdrv_get_aio_context(bs)' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 79 | elif t == 'BdrvChild *': |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame^] | 80 | ctx = 'bdrv_get_aio_context(child->bs)' |
| 81 | elif t == 'BlockBackend *': |
| 82 | ctx = 'blk_get_aio_context(blk)' |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 83 | else: |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame^] | 84 | ctx = 'qemu_get_aio_context()' |
| 85 | self.ctx = ctx |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 86 | |
| 87 | def gen_list(self, format: str) -> str: |
| 88 | return ', '.join(format.format_map(arg.__dict__) for arg in self.args) |
| 89 | |
| 90 | def gen_block(self, format: str) -> str: |
| 91 | return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) |
| 92 | |
| 93 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 94 | # Match wrappers declared with a co_wrapper mark |
| 95 | func_decl_re = re.compile(r'^int\s*co_wrapper' |
| 96 | r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 97 | r'(?P<wrapper_name>[a-z][a-z0-9_]*)' |
| 98 | r'\((?P<args>[^)]*)\);$', re.MULTILINE) |
| 99 | |
| 100 | |
| 101 | def func_decl_iter(text: str) -> Iterator: |
| 102 | for m in func_decl_re.finditer(text): |
| 103 | yield FuncDecl(return_type='int', |
| 104 | name=m.group('wrapper_name'), |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 105 | args=m.group('args'), |
| 106 | variant=m.group('variant')) |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def snake_to_camel(func_name: str) -> str: |
| 110 | """ |
| 111 | Convert underscore names like 'some_function_name' to camel-case like |
| 112 | 'SomeFunctionName' |
| 113 | """ |
| 114 | words = func_name.split('_') |
| 115 | words = [w[0].upper() + w[1:] for w in words] |
| 116 | return ''.join(words) |
| 117 | |
| 118 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 119 | def create_mixed_wrapper(func: FuncDecl) -> str: |
| 120 | """ |
| 121 | Checks if we are already in coroutine |
| 122 | """ |
| 123 | name = func.co_name |
| 124 | struct_name = func.struct_name |
| 125 | return f"""\ |
| 126 | int {func.name}({ func.gen_list('{decl}') }) |
| 127 | {{ |
| 128 | if (qemu_in_coroutine()) {{ |
| 129 | return {name}({ func.gen_list('{name}') }); |
| 130 | }} else {{ |
| 131 | {struct_name} s = {{ |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame^] | 132 | .poll_state.ctx = {func.ctx}, |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 133 | .poll_state.in_progress = true, |
| 134 | |
| 135 | { func.gen_block(' .{name} = {name},') } |
| 136 | }}; |
| 137 | |
| 138 | s.poll_state.co = qemu_coroutine_create({name}_entry, &s); |
| 139 | |
| 140 | return bdrv_poll_co(&s.poll_state); |
| 141 | }} |
| 142 | }}""" |
| 143 | |
| 144 | |
| 145 | def create_co_wrapper(func: FuncDecl) -> str: |
| 146 | """ |
| 147 | Assumes we are not in coroutine, and creates one |
| 148 | """ |
| 149 | name = func.co_name |
| 150 | struct_name = func.struct_name |
| 151 | return f"""\ |
| 152 | int {func.name}({ func.gen_list('{decl}') }) |
| 153 | {{ |
| 154 | {struct_name} s = {{ |
Emanuele Giuseppe Esposito | 0582fb8 | 2022-11-28 09:23:34 -0500 | [diff] [blame^] | 155 | .poll_state.ctx = {func.ctx}, |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 156 | .poll_state.in_progress = true, |
| 157 | |
| 158 | { func.gen_block(' .{name} = {name},') } |
| 159 | }}; |
| 160 | assert(!qemu_in_coroutine()); |
| 161 | |
| 162 | s.poll_state.co = qemu_coroutine_create({name}_entry, &s); |
| 163 | |
| 164 | return bdrv_poll_co(&s.poll_state); |
| 165 | }}""" |
| 166 | |
| 167 | |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 168 | def gen_wrapper(func: FuncDecl) -> str: |
Vladimir Sementsov-Ogievskiy | bb43694 | 2021-06-10 13:07:57 +0300 | [diff] [blame] | 169 | assert not '_co_' in func.name |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 170 | assert func.return_type == 'int' |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 171 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 172 | name = func.co_name |
| 173 | struct_name = func.struct_name |
Vladimir Sementsov-Ogievskiy | bb43694 | 2021-06-10 13:07:57 +0300 | [diff] [blame] | 174 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 175 | creation_function = create_mixed_wrapper |
| 176 | if func.create_only_co: |
| 177 | creation_function = create_co_wrapper |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 178 | |
| 179 | return f"""\ |
| 180 | /* |
| 181 | * Wrappers for {name} |
| 182 | */ |
| 183 | |
| 184 | typedef struct {struct_name} {{ |
| 185 | BdrvPollCo poll_state; |
| 186 | { func.gen_block(' {decl};') } |
| 187 | }} {struct_name}; |
| 188 | |
| 189 | static void coroutine_fn {name}_entry(void *opaque) |
| 190 | {{ |
| 191 | {struct_name} *s = opaque; |
| 192 | |
| 193 | s->poll_state.ret = {name}({ func.gen_list('s->{name}') }); |
| 194 | s->poll_state.in_progress = false; |
| 195 | |
| 196 | aio_wait_kick(); |
| 197 | }} |
| 198 | |
Emanuele Giuseppe Esposito | 76a2f55 | 2022-11-28 09:23:33 -0500 | [diff] [blame] | 199 | {creation_function(func)}""" |
Vladimir Sementsov-Ogievskiy | aaaa20b | 2020-09-24 21:54:11 +0300 | [diff] [blame] | 200 | |
| 201 | |
| 202 | def gen_wrappers(input_code: str) -> str: |
| 203 | res = '' |
| 204 | for func in func_decl_iter(input_code): |
| 205 | res += '\n\n\n' |
| 206 | res += gen_wrapper(func) |
| 207 | |
| 208 | return res |
| 209 | |
| 210 | |
| 211 | if __name__ == '__main__': |
| 212 | if len(sys.argv) < 3: |
| 213 | exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') |
| 214 | |
| 215 | with open(sys.argv[1], 'w', encoding='utf-8') as f_out: |
| 216 | f_out.write(gen_header()) |
| 217 | for fname in sys.argv[2:]: |
| 218 | with open(fname, encoding='utf-8') as f_in: |
| 219 | f_out.write(gen_wrappers(f_in.read())) |
| 220 | f_out.write('\n') |