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 * Abort task identifier.
94 static struct GNUNET_SCHEDULER_Task * abort_task;
97 * Shutdown task identifier.
99 static struct GNUNET_SCHEDULER_Task * shutdown_task;
102 * Scan task identifier;
104 static struct GNUNET_SCHEDULER_Task * 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_new (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);
276 do_shutdown (void *cls)
278 shutdown_task = NULL;
279 if (NULL != abort_task)
280 GNUNET_SCHEDULER_cancel (abort_task);
281 if (NULL != mysql_ctx)
282 GNUNET_MYSQL_context_destroy (mysql_ctx);
286 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
291 * abort task to run on test timed out
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
300 GNUNET_SCHEDULER_cancel (scan_task);
302 result = GNUNET_SYSERR;
303 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
308 * Dummy function for prepared select. Always return GNUNET_OK.
311 * @param num_values number of values.
312 * @param values returned values from select stmt.
317 return_ok (void *cls, unsigned int num_values, MYSQL_BIND * values)
324 * Iterator over all states that inserts each state into the MySQL db.
326 * @param cls closure.
327 * @param key hash for current state.
328 * @param proof proof for current state.
329 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
330 * @param num_edges number of edges leaving current state.
331 * @param edges edges leaving current state.
334 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
335 int accepting, unsigned int num_edges,
336 const struct REGEX_BLOCK_Edge *edges)
340 unsigned long k_length;
341 unsigned long e_length;
342 unsigned long d_length;
344 unsigned long long total;
346 GNUNET_assert (NULL != mysql_ctx);
348 for (i = 0; i < num_edges; i++)
350 k_length = sizeof (struct GNUNET_HashCode);
351 e_length = strlen (edges[i].label);
352 d_length = sizeof (struct GNUNET_HashCode);
353 memset (rbind, 0, sizeof (rbind));
355 rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
356 rbind[0].buffer = &total;
357 rbind[0].is_unsigned = GNUNET_YES;
360 GNUNET_MYSQL_statement_run_prepared_select (mysql_ctx,
361 select_stmt_handle, 1,
362 rbind, &return_ok, NULL,
363 MYSQL_TYPE_BLOB, key,
369 strlen (edges[i].label),
372 if (GNUNET_SYSERR == result)
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 "Error executing prepared mysql select statement\n");
376 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
380 if (-1 != total && total > 0)
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Total: %llu (%s, %s)\n", total,
383 GNUNET_h2s (key), edges[i].label);
387 GNUNET_MYSQL_statement_run_prepared (mysql_ctx, stmt_handle, NULL,
388 MYSQL_TYPE_BLOB, key,
389 sizeof (struct GNUNET_HashCode),
390 &k_length, MYSQL_TYPE_STRING,
392 strlen (edges[i].label), &e_length,
394 &edges[i].destination,
395 sizeof (struct GNUNET_HashCode),
396 &d_length, MYSQL_TYPE_LONG,
397 &accepting, GNUNET_YES, -1);
401 char *key_str = GNUNET_strdup (GNUNET_h2s (key));
402 char *to_key_str = GNUNET_strdup (GNUNET_h2s (&edges[i].destination));
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Merged (%s, %s, %s, %i)\n", key_str,
405 edges[i].label, to_key_str, accepting);
406 GNUNET_free (key_str);
407 GNUNET_free (to_key_str);
408 num_merged_transitions++;
410 else if (-1 != total)
415 if (GNUNET_SYSERR == result || (1 != result && 0 != result))
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
420 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
426 k_length = sizeof (struct GNUNET_HashCode);
431 GNUNET_MYSQL_statement_run_prepared (mysql_ctx, stmt_handle, NULL,
432 MYSQL_TYPE_BLOB, key,
433 sizeof (struct GNUNET_HashCode),
434 &k_length, MYSQL_TYPE_STRING, NULL,
435 0, &e_length, MYSQL_TYPE_BLOB,
437 MYSQL_TYPE_LONG, &accepting,
440 if (1 != result && 0 != result)
442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
445 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
452 * Announce a regex by creating the DFA and iterating over each state, inserting
453 * each state into a MySQL database.
455 * @param regex regular expression.
456 * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
459 announce_regex (const char *regex)
461 struct REGEX_INTERNAL_Automaton *dfa;
464 REGEX_INTERNAL_construct_dfa (regex, strlen (regex), max_path_compression);
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create DFA for regex %s\n",
470 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
471 return GNUNET_SYSERR;
474 REGEX_INTERNAL_iterate_all_edges (dfa, ®ex_iterator, NULL);
476 REGEX_INTERNAL_automaton_destroy (dfa);
483 * Function called with a filename.
486 * @param filename complete filename (absolute path)
487 * @return GNUNET_OK to continue to iterate,
488 * GNUNET_SYSERR to abort iteration with error!
491 policy_filename_cb (void *cls, const char *filename)
499 GNUNET_assert (NULL != filename);
501 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Announcing regexes from file %s\n",
504 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not find policy file %s\n",
511 GNUNET_DISK_file_size (filename, &filesize, 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, "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, "Announcing regex: %s\n", regex);
550 if (GNUNET_OK != announce_regex (regex))
552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not announce regex %s\n",
562 * Iterate over files contained in policy_dir.
567 do_directory_scan (void *cls)
569 struct GNUNET_TIME_Absolute start_time;
570 struct GNUNET_TIME_Relative duration;
573 /* Create an MySQL prepared statement for the inserts */
574 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
575 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
578 GNUNET_asprintf (&stmt, SELECT_KEY_STMT, table_name);
579 select_stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
582 GNUNET_assert (NULL != stmt_handle);
585 create_meter (num_policy_files, "Announcing policy files\n", GNUNET_YES);
586 start_time = GNUNET_TIME_absolute_get ();
587 GNUNET_DISK_directory_scan (policy_dir, &policy_filename_cb, stmt_handle);
588 duration = GNUNET_TIME_absolute_get_duration (start_time);
593 printf ("Announced %u files containing %u policies in %s\n"
594 "Duplicate transitions: %llu\nMerged states: %llu\n",
595 num_policy_files, num_policies,
596 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO),
597 num_merged_transitions, num_merged_states);
600 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
605 * Main function that will be run by the scheduler.
608 * @param args remaining command-line arguments
609 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
610 * @param config configuration
613 run (void *cls, char *const *args, const char *cfgfile,
614 const struct GNUNET_CONFIGURATION_Handle *config)
619 _("No policy directory specified on command line. Exiting.\n"));
620 result = GNUNET_SYSERR;
623 if (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES))
626 _("Specified policies directory does not exist. Exiting.\n"));
627 result = GNUNET_SYSERR;
630 policy_dir = args[0];
632 num_policy_files = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL);
635 if (NULL == table_name)
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638 "No table name specified, using default \"NFA\".\n");
642 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
643 if (NULL == mysql_ctx)
645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create mysql context\n");
646 result = GNUNET_SYSERR;
651 GNUNET_CONFIGURATION_get_value_string (config, "regex-mysql",
652 "REGEX_PREFIX", ®ex_prefix))
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
657 "regexprofiler", "regex_prefix");
658 result = GNUNET_SYSERR;
665 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
667 /* Scheduled the task to clean up when shutdown is called */
669 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
677 * @param argc argument count
678 * @param argv argument values
679 * @return 0 on success
682 main (int argc, char *const *argv)
684 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
685 {'t', "table", "TABLENAME",
686 gettext_noop ("name of the table to write DFAs"),
687 1, &GNUNET_GETOPT_set_string, &table_name},
688 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
689 gettext_noop ("maximum path compression length"),
690 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
691 GNUNET_GETOPT_OPTION_END
695 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
698 result = GNUNET_SYSERR;
700 GNUNET_PROGRAM_run (argc, argv,
701 "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
702 _("Profiler for regex library"), options, &run, NULL);
703 if (GNUNET_OK != ret)
705 if (GNUNET_OK != result)