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
30 #include "gnunet_util_lib.h"
31 #include "regex_internal_lib.h"
32 #include "gnunet_mysql_lib.h"
33 #include <mysql/mysql.h>
36 * MySQL statement to insert an edge.
38 #define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` "\
39 "(`key`, `label`, `to_key`, `accepting`) "\
40 "VALUES (?, ?, ?, ?);"
43 * MySQL statement to select a key count.
45 #define SELECT_KEY_STMT "SELECT COUNT(*) FROM `%s` "\
46 "WHERE `key` = ? AND `label` = ?;"
49 * Simple struct to keep track of progress, and print a
50 * nice little percentage meter for long running tasks.
55 * Total number of elements.
60 * Intervall for printing percentage.
65 * Number of dots to print.
72 unsigned int completed;
75 * Should the meter be printed?
80 * String to print on startup.
87 * Handle for the progress meter
89 static struct ProgressMeter *meter;
92 * Scan task identifier;
94 static struct GNUNET_SCHEDULER_Task *scan_task;
97 * Global testing status.
104 static struct GNUNET_MYSQL_Context *mysql_ctx;
107 * MySQL prepared statement handle.
109 static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
112 * MySQL prepared statement handle for `key` select.
114 static struct GNUNET_MYSQL_StatementHandle *select_stmt_handle;
119 static char *table_name;
122 * Policy dir containing files that contain policies.
124 static char *policy_dir;
127 * Number of policy files.
129 static unsigned int num_policy_files;
132 * Number of policies.
134 static unsigned int num_policies;
137 * Maximal path compression length.
139 static unsigned int max_path_compression;
142 * Number of merged transitions.
144 static unsigned long long num_merged_transitions;
147 * Number of merged states from different policies.
149 static unsigned long long num_merged_states;
152 * Prefix to add before every regex we're announcing.
154 static char *regex_prefix;
158 * Create a meter to keep track of the progress of some task.
160 * @param total the total number of items to complete
161 * @param start_string a string to prefix the meter with (if printing)
162 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
165 * @return the progress meter
167 static struct ProgressMeter *
168 create_meter (unsigned int total, char *start_string, int print)
170 struct ProgressMeter *ret;
172 ret = GNUNET_new (struct ProgressMeter);
175 ret->modnum = total / 4;
176 if (ret->modnum == 0) /* Divide by zero check */
178 ret->dotnum = (total / 50) + 1;
179 if (start_string != NULL)
180 ret->startup_string = GNUNET_strdup (start_string);
182 ret->startup_string = GNUNET_strdup ("");
189 * Update progress meter (increment by one).
191 * @param meter the meter to update and print info for
193 * @return GNUNET_YES if called the total requested,
194 * GNUNET_NO if more items expected
197 update_meter (struct ProgressMeter *meter)
199 if (meter->print == GNUNET_YES)
201 if (meter->completed % meter->modnum == 0)
203 if (meter->completed == 0)
205 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
208 FPRINTF (stdout, "%d%%",
209 (int) (((float) meter->completed / meter->total) * 100));
211 else if (meter->completed % meter->dotnum == 0)
212 FPRINTF (stdout, "%s", ".");
214 if (meter->completed + 1 == meter->total)
215 FPRINTF (stdout, "%d%%]\n", 100);
220 if (meter->completed == meter->total)
222 if (meter->completed > meter->total)
223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
229 * Reset progress meter.
231 * @param meter the meter to reset
233 * @return #GNUNET_YES if meter reset,
234 * #GNUNET_SYSERR on error
237 reset_meter (struct ProgressMeter *meter)
240 return GNUNET_SYSERR;
242 meter->completed = 0;
248 * Release resources for meter
250 * @param meter the meter to free
253 free_meter (struct ProgressMeter *meter)
255 GNUNET_free_non_null (meter->startup_string);
266 do_shutdown (void *cls)
268 if (NULL != mysql_ctx)
270 GNUNET_MYSQL_context_destroy (mysql_ctx);
282 * Abort task to run on test timed out.
284 * FIXME: this doesn't actually work, it used to cancel
285 * the already running 'scan_task', but now that should
286 * always be NULL and do nothing. We instead need to set
287 * a global variable and abort scan_task internally, not
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
296 if (NULL != scan_task)
298 GNUNET_SCHEDULER_cancel (scan_task);
301 result = GNUNET_SYSERR;
302 GNUNET_SCHEDULER_shutdown ();
307 * Dummy function for prepared select. Always returns #GNUNET_OK.
310 * @param num_values number of values.
311 * @param values returned values from select stmt.
316 return_ok (void *cls,
317 unsigned int num_values,
325 * Iterator over all states that inserts each state into the MySQL db.
327 * @param cls closure.
328 * @param key hash for current state.
329 * @param proof proof for current state.
330 * @param accepting #GNUNET_YES if this is an accepting state, #GNUNET_NO if not.
331 * @param num_edges number of edges leaving current state.
332 * @param edges edges leaving current state.
335 regex_iterator (void *cls,
336 const struct GNUNET_HashCode *key,
339 unsigned int num_edges,
340 const struct REGEX_BLOCK_Edge *edges)
344 unsigned long k_length;
345 unsigned long e_length;
346 unsigned long d_length;
348 unsigned long long total;
350 GNUNET_assert (NULL != mysql_ctx);
352 for (i = 0; i < num_edges; i++)
354 k_length = sizeof (struct GNUNET_HashCode);
355 e_length = strlen (edges[i].label);
356 d_length = sizeof (struct GNUNET_HashCode);
357 memset (rbind, 0, sizeof (rbind));
359 rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
360 rbind[0].buffer = &total;
361 rbind[0].is_unsigned = GNUNET_YES;
364 GNUNET_MYSQL_statement_run_prepared_select (mysql_ctx,
365 select_stmt_handle, 1,
366 rbind, &return_ok, NULL,
367 MYSQL_TYPE_BLOB, key,
373 strlen (edges[i].label),
376 if (GNUNET_SYSERR == result)
378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379 "Error executing prepared mysql select statement\n");
380 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
384 if (-1 != total && total > 0)
386 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", total,
387 GNUNET_h2s (key), edges[i].label);
391 GNUNET_MYSQL_statement_run_prepared (mysql_ctx, stmt_handle, NULL,
392 MYSQL_TYPE_BLOB, key,
393 sizeof (struct GNUNET_HashCode),
394 &k_length, MYSQL_TYPE_STRING,
396 strlen (edges[i].label), &e_length,
398 &edges[i].destination,
399 sizeof (struct GNUNET_HashCode),
400 &d_length, MYSQL_TYPE_LONG,
401 &accepting, GNUNET_YES, -1);
405 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
406 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", key_str,
409 edges[i].label, to_key_str, accepting);
410 GNUNET_free (key_str);
411 GNUNET_free (to_key_str);
412 num_merged_transitions++;
414 else if (-1 != total)
419 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
424 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
430 k_length = sizeof (struct GNUNET_HashCode);
435 GNUNET_MYSQL_statement_run_prepared (mysql_ctx, stmt_handle, NULL,
436 MYSQL_TYPE_BLOB, key,
437 sizeof (struct GNUNET_HashCode),
438 &k_length, MYSQL_TYPE_STRING, NULL,
439 0, &e_length, MYSQL_TYPE_BLOB,
441 MYSQL_TYPE_LONG, &accepting,
444 if (1 != result && 0 != result)
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
447 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
449 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
456 * Announce a regex by creating the DFA and iterating over each state, inserting
457 * each state into a MySQL database.
459 * @param regex regular expression.
460 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
463 announce_regex (const char *regex)
465 struct REGEX_INTERNAL_Automaton *dfa;
468 REGEX_INTERNAL_construct_dfa (regex,
470 max_path_compression);
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 "Failed to create DFA for regex %s\n",
477 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
478 return GNUNET_SYSERR;
480 REGEX_INTERNAL_iterate_all_edges (dfa,
481 ®ex_iterator, NULL);
482 REGEX_INTERNAL_automaton_destroy (dfa);
489 * Function called with a filename.
492 * @param filename complete filename (absolute path)
493 * @return #GNUNET_OK to continue to iterate,
494 * #GNUNET_SYSERR to abort iteration with error!
497 policy_filename_cb (void *cls, const char *filename)
505 GNUNET_assert (NULL != filename);
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
508 "Announcing regexes from file %s\n",
511 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
514 "Could not find policy file %s\n",
519 GNUNET_DISK_file_size (filename, &filesize,
520 GNUNET_YES, GNUNET_YES))
524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
528 data = GNUNET_malloc (filesize);
529 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
533 "Could not read policy file %s.\n",
538 update_meter (meter);
543 while (offset < (filesize - 1))
546 if (((data[offset] == '\n')) && (buf != &data[offset]))
550 buf = &data[offset + 1];
552 else if ((data[offset] == '\n') || (data[offset] == '\0'))
553 buf = &data[offset + 1];
556 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
557 GNUNET_assert (NULL != regex);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Announcing regex: %s\n", regex);
561 if (GNUNET_OK != announce_regex (regex))
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "Could not announce regex %s\n",
574 * Iterate over files contained in policy_dir.
579 do_directory_scan (void *cls)
581 struct GNUNET_TIME_Absolute start_time;
582 struct GNUNET_TIME_Relative duration;
585 /* Create an MySQL prepared statement for the inserts */
587 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
588 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
591 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
592 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
595 GNUNET_assert (NULL != stmt_handle);
597 meter = create_meter (num_policy_files,
598 "Announcing policy files\n",
600 start_time = GNUNET_TIME_absolute_get ();
601 GNUNET_DISK_directory_scan (policy_dir,
604 duration = GNUNET_TIME_absolute_get_duration (start_time);
609 printf ("Announced %u files containing %u policies in %s\n"
610 "Duplicate transitions: %llu\nMerged states: %llu\n",
613 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
614 num_merged_transitions,
617 GNUNET_SCHEDULER_shutdown ();
622 * Main function that will be run by the scheduler.
625 * @param args remaining command-line arguments
626 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
627 * @param config configuration
633 const struct GNUNET_CONFIGURATION_Handle *config)
638 _("No policy directory specified on command line. Exiting.\n"));
639 result = GNUNET_SYSERR;
643 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
646 _("Specified policies directory does not exist. Exiting.\n"));
647 result = GNUNET_SYSERR;
650 policy_dir = args[0];
652 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
656 if (NULL == table_name)
658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
659 "No table name specified, using default \"NFA\".\n");
663 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
664 if (NULL == mysql_ctx)
666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667 "Failed to create mysql context\n");
668 result = GNUNET_SYSERR;
673 GNUNET_CONFIGURATION_get_value_string (config,
678 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
681 result = GNUNET_SYSERR;
686 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
688 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
695 * @param argc argument count
696 * @param argv argument values
697 * @return 0 on success
700 main (int argc, char *const *argv)
702 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
703 {'t', "table", "TABLENAME",
704 gettext_noop ("name of the table to write DFAs"),
705 1, &GNUNET_GETOPT_set_string, &table_name},
706 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
707 gettext_noop ("maximum path compression length"),
708 1, &GNUNET_GETOPT_set_uint, &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)