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 "gnunet_regex_lib.h"
32 #include "gnunet_mysql_lib.h"
35 * Simple struct to keep track of progress, and print a
36 * nice little percentage meter for long running tasks.
46 unsigned int completed;
55 * Handle for the progress meter
57 static struct ProgressMeter *meter;
60 * Abort task identifier.
62 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
65 * Scan task identifier;
67 static GNUNET_SCHEDULER_TaskIdentifier scan_task;
70 * Global testing status.
77 static struct GNUNET_MYSQL_Context *mysql_ctx;
82 static char *table_name;
85 * Policy dir containing files that contain policies.
87 static char *policy_dir;
90 * Number of policy files.
92 static unsigned int num_policy_files;
97 static unsigned int num_policies;
100 * Maximal path compression length.
102 static unsigned int max_path_compression;
106 * Create a meter to keep track of the progress of some task.
108 * @param total the total number of items to complete
109 * @param start_string a string to prefix the meter with (if printing)
110 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
113 * @return the progress meter
115 static struct ProgressMeter *
116 create_meter (unsigned int total, char *start_string, int print)
118 struct ProgressMeter *ret;
120 ret = GNUNET_malloc (sizeof (struct ProgressMeter));
123 ret->modnum = total / 4;
124 if (ret->modnum == 0) /* Divide by zero check */
126 ret->dotnum = (total / 50) + 1;
127 if (start_string != NULL)
128 ret->startup_string = GNUNET_strdup (start_string);
130 ret->startup_string = GNUNET_strdup ("");
137 * Update progress meter (increment by one).
139 * @param meter the meter to update and print info for
141 * @return GNUNET_YES if called the total requested,
142 * GNUNET_NO if more items expected
145 update_meter (struct ProgressMeter *meter)
147 if (meter->print == GNUNET_YES)
149 if (meter->completed % meter->modnum == 0)
151 if (meter->completed == 0)
153 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
156 FPRINTF (stdout, "%d%%",
157 (int) (((float) meter->completed / meter->total) * 100));
159 else if (meter->completed % meter->dotnum == 0)
160 FPRINTF (stdout, "%s", ".");
162 if (meter->completed + 1 == meter->total)
163 FPRINTF (stdout, "%d%%]\n", 100);
168 if (meter->completed == meter->total)
170 if (meter->completed > meter->total)
171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
177 * Reset progress meter.
179 * @param meter the meter to reset
181 * @return GNUNET_YES if meter reset,
182 * GNUNET_SYSERR on error
185 reset_meter (struct ProgressMeter *meter)
188 return GNUNET_SYSERR;
190 meter->completed = 0;
196 * Release resources for meter
198 * @param meter the meter to free
201 free_meter (struct ProgressMeter *meter)
203 GNUNET_free_non_null (meter->startup_string);
212 * @param tc the task context
215 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 if (NULL != mysql_ctx)
218 GNUNET_MYSQL_context_destroy (mysql_ctx);
222 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
227 * abort task to run on test timed out
230 * @param tc the task context
233 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
236 abort_task = GNUNET_SCHEDULER_NO_TASK;
237 GNUNET_SCHEDULER_cancel (scan_task);
238 scan_task = GNUNET_SCHEDULER_NO_TASK;
239 result = GNUNET_SYSERR;
240 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
245 * Iterator over all states that inserts each state into the MySQL db.
247 * @param cls closure.
248 * @param key hash for current state.
249 * @param proof proof for current state.
250 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
251 * @param num_edges number of edges leaving current state.
252 * @param edges edges leaving current state.
255 regex_iterator (void *cls,
256 const struct GNUNET_HashCode *key,
259 unsigned int num_edges,
260 const struct GNUNET_REGEX_Edge *edges)
265 GNUNET_assert (NULL != mysql_ctx);
267 for (i = 0; i < num_edges; i++)
269 GNUNET_asprintf (&stmt,
270 "INSERT IGNORE INTO `%s` (`key`, `label`, `to_key`, `accepting`) VALUES ('%s', '%s', '%s', '%d');",
271 table_name, GNUNET_h2s_full (key), edges[i].label,
272 GNUNET_h2s_full (&edges[i].destination), accepting);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Insert statement: %s\n", stmt);
276 if (GNUNET_OK != GNUNET_MYSQL_statement_run (mysql_ctx, stmt))
278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279 "Error executing mysql statement: %s\n", stmt);
288 * Announce a regex by creating the DFA and iterating over each state, inserting
289 * each state into a MySQL database.
291 * @param regex regular expression.
292 * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
295 announce_regex (const char *regex)
297 struct GNUNET_REGEX_Automaton *dfa;
299 dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex), max_path_compression);
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
304 "Failed to create DFA for regex %s\n", regex);
305 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
306 return GNUNET_SYSERR;
309 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
311 GNUNET_REGEX_automaton_destroy (dfa);
318 * Function called with a filename.
321 * @param filename complete filename (absolute path)
322 * @return GNUNET_OK to continue to iterate,
323 * GNUNET_SYSERR to abort iteration with error!
326 policy_filename_cb (void *cls, const char *filename)
334 GNUNET_assert (NULL != filename);
336 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
337 "Announcing regexes from file %s\n",
340 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
343 "Could not find policy file %s\n", filename);
346 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", filename);
353 data = GNUNET_malloc (filesize);
354 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
362 update_meter (meter);
367 while (offset < (filesize - 1))
370 if (((data[offset] == '\n')) && (buf != &data[offset]))
374 GNUNET_assert (NULL != regex);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n",
379 if (GNUNET_OK != announce_regex (regex))
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 "Could not announce regex %s\n", regex);
385 buf = &data[offset + 1];
387 else if ((data[offset] == '\n') || (data[offset] == '\0'))
388 buf = &data[offset + 1];
396 * Iterate over files contained in policy_dir.
399 * @param tc the task context
402 do_directory_scan (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
404 struct GNUNET_TIME_Absolute start_time;
405 struct GNUNET_TIME_Relative duration;
407 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
408 GNUNET_SCHEDULER_cancel (abort_task);
410 meter = create_meter (num_policy_files, "Announcing policy files\n", GNUNET_YES);
411 start_time = GNUNET_TIME_absolute_get ();
412 GNUNET_DISK_directory_scan (policy_dir,
415 duration = GNUNET_TIME_absolute_get_duration (start_time);
420 printf ("Announced %u files containing %u policies in %s\n",
421 num_policy_files, num_policies,
422 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO));
425 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
430 * Main function that will be run by the scheduler.
433 * @param args remaining command-line arguments
434 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
435 * @param config configuration
438 run (void *cls, char *const *args, const char *cfgfile,
439 const struct GNUNET_CONFIGURATION_Handle *config)
443 fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
444 result = GNUNET_SYSERR;
447 if (GNUNET_YES != GNUNET_DISK_directory_test (args[0]))
449 fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
450 result = GNUNET_SYSERR;
453 policy_dir = args[0];
455 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
460 if (NULL == table_name)
462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No table name specified, using default \"NFA\".\n");
466 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
467 if (NULL == mysql_ctx)
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create mysql context\n");
470 result = GNUNET_SYSERR;
476 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
479 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
480 (GNUNET_TIME_UNIT_SECONDS, 10), &do_abort,
488 * @param argc argument count
489 * @param argv argument values
490 * @return 0 on success
493 main (int argc, char *const *argv)
495 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
496 {'t', "table", "TABLENAME",
497 gettext_noop ("name of the table to write DFAs"),
498 1, &GNUNET_GETOPT_set_string, &table_name},
499 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
500 gettext_noop ("maximum path compression length"),
501 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
502 GNUNET_GETOPT_OPTION_END
506 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
509 result = GNUNET_SYSERR;
511 GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
512 _("Profiler for regex library"),
513 options, &run, NULL);
514 if (GNUNET_OK != ret)
516 if (GNUNET_OK != result)