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/>.
21 * @file regex/gnunet-regex-simulation-profiler.c
22 * @brief Regex profiler that dumps all DFAs into a database instead of
23 * using the DHT (with cadet).
24 * @author Maximilian Szengel
25 * @author Christophe Genevey
30 #include "gnunet_util_lib.h"
31 #include "regex_internal_lib.h"
32 #include "gnunet_mysql_lib.h"
33 #include "gnunet_my_lib.h"
34 #include <mysql/mysql.h>
37 * MySQL statement to insert an edge.
39 #define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` "\
40 "(`key`, `label`, `to_key`, `accepting`) "\
41 "VALUES (?, ?, ?, ?);"
44 * MySQL statement to select a key count.
46 #define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` "\
47 "WHERE `key` = ? AND `label` = ?;"
50 * Simple struct to keep track of progress, and print a
51 * nice little percentage meter for long running tasks.
56 * Total number of elements.
61 * Intervall for printing percentage.
66 * Number of dots to print.
73 unsigned int completed;
76 * Should the meter be printed?
81 * String to print on startup.
88 * Handle for the progress meter
90 static struct ProgressMeter *meter;
93 * Scan task identifier;
95 static struct GNUNET_SCHEDULER_Task *scan_task;
98 * Global testing status.
105 static struct GNUNET_MYSQL_Context *mysql_ctx;
108 * MySQL prepared statement handle.
110 static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
113 * MySQL prepared statement handle for `key` select.
115 static struct GNUNET_MYSQL_StatementHandle *select_stmt_handle;
120 static char *table_name;
123 * Policy dir containing files that contain policies.
125 static char *policy_dir;
128 * Number of policy files.
130 static unsigned int num_policy_files;
133 * Number of policies.
135 static unsigned int num_policies;
138 * Maximal path compression length.
140 static unsigned int max_path_compression;
143 * Number of merged transitions.
145 static unsigned long long num_merged_transitions;
148 * Number of merged states from different policies.
150 static unsigned long long num_merged_states;
153 * Prefix to add before every regex we're announcing.
155 static char *regex_prefix;
159 * Create a meter to keep track of the progress of some task.
161 * @param total the total number of items to complete
162 * @param start_string a string to prefix the meter with (if printing)
163 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
166 * @return the progress meter
168 static struct ProgressMeter *
169 create_meter (unsigned int total, char *start_string, int print)
171 struct ProgressMeter *ret;
173 ret = GNUNET_new (struct ProgressMeter);
176 ret->modnum = total / 4;
177 if (ret->modnum == 0) /* Divide by zero check */
179 ret->dotnum = (total / 50) + 1;
180 if (start_string != NULL)
181 ret->startup_string = GNUNET_strdup (start_string);
183 ret->startup_string = GNUNET_strdup ("");
190 * Update progress meter (increment by one).
192 * @param meter the meter to update and print info for
194 * @return GNUNET_YES if called the total requested,
195 * GNUNET_NO if more items expected
198 update_meter (struct ProgressMeter *meter)
200 if (meter->print == GNUNET_YES)
202 if (meter->completed % meter->modnum == 0)
204 if (meter->completed == 0)
206 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
209 FPRINTF (stdout, "%d%%",
210 (int) (((float) meter->completed / meter->total) * 100));
212 else if (meter->completed % meter->dotnum == 0)
213 FPRINTF (stdout, "%s", ".");
215 if (meter->completed + 1 == meter->total)
216 FPRINTF (stdout, "%d%%]\n", 100);
221 if (meter->completed == meter->total)
223 if (meter->completed > meter->total)
224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
230 * Reset progress meter.
232 * @param meter the meter to reset
234 * @return #GNUNET_YES if meter reset,
235 * #GNUNET_SYSERR on error
238 reset_meter (struct ProgressMeter *meter)
241 return GNUNET_SYSERR;
243 meter->completed = 0;
249 * Release resources for meter
251 * @param meter the meter to free
254 free_meter (struct ProgressMeter *meter)
256 GNUNET_free_non_null (meter->startup_string);
267 do_shutdown (void *cls)
269 if (NULL != mysql_ctx)
271 GNUNET_MYSQL_context_destroy (mysql_ctx);
283 * Abort task to run on test timed out.
285 * FIXME: this doesn't actually work, it used to cancel
286 * the already running 'scan_task', but now that should
287 * always be NULL and do nothing. We instead need to set
288 * a global variable and abort scan_task internally, not
296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
297 if (NULL != scan_task)
299 GNUNET_SCHEDULER_cancel (scan_task);
302 result = GNUNET_SYSERR;
303 GNUNET_SCHEDULER_shutdown ();
307 * Iterator over all states that inserts each state into the MySQL db.
309 * @param cls closure.
310 * @param key hash for current state.
311 * @param proof proof for current state.
312 * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
313 * @param num_edges number of edges leaving current state.
314 * @param edges edges leaving current state.
317 regex_iterator (void *cls,
318 const struct GNUNET_HashCode *key,
321 unsigned int num_edges,
322 const struct REGEX_BLOCK_Edge *edges)
327 uint32_t iaccepting = (uint32_t)accepting;
330 GNUNET_assert (NULL != mysql_ctx);
332 for (i = 0; i < num_edges; i++)
334 struct GNUNET_MY_QueryParam params_select[] = {
335 GNUNET_MY_query_param_auto_from_type (key),
336 GNUNET_MY_query_param_string (edges[i].label),
337 GNUNET_MY_query_param_end
340 struct GNUNET_MY_ResultSpec results_select[] = {
341 GNUNET_MY_result_spec_uint64 (&total),
342 GNUNET_MY_result_spec_end
346 GNUNET_MY_exec_prepared (mysql_ctx,
350 if (GNUNET_SYSERR == result)
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 "Error executing prepared mysql select statement\n");
354 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
359 GNUNET_MY_extract_result (select_stmt_handle,
362 if (GNUNET_SYSERR == result)
364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
365 "Error extracting result mysql select statement\n");
366 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
370 if (-1 != total && total > 0)
372 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n",
373 (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_string (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",
401 GNUNET_free (key_str);
402 GNUNET_free (to_key_str);
403 num_merged_transitions++;
405 else if (-1 != total)
410 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
413 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
415 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
421 struct GNUNET_MY_QueryParam params_stmt[] = {
422 GNUNET_MY_query_param_auto_from_type (key),
423 GNUNET_MY_query_param_string (""),
424 GNUNET_MY_query_param_fixed_size (NULL, 0),
425 GNUNET_MY_query_param_uint32 (&iaccepting),
426 GNUNET_MY_query_param_end
430 GNUNET_MY_exec_prepared (mysql_ctx,
434 if (1 != result && 0 != result)
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
439 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
446 * Announce a regex by creating the DFA and iterating over each state, inserting
447 * each state into a MySQL database.
449 * @param regex regular expression.
450 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
453 announce_regex (const char *regex)
455 struct REGEX_INTERNAL_Automaton *dfa;
458 REGEX_INTERNAL_construct_dfa (regex,
460 max_path_compression);
464 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
465 "Failed to create DFA for regex %s\n",
467 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
468 return GNUNET_SYSERR;
470 REGEX_INTERNAL_iterate_all_edges (dfa,
471 ®ex_iterator, NULL);
472 REGEX_INTERNAL_automaton_destroy (dfa);
479 * Function called with a filename.
482 * @param filename complete filename (absolute path)
483 * @return #GNUNET_OK to continue to iterate,
484 * #GNUNET_SYSERR to abort iteration with error!
487 policy_filename_cb (void *cls, const char *filename)
495 GNUNET_assert (NULL != filename);
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
498 "Announcing regexes from file %s\n",
501 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504 "Could not find policy file %s\n",
509 GNUNET_DISK_file_size (filename, &filesize,
510 GNUNET_YES, GNUNET_YES))
514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
518 data = GNUNET_malloc (filesize);
519 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
523 "Could not read policy file %s.\n",
528 update_meter (meter);
533 while (offset < (filesize - 1))
536 if (((data[offset] == '\n')) && (buf != &data[offset]))
540 buf = &data[offset + 1];
542 else if ((data[offset] == '\n') || (data[offset] == '\0'))
543 buf = &data[offset + 1];
546 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
547 GNUNET_assert (NULL != regex);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Announcing regex: %s\n", regex);
551 if (GNUNET_OK != announce_regex (regex))
553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
554 "Could not announce regex %s\n",
564 * Iterate over files contained in policy_dir.
569 do_directory_scan (void *cls)
571 struct GNUNET_TIME_Absolute start_time;
572 struct GNUNET_TIME_Relative duration;
575 /* Create an MySQL prepared statement for the inserts */
577 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
578 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
581 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
582 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
585 GNUNET_assert (NULL != stmt_handle);
587 meter = create_meter (num_policy_files,
588 "Announcing policy files\n",
590 start_time = GNUNET_TIME_absolute_get ();
591 GNUNET_DISK_directory_scan (policy_dir,
594 duration = GNUNET_TIME_absolute_get_duration (start_time);
599 printf ("Announced %u files containing %u policies in %s\n"
600 "Duplicate transitions: %llu\nMerged states: %llu\n",
603 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
604 num_merged_transitions,
607 GNUNET_SCHEDULER_shutdown ();
612 * Main function that will be run by the scheduler.
615 * @param args remaining command-line arguments
616 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
617 * @param config configuration
623 const struct GNUNET_CONFIGURATION_Handle *config)
628 _("No policy directory specified on command line. Exiting.\n"));
629 result = GNUNET_SYSERR;
633 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
636 _("Specified policies directory does not exist. Exiting.\n"));
637 result = GNUNET_SYSERR;
640 policy_dir = args[0];
642 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
646 if (NULL == table_name)
648 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
649 "No table name specified, using default \"NFA\".\n");
653 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
654 if (NULL == mysql_ctx)
656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
657 "Failed to create mysql context\n");
658 result = GNUNET_SYSERR;
663 GNUNET_CONFIGURATION_get_value_string (config,
668 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
671 result = GNUNET_SYSERR;
676 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
678 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
685 * @param argc argument count
686 * @param argv argument values
687 * @return 0 on success
690 main (int argc, char *const *argv)
692 struct GNUNET_GETOPT_CommandLineOption options[] = {
694 GNUNET_GETOPT_option_string ('t',
697 gettext_noop ("name of the table to write DFAs"),
700 GNUNET_GETOPT_option_uint ('p',
701 "max-path-compression",
702 "MAX_PATH_COMPRESSION",
703 gettext_noop ("maximum path compression length"),
704 &max_path_compression),
706 GNUNET_GETOPT_OPTION_END
710 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
713 result = GNUNET_SYSERR;
715 GNUNET_PROGRAM_run (argc, argv,
716 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
717 _("Profiler for regex library"), options, &run, NULL);
718 if (GNUNET_OK != ret)
720 if (GNUNET_OK != result)