1650ab98dSLluís Vilanova# -*- coding: utf-8 -*- 2650ab98dSLluís Vilanova 3650ab98dSLluís Vilanova""" 4650ab98dSLluís VilanovaBackend management. 5650ab98dSLluís Vilanova 6650ab98dSLluís Vilanova 7650ab98dSLluís VilanovaCreating new backends 8650ab98dSLluís Vilanova--------------------- 9650ab98dSLluís Vilanova 10650ab98dSLluís VilanovaA new backend named 'foo-bar' corresponds to Python module 11650ab98dSLluís Vilanova'tracetool/backend/foo_bar.py'. 12650ab98dSLluís Vilanova 13650ab98dSLluís VilanovaA backend module should provide a docstring, whose first non-empty line will be 14650ab98dSLluís Vilanovaconsidered its short description. 15650ab98dSLluís Vilanova 16650ab98dSLluís VilanovaAll backends must generate their contents through the 'tracetool.out' routine. 17650ab98dSLluís Vilanova 18650ab98dSLluís Vilanova 1993fba161SLluís VilanovaBackend attributes 2093fba161SLluís Vilanova------------------ 2193fba161SLluís Vilanova 2293fba161SLluís Vilanova========= ==================================================================== 2393fba161SLluís VilanovaAttribute Description 2493fba161SLluís Vilanova========= ==================================================================== 2593fba161SLluís VilanovaPUBLIC If exists and is set to 'True', the backend is considered "public". 2693fba161SLluís Vilanova========= ==================================================================== 2793fba161SLluís Vilanova 2893fba161SLluís Vilanova 29650ab98dSLluís VilanovaBackend functions 30650ab98dSLluís Vilanova----------------- 31650ab98dSLluís Vilanova 321dad2ce9SLluís VilanovaAll the following functions are optional, and no output will be generated if 331dad2ce9SLluís Vilanovathey do not exist. 341dad2ce9SLluís Vilanova 351dad2ce9SLluís Vilanova=============================== ============================================== 36650ab98dSLluís VilanovaFunction Description 371dad2ce9SLluís Vilanova=============================== ============================================== 381dad2ce9SLluís Vilanovagenerate_<format>_begin(events) Generate backend- and format-specific file 391dad2ce9SLluís Vilanova header contents. 401dad2ce9SLluís Vilanovagenerate_<format>_end(events) Generate backend- and format-specific file 411dad2ce9SLluís Vilanova footer contents. 421dad2ce9SLluís Vilanovagenerate_<format>(event) Generate backend- and format-specific contents 431dad2ce9SLluís Vilanova for the given event. 441dad2ce9SLluís Vilanova=============================== ============================================== 451dad2ce9SLluís Vilanova 46650ab98dSLluís Vilanova""" 47650ab98dSLluís Vilanova 48650ab98dSLluís Vilanova__author__ = "Lluís Vilanova <vilanova@ac.upc.edu>" 491dad2ce9SLluís Vilanova__copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>" 50650ab98dSLluís Vilanova__license__ = "GPL version 2 or (at your option) any later version" 51650ab98dSLluís Vilanova 52650ab98dSLluís Vilanova__maintainer__ = "Stefan Hajnoczi" 53*f892b494SPhilippe Mathieu-Daudé__email__ = "stefanha@redhat.com" 54650ab98dSLluís Vilanova 55650ab98dSLluís Vilanova 569943e0ecSStefan Hajnocziimport os 57650ab98dSLluís Vilanova 58650ab98dSLluís Vilanovaimport tracetool 59650ab98dSLluís Vilanova 60650ab98dSLluís Vilanova 6193fba161SLluís Vilanovadef get_list(only_public = False): 62650ab98dSLluís Vilanova """Get a list of (name, description) pairs.""" 63650ab98dSLluís Vilanova res = [("nop", "Tracing disabled.")] 649943e0ecSStefan Hajnoczi modnames = [] 659943e0ecSStefan Hajnoczi for filename in os.listdir(tracetool.backend.__path__[0]): 669943e0ecSStefan Hajnoczi if filename.endswith('.py') and filename != '__init__.py': 679943e0ecSStefan Hajnoczi modnames.append(filename.rsplit('.', 1)[0]) 68b160d7f8SLluís Vilanova for modname in sorted(modnames): 69650ab98dSLluís Vilanova module = tracetool.try_import("tracetool.backend." + modname) 70650ab98dSLluís Vilanova 71650ab98dSLluís Vilanova # just in case; should never fail unless non-module files are put there 72650ab98dSLluís Vilanova if not module[0]: 73650ab98dSLluís Vilanova continue 74650ab98dSLluís Vilanova module = module[1] 75650ab98dSLluís Vilanova 7693fba161SLluís Vilanova public = getattr(module, "PUBLIC", False) 7793fba161SLluís Vilanova if only_public and not public: 7893fba161SLluís Vilanova continue 7993fba161SLluís Vilanova 80650ab98dSLluís Vilanova doc = module.__doc__ 81650ab98dSLluís Vilanova if doc is None: 82650ab98dSLluís Vilanova doc = "" 83650ab98dSLluís Vilanova doc = doc.strip().split("\n")[0] 84650ab98dSLluís Vilanova 85650ab98dSLluís Vilanova name = modname.replace("_", "-") 86650ab98dSLluís Vilanova res.append((name, doc)) 87650ab98dSLluís Vilanova return res 88650ab98dSLluís Vilanova 89650ab98dSLluís Vilanova 90650ab98dSLluís Vilanovadef exists(name): 91650ab98dSLluís Vilanova """Return whether the given backend exists.""" 92650ab98dSLluís Vilanova if len(name) == 0: 93650ab98dSLluís Vilanova return False 94650ab98dSLluís Vilanova if name == "nop": 95650ab98dSLluís Vilanova return True 96650ab98dSLluís Vilanova name = name.replace("-", "_") 97650ab98dSLluís Vilanova return tracetool.try_import("tracetool.backend." + name)[1] 98650ab98dSLluís Vilanova 99650ab98dSLluís Vilanova 1001dad2ce9SLluís Vilanovaclass Wrapper: 1015b808275SLluís Vilanova def __init__(self, backends, format): 1025b808275SLluís Vilanova self._backends = [backend.replace("-", "_") for backend in backends] 1031dad2ce9SLluís Vilanova self._format = format.replace("-", "_") 10473735f72SStefan Hajnoczi for backend in self._backends: 10573735f72SStefan Hajnoczi assert exists(backend) 1061dad2ce9SLluís Vilanova assert tracetool.format.exists(self._format) 107650ab98dSLluís Vilanova 1081dad2ce9SLluís Vilanova def _run_function(self, name, *args, **kwargs): 1095b808275SLluís Vilanova for backend in self._backends: 1105b808275SLluís Vilanova func = tracetool.try_import("tracetool.backend." + backend, 1111dad2ce9SLluís Vilanova name % self._format, None)[1] 1121dad2ce9SLluís Vilanova if func is not None: 1131dad2ce9SLluís Vilanova func(*args, **kwargs) 114650ab98dSLluís Vilanova 11580dd5c49SDaniel P. Berrange def generate_begin(self, events, group): 11680dd5c49SDaniel P. Berrange self._run_function("generate_%s_begin", events, group) 117650ab98dSLluís Vilanova 11880dd5c49SDaniel P. Berrange def generate(self, event, group): 11980dd5c49SDaniel P. Berrange self._run_function("generate_%s", event, group) 120650ab98dSLluís Vilanova 1213932ef3fSStefan Hajnoczi def generate_backend_dstate(self, event, group): 1223932ef3fSStefan Hajnoczi self._run_function("generate_%s_backend_dstate", event, group) 1233932ef3fSStefan Hajnoczi 12480dd5c49SDaniel P. Berrange def generate_end(self, events, group): 12580dd5c49SDaniel P. Berrange self._run_function("generate_%s_end", events, group) 126