2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 mesh).
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 * Abort task identifier.
94 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
97 * Shutdown task identifier.
99 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
102 * Scan task identifier;
104 static GNUNET_SCHEDULER_TaskIdentifier scan_task;
107 * Global testing status.
114 static struct GNUNET_MYSQL_Context *mysql_ctx;
117 * MySQL prepared statement handle.
119 static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
122 * MySQL prepared statement handle for `key` select.
124 static struct GNUNET_MYSQL_StatementHandle *select_stmt_handle;
129 static char *table_name;
132 * Policy dir containing files that contain policies.
134 static char *policy_dir;
137 * Number of policy files.
139 static unsigned int num_policy_files;
142 * Number of policies.
144 static unsigned int num_policies;
147 * Maximal path compression length.
149 static unsigned int max_path_compression;
152 * Number of merged transitions.
154 static unsigned long long num_merged_transitions;
157 * Number of merged states from different policies.
159 static unsigned long long num_merged_states;
162 * Prefix to add before every regex we're announcing.
164 static char *regex_prefix;
168 * Create a meter to keep track of the progress of some task.
170 * @param total the total number of items to complete
171 * @param start_string a string to prefix the meter with (if printing)
172 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
175 * @return the progress meter
177 static struct ProgressMeter *
178 create_meter (unsigned int total, char *start_string, int print)
180 struct ProgressMeter *ret;
182 ret = GNUNET_malloc (sizeof (struct ProgressMeter));
185 ret->modnum = total / 4;
186 if (ret->modnum == 0) /* Divide by zero check */
188 ret->dotnum = (total / 50) + 1;
189 if (start_string != NULL)
190 ret->startup_string = GNUNET_strdup (start_string);
192 ret->startup_string = GNUNET_strdup ("");
199 * Update progress meter (increment by one).
201 * @param meter the meter to update and print info for
203 * @return GNUNET_YES if called the total requested,
204 * GNUNET_NO if more items expected
207 update_meter (struct ProgressMeter *meter)
209 if (meter->print == GNUNET_YES)
211 if (meter->completed % meter->modnum == 0)
213 if (meter->completed == 0)
215 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
218 FPRINTF (stdout, "%d%%",
219 (int) (((float) meter->completed / meter->total) * 100));
221 else if (meter->completed % meter->dotnum == 0)
222 FPRINTF (stdout, "%s", ".");
224 if (meter->completed + 1 == meter->total)
225 FPRINTF (stdout, "%d%%]\n", 100);
230 if (meter->completed == meter->total)
232 if (meter->completed > meter->total)
233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
239 * Reset progress meter.
241 * @param meter the meter to reset
243 * @return GNUNET_YES if meter reset,
244 * GNUNET_SYSERR on error
247 reset_meter (struct ProgressMeter *meter)
250 return GNUNET_SYSERR;
252 meter->completed = 0;
258 * Release resources for meter
260 * @param meter the meter to free
263 free_meter (struct ProgressMeter *meter)
265 GNUNET_free_non_null (meter->startup_string);
274 * @param tc the task context
277 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
279 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
280 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
281 GNUNET_SCHEDULER_cancel (abort_task);
282 if (NULL != mysql_ctx)
283 GNUNET_MYSQL_context_destroy (mysql_ctx);
287 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
292 * abort task to run on test timed out
295 * @param tc the task context
298 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
301 abort_task = GNUNET_SCHEDULER_NO_TASK;
302 GNUNET_SCHEDULER_cancel (scan_task);
303 scan_task = GNUNET_SCHEDULER_NO_TASK;
304 result = GNUNET_SYSERR;
305 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
310 * Dummy function for prepared select. Always return GNUNET_OK.
313 * @param num_values number of values.
314 * @param values returned values from select stmt.
319 return_ok (void *cls, unsigned int num_values, MYSQL_BIND * values)
326 * Iterator over all states that inserts each state into the MySQL db.
328 * @param cls closure.
329 * @param key hash for current state.
330 * @param proof proof for current state.
331 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
332 * @param num_edges number of edges leaving current state.
333 * @param edges edges leaving current state.
336 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
337 int accepting, unsigned int num_edges,
338 const struct REGEX_BLOCK_Edge *edges)
342 unsigned long k_length;
343 unsigned long e_length;
344 unsigned long d_length;
346 unsigned long long total;
348 GNUNET_assert (NULL != mysql_ctx);
350 for (i = 0; i < num_edges; i++)
352 k_length = sizeof (struct GNUNET_HashCode);
353 e_length = strlen (edges[i].label);
354 d_length = sizeof (struct GNUNET_HashCode);
355 memset (rbind, 0, sizeof (rbind));
357 rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
358 rbind[0].buffer = &total;
359 rbind[0].is_unsigned = GNUNET_YES;
362 GNUNET_MYSQL_statement_run_prepared_select (mysql_ctx,
363 select_stmt_handle, 1,
364 rbind, &return_ok, NULL,
365 MYSQL_TYPE_BLOB, key,
371 strlen (edges[i].label),
374 if (GNUNET_SYSERR == result)
376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377 "Error executing prepared mysql select statement\n");
378 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
382 if (-1 != total && total > 0)
384 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", total,
385 GNUNET_h2s (key), edges[i].label);
389 GNUNET_MYSQL_statement_run_prepared (mysql_ctx, stmt_handle, NULL,
390 MYSQL_TYPE_BLOB, key,
391 sizeof (struct GNUNET_HashCode),
392 &k_length, MYSQL_TYPE_STRING,
394 strlen (edges[i].label), &e_length,
396 &edges[i].destination,
397 sizeof (struct GNUNET_HashCode),
398 &d_length, MYSQL_TYPE_LONG,
399 &accepting, GNUNET_YES, -1);
403 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
404 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", key_str,
407 edges[i].label, to_key_str, accepting);
408 GNUNET_free (key_str);
409 GNUNET_free (to_key_str);
410 num_merged_transitions++;
412 else if (-1 != total)
417 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
422 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
428 k_length = sizeof (struct GNUNET_HashCode);
433 GNUNET_MYSQL_statement_run_prepared (mysql_ctx, stmt_handle, NULL,
434 MYSQL_TYPE_BLOB, key,
435 sizeof (struct GNUNET_HashCode),
436 &k_length, MYSQL_TYPE_STRING, NULL,
437 0, &e_length, MYSQL_TYPE_BLOB,
439 MYSQL_TYPE_LONG, &accepting,
442 if (1 != result && 0 != result)
444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
445 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
447 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
454 * Announce a regex by creating the DFA and iterating over each state, inserting
455 * each state into a MySQL database.
457 * @param regex regular expression.
458 * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
461 announce_regex (const char *regex)
463 struct REGEX_INTERNAL_Automaton *dfa;
466 REGEX_INTERNAL_construct_dfa (regex, strlen (regex), max_path_compression);
470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create DFA for regex %s\n",
472 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
473 return GNUNET_SYSERR;
476 REGEX_INTERNAL_iterate_all_edges (dfa, ®ex_iterator, NULL);
478 REGEX_INTERNAL_automaton_destroy (dfa);
485 * Function called with a filename.
488 * @param filename complete filename (absolute path)
489 * @return GNUNET_OK to continue to iterate,
490 * GNUNET_SYSERR to abort iteration with error!
493 policy_filename_cb (void *cls, const char *filename)
501 GNUNET_assert (NULL != filename);
503 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Announcing regexes from file %s\n",
506 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not find policy file %s\n",
513 GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n",
521 data = GNUNET_malloc (filesize);
522 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
530 update_meter (meter);
535 while (offset < (filesize - 1))
538 if (((data[offset] == '\n')) && (buf != &data[offset]))
542 buf = &data[offset + 1];
544 else if ((data[offset] == '\n') || (data[offset] == '\0'))
545 buf = &data[offset + 1];
548 GNUNET_asprintf (®ex, "%s(%s)", regex_prefix, data);
549 GNUNET_assert (NULL != regex);
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
552 if (GNUNET_OK != announce_regex (regex))
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not announce regex %s\n",
564 * Iterate over files contained in policy_dir.
567 * @param tc the task context
570 do_directory_scan (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
572 struct GNUNET_TIME_Absolute start_time;
573 struct GNUNET_TIME_Relative duration;
576 /* 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);
588 create_meter (num_policy_files, "Announcing policy files\n", GNUNET_YES);
589 start_time = GNUNET_TIME_absolute_get ();
590 GNUNET_DISK_directory_scan (policy_dir, &policy_filename_cb, stmt_handle);
591 duration = GNUNET_TIME_absolute_get_duration (start_time);
596 printf ("Announced %u files containing %u policies in %s\n"
597 "Duplicate transitions: %llu\nMerged states: %llu\n",
598 num_policy_files, num_policies,
599 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
600 num_merged_transitions, num_merged_states);
603 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
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
616 run (void *cls, char *const *args, const char *cfgfile,
617 const struct GNUNET_CONFIGURATION_Handle *config)
622 _("No policy directory specified on command line. Exiting.\n"));
623 result = GNUNET_SYSERR;
626 if (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES))
629 _("Specified policies directory does not exist. Exiting.\n"));
630 result = GNUNET_SYSERR;
633 policy_dir = args[0];
635 num_policy_files = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL);
638 if (NULL == table_name)
640 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
641 "No table name specified, using default \"NFA\".\n");
645 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
646 if (NULL == mysql_ctx)
648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create mysql context\n");
649 result = GNUNET_SYSERR;
654 GNUNET_CONFIGURATION_get_value_string (config, "regex-mysql",
655 "REGEX_PREFIX", ®ex_prefix))
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
659 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
660 "regexprofiler", "regex_prefix");
661 result = GNUNET_SYSERR;
668 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
670 /* Scheduled the task to clean up when shutdown is called */
672 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
680 * @param argc argument count
681 * @param argv argument values
682 * @return 0 on success
685 main (int argc, char *const *argv)
687 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
688 {'t', "table", "TABLENAME",
689 gettext_noop ("name of the table to write DFAs"),
690 1, &GNUNET_GETOPT_set_string, &table_name},
691 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
692 gettext_noop ("maximum path compression length"),
693 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
694 GNUNET_GETOPT_OPTION_END
698 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
701 result = GNUNET_SYSERR;
703 GNUNET_PROGRAM_run (argc, argv,
704 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
705 _("Profiler for regex library"), options, &run, NULL);
706 if (GNUNET_OK != ret)
708 if (GNUNET_OK != result)