Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | /* RPC-internal Census API's. These are designed to be generic enough that |
| 35 | * they can (ultimately) be used in many different RPC systems (with differing |
| 36 | * implementations). */ |
| 37 | |
| 38 | #ifndef CENSUS_CENSUS_H |
| 39 | #define CENSUS_CENSUS_H |
| 40 | |
| 41 | #include <grpc/grpc.h> |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 42 | |
Nicolas "Pixel" Noble | 1ed15e2 | 2015-06-09 02:24:35 +0200 | [diff] [blame] | 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 47 | /* Identify census functionality that can be enabled via census_initialize(). */ |
| 48 | enum census_functions { |
| 49 | CENSUS_NONE = 0, /* Do not enable census. */ |
| 50 | CENSUS_TRACING = 1, /* Enable census tracing. */ |
| 51 | CENSUS_STATS = 2, /* Enable Census stats collection. */ |
| 52 | CENSUS_CPU = 4, /* Enable Census CPU usage collection. */ |
| 53 | CENSUS_ALL = CENSUS_TRACING | CENSUS_STATS | CENSUS_CPU |
| 54 | }; |
| 55 | |
| 56 | /* Shutdown and startup census subsystem. The 'functions' argument should be |
| 57 | * the OR (|) of census_functions values. If census fails to initialize, then |
Alistair Veitch | 2696762 | 2015-06-01 10:45:54 -0700 | [diff] [blame] | 58 | * census_initialize() will return a non-zero value. It is an error to call |
| 59 | * census_initialize() more than once (without an intervening |
| 60 | * census_shutdown()). */ |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 61 | int census_initialize(int functions); |
| 62 | void census_shutdown(); |
| 63 | |
Alistair Veitch | fc62ddd | 2015-06-29 02:52:46 -0700 | [diff] [blame] | 64 | /* If any census feature has been initialized, this funtion will return a |
| 65 | * non-zero value. */ |
| 66 | int census_available(); |
| 67 | |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 68 | /* Internally, Census relies on a context, which should be propagated across |
| 69 | * RPC's. From the RPC subsystems viewpoint, this is an opaque data structure. |
| 70 | * A context must be used as the first argument to all other census |
Alistair Veitch | 9d48ebf | 2015-06-01 10:01:03 -0700 | [diff] [blame] | 71 | * functions. Conceptually, contexts should be thought of as specific to |
| 72 | * single RPC/thread. The context can be serialized for passing across the |
| 73 | * wire. */ |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 74 | typedef struct census_context census_context; |
| 75 | |
| 76 | /* This function is called by the RPC subsystem whenever it needs to get a |
| 77 | * serialized form of the current census context (presumably to pass across |
| 78 | * the wire). Arguments: |
| 79 | * 'buffer': pointer to memory into which serialized context will be placed |
| 80 | * 'buf_size': size of 'buffer' |
| 81 | * |
| 82 | * Returns: the number of bytes used in buffer if successful, or 0 if the |
| 83 | * buffer is of insufficient size. |
| 84 | * |
| 85 | * TODO(aveitch): determine how best to communicate required/max buffer size |
| 86 | * so caller doesn't have to guess. */ |
| 87 | size_t census_context_serialize(const census_context *context, char *buffer, |
| 88 | size_t buf_size); |
| 89 | |
| 90 | /* Create a new census context, possibly from a serialized buffer. If 'buffer' |
| 91 | * is non-NULL, it is assumed that it is a buffer encoded by |
| 92 | * census_context_serialize(). If `buffer` is NULL, a new, empty context is |
Alistair Veitch | 980ef76 | 2015-06-01 14:35:31 -0700 | [diff] [blame] | 93 | * created. The decoded/new contest is returned in 'context'. |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 94 | * |
Alistair Veitch | 980ef76 | 2015-06-01 14:35:31 -0700 | [diff] [blame] | 95 | * Returns 0 if no errors, non-zero if buffer is incorrectly formatted, in |
| 96 | * which case a new empty context will be returned. */ |
| 97 | int census_context_deserialize(const char *buffer, census_context **context); |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 98 | |
| 99 | /* The given context is destroyed. Once destroyed, using the context in |
| 100 | * future census calls will result in undefined behavior. */ |
| 101 | void census_context_destroy(census_context *context); |
| 102 | |
Nicolas "Pixel" Noble | 1ed15e2 | 2015-06-09 02:24:35 +0200 | [diff] [blame] | 103 | #ifdef __cplusplus |
| 104 | } |
| 105 | #endif |
| 106 | |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 107 | #endif /* CENSUS_CENSUS_H */ |