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 (select_stmt_handle, 1,
365 rbind, &return_ok, NULL,
366 MYSQL_TYPE_BLOB, key,
372 strlen (edges[i].label),
375 if (GNUNET_SYSERR == result)
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 "Error executing prepared mysql select statement\n");
379 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
383 if (-1 != total && total > 0)
385 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", total,
386 GNUNET_h2s (key), edges[i].label);
390 GNUNET_MYSQL_statement_run_prepared (stmt_handle, NULL,
391 MYSQL_TYPE_BLOB, key,
392 sizeof (struct GNUNET_HashCode),
393 &k_length, MYSQL_TYPE_STRING,
395 strlen (edges[i].label), &e_length,
397 &edges[i].destination,
398 sizeof (struct GNUNET_HashCode),
399 &d_length, MYSQL_TYPE_LONG,
400 &accepting, GNUNET_YES, -1);
404 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
405 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", key_str,
408 edges[i].label, to_key_str, accepting);
409 GNUNET_free (key_str);
410 GNUNET_free (to_key_str);
411 num_merged_transitions++;
413 else if (-1 != total)
418 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
421 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
423 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
429 k_length = sizeof (struct GNUNET_HashCode);
434 GNUNET_MYSQL_statement_run_prepared (stmt_handle, NULL,
435 MYSQL_TYPE_BLOB, key,
436 sizeof (struct GNUNET_HashCode),
437 &k_length, MYSQL_TYPE_STRING, NULL,
438 0, &e_length, MYSQL_TYPE_BLOB,
440 MYSQL_TYPE_LONG, &accepting,
443 if (1 != result && 0 != result)
445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
446 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
448 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
455 * Announce a regex by creating the DFA and iterating over each state, inserting
456 * each state into a MySQL database.
458 * @param regex regular expression.
459 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure.
462 announce_regex (const char *regex)
464 struct REGEX_INTERNAL_Automaton *dfa;
467 REGEX_INTERNAL_construct_dfa (regex,
469 max_path_compression);
473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
474 "Failed to create DFA for regex %s\n",
476 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
477 return GNUNET_SYSERR;
479 REGEX_INTERNAL_iterate_all_edges (dfa,
480 ®ex_iterator, NULL);
481 REGEX_INTERNAL_automaton_destroy (dfa);
488 * Function called with a filename.
491 * @param filename complete filename (absolute path)
492 * @return #GNUNET_OK to continue to iterate,
493 * #GNUNET_SYSERR to abort iteration with error!
496 policy_filename_cb (void *cls, const char *filename)
504 GNUNET_assert (NULL != filename);
506 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
507 "Announcing regexes from file %s\n",
510 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
513 "Could not find policy file %s\n",
518 GNUNET_DISK_file_size (filename, &filesize,
519 GNUNET_YES, GNUNET_YES))
523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
527 data = GNUNET_malloc (filesize);
528 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
532 "Could not read policy file %s.\n",
537 update_meter (meter);
542 while (offset < (filesize - 1))
545 if (((data[offset] == '\n')) && (buf != &data[offset]))
549 buf = &data[offset + 1];
551 else if ((data[offset] == '\n') || (data[offset] == '\0'))
552 buf = &data[offset + 1];
555 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
556 GNUNET_assert (NULL != regex);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "Announcing regex: %s\n", regex);
560 if (GNUNET_OK != announce_regex (regex))
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
563 "Could not announce regex %s\n",
573 * Iterate over files contained in policy_dir.
578 do_directory_scan (void *cls)
580 struct GNUNET_TIME_Absolute start_time;
581 struct GNUNET_TIME_Relative duration;
584 /* Create an MySQL prepared statement for the inserts */
586 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
587 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
590 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
591 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
594 GNUNET_assert (NULL != stmt_handle);
596 meter = create_meter (num_policy_files,
597 "Announcing policy files\n",
599 start_time = GNUNET_TIME_absolute_get ();
600 GNUNET_DISK_directory_scan (policy_dir,
603 duration = GNUNET_TIME_absolute_get_duration (start_time);
608 printf ("Announced %u files containing %u policies in %s\n"
609 "Duplicate transitions: %llu\nMerged states: %llu\n",
612 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
613 num_merged_transitions,
616 GNUNET_SCHEDULER_shutdown ();
621 * Main function that will be run by the scheduler.
624 * @param args remaining command-line arguments
625 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
626 * @param config configuration
632 const struct GNUNET_CONFIGURATION_Handle *config)
637 _("No policy directory specified on command line. Exiting.\n"));
638 result = GNUNET_SYSERR;
642 GNUNET_DISK_directory_test (args[0], GNUNET_YES))
645 _("Specified policies directory does not exist. Exiting.\n"));
646 result = GNUNET_SYSERR;
649 policy_dir = args[0];
651 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
655 if (NULL == table_name)
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 "No table name specified, using default \"NFA\".\n");
662 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
663 if (NULL == mysql_ctx)
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
666 "Failed to create mysql context\n");
667 result = GNUNET_SYSERR;
672 GNUNET_CONFIGURATION_get_value_string (config,
677 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
680 result = GNUNET_SYSERR;
685 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
687 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
694 * @param argc argument count
695 * @param argv argument values
696 * @return 0 on success
699 main (int argc, char *const *argv)
701 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
702 {'t', "table", "TABLENAME",
703 gettext_noop ("name of the table to write DFAs"),
704 1, &GNUNET_GETOPT_set_string, &table_name},
705 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
706 gettext_noop ("maximum path compression length"),
707 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
708 GNUNET_GETOPT_OPTION_END
712 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
715 result = GNUNET_SYSERR;
717 GNUNET_PROGRAM_run (argc, argv,
718 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
719 _("Profiler for regex library"), options, &run, NULL);
720 if (GNUNET_OK != ret)
722 if (GNUNET_OK != result)