blob: 3fc07affc8460cb84675fdbc4a76fbba9e29f904 [file] [log] [blame]
Alistair Veitch9686dab2015-05-26 14:26:47 -07001/*
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 Veitch9686dab2015-05-26 14:26:47 -070042
Nicolas "Pixel" Noble1ed15e22015-06-09 02:24:35 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Alistair Veitch9686dab2015-05-26 14:26:47 -070047/* Identify census functionality that can be enabled via census_initialize(). */
48enum 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 Veitch26967622015-06-01 10:45:54 -070058 * 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 Veitch9686dab2015-05-26 14:26:47 -070061int census_initialize(int functions);
62void census_shutdown();
63
Alistair Veitchfc62ddd2015-06-29 02:52:46 -070064/* If any census feature has been initialized, this funtion will return a
65 * non-zero value. */
66int census_available();
67
Alistair Veitch9686dab2015-05-26 14:26:47 -070068/* 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 Veitch9d48ebf2015-06-01 10:01:03 -070071 * 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 Veitch9686dab2015-05-26 14:26:47 -070074typedef 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. */
87size_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 Veitch980ef762015-06-01 14:35:31 -070093 * created. The decoded/new contest is returned in 'context'.
Alistair Veitch9686dab2015-05-26 14:26:47 -070094 *
Alistair Veitch980ef762015-06-01 14:35:31 -070095 * Returns 0 if no errors, non-zero if buffer is incorrectly formatted, in
96 * which case a new empty context will be returned. */
97int census_context_deserialize(const char *buffer, census_context **context);
Alistair Veitch9686dab2015-05-26 14:26:47 -070098
99/* The given context is destroyed. Once destroyed, using the context in
100 * future census calls will result in undefined behavior. */
101void census_context_destroy(census_context *context);
102
Nicolas "Pixel" Noble1ed15e22015-06-09 02:24:35 +0200103#ifdef __cplusplus
104}
105#endif
106
Alistair Veitch9686dab2015-05-26 14:26:47 -0700107#endif /* CENSUS_CENSUS_H */