RPS tests: Provide valgrind logging
[oweals/gnunet.git] / src / rps / rps-sampler_client.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file rps/gnunet-service-rps_sampler.c
23  * @brief sampler implementation
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "rps.h"
30
31 #include "rps-sampler_common.h"
32 #include "gnunet-service-rps_sampler.h"
33 #include "gnunet-service-rps_sampler_elem.h"
34
35 #include <math.h>
36 #include <inttypes.h>
37
38 #include "rps-test_util.h"
39
40 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
41
42
43 // multiple 'clients'?
44
45 // TODO check for overflows
46
47 // TODO align message structs
48
49 // hist_size_init, hist_size_max
50
51 /***********************************************************************
52  * WARNING: This section needs to be reviewed regarding the use of
53  * functions providing (pseudo)randomness!
54 ***********************************************************************/
55
56 // TODO care about invalid input of the caller (size 0 or less...)
57
58 /**
59  * @brief Callback called each time a new peer was put into the sampler
60  *
61  * @param cls A possibly given closure
62  */
63 typedef void
64 (*SamplerNotifyUpdateCB) (void *cls);
65
66 /**
67  * @brief Context for a callback. Contains callback and closure.
68  *
69  * Meant to be an entry in an DLL.
70  */
71 struct SamplerNotifyUpdateCTX
72 {
73   /**
74    * @brief The Callback to call on updates
75    */
76   SamplerNotifyUpdateCB notify_cb;
77
78   /**
79    * @brief The according closure.
80    */
81   void *cls;
82
83   /**
84    * @brief Next element in DLL.
85    */
86   struct SamplerNotifyUpdateCTX *next;
87
88   /**
89    * @brief Previous element in DLL.
90    */
91   struct SamplerNotifyUpdateCTX *prev;
92 };
93
94
95 /**
96  * Type of function used to differentiate between modified and not modified
97  * Sampler.
98  */
99 typedef void
100 (*RPS_get_peers_type) (void *cls);
101
102
103 /**
104  * Get one random peer out of the sampled peers.
105  *
106  * We might want to reinitialise this sampler after giving the
107  * corrsponding peer to the client.
108  */
109 static void
110 sampler_mod_get_rand_peer (void *cls);
111
112
113 /**
114  * Closure to _get_n_rand_peers_ready_cb()
115  */
116 struct RPS_SamplerRequestHandle
117 {
118   /**
119    * DLL
120    */
121   struct RPS_SamplerRequestHandle *next;
122   struct RPS_SamplerRequestHandle *prev;
123
124   /**
125    * Number of peers we are waiting for.
126    */
127   uint32_t num_peers;
128
129   /**
130    * Number of peers we currently have.
131    */
132   uint32_t cur_num_peers;
133
134   /**
135    * Pointer to the array holding the ids.
136    */
137   struct GNUNET_PeerIdentity *ids;
138
139   /**
140    * Head and tail for the DLL to store the tasks for single requests
141    */
142   struct GetPeerCls *gpc_head;
143   struct GetPeerCls *gpc_tail;
144
145   /**
146    * Sampler.
147    */
148   struct RPS_Sampler *sampler;
149
150   /**
151    * Callback to be called when all ids are available.
152    */
153   RPS_sampler_n_rand_peers_ready_cb callback;
154
155   /**
156    * Closure given to the callback
157    */
158   void *cls;
159 };
160
161 ///**
162 // * Global sampler variable.
163 // */
164 //struct RPS_Sampler *sampler;
165
166
167 /**
168  * The minimal size for the extended sampler elements.
169  */
170 static size_t min_size;
171
172 /**
173  * The maximal size the extended sampler elements should grow to.
174  */
175 static size_t max_size;
176
177 /**
178  * The size the extended sampler elements currently have.
179  */
180 //static size_t extra_size;
181
182 /**
183  * Inedex to the sampler element that is the next to be returned
184  */
185 static uint32_t client_get_index;
186
187
188 /**
189  * Initialise a modified tuple of sampler elements.
190  *
191  * @param init_size the size the sampler is initialised with
192  * @param max_round_interval maximum time a round takes
193  * @return a handle to a sampler that consists of sampler elements.
194  */
195 struct RPS_Sampler *
196 RPS_sampler_mod_init (size_t init_size,
197                       struct GNUNET_TIME_Relative max_round_interval)
198 {
199   struct RPS_Sampler *sampler;
200
201   /* Initialise context around extended sampler */
202   min_size = 10; // TODO make input to _samplers_init()
203   max_size = 1000; // TODO make input to _samplers_init()
204
205   sampler = GNUNET_new (struct RPS_Sampler);
206   sampler->max_round_interval = max_round_interval;
207   sampler->get_peers = sampler_mod_get_rand_peer;
208   //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
209   //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
210
211   client_get_index = 0;
212
213   //GNUNET_assert (init_size == sampler->sampler_size);
214
215   RPS_sampler_resize (sampler, init_size);
216
217   return sampler;
218 }
219
220
221 /**
222  * Get one random peer out of the sampled peers.
223  *
224  * This reinitialises the queried sampler element.
225  */
226 static void
227 sampler_mod_get_rand_peer (void *cls)
228 {
229   struct GetPeerCls *gpc = cls;
230   struct RPS_SamplerElement *s_elem;
231   struct GNUNET_TIME_Relative last_request_diff;
232   struct RPS_Sampler *sampler;
233
234   gpc->get_peer_task = NULL;
235   gpc->notify_ctx = NULL;
236   sampler = gpc->req_handle->sampler;
237
238   LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
239
240   /* Cycle the #client_get_index one step further */
241   client_get_index = (client_get_index + 1) % sampler->sampler_size;
242
243   s_elem = sampler->sampler_elements[client_get_index];
244   *gpc->id = s_elem->peer_id;
245   GNUNET_assert (NULL != s_elem);
246
247   if (EMPTY == s_elem->is_empty)
248   {
249     LOG (GNUNET_ERROR_TYPE_DEBUG,
250          "Sampler_mod element empty, rescheduling.\n");
251     GNUNET_assert (NULL == gpc->notify_ctx);
252     gpc->notify_ctx =
253       sampler_notify_on_update (sampler,
254                                 &sampler_mod_get_rand_peer,
255                                 gpc);
256     return;
257   }
258
259   /* Check whether we may use this sampler to give it back to the client */
260   if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
261   {
262     // TODO remove this condition at least for the client sampler
263     last_request_diff =
264       GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
265                                            GNUNET_TIME_absolute_get ());
266     /* We're not going to give it back now if it was
267      * already requested by a client this round */
268     if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
269     {
270       LOG (GNUNET_ERROR_TYPE_DEBUG,
271           "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
272       ///* How many time remains untile the next round has started? */
273       //inv_last_request_diff =
274       //  GNUNET_TIME_absolute_get_difference (last_request_diff,
275       //                                       sampler->max_round_interval);
276       // add a little delay
277       /* Schedule it one round later */
278       GNUNET_assert (NULL == gpc->notify_ctx);
279       gpc->notify_ctx =
280         sampler_notify_on_update (sampler,
281                                   &sampler_mod_get_rand_peer,
282                                   gpc);
283       return;
284     }
285   }
286   if (2 > s_elem->num_peers)
287   {
288     LOG (GNUNET_ERROR_TYPE_DEBUG,
289         "This s_elem saw less than two peers -- scheduling for later\n");
290     GNUNET_assert (NULL == gpc->notify_ctx);
291     gpc->notify_ctx =
292       sampler_notify_on_update (sampler,
293                                 &sampler_mod_get_rand_peer,
294                                 gpc);
295     return;
296   }
297   /* More reasons to wait could be added here */
298
299 //  GNUNET_STATISTICS_set (stats,
300 //                         "# client sampler element input",
301 //                         s_elem->num_peers,
302 //                         GNUNET_NO);
303 //  GNUNET_STATISTICS_set (stats,
304 //                         "# client sampler element change",
305 //                         s_elem->num_change,
306 //                         GNUNET_NO);
307
308   RPS_sampler_elem_reinit (s_elem);
309   s_elem->last_client_request = GNUNET_TIME_absolute_get ();
310
311   GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
312                                gpc->req_handle->gpc_tail,
313                                gpc);
314   gpc->cont (gpc->cont_cls, gpc->id);
315   GNUNET_free (gpc);
316 }
317
318
319 /* end of gnunet-service-rps.c */
320