7e6b969e234a99949c5529740d825474c59757df
[oweals/gnunet.git] / src / include / gnunet_statistics_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * API to create, modify and access statistics.
26  *
27  * @defgroup statistics  Statistics service
28  * Track statistics or provide access to statistics.
29  *
30  * Create, modify and access statistics about the operation of GNUnet.
31  *
32  * All statistical values must be of type `unsigned long long`.
33  *
34  * @{
35  */
36
37 #ifndef GNUNET_STATISTICS_SERVICE_H
38 #define GNUNET_STATISTICS_SERVICE_H
39
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #if 0                           /* keep Emacsens' auto-indent happy */
44 }
45 #endif
46 #endif
47
48 #include "gnunet_util_lib.h"
49
50 /**
51  * Version of the statistics API.
52  */
53 #define GNUNET_STATISTICS_VERSION 0x00000000
54
55 /**
56  * Opaque handle for the statistics service.
57  */
58 struct GNUNET_STATISTICS_Handle;
59
60 /**
61  * Callback function to process statistic values.
62  *
63  * @param cls closure
64  * @param subsystem name of subsystem that created the statistic
65  * @param name the name of the datum
66  * @param value the current value
67  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
68  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
69  */
70 typedef int
71 (*GNUNET_STATISTICS_Iterator) (void *cls,
72                                const char *subsystem,
73                                const char *name,
74                                uint64_t value,
75                                int is_persistent);
76
77
78 /**
79  * Get handle for the statistics service.
80  *
81  * @param subsystem name of subsystem using the service
82  * @param cfg services configuration in use
83  * @return handle to use
84  */
85 struct GNUNET_STATISTICS_Handle *
86 GNUNET_STATISTICS_create (const char *subsystem,
87                           const struct GNUNET_CONFIGURATION_Handle *cfg);
88
89
90 /**
91  * Destroy a handle (free all state associated with it).
92  *
93  * @param h statistics handle to destroy
94  * @param sync_first set to #GNUNET_YES if pending SET requests should
95  *        be completed
96  */
97 void
98 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
99                            int sync_first);
100
101
102 /**
103  * Watch statistics from the peer (be notified whenever they change).
104  *
105  * @param handle identification of the statistics service
106  * @param subsystem limit to the specified subsystem, never NULL
107  * @param name name of the statistic value, never NULL
108  * @param proc function to call on each value
109  * @param proc_cls closure for @a proc
110  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
111  */
112 int
113 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
114                          const char *subsystem,
115                          const char *name,
116                          GNUNET_STATISTICS_Iterator proc,
117                          void *proc_cls);
118
119
120 /**
121  * Stop watching statistics from the peer.
122  *
123  * @param handle identification of the statistics service
124  * @param subsystem limit to the specified subsystem, never NULL
125  * @param name name of the statistic value, never NULL
126  * @param proc function to call on each value
127  * @param proc_cls closure for @a proc
128  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch)
129  */
130 int
131 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
132                                 const char *subsystem,
133                                 const char *name,
134                                 GNUNET_STATISTICS_Iterator proc,
135                                 void *proc_cls);
136
137
138 /**
139  * Continuation called by #GNUNET_STATISTICS_get functions.
140  *
141  * @param cls closure
142  * @param success #GNUNET_OK if statistics were
143  *        successfully obtained, #GNUNET_SYSERR if not.
144  */
145 typedef void (*GNUNET_STATISTICS_Callback) (void *cls,
146                                             int success);
147
148
149 /**
150  * Handle that can be used to cancel a statistics 'get' operation.
151  */
152 struct GNUNET_STATISTICS_GetHandle;
153
154
155 /**
156  * Get statistic from the peer.
157  *
158  * @param handle identification of the statistics service
159  * @param subsystem limit to the specified subsystem, NULL for all subsystems
160  * @param name name of the statistic value, NULL for all values
161  * @param timeout after how long should we give up (and call
162  *        notify with buf NULL and size 0)?
163  * @param cont continuation to call when done (can be NULL)
164  *        This callback CANNOT destroy the statistics handle in the same call.
165  * @param proc function to call on each value
166  * @param cls closure for @a proc and @a cont
167  * @return NULL on error
168  */
169 struct GNUNET_STATISTICS_GetHandle *
170 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
171                        const char *subsystem,
172                        const char *name,
173                        struct GNUNET_TIME_Relative timeout,
174                        GNUNET_STATISTICS_Callback cont,
175                        GNUNET_STATISTICS_Iterator proc, void *cls);
176
177
178 /**
179  * Cancel a #GNUNET_STATISTICS_get request.  Must be called before the 'cont'
180  * function is called.
181  *
182  * @param gh handle of the request to cancel
183  */
184 void
185 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh);
186
187
188 /**
189  * Set statistic value for the peer.  Will always use our
190  * subsystem (the argument used when @a handle was created).
191  *
192  * @param handle identification of the statistics service
193  * @param name name of the statistic value
194  * @param value new value to set
195  * @param make_persistent should the value be kept across restarts?
196  */
197 void
198 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
199                        const char *name,
200                        uint64_t value,
201                        int make_persistent);
202
203
204 /**
205  * Set statistic value for the peer.  Will always use our
206  * subsystem (the argument used when @a handle was created).
207  *
208  * @param handle identification of the statistics service
209  * @param name name of the statistic value
210  * @param delta change in value (added to existing value)
211  * @param make_persistent should the value be kept across restarts?
212  */
213 void
214 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
215                           const char *name,
216                           int64_t delta,
217                           int make_persistent);
218
219
220
221 #if 0                           /* keep Emacsens' auto-indent happy */
222 {
223 #endif
224 #ifdef __cplusplus
225 }
226 #endif
227
228 /** @} */ /* end of group statistics */
229
230 #endif