2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
32 #include "gnunet_util_lib.h"
33 #include "regex_internal_lib.h"
34 #include "gnunet_mysql_lib.h"
35 #include "gnunet_my_lib.h"
36 #include <mysql/mysql.h>
39 * MySQL statement to insert an edge.
41 #define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` " \
42 "(`key`, `label`, `to_key`, `accepting`) " \
43 "VALUES (?, ?, ?, ?);"
46 * MySQL statement to select a key count.
48 #define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` " \
49 "WHERE `key` = ? AND `label` = ?;"
52 * Simple struct to keep track of progress, and print a
53 * nice little percentage meter for long running tasks.
58 * Total number of elements.
63 * Intervall for printing percentage.
68 * Number of dots to print.
75 unsigned int completed;
78 * Should the meter be printed?
83 * String to print on startup.
90 * Handle for the progress meter
92 static struct ProgressMeter *meter;
95 * Scan task identifier;
97 static struct GNUNET_SCHEDULER_Task *scan_task;
100 * Global testing status.
107 static struct GNUNET_MYSQL_Context *mysql_ctx;
110 * MySQL prepared statement handle.
112 static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
115 * MySQL prepared statement handle for `key` select.
117 static struct GNUNET_MYSQL_StatementHandle *select_stmt_handle;
122 static char *table_name;
125 * Policy dir containing files that contain policies.
127 static char *policy_dir;
130 * Number of policy files.
132 static unsigned int num_policy_files;
135 * Number of policies.
137 static unsigned int num_policies;
140 * Maximal path compression length.
142 static unsigned int max_path_compression;
145 * Number of merged transitions.
147 static unsigned long long num_merged_transitions;
150 * Number of merged states from different policies.
152 static unsigned long long num_merged_states;
155 * Prefix to add before every regex we're announcing.
157 static char *regex_prefix;
161 * Create a meter to keep track of the progress of some task.
163 * @param total the total number of items to complete
164 * @param start_string a string to prefix the meter with (if printing)
165 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
168 * @return the progress meter
170 static struct ProgressMeter *
171 create_meter (unsigned int total, char *start_string, int print)
173 struct ProgressMeter *ret;
175 ret = GNUNET_new (struct ProgressMeter);
178 ret->modnum = total / 4;
179 if (ret->modnum == 0) /* Divide by zero check */
181 ret->dotnum = (total / 50) + 1;
182 if (start_string != NULL)
183 ret->startup_string = GNUNET_strdup (start_string);
185 ret->startup_string = GNUNET_strdup ("");
192 * Update progress meter (increment by one).
194 * @param meter the meter to update and print info for
196 * @return GNUNET_YES if called the total requested,
197 * GNUNET_NO if more items expected
200 update_meter (struct ProgressMeter *meter)
202 if (meter->print == GNUNET_YES)
204 if (meter->completed % meter->modnum == 0)
206 if (meter->completed == 0)
208 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
211 fprintf (stdout, "%d%%",
212 (int) (((float) meter->completed / meter->total) * 100));
214 else if (meter->completed % meter->dotnum == 0)
215 fprintf (stdout, "%s", ".");
217 if (meter->completed + 1 == meter->total)
218 fprintf (stdout, "%d%%]\n", 100);
223 if (meter->completed == meter->total)
225 if (meter->completed > meter->total)
226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
232 * Reset progress meter.
234 * @param meter the meter to reset
236 * @return #GNUNET_YES if meter reset,
237 * #GNUNET_SYSERR on error
240 reset_meter (struct ProgressMeter *meter)
243 return GNUNET_SYSERR;
245 meter->completed = 0;
251 * Release resources for meter
253 * @param meter the meter to free
256 free_meter (struct ProgressMeter *meter)
258 GNUNET_free_non_null (meter->startup_string);
269 do_shutdown (void *cls)
271 if (NULL != mysql_ctx)
273 GNUNET_MYSQL_context_destroy (mysql_ctx);
285 * Abort task to run on test timed out.
287 * FIXME: this doesn't actually work, it used to cancel
288 * the already running 'scan_task', but now that should
289 * always be NULL and do nothing. We instead need to set
290 * a global variable and abort scan_task internally, not
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
299 if (NULL != scan_task)
301 GNUNET_SCHEDULER_cancel (scan_task);
304 result = GNUNET_SYSERR;
305 GNUNET_SCHEDULER_shutdown ();
310 * Iterator over all states that inserts each state into the MySQL db.
312 * @param cls closure.
313 * @param key hash for current state.
314 * @param proof proof for current state.
315 * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
316 * @param num_edges number of edges leaving current state.
317 * @param edges edges leaving current state.
320 regex_iterator (void *cls,
321 const struct GNUNET_HashCode *key,
324 unsigned int num_edges,
325 const struct REGEX_BLOCK_Edge *edges)
330 uint32_t iaccepting = (uint32_t) accepting;
333 GNUNET_assert (NULL != mysql_ctx);
335 for (i = 0; i < num_edges; i++)
337 struct GNUNET_MY_QueryParam params_select[] = {
338 GNUNET_MY_query_param_auto_from_type (key),
339 GNUNET_MY_query_param_string (edges[i].label),
340 GNUNET_MY_query_param_end
343 struct GNUNET_MY_ResultSpec results_select[] = {
344 GNUNET_MY_result_spec_uint64 (&total),
345 GNUNET_MY_result_spec_end
349 GNUNET_MY_exec_prepared (mysql_ctx,
353 if (GNUNET_SYSERR == result)
355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
356 "Error executing prepared mysql select statement\n");
357 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
362 GNUNET_MY_extract_result (select_stmt_handle,
365 if (GNUNET_SYSERR == result)
367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
368 "Error extracting result mysql select statement\n");
369 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
373 if ((-1 != total) && (total > 0) )
375 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n",
376 (unsigned long long) total,
377 GNUNET_h2s (key), edges[i].label);
380 struct GNUNET_MY_QueryParam params_stmt[] = {
381 GNUNET_MY_query_param_auto_from_type (&key),
382 GNUNET_MY_query_param_string (edges[i].label),
383 GNUNET_MY_query_param_auto_from_type (&edges[i].destination),
384 GNUNET_MY_query_param_uint32 (&iaccepting),
385 GNUNET_MY_query_param_end
389 GNUNET_MY_exec_prepared (mysql_ctx,
395 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
396 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n",
404 GNUNET_free (key_str);
405 GNUNET_free (to_key_str);
406 num_merged_transitions++;
408 else if (-1 != total)
413 if ((GNUNET_SYSERR == result) || ((1 != result) && (0 != result) ))
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
418 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
424 struct GNUNET_MY_QueryParam params_stmt[] = {
425 GNUNET_MY_query_param_auto_from_type (key),
426 GNUNET_MY_query_param_string (""),
427 GNUNET_MY_query_param_fixed_size (NULL, 0),
428 GNUNET_MY_query_param_uint32 (&iaccepting),
429 GNUNET_MY_query_param_end
433 GNUNET_MY_exec_prepared (mysql_ctx,
437 if ((1 != result) && (0 != result) )
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
442 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
449 * Announce a regex by creating the DFA and iterating over each state, inserting
450 * each state into a MySQL database.
452 * @param regex regular expression.
453 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
456 announce_regex (const char *regex)
458 struct REGEX_INTERNAL_Automaton *dfa;
461 REGEX_INTERNAL_construct_dfa (regex,
463 max_path_compression);
467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
468 "Failed to create DFA for regex %s\n",
470 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
471 return GNUNET_SYSERR;
473 REGEX_INTERNAL_iterate_all_edges (dfa,
474 ®ex_iterator, NULL);
475 REGEX_INTERNAL_automaton_destroy (dfa);
482 * Function called with a filename.
485 * @param filename complete filename (absolute path)
486 * @return #GNUNET_OK to continue to iterate,
487 * #GNUNET_SYSERR to abort iteration with error!
490 policy_filename_cb (void *cls, const char *filename)
498 GNUNET_assert (NULL != filename);
500 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
501 "Announcing regexes from file %s\n",
504 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
507 "Could not find policy file %s\n",
512 GNUNET_DISK_file_size (filename, &filesize,
513 GNUNET_YES, GNUNET_YES))
517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
521 data = GNUNET_malloc (filesize);
522 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
526 "Could not read policy file %s.\n",
531 update_meter (meter);
536 while (offset < (filesize - 1))
539 if (((data[offset] == '\n')) && (buf != &data[offset]))
543 buf = &data[offset + 1];
545 else if ((data[offset] == '\n') || (data[offset] == '\0'))
546 buf = &data[offset + 1];
549 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
550 GNUNET_assert (NULL != regex);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Announcing regex: %s\n", regex);
554 if (GNUNET_OK != announce_regex (regex))
556 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
557 "Could not announce regex %s\n",
567 * Iterate over files contained in policy_dir.
572 do_directory_scan (void *cls)
574 struct GNUNET_TIME_Absolute start_time;
575 struct GNUNET_TIME_Relative duration;
578 /* Create an MySQL prepared statement for the inserts */
580 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
581 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
584 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
585 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
588 GNUNET_assert (NULL != stmt_handle);
590 meter = create_meter (num_policy_files,
591 "Announcing policy files\n",
593 start_time = GNUNET_TIME_absolute_get ();
594 GNUNET_DISK_directory_scan (policy_dir,
597 duration = GNUNET_TIME_absolute_get_duration (start_time);
602 printf ("Announced %u files containing %u policies in %s\n"
603 "Duplicate transitions: %llu\nMerged states: %llu\n",
606 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
607 num_merged_transitions,
610 GNUNET_SCHEDULER_shutdown ();
615 * Main function that will be run by the scheduler.
618 * @param args remaining command-line arguments
619 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
620 * @param config configuration
626 const struct GNUNET_CONFIGURATION_Handle *config)
631 _ ("No policy directory specified on command line. Exiting.\n"));
632 result = GNUNET_SYSERR;
636 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
639 _ ("Specified policies directory does not exist. Exiting.\n"));
640 result = GNUNET_SYSERR;
643 policy_dir = args[0];
645 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
649 if (NULL == table_name)
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
652 "No table name specified, using default \"NFA\".\n");
656 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
657 if (NULL == mysql_ctx)
659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
660 "Failed to create mysql context\n");
661 result = GNUNET_SYSERR;
666 GNUNET_CONFIGURATION_get_value_string (config,
671 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
674 result = GNUNET_SYSERR;
679 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
681 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
688 * @param argc argument count
689 * @param argv argument values
690 * @return 0 on success
693 main (int argc, char *const *argv)
695 struct GNUNET_GETOPT_CommandLineOption options[] = {
696 GNUNET_GETOPT_option_string ('t',
700 "name of the table to write DFAs"),
703 GNUNET_GETOPT_option_uint ('p',
704 "max-path-compression",
705 "MAX_PATH_COMPRESSION",
706 gettext_noop ("maximum path compression length"),
707 &max_path_compression),
709 GNUNET_GETOPT_OPTION_END
713 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
716 result = GNUNET_SYSERR;
718 GNUNET_PROGRAM_run (argc, argv,
719 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
720 _ ("Profiler for regex library"), options, &run, NULL);
721 if (GNUNET_OK != ret)
723 if (GNUNET_OK != result)