aboutsummaryrefslogtreecommitdiff
path: root/bindings/racket/main.rkt
blob: e4db14302d54d407f30d8ed95dadc6797364de62 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#lang racket/base
#| ppamltracer -- Racket bindings to ppamltracer
Copyright (C) 2013, 2014  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. |#

(require (for-syntax racket/base
                     racket/syntax
                     (only-in racket/list drop-right)
                     (only-in racket/string string-join)
                     (only-in srfi/13 string-tokenize)
                     (only-in srfi/14 char-set char-set-complement)
                     srfi/26; hole notation
                     ))

(require (rename-in ffi/unsafe [-> ~>])
         ffi/unsafe/define)
(require racket/contract)

#| A shorthand for 'let-syntax-rule' with only one macro.  For example,

    (let-syntax-rule (take-fst x y) x
      (take-fst 'foo 'bar))

expands to

    'foo
|#
(define-syntax-rule (let-syntax-rule (name . args) template body)
  (let-syntax ([name (syntax-rules () [(_ . args) template])])
    body))

;; Convenience macro to instantiate an exception at the current location.
(define-syntax-rule (fail exception args ...)
  (apply exception (list args ... (current-continuation-marks))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Low-level interface ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Load the library.
(define-ffi-definer define-ppamltracer (ffi-lib "libppamltracer"))

#| We're going to model ppaml_tracer_t and ppaml_phase_t as abstract types by
having Racket treat them as buffers.  These two symbols define how long those
buffers need to be.  Ideally, they should be of type 'size_t', but Racket
doesn't have a size_t builtin, so we just use the largest C unsigned integer
type. |#
(define-ppamltracer ppaml_tracer_t_size _uint64)
(define-ppamltracer ppaml_phase_t_size _uint64)

#| Define the struct types we're working with as buffer types.  Instead of
mucking around with initializers and finalizers, which we can't seem to get
working correctly, we simply export an API which enforces proper resource
cleanup. |#
(define (make-buffer size) (make-ctype (_bytes o size) #f #f))
(define _ppaml_tracer_t (make-buffer ppaml_tracer_t_size))
(define _ppaml_phase_t (make-buffer ppaml_phase_t_size))

#| To help avoid passing structs of the wrong type in API internals, we'll
manipulate the structs as tagged pointers. |#
(define _tracer (_cpointer 'ppaml_tracer_t))
(define _phase (_cpointer 'ppaml_phase_t))

;; Allocation functions for _tracer and _phase.
(define (malloc-tagged-pointer size-or-type tag)
  (let ([result (malloc size-or-type 'atomic)])
    (cpointer-push-tag! result tag)
    result))
(define (malloc-_tracer)
  (malloc-tagged-pointer _ppaml_tracer_t 'ppaml_tracer_t))
(define (malloc-_phase)
  (malloc-tagged-pointer _ppaml_phase_t 'ppaml_phase_t))

;; Import library functions.
(define-ppamltracer ppaml_tracer_init (_fun _tracer _path ~> _int))
(define-ppamltracer ppaml_tracer_done (_fun _tracer ~> _int))
(define-ppamltracer ppaml_phase_init (_fun _tracer _phase _string ~> _int))
(define-ppamltracer ppaml_phase_done (_fun _phase ~> _int))
(define-ppamltracer ppaml_phase_start (_fun _phase ~> _int))
(define-ppamltracer ppaml_phase_stop (_fun _phase ~> _int))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Error handling ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#| Convenience macro to create an exception.  For example,

    (define-exception exn:fail:whatever)

expands to

    (struct exn:fail:whatever exn:fail ()
            #:extra-constructor-name make-exn:fail:whatever
            #:transparent)

You can also call 'define-exception' with a message.  Then, instead of creating
an extra binary make-prefixed constructor, 'define-exception' will create an
extra /unary/ make-prefixed constructor.  All instances of the exception will
then have the same message.  For example,

    (define-exception exn:fail:whatever "whatever happened!")

expands to

    (struct exn:fail:whatever exn:fail ()
            #:transparent)
    (define (make-exn:fail:whatever continuation-mark-set)
      (exn:fail:whatever "whatever happened!" continuation-mark-set))
|#
(define-syntax (define-exception stx)
  (let ([#| Lifts a function which operates on data to operate on syntax
         instead. |#
         map-as-datum
         (lambda (f stx) (datum->syntax stx (f (syntax->datum stx))))]
        [#| Given an exception name (as a symbol), produces the conventional
         super-exception.  For instance,

             (take-base-exn 'exn:fail:whatever)

         expands to

             'exn:fail
         |#
         take-base-exn
         (compose1 string->symbol
                   (cut string-join <> ":")
                   (cut drop-right <> 1)
                   (cut string-tokenize <> (char-set-complement
                                            (char-set #\:)))
                   symbol->string)])
    (syntax-case stx ()
      [(_ name)
       (with-syntax ([base-name (map-as-datum take-base-exn #'name)]
                     [make-name (format-id stx "make-~a" #'name
                                           #:source #'name)])
         #'(struct name base-name ()
                   #:extra-constructor-name make-name
                   #:transparent))]
      [(_ name message)
       (with-syntax ([base-name (map-as-datum take-base-exn #'name)]
                     [make-name (format-id stx "make-~a" #'name
                                           #:source #'name)])
         #'(begin
             (struct name base-name () #:transparent)
             (define (make-name continuation-mark-set)
               (name message continuation-mark-set))))])))

#| Like define-exception, but does not create an extra make-prefixed
constructor at all. |#
(define-syntax (define-exception* stx)
  (let ([map-as-datum
         (lambda (f stx) (datum->syntax stx (f (syntax->datum stx))))]
        [take-base-exn
         (compose1 string->symbol
                   (cut string-join <> ":")
                   (cut drop-right <> 1)
                   (cut string-tokenize <> (char-set-complement
                                            (char-set #\:)))
                   symbol->string)])
    (syntax-case stx ()
      [(_ name)
       (with-syntax ([base-name (map-as-datum take-base-exn #'name)])
         #'(struct name base-name () #:transparent))])))

;;;;; Exception hierarchy ;;;;;

(define-exception exn:fail:ppamltracer)

(define-exception exn:fail:ppamltracer:otf)

(define-exception exn:fail:ppamltracer:otf:manager)

(define-exception exn:fail:ppamltracer:otf:manager:initialization
  "could not initialize Open Trace Format file manager")

(define-exception exn:fail:ppamltracer:otf:writer)

(define-exception exn:fail:ppamltracer:otf:writer:initialization
  "could not open Open Trace Format writer")

(define-exception exn:fail:ppamltracer:otf:writer:phase-definition
  "could not define phase")

(define-exception exn:fail:ppamltracer:otf:writer:entry
  "could not record phase start")

(define-exception exn:fail:ppamltracer:otf:writer:exit
  "could not record phase end")

(define-exception exn:fail:ppamltracer:otf:writer:close
  "could not close Open Trace Format writer")

(define-exception exn:fail:ppamltracer:otf:writer:resolution
  "could not set trace resolution")

(define-exception* exn:fail:ppamltracer:otf:writer:process-definition)
(define (make-exn:fail:ppamltracer:otf:writer:process-definition
         process-name
         continuation-mark-set)
  (exn:fail:ppamltracer:otf:writer:process-definition
   (format "could not define Open Trace Format process ~v" process-name)
   continuation-mark-set))

(define-exception exn:fail:ppamltracer:timing)

(define-exception exn:fail:ppamltracer:timing:clock-acquisition
  "could not get current time")


;;;;; Error-checked versions of library functions ;;;;;

#| This macro makes checking C return codes trivial.  Invocation is analogous
to that for the 'cond' form:

    (define some-function
      (checked some-c-function
               [0 'ok]
               [1 (exn make-exn:fail:ppamltracer "bad data format")]
               [-1 'ok]))

This defines 'some-function' as a function taking an arbitrary number of
arguments.  'some-function', when invoked, will pass all its arguments to
'some-c-function'.  If 'some-c-function' returns 0 or -1, then some-function
will evaluate successfully to whatever 'some-c-function' returned; if
'some-c-function' returns 1, though, 'some-function' will raise an
'exn:fail:ppamltracer' exception with the specified error message.

If 'some-c-function' returns an unexpected value, 'some-function' will raise an
'exn:fail' noting the actual return value and declaring it a bug. |#
(define-syntax-rule (checked proc [expected consequence] ...)
  (let-syntax-rule (cond-rhs result consequence-or-exception)
      (if (eq? consequence-or-exception 'ok)
          result
          (raise consequence-or-exception))
    (lambda args
      (let ([result (apply proc args)])
        (case result
            [(expected) (cond-rhs result consequence)]
            ...
            [else (error (quote proc)
                         "unexpected return value ~v\n\
This is a bug in ppamltracer!  Report it to the maintainers."
                         result)])))))

(define ((as-void proc) . args)
  (void (apply proc args)))

(define ppaml-tracer-init
  (as-void
   (checked ppaml_tracer_init
            [0 'ok]
            [1 (fail make-exn:fail:ppamltracer:otf:manager:initialization)]
            [2 (fail make-exn:fail:ppamltracer:otf:writer:initialization)]
            [3 (fail make-exn:fail:ppamltracer:otf:writer:resolution)]
            [4 (fail make-exn:fail:ppamltracer:otf:writer:process-definition
                     "main")])))

(define ppaml-tracer-done
  (as-void
   (checked ppaml_tracer_done
            [0 'ok]
            [1 (fail make-exn:fail:ppamltracer:otf:writer:close)])))

(define ppaml-phase-init

  (as-void
   (checked ppaml_phase_init
            [0 'ok]
            [1 (fail make-exn:fail:ppamltracer:otf:writer:phase-definition)])))

(define ppaml-phase-start
  (as-void
   (checked ppaml_phase_start
            [0 'ok]
            [1 (fail make-exn:fail:ppamltracer:timing:clock-acquisition)]
            [2 (fail make-exn:fail:ppamltracer:otf:writer:entry)])))

(define ppaml-phase-stop
  (as-void
   (checked ppaml_phase_stop
            [0 'ok]
            [1 (fail make-exn:fail:ppamltracer:timing:clock-acquisition)]
            [2 (fail make-exn:fail:ppamltracer:otf:writer:exit)])))

(define ppaml-phase-done
  (as-void
   (checked ppaml_phase_done
            [0 'ok])))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; High-level API ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#| The functions in this section comprise a relatively safe API for this
library--i.e., using them will help avoid resource leaks.  Of course, Racket
gives you plenty of rope to hang yourself--you could, for instance, call/cc
your way back into the middle of some traced function. |#

;; Predicates on '_tracer' and '_phase', to emulate declaration as structs.
(define (cpointer-with-tag? obj tag)
  (and (cpointer? obj)
       (cpointer-has-tag? obj tag)))
(define (tracer? obj) (cpointer-with-tag? obj 'ppaml_tracer_t))
(define (phase? obj) (cpointer-with-tag? obj 'ppaml_phase_t))

#| Execute a procedure in an exception-safe environment such that its resources
are properly released upon exit.  With the exception handling stripped away,

    (managed-execution resource initialize initialize-args proc finalize)

evaluates to

    (begin (apply initialize (cons resource initialize-args))
           (proc resource)
           (finalize resource))
|#
(define (managed-execution resource initialize initialize-args proc finalize)
  (apply initialize (cons resource initialize-args))
  (let ([result
         (with-handlers
             ([exn? (lambda (exn)
                      #| An exception occurred during the execution of 'proc'.
                      Clean up and re-throw. |#
                      (with-handlers
                          ([exn? (lambda (exn)
                                   #| An exception occurred during cleanup!
                                   We're already responding to an existing
                                   exception, though, so just ignore it. |#
                                   (void))])
                        (finalize resource))
                      (raise exn))])
           (proc resource))])
    (finalize resource)
    result))

(define (call-with-tracer report-name-base proc)
  (managed-execution (malloc-_tracer)
                     ppaml-tracer-init
                     (list report-name-base)
                     proc
                     ppaml-tracer-done))

(define (call-with-phase tracer phase-name proc)
  (managed-execution (malloc-_phase)
                     (lambda (phase tracer name)
                       ;; Reorder arguments to fit C API.
                       (ppaml-phase-init tracer phase name))
                     (list tracer phase-name)
                     proc
                     ppaml-phase-done))

(define (call-with-phase-running phase proc)
  (managed-execution phase
                     ppaml-phase-start
                     '()
                     (lambda (ignored) (proc))
                     ppaml-phase-stop))


;;;;; Macro versions ;;;;;

(define-syntax-rule (let/tracer [tracer report-name-base] body ...)
  (call-with-tracer report-name-base (lambda (tracer) body ...)))

(define-syntax-rule (let/phase tracer [phase phase-name] body ...)
  (call-with-phase tracer phase-name (lambda (phase) body ...)))

(define-syntax-rule (with-phase-running phase body ...)
  (call-with-phase-running phase (lambda () body ...)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Module interface ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(provide
 (contract-out [tracer? (-> any/c boolean?)]
               [phase? (-> any/c boolean?)]
               [call-with-tracer (parametric->/c [A]
                                   (-> path-string? (-> tracer? A) A))]
               [call-with-phase (parametric->/c [A]
                                  (-> tracer? string? (-> phase? A) A))]
               [call-with-phase-running (parametric->/c [A]
                                          (-> phase? (-> A) A))])
 let/tracer
 let/phase
 with-phase-running)