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
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
(* This module provides a simple way to audit calls to functions that are deemed
expensive: have such a call explicitly pass a "token" that describes the
result of a manual audit of the call.
Tokens are of type `audit`. This type is abstract outside this module, and
the only ways of "creating" values of that type are by using the constants
below. To further keep the overhead of this mechanism down, we implement
these tokens as unit values; in principle we could have used an ADT instead.
Currently, this mechanism is to track accesses to shared memory, which
involve expensive (both in time and in space!) serialization and
deserialization of data. Since the API for these accesses doesn't look very
different from calls to "normal" functions, we can easily abuse the API. The
solution is to `wrap` the API so that the type system then *demands* passing
tokens describing the results of manual audits of calls to the API.
*)
(* Use this token to describe expensive calls that are deemed OK.
For example, shared memory accesses that happen in (short-lived, parallel)
worker processes are usually OK, since they happen, well, in parallel (so
time cost is OK) and their memory is reclaimed after they die (so memory cost
is OK).
*)
(
(* Use this token to describe expensive calls that either need immediate fixing
or careful monitoring of costs.
For example, shared memory accesses that happen in the master deserve
warning. Especially when these calls are multiplied over a large number of
files, they cause both a time bottleneck in the master *and* build up memory
pressure that might eventually cultimate in memory leaks or long / frequent
GC pauses.
*)
(
(* Given a function `f`, `wrap f` demands an extra argument, ~audit preceding
other arguments, but otherwise behaves the same way. *)
f