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