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_string (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",
374 (unsigned long long)total,
375 GNUNET_h2s (key), edges[i].label);
378 struct GNUNET_MY_QueryParam params_stmt[] = {
379 GNUNET_MY_query_param_auto_from_type (&key),
380 GNUNET_MY_query_param_string (edges[i].label),
381 GNUNET_MY_query_param_auto_from_type (&edges[i].destination),
382 GNUNET_MY_query_param_uint32 (&iaccepting),
383 GNUNET_MY_query_param_end
387 GNUNET_MY_exec_prepared (mysql_ctx,
393 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
394 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n",
402 GNUNET_free (key_str);
403 GNUNET_free (to_key_str);
404 num_merged_transitions++;
406 else if (-1 != total)
411 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
416 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
422 struct GNUNET_MY_QueryParam params_stmt[] = {
423 GNUNET_MY_query_param_auto_from_type (key),
424 GNUNET_MY_query_param_string (""),
425 GNUNET_MY_query_param_auto_from_type (NULL),
426 GNUNET_MY_query_param_uint32 (&iaccepting),
427 GNUNET_MY_query_param_end
431 GNUNET_MY_exec_prepared(mysql_ctx,
435 if (1 != result && 0 != result)
437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
438 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
440 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
447 * Announce a regex by creating the DFA and iterating over each state, inserting
448 * each state into a MySQL database.
450 * @param regex regular expression.
451 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
454 announce_regex (const char *regex)
456 struct REGEX_INTERNAL_Automaton *dfa;
459 REGEX_INTERNAL_construct_dfa (regex,
461 max_path_compression);
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 "Failed to create DFA for regex %s\n",
468 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
469 return GNUNET_SYSERR;
471 REGEX_INTERNAL_iterate_all_edges (dfa,
472 ®ex_iterator, NULL);
473 REGEX_INTERNAL_automaton_destroy (dfa);
480 * Function called with a filename.
483 * @param filename complete filename (absolute path)
484 * @return #GNUNET_OK to continue to iterate,
485 * #GNUNET_SYSERR to abort iteration with error!
488 policy_filename_cb (void *cls, const char *filename)
496 GNUNET_assert (NULL != filename);
498 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
499 "Announcing regexes from file %s\n",
502 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
504 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
505 "Could not find policy file %s\n",
510 GNUNET_DISK_file_size (filename, &filesize,
511 GNUNET_YES, GNUNET_YES))
515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
519 data = GNUNET_malloc (filesize);
520 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
524 "Could not read policy file %s.\n",
529 update_meter (meter);
534 while (offset < (filesize - 1))
537 if (((data[offset] == '\n')) && (buf != &data[offset]))
541 buf = &data[offset + 1];
543 else if ((data[offset] == '\n') || (data[offset] == '\0'))
544 buf = &data[offset + 1];
547 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
548 GNUNET_assert (NULL != regex);
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Announcing regex: %s\n", regex);
552 if (GNUNET_OK != announce_regex (regex))
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 "Could not announce regex %s\n",
565 * Iterate over files contained in policy_dir.
570 do_directory_scan (void *cls)
572 struct GNUNET_TIME_Absolute start_time;
573 struct GNUNET_TIME_Relative duration;
576 /* Create an MySQL prepared statement for the inserts */
578 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
579 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
582 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
583 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
586 GNUNET_assert (NULL != stmt_handle);
588 meter = create_meter (num_policy_files,
589 "Announcing policy files\n",
591 start_time = GNUNET_TIME_absolute_get ();
592 GNUNET_DISK_directory_scan (policy_dir,
595 duration = GNUNET_TIME_absolute_get_duration (start_time);
600 printf ("Announced %u files containing %u policies in %s\n"
601 "Duplicate transitions: %llu\nMerged states: %llu\n",
604 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
605 num_merged_transitions,
608 GNUNET_SCHEDULER_shutdown ();
613 * Main function that will be run by the scheduler.
616 * @param args remaining command-line arguments
617 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
618 * @param config configuration
624 const struct GNUNET_CONFIGURATION_Handle *config)
629 _("No policy directory specified on command line. Exiting.\n"));
630 result = GNUNET_SYSERR;
634 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
637 _("Specified policies directory does not exist. Exiting.\n"));
638 result = GNUNET_SYSERR;
641 policy_dir = args[0];
643 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
647 if (NULL == table_name)
649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
650 "No table name specified, using default \"NFA\".\n");
654 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
655 if (NULL == mysql_ctx)
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 "Failed to create mysql context\n");
659 result = GNUNET_SYSERR;
664 GNUNET_CONFIGURATION_get_value_string (config,
669 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
672 result = GNUNET_SYSERR;
677 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
679 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
686 * @param argc argument count
687 * @param argv argument values
688 * @return 0 on success
691 main (int argc, char *const *argv)
693 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
694 {'t', "table", "TABLENAME",
695 gettext_noop ("name of the table to write DFAs"),
696 1, &GNUNET_GETOPT_set_string, &table_name},
697 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
698 gettext_noop ("maximum path compression length"),
699 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
700 GNUNET_GETOPT_OPTION_END
704 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
707 result = GNUNET_SYSERR;
709 GNUNET_PROGRAM_run (argc, argv,
710 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
711 _("Profiler for regex library"), options, &run, NULL);
712 if (GNUNET_OK != ret)
714 if (GNUNET_OK != result)