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"
33 #include <mysql/mysql.h>
35 #define INSERT_EDGE_STMT "INSERT IGNORE INTO `%s` "\
36 "(`key`, `label`, `to_key`, `accepting`) "\
37 "VALUES (?, ?, ?, ?);"
40 * Simple struct to keep track of progress, and print a
41 * nice little percentage meter for long running tasks.
51 unsigned int completed;
60 * Handle for the progress meter
62 static struct ProgressMeter *meter;
65 * Abort task identifier.
67 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
70 * Scan task identifier;
72 static GNUNET_SCHEDULER_TaskIdentifier scan_task;
75 * Global testing status.
82 static struct GNUNET_MYSQL_Context *mysql_ctx;
85 * MySQL prepared statement handle.
87 static struct GNUNET_MYSQL_StatementHandle *stmt_handle;
92 static char *table_name;
95 * Policy dir containing files that contain policies.
97 static char *policy_dir;
100 * Number of policy files.
102 static unsigned int num_policy_files;
105 * Number of policies.
107 static unsigned int num_policies;
110 * Maximal path compression length.
112 static unsigned int max_path_compression;
116 * Create a meter to keep track of the progress of some task.
118 * @param total the total number of items to complete
119 * @param start_string a string to prefix the meter with (if printing)
120 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
123 * @return the progress meter
125 static struct ProgressMeter *
126 create_meter (unsigned int total, char *start_string, int print)
128 struct ProgressMeter *ret;
130 ret = GNUNET_malloc (sizeof (struct ProgressMeter));
133 ret->modnum = total / 4;
134 if (ret->modnum == 0) /* Divide by zero check */
136 ret->dotnum = (total / 50) + 1;
137 if (start_string != NULL)
138 ret->startup_string = GNUNET_strdup (start_string);
140 ret->startup_string = GNUNET_strdup ("");
147 * Update progress meter (increment by one).
149 * @param meter the meter to update and print info for
151 * @return GNUNET_YES if called the total requested,
152 * GNUNET_NO if more items expected
155 update_meter (struct ProgressMeter *meter)
157 if (meter->print == GNUNET_YES)
159 if (meter->completed % meter->modnum == 0)
161 if (meter->completed == 0)
163 FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
166 FPRINTF (stdout, "%d%%",
167 (int) (((float) meter->completed / meter->total) * 100));
169 else if (meter->completed % meter->dotnum == 0)
170 FPRINTF (stdout, "%s", ".");
172 if (meter->completed + 1 == meter->total)
173 FPRINTF (stdout, "%d%%]\n", 100);
178 if (meter->completed == meter->total)
180 if (meter->completed > meter->total)
181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Progress meter overflow!!\n");
187 * Reset progress meter.
189 * @param meter the meter to reset
191 * @return GNUNET_YES if meter reset,
192 * GNUNET_SYSERR on error
195 reset_meter (struct ProgressMeter *meter)
198 return GNUNET_SYSERR;
200 meter->completed = 0;
206 * Release resources for meter
208 * @param meter the meter to free
211 free_meter (struct ProgressMeter *meter)
213 GNUNET_free_non_null (meter->startup_string);
222 * @param tc the task context
225 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 if (NULL != mysql_ctx)
228 GNUNET_MYSQL_context_destroy (mysql_ctx);
232 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
237 * abort task to run on test timed out
240 * @param tc the task context
243 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
246 abort_task = GNUNET_SCHEDULER_NO_TASK;
247 GNUNET_SCHEDULER_cancel (scan_task);
248 scan_task = GNUNET_SCHEDULER_NO_TASK;
249 result = GNUNET_SYSERR;
250 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
255 * Iterator over all states that inserts each state into the MySQL db.
257 * @param cls closure.
258 * @param key hash for current state.
259 * @param proof proof for current state.
260 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
261 * @param num_edges number of edges leaving current state.
262 * @param edges edges leaving current state.
265 regex_iterator (void *cls,
266 const struct GNUNET_HashCode *key,
269 unsigned int num_edges,
270 const struct GNUNET_REGEX_Edge *edges)
274 unsigned long k_length;
275 unsigned long e_length;
276 unsigned long d_length;
278 GNUNET_assert (NULL != mysql_ctx);
280 for (i = 0; i < num_edges; i++)
282 k_length = sizeof (struct GNUNET_HashCode);
283 e_length = strlen (edges[i].label);
284 d_length = sizeof (struct GNUNET_HashCode);
287 GNUNET_MYSQL_statement_run_prepared (
291 MYSQL_TYPE_BLOB, key, sizeof (struct GNUNET_HashCode), &k_length,
292 MYSQL_TYPE_STRING, edges[i].label, strlen (edges[i].label), &e_length,
293 MYSQL_TYPE_BLOB, &edges[i].destination, sizeof (struct GNUNET_HashCode), &d_length,
294 MYSQL_TYPE_LONG, &accepting, GNUNET_YES,
297 if (1 != result && 0 != result)
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
302 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
308 k_length = sizeof (struct GNUNET_HashCode);
313 GNUNET_MYSQL_statement_run_prepared (
317 MYSQL_TYPE_BLOB, key, sizeof (struct GNUNET_HashCode), &k_length,
318 MYSQL_TYPE_STRING, NULL, 0, &e_length,
319 MYSQL_TYPE_BLOB, NULL, 0, &d_length,
320 MYSQL_TYPE_LONG, &accepting, GNUNET_YES,
323 if (1 != result && 0 != result)
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 "Error executing prepared mysql statement for edge: Affected rows: %i, expected 0 or 1!\n",
328 GNUNET_SCHEDULER_add_now (&do_abort, NULL);
335 * Announce a regex by creating the DFA and iterating over each state, inserting
336 * each state into a MySQL database.
338 * @param regex regular expression.
339 * @return GNUNET_OK on success, GNUNET_SYSERR on failure.
342 announce_regex (const char *regex)
344 struct GNUNET_REGEX_Automaton *dfa;
346 dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex), max_path_compression);
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 "Failed to create DFA for regex %s\n", regex);
352 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
353 return GNUNET_SYSERR;
356 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
358 GNUNET_REGEX_automaton_destroy (dfa);
365 * Function called with a filename.
368 * @param filename complete filename (absolute path)
369 * @return GNUNET_OK to continue to iterate,
370 * GNUNET_SYSERR to abort iteration with error!
373 policy_filename_cb (void *cls, const char *filename)
381 GNUNET_assert (NULL != filename);
383 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
384 "Announcing regexes from file %s\n",
387 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 "Could not find policy file %s\n", filename);
393 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
397 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", filename);
400 data = GNUNET_malloc (filesize);
401 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
409 update_meter (meter);
414 while (offset < (filesize - 1))
417 if (((data[offset] == '\n')) && (buf != &data[offset]))
421 GNUNET_assert (NULL != regex);
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n",
426 if (GNUNET_OK != announce_regex (regex))
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429 "Could not announce regex %s\n", regex);
432 buf = &data[offset + 1];
434 else if ((data[offset] == '\n') || (data[offset] == '\0'))
435 buf = &data[offset + 1];
443 * Iterate over files contained in policy_dir.
446 * @param tc the task context
449 do_directory_scan (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
451 struct GNUNET_TIME_Absolute start_time;
452 struct GNUNET_TIME_Relative duration;
455 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
456 GNUNET_SCHEDULER_cancel (abort_task);
458 /* Create an MySQL prepared statement for the inserts */
459 GNUNET_asprintf (&stmt, INSERT_EDGE_STMT, table_name);
460 stmt_handle = GNUNET_MYSQL_statement_prepare (mysql_ctx, stmt);
463 GNUNET_assert (NULL != stmt_handle);
465 meter = create_meter (num_policy_files, "Announcing policy files\n", GNUNET_YES);
466 start_time = GNUNET_TIME_absolute_get ();
467 GNUNET_DISK_directory_scan (policy_dir,
470 duration = GNUNET_TIME_absolute_get_duration (start_time);
475 printf ("Announced %u files containing %u policies in %s\n",
476 num_policy_files, num_policies,
477 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_NO));
480 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
485 * Main function that will be run by the scheduler.
488 * @param args remaining command-line arguments
489 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
490 * @param config configuration
493 run (void *cls, char *const *args, const char *cfgfile,
494 const struct GNUNET_CONFIGURATION_Handle *config)
498 fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
499 result = GNUNET_SYSERR;
502 if (GNUNET_YES != GNUNET_DISK_directory_test (args[0]))
504 fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
505 result = GNUNET_SYSERR;
508 policy_dir = args[0];
510 num_policy_files = GNUNET_DISK_directory_scan (policy_dir,
515 if (NULL == table_name)
517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No table name specified, using default \"NFA\".\n");
521 mysql_ctx = GNUNET_MYSQL_context_create (config, "regex-mysql");
522 if (NULL == mysql_ctx)
524 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create mysql context\n");
525 result = GNUNET_SYSERR;
531 scan_task = GNUNET_SCHEDULER_add_now (&do_directory_scan, NULL);
534 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
535 (GNUNET_TIME_UNIT_SECONDS, 10), &do_abort,
543 * @param argc argument count
544 * @param argv argument values
545 * @return 0 on success
548 main (int argc, char *const *argv)
550 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
551 {'t', "table", "TABLENAME",
552 gettext_noop ("name of the table to write DFAs"),
553 1, &GNUNET_GETOPT_set_string, &table_name},
554 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
555 gettext_noop ("maximum path compression length"),
556 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
557 GNUNET_GETOPT_OPTION_END
561 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
564 result = GNUNET_SYSERR;
566 GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-simulationprofiler [OPTIONS] policy-dir",
567 _("Profiler for regex library"),
568 options, &run, NULL);
569 if (GNUNET_OK != ret)
571 if (GNUNET_OK != result)