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 | |
Alistair Veitch | f68bf38 | 2015-07-28 14:27:28 -0700 | [diff] [blame^] | 103 | /* Max number of characters in tag key */ |
| 104 | #define CENSUS_MAX_TAG_KEY_LENGTH 20 |
| 105 | /* Max number of tag value characters */ |
| 106 | #define CENSUS_MAX_TAG_VALUE_LENGTH 50 |
| 107 | |
| 108 | /* A Census tag set is a collection of key:value string pairs; these form the |
| 109 | basis against which Census metrics will be recorded. Keys are unique within |
| 110 | a tag set. All contexts have an associated tag set. */ |
| 111 | typedef struct census_tag_set census_tag_set; |
| 112 | |
| 113 | /* Returns a pointer to a newly created, empty tag set. If size_hint > 0, |
| 114 | indicates that the tag set is intended to hold approximately that number |
| 115 | of tags. */ |
| 116 | census_tag_set *census_tag_set_create(size_t size_hint); |
| 117 | |
| 118 | /* Add a new tag key/value to an existing tag set; if the tag key already exists |
| 119 | in the tag set, then its value is overwritten with the new one. Can also be |
| 120 | used to delete a tag, by specifying a NULL value. If key is NULL, returns |
| 121 | the number of tags in the tag set. |
| 122 | Return values: |
| 123 | -1: invalid length key or value |
| 124 | positive values: the number of tags in the tag set. */ |
| 125 | int census_tag_set_add(census_tag_set *tags, const char *key, |
| 126 | const char *value); |
| 127 | |
| 128 | /* Destroys a tag set. This function must be called to prevent memory leaks. |
| 129 | Once called, the tag set cannot be used again. */ |
| 130 | void census_tag_set_destroy(census_tag_set *tags); |
| 131 | |
| 132 | /* Get a contexts tag set. */ |
| 133 | census_tag_set *census_context_tag_set(census_context *context); |
| 134 | |
| 135 | /* A read-only representation of a tag for use by census clients. */ |
| 136 | typedef struct { |
| 137 | size_t key_len; /* Number of bytes in tag key. */ |
| 138 | const char *key; /* A pointer to the tag key. May not be null-terminated. */ |
| 139 | size_t value_len; /* Number of bytes in tag value. */ |
| 140 | const char *value; /* Pointer to the tag value. May not be null-terminated. */ |
| 141 | } census_tag_const; |
| 142 | |
| 143 | /* Used to iterate through a tag sets contents. */ |
| 144 | typedef struct census_tag_set_iterator census_tag_set_iterator; |
| 145 | |
| 146 | /* Open a tag set for iteration. The tag set must not be modified while |
| 147 | iteration is ongoing. Returns an iterator for use in following functions. */ |
| 148 | census_tag_set_iterator *census_tag_set_open(census_tag_set *tags); |
| 149 | |
| 150 | /* Get the next tag in the tag set, by writing into the 'tag' argument. Returns |
| 151 | 1 if there is a "next" tag, 0 if there are no more tags. */ |
| 152 | int census_tag_set_next(census_tag_set_iterator *it, census_tag_const *tag); |
| 153 | |
| 154 | /* Close an iterator opened by census_tag_set_open(). The iterator will be |
| 155 | invalidated, and should not be used once close is called. */ |
| 156 | void census_tag_set_close(census_tag_set_iterator *it); |
| 157 | |
Alistair Veitch | 851032a | 2015-07-20 11:59:13 -0700 | [diff] [blame] | 158 | /* A census statistic to be recorded comprises two parts: an ID for the |
| 159 | * particular statistic and the value to be recorded against it. */ |
| 160 | typedef struct { |
| 161 | int id; |
| 162 | double value; |
| 163 | } census_stat; |
| 164 | |
| 165 | /* Record new stats against the given context. */ |
| 166 | void census_record_stat(census_context *context, census_stat *stats, |
| 167 | size_t nstats); |
Alistair Veitch | 4d1589a | 2015-07-17 15:13:04 -0700 | [diff] [blame] | 168 | |
Nicolas "Pixel" Noble | 1ed15e2 | 2015-06-09 02:24:35 +0200 | [diff] [blame] | 169 | #ifdef __cplusplus |
| 170 | } |
| 171 | #endif |
| 172 | |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 173 | #endif /* CENSUS_CENSUS_H */ |