aboutsummaryrefslogtreecommitdiff
path: root/bindings/python/ppamltracer.py
blob: dde85a302827d3402232545cbc499c641d10e161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# ppamltracer -- Python bindings to ppamltracer
# Copyright (C) 2013  Galois, Inc.
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this library.  If not, see <http://www.gnu.org/licenses/>.
#
# To contact Galois, complete the Web form at <http://corp.galois.com/contact/>
# or write to Galois, Inc., 421 Southwest 6th Avenue, Suite 300, Portland,
# Oregon, 97204-1622.

"""A tracing library for explicit instrumentation of generated code.

ppamltracer is a lightweight, portable tracing library designed for
explicit instrumention of generated code.  If you're writing a compiler
and need hard data on your optimizer's efficacy, ppamltracer is the
library for you.  ppamltracer-python provides a high-level Python API on
top of the libppamltracer C API.

ppamltracer-python's usage can be summed up in a couple lines:

    from ppamltracer import Tracer
    with Tracer("/tmp/my_report") as tracer:
        with tracer.create_phase("phase 1") as phase:
            with phase.running():
                do_stuff()
        with tracer.create_phase("phase 2") as phase:
            with phase.running():
                do_other_stuff()
            with phase.running():
                do_yet_more_stuff()

This creates a report with the total runtime of do_stuff recorded as
"phase 1", and the total runtime of do_other_stuff and do_yet_more_stuff
combined as "phase 2".

The ppamltracer-python distribution also contains a more lengthy example
in the "examples" directory.

ppamltracer writes trace logs in the Open Trace Format [1], a free and
open standard developed by the Zentrum fuer Informationsdienste und
Hochleistungsrechnen (Center for Information Services and
High-Performance Computing) at the Technical University of Dresden.

We developed ppamltracer and ppamltracer-python as part of DARPA's
Probabilistic Programming for Advancing Machine Learning (PPAML)
project [2].

References:
    [1] http://tu-dresden.de/zih/otf/
    [2] http://darpa.mil/Our_Work/I2O/Programs/Probabilistic_Programming_for_Advanced_Machine_Learning_(PPAML).aspx
"""
# TODO: Replace "ue" in "fuer" with Unicode u-with-diaresis (blocking on
# deployment of fix to Python #1065986, "Fix pydoc crashing on unicode
# strings").

import ctypes
import ctypes.util
import warnings


################################ Low-level API ################################

_lib = ctypes.cdll.LoadLibrary(ctypes.util.find_library("ppamltracer"))
"""ctypes binding to the ppamltracer C library."""

# The code in this section binds the C API using ctypes, translating
# return codes into the exception hierarchy defined below.  Since these
# functions are not intended to be used externally, they're not
# documented using pydoc; see the ppamltracer C API documentation for
# information on their behavior.

_sizeof_ppaml_tracer_t = \
    ctypes.c_size_t.in_dll(_lib, "ppaml_tracer_t_size").value

_sizeof_ppaml_phase_t = ctypes.c_size_t.in_dll(_lib, "ppaml_phase_t_size").value

def _ppaml_tracer_init(tracer, report_name_base):
    result = _lib.ppaml_tracer_init(tracer, report_name_base)
    if result == 0:
        return None
    elif result == 1:
        raise OTFManagerInitializationError()
    elif result == 2:
        raise OTFWriterInitializationError()
    elif result == 3:
        raise OTFWriterResolutionError()
    elif result == 4:
        raise OTFWriterProcessDefinitionError("main")
    else:
        _warn_unexpected_return_code()
        raise TracerError()

def _ppaml_tracer_done(tracer):
    result = _lib.ppaml_tracer_done(tracer)
    if result == 0:
        return None
    elif result == 1:
        raise OTFWriterCloseError()
    else:
        _warn_unexpected_return_code()
        raise TracerError()

def _ppaml_phase_init(tracer, phase, name):
    result = _lib.ppaml_phase_init(tracer, phase, name)
    if result == 0:
        return None
    elif result == 1:
        raise OTFWriterPhaseDefinitionError()
    else:
        _warn_unexpected_return_code()
        raise TracerError()

def _ppaml_phase_start(phase):
    result = _lib.ppaml_phase_start(phase)
    if result == 0:
        return None
    elif result == 1:
        raise ClockAcquisitionError()
    elif result == 2:
        raise OTFWriterEntryError()
    else:
        _warn_unexpected_return_code()
        raise TracerError()

def _ppaml_phase_stop(phase):
    result = _lib.ppaml_phase_stop(phase)
    if result == 0:
        return None
    elif result == 1:
        raise ClockAcquisitionError()
    elif result == 2:
        raise OTFWriterExitError()
    else:
        _warn_unexpected_return_code()
        raise TracerError()

def _ppaml_phase_done(phase):
    result = _lib.ppaml_phase_done(phase)
    if result == 0:
        return None
    else:
        _warn_unexpected_return_code()
        raise TracerError()


############################# Resource-based API ##############################

class Tracer(object):
    """A tracer for programs, which records execution timing information.

    This class is designed to be used with Python's "with" statement-- e.g.,

        with Tracer("/tmp/my_report") as tracer:
            main(tracer)

    """

    def __init__(self, report_name_base):
        """Create a new tracer.

        The tracer will create an Open Trace Format report during program
        execution.  The multiple files of the report will all start with the
        specified base name.

        """
        self._report_name_base = report_name_base
        self._underlying = None

    def __enter__(self):
        if self._underlying is None:
            self._underlying = \
                ctypes.create_string_buffer(_sizeof_ppaml_tracer_t)
            _ppaml_tracer_init(self._underlying, self._report_name_base)
        return self

    def __exit__(self, *exception_info):
        if self._underlying is not None:
            _ppaml_tracer_done(self._underlying)
            self._underlying = None

    def create_phase(self, name):
        """Construct a Phase associated with this Tracer.

        This function is merely a convenience function; internally, it shells
        out to the Phase constructor.

        """
        return Phase(self, name)


class Phase(object):
    """A phase of computation traced by ppamltracer.

    This class is designed to be used with Python's "with" statement-- e.g.,

        with Phase(tracer, "my phase") as phase:
            with phase.running():
                do_stuff()
            with phase.running():
                do_stuff_again()

    Note the double use of "with".  The outer "with" manages the lifetime of
    a Phase; the inner "with" actually starts and stops the phase timer.

    """

    def __init__(self, tracer, name):
        """Define a new phase tracked by a given tracer."""
        self._tracer = tracer
        self._name = name
        self._underlying = None

    def __enter__(self):
        if self._underlying is None:
            self._underlying = \
                ctypes.create_string_buffer(_sizeof_ppaml_phase_t)
            _ppaml_phase_init(
                self._tracer._underlying,
                self._underlying,
                self._name)
        return self

    def __exit__(self, *exception_info):
        if self._underlying is not None:
            _ppaml_phase_done(self._underlying)
            self._underlying = None

    def _start(self):
        _ppaml_phase_start(self._underlying)

    def _stop(self):
        _ppaml_phase_stop(self._underlying)

    def running(self):
        """ Create a resource manager for the phase timer.

        This manager handles starting and stopping the phase, allowing you to
        time operations by saying, e.g.,

            with phase.running():
                do_stuff()

        """
        return _PhaseTimerManager(self)


class _PhaseTimerManager(object):
    """Manage entry and exit from a Phase using the "with" statement."""

    def __init__(self, phase):
        self._phase = phase

    def __enter__(self):
        self._phase._start()

    def __exit__(self, *exception_info):
        self._phase._stop()


############################# Exception hierarchy #############################

class TracerError(Exception):
    """A generic ppamltracer error."""
    pass

class OTFError(TracerError):
    """An error related to Open Trace Format input and output."""
    pass

class OTFManagerError(OTFError):
    """An error caused by the Open Trace Format manager."""
    pass

class OTFManagerInitializationError(OTFManagerError):
    """Failure to initialize the Open Trace Format manager."""

    def __init__(self):
        super.__init__(
            self,
            "could not initialize Open Trace Format file manager")

class OTFWriterError(OTFError):
    """An error caused by the Open Trace Format writer."""
    pass

class OTFWriterInitializationError(OTFWriterError):
    """Failure to initialize the Open Trace Format writer."""

    def __init__(self):
        super.__init__(self, "could not open Open Trace Format writer")

class OTFWriterPhaseDefinitionError(OTFWriterError):
    """Failure to define a phase. """

    def __init__(self):
        super.__init__(self, "could not define phase")

class OTFWriterEntryError(OTFWriterError):
    """Failure to record entry into a phase."""

    def __init__(self):
        super.__init__(self, "could not record phase start")

class OTFWriterExitError(OTFWriterError):
    """Failure to record exit from a phase."""

    def __init__(self):
        super.__init__(self, "could not record phase end")

class OTFWriterCloseError(OTFWriterError):
    """Failure to close the Open Trace Format writer."""

    def __init__(self):
        super.__init__(self, "could not close Open Trace Format writer")

class OTFWriterResolutionError(OTFWriterError):
    """Failure to set the tracer resolution."""

    def __init__(self):
        super.__init__(self, "could not set trace resolution")

class OTFWriterProcessDefinitionError(OTFWriterError):
    """Failure to define an Open Trace Format process."""

    def __init__(self, process_name=None):
        if process_name is None:
            super.__init__(self, "could not define Open Trace Format process")
        else:
            super.__init__(self,
                           ("could not define Open Trace Format process \""
                            + process_name
                            + "\""))

class TimingError(TracerError):
    """An error related to system timers."""
    pass

class ClockAcquisitionError(TimingError):
    """A failure to get the current clock time."""

    def __init__(self):
        super.__init__(self, "could not get current time")


############################### Warnings ###############################

class UnwrappedCErrorWarning(Warning):
    """A warning indicating a failure to correctly wrap a C API."""
    pass

def _warn_unexpected_return_code():
    warnings.warn(("Unexpected C return code\n"
                   + "*** This is a bug in ppamltracer-python!  Report it to the maintainers."),
                  UnwrappedCErrorWarning,
                  2)