aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-test_util.c
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2018-06-12 23:03:47 +0200
committerJulius Bünger <buenger@mytum.de>2018-06-12 23:03:47 +0200
commitf24f95b6664f5e1d2a8e3cd6d03c957b0d2efd74 (patch)
tree7ed84cea462fd421379cc84d0504e7e634e44f43 /src/rps/rps-test_util.c
parentc4ea87522edf51a18538715bec7f4baa87c13cd4 (diff)
downloadgnunet-f24f95b6664f5e1d2a8e3cd6d03c957b0d2efd74.tar.gz
gnunet-f24f95b6664f5e1d2a8e3cd6d03c957b0d2efd74.zip
rps profiler: generate output for randomness tests
Diffstat (limited to 'src/rps/rps-test_util.c')
-rw-r--r--src/rps/rps-test_util.c219
1 files changed, 206 insertions, 13 deletions
diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c
index ea55deac5..64ef5b986 100644
--- a/src/rps/rps-test_util.c
+++ b/src/rps/rps-test_util.c
@@ -29,13 +29,37 @@
29 29
30#include <inttypes.h> 30#include <inttypes.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
33 33
34#ifndef TO_FILE 34#ifndef TO_FILE
35#define TO_FILE 35#define TO_FILE
36#endif /* TO_FILE */ 36#endif /* TO_FILE */
37 37
38#ifdef TO_FILE 38#ifdef TO_FILE
39
40#define B2B_PAT "%c%c%c%c%c%c%c%c"
41#define B2B(byte) \
42 (byte & 0x80 ? '1' : '0'), \
43 (byte & 0x40 ? '1' : '0'), \
44 (byte & 0x20 ? '1' : '0'), \
45 (byte & 0x10 ? '1' : '0'), \
46 (byte & 0x08 ? '1' : '0'), \
47 (byte & 0x04 ? '1' : '0'), \
48 (byte & 0x02 ? '1' : '0'), \
49 (byte & 0x01 ? '1' : '0')
50
51#define min(x,y) ((x) > (y) ? (y) : (x))
52
53/**
54 * @brief buffer for storing the unaligned bits for the next write
55 */
56static char buf_unaligned;
57
58/**
59 * @brief number of bits in unaligned buffer
60 */
61static unsigned num_bits_buf_unaligned;
62
39void 63void
40to_file_ (const char *file_name, char *line) 64to_file_ (const char *file_name, char *line)
41{ 65{
@@ -108,11 +132,10 @@ to_file_ (const char *file_name, char *line)
108} 132}
109 133
110void 134void
111to_file_raw (const char *file_name, void *buf, size_t size_buf) 135to_file_raw (const char *file_name, const char *buf, size_t size_buf)
112{ 136{
113 struct GNUNET_DISK_FileHandle *f; 137 struct GNUNET_DISK_FileHandle *f;
114 size_t size2; 138 size_t size_written;
115
116 139
117 if (NULL == (f = GNUNET_DISK_file_open (file_name, 140 if (NULL == (f = GNUNET_DISK_file_open (file_name,
118 GNUNET_DISK_OPEN_APPEND | 141 GNUNET_DISK_OPEN_APPEND |
@@ -129,13 +152,13 @@ to_file_raw (const char *file_name, void *buf, size_t size_buf)
129 return; 152 return;
130 } 153 }
131 154
132 size2 = GNUNET_DISK_file_write (f, buf, size_buf); 155 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
133 if (size_buf != size2) 156 if (size_buf != size_written)
134 { 157 {
135 LOG (GNUNET_ERROR_TYPE_WARNING, 158 LOG (GNUNET_ERROR_TYPE_WARNING,
136 "Unable to write to file! (Size: %u, size2: %u)\n", 159 "Unable to write to file! (Size: %u, size_written: %u)\n",
137 size_buf, 160 size_buf,
138 size2); 161 size_written);
139 162
140 if (GNUNET_YES != GNUNET_DISK_file_close (f)) 163 if (GNUNET_YES != GNUNET_DISK_file_close (f))
141 LOG (GNUNET_ERROR_TYPE_WARNING, 164 LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -143,15 +166,185 @@ to_file_raw (const char *file_name, void *buf, size_t size_buf)
143 166
144 return; 167 return;
145 } 168 }
169}
146 170
147 //if (512 < size_buf) 171void
172to_file_raw_unaligned (const char *file_name,
173 const char *buf,
174 size_t size_buf,
175 unsigned bits_needed)
176{
177 // TODO endianness!
178 GNUNET_assert (size_buf >= (bits_needed/8));
179 //if (0 == num_bits_buf_unaligned)
148 //{ 180 //{
149 // GNUNET_free (output_buffer_p); 181 // if (0 == (bits_needed % 8))
182 // {
183 // to_file_raw (file_name, buf, size_buf);
184 // return;
185 // }
186 // to_file_raw (file_name, buf, size_buf - 1);
187 // buf_unaligned = buf[size_buf - 1];
188 // num_bits_buf_unaligned = bits_needed % 8;
189 // return;
150 //} 190 //}
151 191
152 //if (GNUNET_YES != GNUNET_DISK_file_close (f)) 192 char buf_write[size_buf + 1];
153 // LOG (GNUNET_ERROR_TYPE_WARNING, 193 const unsigned bytes_iter = (0 != bits_needed % 8?
154 // "Unable to close file\n"); 194 (bits_needed/8)+1:
195 bits_needed/8);
196 // TODO what if no iteration happens?
197 unsigned size_buf_write = 0;
198 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "num_bits_buf_unaligned: %u\n",
200 num_bits_buf_unaligned);
201 LOG (GNUNET_ERROR_TYPE_DEBUG,
202 "ua args: size_buf: %u, bits_needed: %u -> iter: %u\n",
203 size_buf,
204 bits_needed,
205 bytes_iter);
206 buf_write[0] = buf_unaligned;
207 /* Iterate over input bytes */
208 for (unsigned i = 0; i < bytes_iter; i++)
209 {
210 /* Number of bits needed in this iteration - 8 for all except last iter */
211 unsigned num_bits_needed_iter;
212 /* Mask for bits to actually use */
213 unsigned mask_bits_needed_iter;
214 char byte_input;
215 /* Number of bits needed to align unaligned byte */
216 unsigned num_bits_to_align;
217 /* Number of bits that are to be moved */
218 unsigned num_bits_to_move;
219 /* Mask for bytes to be moved */
220 char mask_input_to_move;
221 /* Masked bits to be moved */
222 char bits_to_move;
223 /* The amount of bits needed to fit the bits to shift to the nearest spot */
224 unsigned distance_shift_bits;
225 /* Shifted bits on the move */
226 char bits_moving;
227 /* (unaligned) byte being filled with bits */
228 char byte_to_fill;
229 /* mask for needed bits of the input byte that have not been moved */
230 char mask_input_leftover;
231 /* needed bits of the input byte that have not been moved */
232 char byte_input_leftover;
233 unsigned num_bits_leftover;
234 unsigned num_bits_discard;
235 char byte_unaligned_new;
236
237 if ( (bits_needed - (i * 8)) <= 8)
238 {
239 /* last iteration */
240 num_bits_needed_iter = bits_needed - (i * 8);
241 }
242 else
243 {
244 num_bits_needed_iter = 8;
245 }
246 LOG (GNUNET_ERROR_TYPE_DEBUG,
247 "number of bits needed in this iteration: %u\n",
248 num_bits_needed_iter);
249 mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
250 LOG (GNUNET_ERROR_TYPE_DEBUG,
251 "mask needed bits (current iter): "B2B_PAT"\n",
252 B2B(mask_bits_needed_iter));
253 LOG (GNUNET_ERROR_TYPE_DEBUG,
254 "Unaligned byte: "B2B_PAT" (%u bits)\n",
255 B2B(buf_unaligned),
256 num_bits_buf_unaligned);
257 byte_input = buf[i];
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "next whole input byte: "B2B_PAT"\n",
260 B2B(byte_input));
261 byte_input &= mask_bits_needed_iter;
262 LOG (GNUNET_ERROR_TYPE_DEBUG,
263 "input byte, needed bits: "B2B_PAT"\n",
264 B2B(byte_input));
265 num_bits_to_align = 8 - num_bits_buf_unaligned;
266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "number of bits needed to align unaligned bit: %u\n",
268 num_bits_to_align);
269 num_bits_to_move = min (num_bits_to_align, num_bits_needed_iter);
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "number of bits of new byte to move: %u\n",
272 num_bits_to_move);
273 mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "mask of bits of new byte to take for moving: "B2B_PAT"\n",
276 B2B(mask_input_to_move));
277 bits_to_move = byte_input & mask_input_to_move;
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "masked bits of new byte to take for moving: "B2B_PAT"\n",
280 B2B(bits_to_move));
281 distance_shift_bits = num_bits_buf_unaligned;
282 LOG (GNUNET_ERROR_TYPE_DEBUG,
283 "distance needed to shift bits to their correct spot: %u\n",
284 distance_shift_bits);
285 bits_moving = bits_to_move << distance_shift_bits;
286 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "shifted, masked bits of new byte being moved: "B2B_PAT"\n",
288 B2B(bits_moving));
289 byte_to_fill = buf_unaligned | bits_moving;
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "byte being filled: "B2B_PAT"\n",
292 B2B(byte_to_fill));
293 if (num_bits_buf_unaligned + num_bits_needed_iter > 8)
294 {
295 /* buf_unaligned was aligned by filling
296 * -> can be written to storage */
297 buf_write[i] = byte_to_fill;
298 size_buf_write++;
299
300 /* store the leftover, unaligned bits in buffer */
301 mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
302 LOG (GNUNET_ERROR_TYPE_DEBUG,
303 "mask of leftover bits of new byte: "B2B_PAT"\n",
304 B2B(mask_input_leftover));
305 byte_input_leftover = byte_input & mask_input_leftover;
306 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "masked, leftover bits of new byte: "B2B_PAT"\n",
308 B2B(byte_input_leftover));
309 num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "number of unaligned bits left: %u\n",
312 num_bits_leftover);
313 num_bits_discard = 8 - num_bits_needed_iter;
314 byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
315 LOG (GNUNET_ERROR_TYPE_DEBUG,
316 "new unaligned byte: "B2B_PAT"\n",
317 B2B(byte_unaligned_new));
318 buf_unaligned = byte_unaligned_new;
319 num_bits_buf_unaligned = num_bits_leftover % 8;
320 }
321 else
322 {
323 /* unaligned buffer still unaligned but 'fuller' */
324 buf_unaligned = byte_to_fill;
325 num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
326 }
327 ///* Byte to be completed will consist of what is left in the unaligned
328 // * byte and the rest of the input byte */
329 //LOG (GNUNET_ERROR_TYPE_DEBUG,
330 // "ua: %u - %x, %x << %u = %x\n",
331 // i,
332 // buf_unaligned,
333 // num_bits_buf_unaligned,
334 // (char) ((char) buf_unaligned << num_bits_buf_unaligned));
335 //buf_write[i] = buf_write[i] |
336 // (char) ((char) buf_unaligned << num_bits_buf_unaligned);
337 //LOG (GNUNET_ERROR_TYPE_DEBUG,
338 // "ua: %x\n",
339 // buf_write[i]);
340 //buf_unaligned = buf[i] >> num_bits_buf_unaligned;
341 //LOG (GNUNET_ERROR_TYPE_DEBUG,
342 // "ua: %x\n"
343 // "---\n",
344 // buf_unaligned);
345 }
346 to_file_raw (file_name, buf_write, size_buf_write);
347 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
155} 348}
156 349
157char * 350char *