aboutsummaryrefslogtreecommitdiff
path: root/src/service/regex/gnunet-regex-simulation-profiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/regex/gnunet-regex-simulation-profiler.c')
-rw-r--r--src/service/regex/gnunet-regex-simulation-profiler.c727
1 files changed, 727 insertions, 0 deletions
diff --git a/src/service/regex/gnunet-regex-simulation-profiler.c b/src/service/regex/gnunet-regex-simulation-profiler.c
new file mode 100644
index 000000000..abdb1abeb
--- /dev/null
+++ b/src/service/regex/gnunet-regex-simulation-profiler.c
@@ -0,0 +1,727 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2011, 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
21
22/**
23 * @file regex/gnunet-regex-simulation-profiler.c
24 * @brief Regex profiler that dumps all DFAs into a database instead of
25 * using the DHT (with cadet).
26 * @author Maximilian Szengel
27 * @author Christophe Genevey
28 *
29 */
30
31#include "platform.h"
32#include "gnunet_util_lib.h"
33#include "regex_internal_lib.h"
34#include "gnunet_mysql_lib.h"
35#include "gnunet_mysql_compat.h"
36#include "gnunet_my_lib.h"
37#include <mysql/mysql.h>
38
39/**
40 * MySQL statement to insert an edge.
41 */
42#define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` " \
43 "(`key`, `label`, `to_key`, `accepting`) " \
44 "VALUES (?, ?, ?, ?);"
45
46/**
47 * MySQL statement to select a key count.
48 */
49#define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` " \
50 "WHERE `key` = ? AND `label` = ?;"
51
52/**
53 * Simple struct to keep track of progress, and print a
54 * nice little percentage meter for long running tasks.
55 */
56struct ProgressMeter
57{
58 /**
59 * Total number of elements.
60 */
61 unsigned int total;
62
63 /**
64 * Interval for printing percentage.
65 */
66 unsigned int modnum;
67
68 /**
69 * Number of dots to print.
70 */
71 unsigned int dotnum;
72
73 /**
74 * Completed number.
75 */
76 unsigned int completed;
77
78 /**
79 * Should the meter be printed?
80 */
81 int print;
82
83 /**
84 * String to print on startup.
85 */
86 char *startup_string;
87};
88
89
90/**
91 * Handle for the progress meter
92 */
93static struct ProgressMeter *meter;
94
95/**
96 * Scan task identifier;
97 */
98static struct GNUNET_SCHEDULER_Task *scan_task;
99
100/**
101 * Global testing status.
102 */
103static int result;
104
105/**
106 * MySQL context.
107 */
108static struct GNUNET_MYSQL_Context *mysql_ctx;
109
110/**
111 * MySQL prepared statement handle.
112 */
113static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
114
115/**
116 * MySQL prepared statement handle for `key` select.
117 */
118static struct GNUNET_MYSQL_StatementHandle *select_stmt_handle;
119
120/**
121 * MySQL table name.
122 */
123static char *table_name;
124
125/**
126 * Policy dir containing files that contain policies.
127 */
128static char *policy_dir;
129
130/**
131 * Number of policy files.
132 */
133static unsigned int num_policy_files;
134
135/**
136 * Number of policies.
137 */
138static unsigned int num_policies;
139
140/**
141 * Maximal path compression length.
142 */
143static unsigned int max_path_compression;
144
145/**
146 * Number of merged transitions.
147 */
148static unsigned long long num_merged_transitions;
149
150/**
151 * Number of merged states from different policies.
152 */
153static unsigned long long num_merged_states;
154
155/**
156 * Prefix to add before every regex we're announcing.
157 */
158static char *regex_prefix;
159
160
161/**
162 * Create a meter to keep track of the progress of some task.
163 *
164 * @param total the total number of items to complete
165 * @param start_string a string to prefix the meter with (if printing)
166 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
167 * internally only
168 *
169 * @return the progress meter
170 */
171static struct ProgressMeter *
172create_meter (unsigned int total, char *start_string, int print)
173{
174 struct ProgressMeter *ret;
175
176 ret = GNUNET_new (struct ProgressMeter);
177 ret->print = print;
178 ret->total = total;
179 ret->modnum = total / 4;
180 if (ret->modnum == 0) /* Divide by zero check */
181 ret->modnum = 1;
182 ret->dotnum = (total / 50) + 1;
183 if (start_string != NULL)
184 ret->startup_string = GNUNET_strdup (start_string);
185 else
186 ret->startup_string = GNUNET_strdup ("");
187
188 return ret;
189}
190
191
192/**
193 * Update progress meter (increment by one).
194 *
195 * @param meter the meter to update and print info for
196 *
197 * @return GNUNET_YES if called the total requested,
198 * GNUNET_NO if more items expected
199 */
200static int
201update_meter (struct ProgressMeter *meter)
202{
203 if (meter->print == GNUNET_YES)
204 {
205 if (meter->completed % meter->modnum == 0)
206 {
207 if (meter->completed == 0)
208 {
209 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
210 }
211 else
212 fprintf (stdout, "%d%%",
213 (int) (((float) meter->completed / meter->total) * 100));
214 }
215 else if (meter->completed % meter->dotnum == 0)
216 fprintf (stdout, "%s", ".");
217
218 if (meter->completed + 1 == meter->total)
219 fprintf (stdout, "%d%%]\n", 100);
220 fflush (stdout);
221 }
222 meter->completed++;
223
224 if (meter->completed == meter->total)
225 return GNUNET_YES;
226 if (meter->completed > meter->total)
227 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
228 return GNUNET_NO;
229}
230
231
232/**
233 * Reset progress meter.
234 *
235 * @param meter the meter to reset
236 *
237 * @return #GNUNET_YES if meter reset,
238 * #GNUNET_SYSERR on error
239 */
240static int
241reset_meter (struct ProgressMeter *meter)
242{
243 if (meter == NULL)
244 return GNUNET_SYSERR;
245
246 meter->completed = 0;
247 return GNUNET_YES;
248}
249
250
251/**
252 * Release resources for meter
253 *
254 * @param meter the meter to free
255 */
256static void
257free_meter (struct ProgressMeter *meter)
258{
259 GNUNET_free (meter->startup_string);
260 GNUNET_free (meter);
261}
262
263
264/**
265 * Shutdown task.
266 *
267 * @param cls NULL
268 */
269static void
270do_shutdown (void *cls)
271{
272 if (NULL != mysql_ctx)
273 {
274 GNUNET_MYSQL_context_destroy (mysql_ctx);
275 mysql_ctx = NULL;
276 }
277 if (NULL != meter)
278 {
279 free_meter (meter);
280 meter = NULL;
281 }
282}
283
284
285/**
286 * Abort task to run on test timed out.
287 *
288 * FIXME: this doesn't actually work, it used to cancel
289 * the already running 'scan_task', but now that should
290 * always be NULL and do nothing. We instead need to set
291 * a global variable and abort scan_task internally, not
292 * via scheduler.
293 *
294 * @param cls NULL
295 */
296static void
297do_abort (void *cls)
298{
299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
300 if (NULL != scan_task)
301 {
302 GNUNET_SCHEDULER_cancel (scan_task);
303 scan_task = NULL;
304 }
305 result = GNUNET_SYSERR;
306 GNUNET_SCHEDULER_shutdown ();
307}
308
309
310/**
311 * Iterator over all states that inserts each state into the MySQL db.
312 *
313 * @param cls closure.
314 * @param key hash for current state.
315 * @param proof proof for current state.
316 * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
317 * @param num_edges number of edges leaving current state.
318 * @param edges edges leaving current state.
319 */
320static void
321regex_iterator (void *cls,
322 const struct GNUNET_HashCode *key,
323 const char *proof,
324 int accepting,
325 unsigned int num_edges,
326 const struct REGEX_BLOCK_Edge *edges)
327{
328 unsigned int i;
329 int result;
330
331 uint32_t iaccepting = (uint32_t) accepting;
332 uint64_t total;
333
334 GNUNET_assert (NULL != mysql_ctx);
335
336 for (i = 0; i < num_edges; i++)
337 {
338 struct GNUNET_MY_QueryParam params_select[] = {
339 GNUNET_MY_query_param_auto_from_type (key),
340 GNUNET_MY_query_param_string (edges[i].label),
341 GNUNET_MY_query_param_end
342 };
343
344 struct GNUNET_MY_ResultSpec results_select[] = {
345 GNUNET_MY_result_spec_uint64 (&total),
346 GNUNET_MY_result_spec_end
347 };
348
349 result =
350 GNUNET_MY_exec_prepared (mysql_ctx,
351 select_stmt_handle,
352 params_select);
353
354 if (GNUNET_SYSERR == result)
355 {
356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357 "Error executing prepared mysql select statement\n");
358 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
359 return;
360 }
361
362 result =
363 GNUNET_MY_extract_result (select_stmt_handle,
364 results_select);
365
366 if (GNUNET_SYSERR == result)
367 {
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 "Error extracting result mysql select statement\n");
370 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
371 return;
372 }
373
374 if ((-1 != total) && (total > 0) )
375 {
376 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n",
377 (unsigned long long) total,
378 GNUNET_h2s (key), edges[i].label);
379 }
380
381 struct GNUNET_MY_QueryParam params_stmt[] = {
382 GNUNET_MY_query_param_auto_from_type (&key),
383 GNUNET_MY_query_param_string (edges[i].label),
384 GNUNET_MY_query_param_auto_from_type (&edges[i].destination),
385 GNUNET_MY_query_param_uint32 (&iaccepting),
386 GNUNET_MY_query_param_end
387 };
388
389 result =
390 GNUNET_MY_exec_prepared (mysql_ctx,
391 stmt_handle,
392 params_stmt);
393
394 if (0 == result)
395 {
396 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
397 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
398
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n",
400 key_str,
401 edges[i].label,
402 to_key_str,
403 accepting);
404
405 GNUNET_free (key_str);
406 GNUNET_free (to_key_str);
407 num_merged_transitions++;
408 }
409 else if (-1 != total)
410 {
411 num_merged_states++;
412 }
413
414 if ((GNUNET_SYSERR == result) || ((1 != result) && (0 != result) ))
415 {
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
418 result);
419 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
420 }
421 }
422
423 if (0 == num_edges)
424 {
425 struct GNUNET_MY_QueryParam params_stmt[] = {
426 GNUNET_MY_query_param_auto_from_type (key),
427 GNUNET_MY_query_param_string (""),
428 GNUNET_MY_query_param_fixed_size (NULL, 0),
429 GNUNET_MY_query_param_uint32 (&iaccepting),
430 GNUNET_MY_query_param_end
431 };
432
433 result =
434 GNUNET_MY_exec_prepared (mysql_ctx,
435 stmt_handle,
436 params_stmt);
437
438 if ((1 != result) && (0 != result) )
439 {
440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
442 result);
443 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
444 }
445 }
446}
447
448
449/**
450 * Announce a regex by creating the DFA and iterating over each state, inserting
451 * each state into a MySQL database.
452 *
453 * @param regex regular expression.
454 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
455 */
456static int
457announce_regex (const char *regex)
458{
459 struct REGEX_INTERNAL_Automaton *dfa;
460
461 dfa =
462 REGEX_INTERNAL_construct_dfa (regex,
463 strlen (regex),
464 max_path_compression);
465
466 if (NULL == dfa)
467 {
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469 "Failed to create DFA for regex %s\n",
470 regex);
471 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
472 return GNUNET_SYSERR;
473 }
474 REGEX_INTERNAL_iterate_all_edges (dfa,
475 &regex_iterator, NULL);
476 REGEX_INTERNAL_automaton_destroy (dfa);
477
478 return GNUNET_OK;
479}
480
481
482/**
483 * Function called with a filename.
484 *
485 * @param cls closure
486 * @param filename complete filename (absolute path)
487 * @return #GNUNET_OK to continue to iterate,
488 * #GNUNET_SYSERR to abort iteration with error!
489 */
490static int
491policy_filename_cb (void *cls, const char *filename)
492{
493 char *regex;
494 char *data;
495 char *buf;
496 uint64_t filesize;
497 unsigned int offset;
498
499 GNUNET_assert (NULL != filename);
500
501 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
502 "Announcing regexes from file %s\n",
503 filename);
504
505 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
506 {
507 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
508 "Could not find policy file %s\n",
509 filename);
510 return GNUNET_OK;
511 }
512 if (GNUNET_OK !=
513 GNUNET_DISK_file_size (filename, &filesize,
514 GNUNET_YES, GNUNET_YES))
515 filesize = 0;
516 if (0 == filesize)
517 {
518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
519 filename);
520 return GNUNET_OK;
521 }
522 data = GNUNET_malloc (filesize);
523 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
524 {
525 GNUNET_free (data);
526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
527 "Could not read policy file %s.\n",
528 filename);
529 return GNUNET_OK;
530 }
531
532 update_meter (meter);
533
534 buf = data;
535 offset = 0;
536 regex = NULL;
537 while (offset < (filesize - 1))
538 {
539 offset++;
540 if (((data[offset] == '\n')) && (buf != &data[offset]))
541 {
542 data[offset] = '|';
543 num_policies++;
544 buf = &data[offset + 1];
545 }
546 else if ((data[offset] == '\n') || (data[offset] == '\0'))
547 buf = &data[offset + 1];
548 }
549 data[offset] = '\0';
550 GNUNET_asprintf (&regex, "%s(%s)", regex_prefix, data);
551 GNUNET_assert (NULL != regex);
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Announcing regex: %s\n", regex);
554
555 if (GNUNET_OK != announce_regex (regex))
556 {
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 "Could not announce regex %s\n",
559 regex);
560 }
561 GNUNET_free (regex);
562 GNUNET_free (data);
563 return GNUNET_OK;
564}
565
566
567/**
568 * Iterate over files contained in policy_dir.
569 *
570 * @param cls NULL
571 */
572static void
573do_directory_scan (void *cls)
574{
575 struct GNUNET_TIME_Absolute start_time;
576 struct GNUNET_TIME_Relative duration;
577 char *stmt;
578
579 /* Create an MySQL prepared statement for the inserts */
580 scan_task = NULL;
581 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
582 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
583 GNUNET_free (stmt);
584
585 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
586 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
587 GNUNET_free (stmt);
588
589 GNUNET_assert (NULL != stmt_handle);
590
591 meter = create_meter (num_policy_files,
592 "Announcing policy files\n",
593 GNUNET_YES);
594 start_time = GNUNET_TIME_absolute_get ();
595 GNUNET_DISK_directory_scan (policy_dir,
596 &policy_filename_cb,
597 stmt_handle);
598 duration = GNUNET_TIME_absolute_get_duration (start_time);
599 reset_meter (meter);
600 free_meter (meter);
601 meter = NULL;
602
603 printf ("Announced %u files containing %u policies in %s\n"
604 "Duplicate transitions: %llu\nMerged states: %llu\n",
605 num_policy_files,
606 num_policies,
607 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
608 num_merged_transitions,
609 num_merged_states);
610 result = GNUNET_OK;
611 GNUNET_SCHEDULER_shutdown ();
612}
613
614
615/**
616 * Main function that will be run by the scheduler.
617 *
618 * @param cls closure
619 * @param args remaining command-line arguments
620 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
621 * @param config configuration
622 */
623static void
624run (void *cls,
625 char *const *args,
626 const char *cfgfile,
627 const struct GNUNET_CONFIGURATION_Handle *config)
628{
629 if (NULL == args[0])
630 {
631 fprintf (stderr,
632 _ ("No policy directory specified on command line. Exiting.\n"));
633 result = GNUNET_SYSERR;
634 return;
635 }
636 if (GNUNET_YES !=
637 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
638 {
639 fprintf (stderr,
640 _ ("Specified policies directory does not exist. Exiting.\n"));
641 result = GNUNET_SYSERR;
642 return;
643 }
644 policy_dir = args[0];
645
646 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
647 NULL, NULL);
648 meter = NULL;
649
650 if (NULL == table_name)
651 {
652 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
653 "No table name specified, using default \"NFA\".\n");
654 table_name = "NFA";
655 }
656
657 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
658 if (NULL == mysql_ctx)
659 {
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661 "Failed to create mysql context\n");
662 result = GNUNET_SYSERR;
663 return;
664 }
665
666 if (GNUNET_OK !=
667 GNUNET_CONFIGURATION_get_value_string (config,
668 "regex-mysql",
669 "REGEX_PREFIX",
670 &regex_prefix))
671 {
672 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
673 "regex-mysql",
674 "REGEX_PREFIX");
675 result = GNUNET_SYSERR;
676 return;
677 }
678
679 result = GNUNET_OK;
680 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
681 NULL);
682 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
683}
684
685
686/**
687 * Main function.
688 *
689 * @param argc argument count
690 * @param argv argument values
691 * @return 0 on success
692 */
693int
694main (int argc, char *const *argv)
695{
696 struct GNUNET_GETOPT_CommandLineOption options[] = {
697 GNUNET_GETOPT_option_string ('t',
698 "table",
699 "TABLENAME",
700 gettext_noop (
701 "name of the table to write DFAs"),
702 &table_name),
703
704 GNUNET_GETOPT_option_uint ('p',
705 "max-path-compression",
706 "MAX_PATH_COMPRESSION",
707 gettext_noop ("maximum path compression length"),
708 &max_path_compression),
709
710 GNUNET_GETOPT_OPTION_END
711 };
712 int ret;
713
714 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
715 return 2;
716
717 result = GNUNET_SYSERR;
718 ret =
719 GNUNET_PROGRAM_run (argc, argv,
720 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
721 _ ("Profiler for regex library"), options, &run, NULL);
722 if (GNUNET_OK != ret)
723 return ret;
724 if (GNUNET_OK != result)
725 return 1;
726 return 0;
727}