GNUnet  0.10.x
gnunet-set-profiler.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2013 GNUnet e.V.
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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
29 #include "gnunet_set_service.h"
30 #include "gnunet_testbed_service.h"
31 
32 
33 static int ret;
34 
35 static unsigned int num_a = 5;
36 static unsigned int num_b = 5;
37 static unsigned int num_c = 20;
38 
39 static char *op_str = "union";
40 
41 const static struct GNUNET_CONFIGURATION_Handle *config;
42 
43 struct SetInfo {
44  char *id;
45  struct GNUNET_SET_Handle *set;
49  int done;
50 } info1, info2;
51 
53 
54 static struct GNUNET_HashCode app_id;
55 
57 
59 
60 static int byzantine;
61 static unsigned int force_delta;
62 static unsigned int force_full;
63 static unsigned int element_size = 32;
64 
69 
74 static char *statistics_filename;
75 
80 static FILE *statistics_file;
81 
82 
83 static int
85  const struct GNUNET_HashCode *key,
86  void *value)
87 {
88  struct GNUNET_CONTAINER_MultiHashMap *m = cls;
89  int ret;
90 
91  GNUNET_assert(NULL != key);
92 
94  if (GNUNET_OK != ret)
95  printf("spurious element\n");
96  return GNUNET_YES;
97 }
98 
99 
110 static int
112  const char *subsystem,
113  const char *name,
114  uint64_t value,
115  int is_persistent)
116 {
117  if (NULL != statistics_file)
118  {
119  fprintf(statistics_file, "%s\t%s\t%lu\n", subsystem, name, (unsigned long)value);
120  }
121  return GNUNET_OK;
122 }
123 
124 
125 static void
126 statistics_done(void *cls,
127  int success)
128 {
129  GNUNET_assert(GNUNET_YES == success);
130  if (NULL != statistics_file)
131  fclose(statistics_file);
133 }
134 
135 
136 static void
138 {
139  if (info1.done == GNUNET_NO || info2.done == GNUNET_NO)
140  return;
141 
144 
145  printf("set a: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size(info1.sent));
146  printf("set b: %d missing elements\n", GNUNET_CONTAINER_multihashmap_size(info2.sent));
147 
148  if (NULL == statistics_filename)
149  {
151  return;
152  }
153 
154  statistics_file = fopen(statistics_filename, "w");
155  GNUNET_STATISTICS_get(statistics, NULL, NULL,
157  &statistics_result, NULL);
158 }
159 
160 
161 static void
162 set_result_cb(void *cls,
163  const struct GNUNET_SET_Element *element,
164  uint64_t current_size,
166 {
167  struct SetInfo *info = cls;
168  struct GNUNET_HashCode hash;
169 
170  GNUNET_assert(GNUNET_NO == info->done);
171  switch (status)
172  {
175  info->done = GNUNET_YES;
176  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s done\n", info->id);
177  check_all_done();
178  info->oh = NULL;
179  return;
180 
182  info->oh = NULL;
183  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "failure\n");
185  return;
186 
188  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s: local element\n", info->id);
189  break;
190 
192  GNUNET_CRYPTO_hash(element->data, element->size, &hash);
193  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s: remote element %s\n", info->id,
194  GNUNET_h2s(&hash));
195  // XXX: record and check
196  return;
197 
198  default:
199  GNUNET_assert(0);
200  }
201 
202  if (element->size != element_size)
203  {
205  "wrong element size: %u, expected %u\n",
206  element->size,
207  (unsigned int)sizeof(struct GNUNET_HashCode));
208  GNUNET_assert(0);
209  }
210 
211  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "set %s: got element (%s)\n",
212  info->id, GNUNET_h2s(element->data));
213  GNUNET_assert(NULL != element->data);
214  struct GNUNET_HashCode data_hash;
215  GNUNET_CRYPTO_hash(element->data, element_size, &data_hash);
217  &data_hash, NULL,
219 }
220 
221 
222 static void
223 set_listen_cb(void *cls,
224  const struct GNUNET_PeerIdentity *other_peer,
225  const struct GNUNET_MessageHeader *context_msg,
226  struct GNUNET_SET_Request *request)
227 {
228  /* max. 2 options plus terminator */
229  struct GNUNET_SET_Option opts[3] = { { 0 } };
230  unsigned int n_opts = 0;
231 
232  if (NULL == request)
233  {
235  "listener failed\n");
236  return;
237  }
238  GNUNET_assert(NULL == info2.oh);
240  "set listen cb called\n");
241  if (byzantine)
242  {
243  opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
244  }
246  if (force_full)
247  {
248  opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
249  }
250  if (force_delta)
251  {
252  opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
253  }
254 
255  opts[n_opts].type = 0;
257  opts,
258  set_result_cb, &info2);
260 }
261 
262 
263 static int
265  const struct GNUNET_HashCode *key,
266  void *value)
267 {
268  struct GNUNET_SET_Handle *set = cls;
269  struct GNUNET_SET_Element el;
270 
271  el.element_type = 0;
272  el.data = value;
273  el.size = element_size;
274  GNUNET_SET_add_element(set, &el, NULL, NULL);
275  return GNUNET_YES;
276 }
277 
278 
279 static void
280 handle_shutdown(void *cls)
281 {
283  "Shutting down set profiler\n");
284  if (NULL != set_listener)
285  {
286  GNUNET_SET_listen_cancel(set_listener);
287  set_listener = NULL;
288  }
289  if (NULL != info1.oh)
290  {
292  info1.oh = NULL;
293  }
294  if (NULL != info2.oh)
295  {
297  info2.oh = NULL;
298  }
299  if (NULL != info1.set)
300  {
302  info1.set = NULL;
303  }
304  if (NULL != info2.set)
305  {
307  info2.set = NULL;
308  }
310 }
311 
312 
313 static void
314 run(void *cls,
315  const struct GNUNET_CONFIGURATION_Handle *cfg,
316  struct GNUNET_TESTING_Peer *peer)
317 {
318  unsigned int i;
319  struct GNUNET_HashCode hash;
320  /* max. 2 options plus terminator */
321  struct GNUNET_SET_Option opts[3] = { { 0 } };
322  unsigned int n_opts = 0;
323 
324  config = cfg;
325 
327 
329  {
330  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
331  ret = 0;
332  return;
333  }
334 
335  statistics = GNUNET_STATISTICS_create("set-profiler", cfg);
336 
338 
339  info1.id = "a";
340  info2.id = "b";
341 
345 
348 
349  for (i = 0; i < num_a; i++)
350  {
353  GNUNET_CRYPTO_hash(data, element_size, &hash);
356  }
357 
358  for (i = 0; i < num_b; i++)
359  {
362  GNUNET_CRYPTO_hash(data, element_size, &hash);
365  }
366 
367  for (i = 0; i < num_c; i++)
368  {
371  GNUNET_CRYPTO_hash(data, element_size, &hash);
372  GNUNET_CONTAINER_multihashmap_put(common_sent, &hash, data,
374  }
375 
377 
378  /* FIXME: also implement intersection etc. */
381 
386 
387  set_listener = GNUNET_SET_listen(config, GNUNET_SET_OPERATION_UNION,
388  &app_id, set_listen_cb, NULL);
389 
390 
391  if (byzantine)
392  {
393  opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_BYZANTINE };
394  }
396  if (force_full)
397  {
398  opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_FULL };
399  }
400  if (force_delta)
401  {
402  opts[n_opts++] = (struct GNUNET_SET_Option) { .type = GNUNET_SET_OPTION_FORCE_DELTA };
403  }
404 
405  opts[n_opts].type = 0;
406 
409  opts,
410  set_result_cb, &info1);
413  info1.set = NULL;
414 }
415 
416 
417 static void
418 pre_run(void *cls, char *const *args, const char *cfgfile,
419  const struct GNUNET_CONFIGURATION_Handle *cfg)
420 {
421  if (0 != GNUNET_TESTING_peer_run("set-profiler",
422  cfgfile,
423  &run, NULL))
424  ret = 2;
425 }
426 
427 
428 int
429 main(int argc, char **argv)
430 {
431  struct GNUNET_GETOPT_CommandLineOption options[] = {
433  "num-first",
434  NULL,
435  gettext_noop("number of values"),
436  &num_a),
437 
439  "num-second",
440  NULL,
441  gettext_noop("number of values"),
442  &num_b),
443 
445  "byzantine",
446  gettext_noop("use byzantine mode"),
447  &byzantine),
448 
450  "force-full",
451  NULL,
452  gettext_noop("force sending full set"),
453  &force_full),
454 
456  "force-delta",
457  NULL,
458  gettext_noop("number delta operation"),
459  &force_delta),
460 
462  "num-common",
463  NULL,
464  gettext_noop("number of values"),
465  &num_c),
466 
468  "operation",
469  NULL,
470  gettext_noop("operation to execute"),
471  &op_str),
472 
474  "element-size",
475  NULL,
476  gettext_noop("element size"),
477  &element_size),
478 
480  "statistics",
481  "FILENAME",
482  gettext_noop("write statistics to file"),
484 
486  };
487 
488  GNUNET_PROGRAM_run2(argc, argv, "gnunet-set-profiler",
489  "help",
490  options, &pre_run, NULL, GNUNET_YES);
491  return ret;
492 }
static struct GNUNET_STATISTICS_Handle * statistics
Handle to the statistics service.
struct GNUNET_SET_ListenHandle * GNUNET_SET_listen(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_SET_OperationType op_type, const struct GNUNET_HashCode *app_id, GNUNET_SET_ListenCallback listen_cb, void *listen_cls)
Wait for set operation requests for the given application ID.
Definition: set_api.c:1012
static char * subsystem
Set to subsystem that we&#39;re going to get stats for (or NULL for all).
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition: gnunet-vpn.c:41
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
static unsigned int element_size
static int byzantine
int GNUNET_SET_add_element(struct GNUNET_SET_Handle *set, const struct GNUNET_SET_Element *element, GNUNET_SET_Continuation cont, void *cont_cls)
Add an element to the given set.
Definition: set_api.c:682
If a value with the given key exists, replace it.
Handle for a set operation request from another peer.
Definition: set_api.c:113
int GNUNET_PROGRAM_run2(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls, int run_without_scheduler)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:140
Element should be added to the result set of the remote peer, i.e.
Handle for a GNUnet peer controlled by testing.
Definition: testing.c:167
int GNUNET_TESTING_peer_run(const char *testdir, const char *cfgfilename, GNUNET_TESTING_TestMain tm, void *tm_cls)
Start a single peer and run a test using the testing library.
Definition: testing.c:1594
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
Element stored in a set.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
struct GNUNET_SET_Handle * GNUNET_SET_create(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_SET_OperationType op)
Create an empty set, supporting the specified operation.
Definition: set_api.c:652
Element should be added to the result set of the local peer, i.e.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Do not use the optimized set operation, but send full sets.
static void set_listen_cb(void *cls, const struct GNUNET_PeerIdentity *other_peer, const struct GNUNET_MessageHeader *context_msg, struct GNUNET_SET_Request *request)
static unsigned int num_b
#define GNUNET_NO
Definition: gnunet_common.h:78
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static char * op_str
static struct GNUNET_HashCode app_id
Definition of a command line option.
int GNUNET_CONTAINER_multihashmap_remove_all(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Remove all entries for the given key from the map.
struct GNUNET_SET_OperationHandle * GNUNET_SET_prepare(const struct GNUNET_PeerIdentity *other_peer, const struct GNUNET_HashCode *app_id, const struct GNUNET_MessageHeader *context_msg, enum GNUNET_SET_ResultMode result_mode, struct GNUNET_SET_Option options[], GNUNET_SET_ResultIterator result_cb, void *result_cls)
Prepare a set operation to be evaluated with another peer.
Definition: set_api.c:808
static struct GNUNET_PeerIdentity local_peer
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
Handle for the service.
static unsigned int force_full
Success, all elements have been sent (and received).
Internal representation of the hash map.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
const void * data
Actual data of the element.
enum GNUNET_SET_OptionType type
Type of the option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
static void statistics_done(void *cls, int success)
void GNUNET_SET_destroy(struct GNUNET_SET_Handle *set)
Destroy the set handle, and free all associated resources.
Definition: set_api.c:767
struct GNUNET_CONTAINER_MultiHashMap * received
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:99
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_filename(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a filename (automatically path expanded).
Only use optimized set operations, even though for this particular set operation they might be much s...
void GNUNET_CRYPTO_hash_create_random(enum GNUNET_CRYPTO_Quality mode, struct GNUNET_HashCode *result)
Create a random hash code.
Definition: crypto_hash.c:138
static char * value
Value of the record to add/remove.
The other peer refused to to the operation with us, or something went wrong.
Success, all elements have been returned (but the other peer might still be receiving some from us...
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
GNUNET_SET_Status
Status for the result callback.
static unsigned int num_a
static int map_remove_iterator(void *cls, const struct GNUNET_HashCode *key, void *value)
uint16_t status
See PRISM_STATUS_*-constants.
static const struct GNUNET_CONFIGURATION_Handle * config
static int set_insert_iterator(void *cls, const struct GNUNET_HashCode *key, void *value)
static struct GNUNET_SET_ListenHandle * set_listener
static int statistics_result(void *cls, const char *subsystem, const char *name, uint64_t value, int is_persistent)
Callback function to process statistic values.
static int ret
A 512-bit hashcode.
Client gets notified of the required changes for both the local and the remote set.
static unsigned int num_c
Opaque handle to a set.
Definition: set_api.c:48
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static char * statistics_filename
The profiler will write statistics for all peers to the file with this name.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
static FILE * statistics_file
The profiler will write statistics for all peers to this file.
int main(int argc, char **argv)
const char * name
static void check_all_done(void)
void GNUNET_SET_listen_cancel(struct GNUNET_SET_ListenHandle *lh)
Cancel the given listen operation.
Definition: set_api.c:1046
Option for set operations.
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
static void handle_shutdown(void *cls)
struct SetInfo info1
Handle to an operation.
Definition: set_api.c:132
The identity of the host (wraps the signing key of the peer).
int GNUNET_SET_commit(struct GNUNET_SET_OperationHandle *oh, struct GNUNET_SET_Handle *set)
Commit a set to be used with a set operation.
Definition: set_api.c:1123
struct GNUNET_STATISTICS_GetHandle * GNUNET_STATISTICS_get(struct GNUNET_STATISTICS_Handle *handle, const char *subsystem, const char *name, GNUNET_STATISTICS_Callback cont, GNUNET_STATISTICS_Iterator proc, void *cls)
Get statistic from the peer.
struct GNUNET_SET_Handle * set
configuration data
Definition: configuration.c:83
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
uint16_t size
Number of bytes in the buffer pointed to by data.
#define GNUNET_log(kind,...)
Opaque handle to a listen operation.
Definition: set_api.c:182
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
int GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host&#39;s peer.
void GNUNET_SET_operation_cancel(struct GNUNET_SET_OperationHandle *oh)
Cancel the given set operation.
Definition: set_api.c:511
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Peer *peer)
Header for all communications.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
static void pre_run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
struct GNUNET_SET_OperationHandle * oh
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
Set union, return all elements that are in at least one of the sets.
static struct GNUNET_CONTAINER_MultiHashMap * common_sent
uint32_t data
The data value.
static void set_result_cb(void *cls, const struct GNUNET_SET_Element *element, uint64_t current_size, enum GNUNET_SET_Status status)
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_malloc(size)
Wrapper around malloc.
static unsigned int force_delta
struct SetInfo info2
uint16_t element_type
Application-specific element type.
struct GNUNET_SET_OperationHandle * GNUNET_SET_accept(struct GNUNET_SET_Request *request, enum GNUNET_SET_ResultMode result_mode, struct GNUNET_SET_Option options[], GNUNET_SET_ResultIterator result_cb, void *result_cls)
Accept a request we got via GNUNET_SET_listen().
Definition: set_api.c:1080
#define gettext_noop(String)
Definition: gettext.h:69
struct GNUNET_CONTAINER_MultiHashMap * sent
Fail set operations when the other peer shows weird behavior that might by a Byzantine fault...
High-quality operations are desired.