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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file regex/gnunet-regex-simulation-profiler.c
23 * @brief Regex profiler that dumps all DFAs into a database instead of
24 * using the DHT (with cadet).
25 * @author Maximilian Szengel
26 * @author Christophe Genevey
31 #include "gnunet_util_lib.h"
32 #include "regex_internal_lib.h"
33 #include "gnunet_mysql_lib.h"
34 #include "gnunet_my_lib.h"
35 #include <mysql/mysql.h>
38 * MySQL statement to insert an edge.
40 #define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` "\
41 "(`key`, `label`, `to_key`, `accepting`) "\
42 "VALUES (?, ?, ?, ?);"
45 * MySQL statement to select a key count.
47 #define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` "\
48 "WHERE `key` = ? AND `label` = ?;"
51 * Simple struct to keep track of progress, and print a
52 * nice little percentage meter for long running tasks.
57 * Total number of elements.
62 * Intervall for printing percentage.
67 * Number of dots to print.
74 unsigned int completed;
77 * Should the meter be printed?
82 * String to print on startup.
89 * Handle for the progress meter
91 static struct ProgressMeter *meter;
94 * Scan task identifier;
96 static struct GNUNET_SCHEDULER_Task *scan_task;
99 * Global testing status.
106 static struct GNUNET_MYSQL_Context *mysql_ctx;
109 * MySQL prepared statement handle.
111 static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
114 * MySQL prepared statement handle for `key` select.
116 static struct GNUNET_MYSQL_StatementHandle *select_stmt_handle;
121 static char *table_name;
124 * Policy dir containing files that contain policies.
126 static char *policy_dir;
129 * Number of policy files.
131 static unsigned int num_policy_files;
134 * Number of policies.
136 static unsigned int num_policies;
139 * Maximal path compression length.
141 static unsigned int max_path_compression;
144 * Number of merged transitions.
146 static unsigned long long num_merged_transitions;
149 * Number of merged states from different policies.
151 static unsigned long long num_merged_states;
154 * Prefix to add before every regex we're announcing.
156 static char *regex_prefix;
160 * Create a meter to keep track of the progress of some task.
162 * @param total the total number of items to complete
163 * @param start_string a string to prefix the meter with (if printing)
164 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
167 * @return the progress meter
169 static struct ProgressMeter *
170 create_meter (unsigned int total, char *start_string, int print)
172 struct ProgressMeter *ret;
174 ret = GNUNET_new (struct ProgressMeter);
177 ret->modnum = total / 4;
178 if (ret->modnum == 0) /* Divide by zero check */
180 ret->dotnum = (total / 50) + 1;
181 if (start_string != NULL)
182 ret->startup_string = GNUNET_strdup (start_string);
184 ret->startup_string = GNUNET_strdup ("");
191 * Update progress meter (increment by one).
193 * @param meter the meter to update and print info for
195 * @return GNUNET_YES if called the total requested,
196 * GNUNET_NO if more items expected
199 update_meter (struct ProgressMeter *meter)
201 if (meter->print == GNUNET_YES)
203 if (meter->completed % meter->modnum == 0)
205 if (meter->completed == 0)
207 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
210 FPRINTF (stdout, "%d%%",
211 (int) (((float) meter->completed / meter->total) * 100));
213 else if (meter->completed % meter->dotnum == 0)
214 FPRINTF (stdout, "%s", ".");
216 if (meter->completed + 1 == meter->total)
217 FPRINTF (stdout, "%d%%]\n", 100);
222 if (meter->completed == meter->total)
224 if (meter->completed > meter->total)
225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
231 * Reset progress meter.
233 * @param meter the meter to reset
235 * @return #GNUNET_YES if meter reset,
236 * #GNUNET_SYSERR on error
239 reset_meter (struct ProgressMeter *meter)
242 return GNUNET_SYSERR;
244 meter->completed = 0;
250 * Release resources for meter
252 * @param meter the meter to free
255 free_meter (struct ProgressMeter *meter)
257 GNUNET_free_non_null (meter->startup_string);
268 do_shutdown (void *cls)
270 if (NULL != mysql_ctx)
272 GNUNET_MYSQL_context_destroy (mysql_ctx);
284 * Abort task to run on test timed out.
286 * FIXME: this doesn't actually work, it used to cancel
287 * the already running 'scan_task', but now that should
288 * always be NULL and do nothing. We instead need to set
289 * a global variable and abort scan_task internally, not
297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
298 if (NULL != scan_task)
300 GNUNET_SCHEDULER_cancel (scan_task);
303 result = GNUNET_SYSERR;
304 GNUNET_SCHEDULER_shutdown ();
308 * Iterator over all states that inserts each state into the MySQL db.
310 * @param cls closure.
311 * @param key hash for current state.
312 * @param proof proof for current state.
313 * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
314 * @param num_edges number of edges leaving current state.
315 * @param edges edges leaving current state.
318 regex_iterator (void *cls,
319 const struct GNUNET_HashCode *key,
322 unsigned int num_edges,
323 const struct REGEX_BLOCK_Edge *edges)
328 uint32_t iaccepting = (uint32_t)accepting;
331 GNUNET_assert (NULL != mysql_ctx);
333 for (i = 0; i < num_edges; i++)
335 struct GNUNET_MY_QueryParam params_select[] = {
336 GNUNET_MY_query_param_auto_from_type (key),
337 GNUNET_MY_query_param_fixed_size(edges[i].label, strlen (edges[i].label)),
338 GNUNET_MY_query_param_end
341 struct GNUNET_MY_ResultSpec results_select[] = {
342 GNUNET_MY_result_spec_uint64 (&total),
343 GNUNET_MY_result_spec_end
347 GNUNET_MY_exec_prepared (mysql_ctx,
351 if (GNUNET_SYSERR == result)
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354 "Error executing prepared mysql select statement\n");
355 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
360 GNUNET_MY_extract_result (select_stmt_handle,
363 if (GNUNET_SYSERR == result)
365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
366 "Error extracting result mysql select statement\n");
367 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
371 if (-1 != total && total > 0)
373 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", (unsigned long long)total,
374 GNUNET_h2s (key), edges[i].label);
377 struct GNUNET_MY_QueryParam params_stmt[] = {
378 GNUNET_MY_query_param_auto_from_type (&key),
379 GNUNET_MY_query_param_fixed_size (edges[i].label, strlen (edges[i].label)),
380 GNUNET_MY_query_param_auto_from_type (&edges[i].destination),
381 GNUNET_MY_query_param_uint32 (&iaccepting),
382 GNUNET_MY_query_param_end
386 GNUNET_MY_exec_prepared (mysql_ctx,
392 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
393 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", key_str,
396 edges[i].label, to_key_str, accepting);
397 GNUNET_free (key_str);
398 GNUNET_free (to_key_str);
399 num_merged_transitions++;
401 else if (-1 != total)
406 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
411 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
417 struct GNUNET_MY_QueryParam params_stmt[] = {
418 GNUNET_MY_query_param_auto_from_type (key),
419 GNUNET_MY_query_param_fixed_size (NULL, 0),
420 GNUNET_MY_query_param_auto_from_type (NULL),
421 GNUNET_MY_query_param_uint32 (&iaccepting),
422 GNUNET_MY_query_param_end
426 GNUNET_MY_exec_prepared(mysql_ctx,
430 if (1 != result && 0 != result)
432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
433 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
435 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
442 * Announce a regex by creating the DFA and iterating over each state, inserting
443 * each state into a MySQL database.
445 * @param regex regular expression.
446 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
449 announce_regex (const char *regex)
451 struct REGEX_INTERNAL_Automaton *dfa;
454 REGEX_INTERNAL_construct_dfa (regex,
456 max_path_compression);
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461 "Failed to create DFA for regex %s\n",
463 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
464 return GNUNET_SYSERR;
466 REGEX_INTERNAL_iterate_all_edges (dfa,
467 ®ex_iterator, NULL);
468 REGEX_INTERNAL_automaton_destroy (dfa);
475 * Function called with a filename.
478 * @param filename complete filename (absolute path)
479 * @return #GNUNET_OK to continue to iterate,
480 * #GNUNET_SYSERR to abort iteration with error!
483 policy_filename_cb (void *cls, const char *filename)
491 GNUNET_assert (NULL != filename);
493 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
494 "Announcing regexes from file %s\n",
497 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
500 "Could not find policy file %s\n",
505 GNUNET_DISK_file_size (filename, &filesize,
506 GNUNET_YES, GNUNET_YES))
510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
514 data = GNUNET_malloc (filesize);
515 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
519 "Could not read policy file %s.\n",
524 update_meter (meter);
529 while (offset < (filesize - 1))
532 if (((data[offset] == '\n')) && (buf != &data[offset]))
536 buf = &data[offset + 1];
538 else if ((data[offset] == '\n') || (data[offset] == '\0'))
539 buf = &data[offset + 1];
542 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
543 GNUNET_assert (NULL != regex);
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Announcing regex: %s\n", regex);
547 if (GNUNET_OK != announce_regex (regex))
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 "Could not announce regex %s\n",
560 * Iterate over files contained in policy_dir.
565 do_directory_scan (void *cls)
567 struct GNUNET_TIME_Absolute start_time;
568 struct GNUNET_TIME_Relative duration;
571 /* Create an MySQL prepared statement for the inserts */
573 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
574 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
577 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
578 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
581 GNUNET_assert (NULL != stmt_handle);
583 meter = create_meter (num_policy_files,
584 "Announcing policy files\n",
586 start_time = GNUNET_TIME_absolute_get ();
587 GNUNET_DISK_directory_scan (policy_dir,
590 duration = GNUNET_TIME_absolute_get_duration (start_time);
595 printf ("Announced %u files containing %u policies in %s\n"
596 "Duplicate transitions: %llu\nMerged states: %llu\n",
599 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
600 num_merged_transitions,
603 GNUNET_SCHEDULER_shutdown ();
608 * Main function that will be run by the scheduler.
611 * @param args remaining command-line arguments
612 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
613 * @param config configuration
619 const struct GNUNET_CONFIGURATION_Handle *config)
624 _("No policy directory specified on command line. Exiting.\n"));
625 result = GNUNET_SYSERR;
629 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
632 _("Specified policies directory does not exist. Exiting.\n"));
633 result = GNUNET_SYSERR;
636 policy_dir = args[0];
638 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
642 if (NULL == table_name)
644 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
645 "No table name specified, using default \"NFA\".\n");
649 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
650 if (NULL == mysql_ctx)
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "Failed to create mysql context\n");
654 result = GNUNET_SYSERR;
659 GNUNET_CONFIGURATION_get_value_string (config,
664 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
667 result = GNUNET_SYSERR;
672 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
674 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
681 * @param argc argument count
682 * @param argv argument values
683 * @return 0 on success
686 main (int argc, char *const *argv)
688 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
689 {'t', "table", "TABLENAME",
690 gettext_noop ("name of the table to write DFAs"),
691 1, &GNUNET_GETOPT_set_string, &table_name},
692 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
693 gettext_noop ("maximum path compression length"),
694 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
695 GNUNET_GETOPT_OPTION_END
699 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
702 result = GNUNET_SYSERR;
704 GNUNET_PROGRAM_run (argc, argv,
705 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
706 _("Profiler for regex library"), options, &run, NULL);
707 if (GNUNET_OK != ret)
709 if (GNUNET_OK != result)