GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
gnunet-setu-ibf-profiler.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012 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
27#include "platform.h"
28#include "gnunet_util_lib.h"
29
30#include "ibf.h"
31
32static unsigned int asize = 10;
33static unsigned int bsize = 10;
34static unsigned int csize = 10;
35static unsigned int hash_num = 4;
36static unsigned int ibf_size = 80;
37
40/* common elements in a and b */
42
44
47
48
49static void
51{
52 struct GNUNET_HashCode replicated;
53 struct IBF_Key key;
54
56 ibf_hashcode_from_key (key, &replicated);
59 &replicated,
60 GNUNET_memdup (hash, sizeof *hash),
62}
63
64
65static void
68 void *cls)
69{
70 struct GNUNET_HashCode replicated;
71
72 ibf_hashcode_from_key (key, &replicated);
74 &replicated,
75 iter,
76 cls);
77}
78
79
80static int
81insert_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
82{
83 struct InvertibleBloomFilter *ibf = cls;
84
86 return GNUNET_YES;
87}
88
89
90static int
91remove_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
92{
93 struct GNUNET_CONTAINER_MultiHashMap *hashmap = cls;
94
95 /* if remove fails, there just was a collision with another key */
96 (void) GNUNET_CONTAINER_multihashmap_remove (hashmap, value, NULL);
97 return GNUNET_YES;
98}
99
100
101static void
102run (void *cls,
103 char *const *args,
104 const char *cfgfile,
105 const struct GNUNET_CONFIGURATION_Handle *cfg)
106{
107 struct GNUNET_HashCode id;
108 struct IBF_Key ibf_key;
109 int i;
110 int side;
111 int res;
113 struct GNUNET_TIME_Relative delta_time;
114
115 set_a =
117 GNUNET_NO);
118 set_b =
120 GNUNET_NO);
122 GNUNET_NO);
123
126 ? 1
127 : (asize + bsize + csize)),
128 GNUNET_NO);
129
130 printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
131 hash_num,
132 ibf_size,
133 asize,
134 bsize,
135 csize);
136
137 i = 0;
138 while (i < asize)
139 {
140 GNUNET_CRYPTO_random_block (&id, sizeof id);
142 continue;
145 set_a,
146 &id,
147 NULL,
149 register_hashcode (&id);
150 i++;
151 }
152 i = 0;
153 while (i < bsize)
154 {
155 GNUNET_CRYPTO_random_block (&id, sizeof id);
157 continue;
159 continue;
162 set_b,
163 &id,
164 NULL,
166 register_hashcode (&id);
167 i++;
168 }
169 i = 0;
170 while (i < csize)
171 {
172 GNUNET_CRYPTO_random_block (&id, sizeof id);
174 continue;
176 continue;
178 continue;
181 set_c,
182 &id,
183 NULL,
185 register_hashcode (&id);
186 i++;
187 }
188
191 if ((NULL == ibf_a) || (NULL == ibf_b))
192 {
193 /* insufficient memory */
194 GNUNET_break (0);
196 return;
197 }
198
199
200 printf ("generated sets\n");
201
203
208
210
211 printf ("encoded in: %s\n",
213
215
216
218
219 for (i = 0; i <= asize + bsize; i++)
220 {
221 res = ibf_decode (ibf_a, &side, &ibf_key);
222 if (GNUNET_SYSERR == res)
223 {
224 printf ("decode failed, %u/%u elements left\n",
227 asize + bsize);
228 return;
229 }
230 if (GNUNET_NO == res)
231 {
234 {
236 printf ("decoded successfully in: %s\n",
238 }
239 else
240 {
241 printf ("decode missed elements (should never happen)\n");
242 }
243 return;
244 }
245
246 if (side == 1)
248 if (side == -1)
250 }
251 printf ("cyclic IBF, %u/%u elements left\n",
254 asize + bsize);
255}
256
257
258int
259main (int argc, char **argv)
260{
263 "asize",
264 NULL,
265 gettext_noop ("number of element in set A-B"),
266 &asize),
267
269 "bsize",
270 NULL,
271 gettext_noop ("number of element in set B-A"),
272 &bsize),
273
275 "csize",
276 NULL,
278 "number of common elements in A and B"),
279 &csize),
280
282 "hash-num",
283 NULL,
284 gettext_noop ("hash num"),
285 &hash_num),
286
288 "ibf-size",
289 NULL,
290 gettext_noop ("ibf size"),
291 &ibf_size),
292
294 };
295
297 argc,
298 argv,
299 "gnunet-consensus-ibf",
300 "help",
301 options,
302 &run,
303 NULL,
304 GNUNET_YES);
305 return 0;
306}
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
void ibf_subtract(struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2)
Subtract ibf2 from ibf1, storing the result in ibf1.
Definition ibf.c:357
int ibf_decode(struct InvertibleBloomFilter *ibf, int *ret_side, struct IBF_Key *ret_id)
Decode and remove an element from the IBF, if possible.
Definition ibf.c:229
struct IBF_Key ibf_key_from_hashcode(const struct GNUNET_HashCode *hash)
Create a key from a hashcode.
Definition ibf.c:44
void ibf_hashcode_from_key(struct IBF_Key key, struct GNUNET_HashCode *dst)
Create a hashcode from a key, by replicating the key until the hascode is filled.
Definition ibf.c:58
void ibf_insert(struct InvertibleBloomFilter *ibf, struct IBF_Key key)
Insert a key into an IBF.
Definition ibf.c:168
struct InvertibleBloomFilter * ibf_create(uint32_t size, uint8_t hash_num)
Create an invertible bloom filter.
Definition ibf.c:80
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static struct GNUNET_TIME_Absolute start_time
Start time of the current round; used to determine how long one iteration takes (which influences how...
struct GNUNET_HashCode key
The key used in the DHT.
static char * res
Currently read line or NULL on EOF.
static struct GNUNET_IDENTITY_Handle * id
Handle to IDENTITY.
static char * value
Value of the record to add/remove.
static int remove_iterator(void *cls, const struct GNUNET_HashCode *key, void *value)
static struct GNUNET_CONTAINER_MultiHashMap * set_a
static void iter_hashcodes(struct IBF_Key key, GNUNET_CONTAINER_MultiHashMapIteratorCallback iter, void *cls)
static struct GNUNET_CONTAINER_MultiHashMap * key_to_hashcode
static unsigned int hash_num
static struct GNUNET_CONTAINER_MultiHashMap * set_c
static struct InvertibleBloomFilter * ibf_a
static void register_hashcode(struct GNUNET_HashCode *hash)
static struct InvertibleBloomFilter * ibf_b
static unsigned int csize
static unsigned int asize
static unsigned int bsize
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
static unsigned int ibf_size
static struct GNUNET_CONTAINER_MultiHashMap * set_b
static int insert_iterator(void *cls, const struct GNUNET_HashCode *key, void *value)
void GNUNET_CRYPTO_random_block(void *buffer, size_t length)
Fill block with a random values.
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.
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
enum GNUNET_GenericReturnValue 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.
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_get_multiple(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map that match a particular key.
enum GNUNET_GenericReturnValue(* GNUNET_CONTAINER_MultiHashMapIteratorCallback)(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over hash map entries.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run2(const struct GNUNET_OS_ProjectData *pd, 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,...
Definition program.c:131
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition time.c:438
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition strings.c:610
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
Internal representation of the hash map.
Definition of a command line option.
A 512-bit hashcode.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
Keys that can be inserted into and removed from an IBF.
Definition ibf.h:46
Invertible bloom filter (IBF).
Definition ibf.h:83