Implement a Full Domain Hash (FDH) for RSA signatures and blind signatures
[oweals/gnunet.git] / src / util / speedup.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-2013 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/speedup.c
23  * @author Matthias Wachs
24  * @brief functions to speedup peer execution by manipulation system time
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "speedup.h"
29
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31
32
33 static struct GNUNET_TIME_Relative interval;
34
35 static struct GNUNET_TIME_Relative delta;
36
37 static struct GNUNET_SCHEDULER_Task * speedup_task;
38
39
40 static void
41 do_speedup (void *cls,
42             const struct GNUNET_SCHEDULER_TaskContext *tc)
43 {
44   static long long current_offset;
45
46   speedup_task = NULL;
47   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
48     return;
49   current_offset += delta.rel_value_us;
50   GNUNET_TIME_set_offset (current_offset);
51   LOG (GNUNET_ERROR_TYPE_DEBUG,
52        "Speeding up execution time by %s\n",
53        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
54   speedup_task = GNUNET_SCHEDULER_add_delayed (interval, &do_speedup, NULL);
55 }
56
57
58 /**
59  * Start task that may speed up our system clock artificially
60  *
61  * @param cfg configuration to use
62  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured
63  */
64 int
65 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   if (GNUNET_OK !=
68       GNUNET_CONFIGURATION_get_value_time (cfg, "testing",
69                                            "SPEEDUP_INTERVAL", &interval))
70     return GNUNET_SYSERR;
71   if (GNUNET_OK !=
72       GNUNET_CONFIGURATION_get_value_time (cfg, "testing",
73                                            "SPEEDUP_DELTA", &delta))
74     return GNUNET_SYSERR;
75
76   if ((0 == interval.rel_value_us) || (0 == delta.rel_value_us))
77   {
78     LOG (GNUNET_ERROR_TYPE_DEBUG,
79          "Speed up disabled\n");
80     return GNUNET_OK;
81   }
82   LOG (GNUNET_ERROR_TYPE_DEBUG,
83        "Speed up execution by %s\n",
84        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
85   LOG (GNUNET_ERROR_TYPE_DEBUG,
86        "Speed up executed every %s\n",
87        GNUNET_STRINGS_relative_time_to_string (interval, GNUNET_NO));
88   speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
89                                                          &do_speedup, NULL);
90   return GNUNET_OK;
91 }
92
93
94 /**
95  * Stop tasks that modify clock behavior.
96  */
97 void
98 GNUNET_SPEEDUP_stop_ ()
99 {
100   if (NULL != speedup_task)
101   {
102     GNUNET_SCHEDULER_cancel (speedup_task);
103     speedup_task = NULL;
104   }
105   if ( (0 != interval.rel_value_us) &&
106        (0 != delta.rel_value_us) )
107     LOG (GNUNET_ERROR_TYPE_DEBUG,
108          "Stopped execution speed up\n");
109 }
110
111
112
113 /* end of speedup.c */