2012-04-03 20:47:39 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Backend management.
|
|
|
|
|
|
|
|
|
|
|
|
Creating new backends
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
A new backend named 'foo-bar' corresponds to Python module
|
|
|
|
'tracetool/backend/foo_bar.py'.
|
|
|
|
|
|
|
|
A backend module should provide a docstring, whose first non-empty line will be
|
|
|
|
considered its short description.
|
|
|
|
|
|
|
|
All backends must generate their contents through the 'tracetool.out' routine.
|
|
|
|
|
|
|
|
|
2013-03-05 14:47:26 +01:00
|
|
|
Backend attributes
|
|
|
|
------------------
|
|
|
|
|
|
|
|
========= ====================================================================
|
|
|
|
Attribute Description
|
|
|
|
========= ====================================================================
|
|
|
|
PUBLIC If exists and is set to 'True', the backend is considered "public".
|
|
|
|
========= ====================================================================
|
|
|
|
|
|
|
|
|
2012-04-03 20:47:39 +02:00
|
|
|
Backend functions
|
|
|
|
-----------------
|
|
|
|
|
2014-02-23 20:37:40 +01:00
|
|
|
All the following functions are optional, and no output will be generated if
|
|
|
|
they do not exist.
|
|
|
|
|
|
|
|
=============================== ==============================================
|
|
|
|
Function Description
|
|
|
|
=============================== ==============================================
|
|
|
|
generate_<format>_begin(events) Generate backend- and format-specific file
|
|
|
|
header contents.
|
|
|
|
generate_<format>_end(events) Generate backend- and format-specific file
|
|
|
|
footer contents.
|
|
|
|
generate_<format>(event) Generate backend- and format-specific contents
|
|
|
|
for the given event.
|
|
|
|
=============================== ==============================================
|
|
|
|
|
2012-04-03 20:47:39 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
|
2014-02-23 20:37:40 +01:00
|
|
|
__copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
|
2012-04-03 20:47:39 +02:00
|
|
|
__license__ = "GPL version 2 or (at your option) any later version"
|
|
|
|
|
|
|
|
__maintainer__ = "Stefan Hajnoczi"
|
|
|
|
__email__ = "stefanha@linux.vnet.ibm.com"
|
|
|
|
|
|
|
|
|
2012-04-30 13:00:23 +02:00
|
|
|
import os
|
2012-04-03 20:47:39 +02:00
|
|
|
|
|
|
|
import tracetool
|
|
|
|
|
|
|
|
|
2013-03-05 14:47:26 +01:00
|
|
|
def get_list(only_public = False):
|
2012-04-03 20:47:39 +02:00
|
|
|
"""Get a list of (name, description) pairs."""
|
|
|
|
res = [("nop", "Tracing disabled.")]
|
2012-04-30 13:00:23 +02:00
|
|
|
modnames = []
|
|
|
|
for filename in os.listdir(tracetool.backend.__path__[0]):
|
|
|
|
if filename.endswith('.py') and filename != '__init__.py':
|
|
|
|
modnames.append(filename.rsplit('.', 1)[0])
|
2014-02-23 20:37:24 +01:00
|
|
|
for modname in sorted(modnames):
|
2012-04-03 20:47:39 +02:00
|
|
|
module = tracetool.try_import("tracetool.backend." + modname)
|
|
|
|
|
|
|
|
# just in case; should never fail unless non-module files are put there
|
|
|
|
if not module[0]:
|
|
|
|
continue
|
|
|
|
module = module[1]
|
|
|
|
|
2013-03-05 14:47:26 +01:00
|
|
|
public = getattr(module, "PUBLIC", False)
|
|
|
|
if only_public and not public:
|
|
|
|
continue
|
|
|
|
|
2012-04-03 20:47:39 +02:00
|
|
|
doc = module.__doc__
|
|
|
|
if doc is None:
|
|
|
|
doc = ""
|
|
|
|
doc = doc.strip().split("\n")[0]
|
|
|
|
|
|
|
|
name = modname.replace("_", "-")
|
|
|
|
res.append((name, doc))
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
def exists(name):
|
|
|
|
"""Return whether the given backend exists."""
|
|
|
|
if len(name) == 0:
|
|
|
|
return False
|
|
|
|
if name == "nop":
|
|
|
|
return True
|
|
|
|
name = name.replace("-", "_")
|
|
|
|
return tracetool.try_import("tracetool.backend." + name)[1]
|
|
|
|
|
|
|
|
|
2014-02-23 20:37:40 +01:00
|
|
|
class Wrapper:
|
2014-05-27 15:02:14 +02:00
|
|
|
def __init__(self, backends, format):
|
|
|
|
self._backends = [backend.replace("-", "_") for backend in backends]
|
2014-02-23 20:37:40 +01:00
|
|
|
self._format = format.replace("-", "_")
|
2014-08-27 13:08:53 +02:00
|
|
|
for backend in self._backends:
|
|
|
|
assert exists(backend)
|
2014-02-23 20:37:40 +01:00
|
|
|
assert tracetool.format.exists(self._format)
|
2012-04-03 20:47:39 +02:00
|
|
|
|
2014-02-23 20:37:40 +01:00
|
|
|
def _run_function(self, name, *args, **kwargs):
|
2014-05-27 15:02:14 +02:00
|
|
|
for backend in self._backends:
|
|
|
|
func = tracetool.try_import("tracetool.backend." + backend,
|
|
|
|
name % self._format, None)[1]
|
|
|
|
if func is not None:
|
|
|
|
func(*args, **kwargs)
|
2012-04-03 20:47:39 +02:00
|
|
|
|
2014-02-23 20:37:40 +01:00
|
|
|
def generate_begin(self, events):
|
|
|
|
self._run_function("generate_%s_begin", events)
|
2012-04-03 20:47:39 +02:00
|
|
|
|
2014-02-23 20:37:40 +01:00
|
|
|
def generate(self, event):
|
|
|
|
self._run_function("generate_%s", event)
|
2012-04-03 20:47:39 +02:00
|
|
|
|
2014-02-23 20:37:40 +01:00
|
|
|
def generate_end(self, events):
|
|
|
|
self._run_function("generate_%s_end", events)
|