notes
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file util/speedup.c
21  * @author Matthias Wachs
22  * @brief functions to speedup peer execution by manipulation system time
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "speedup.h"
27
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-speedup", __VA_ARGS__)
29
30
31 static struct GNUNET_TIME_Relative interval;
32
33 static struct GNUNET_TIME_Relative delta;
34
35 static struct GNUNET_SCHEDULER_Task *speedup_task;
36
37
38 static void
39 do_speedup (void *cls)
40 {
41   static long long current_offset;
42
43   (void) cls;
44   speedup_task = NULL;
45   current_offset += delta.rel_value_us;
46   GNUNET_TIME_set_offset (current_offset);
47   LOG (GNUNET_ERROR_TYPE_DEBUG,
48        "Speeding up execution time by %s\n",
49        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
50   speedup_task = GNUNET_SCHEDULER_add_delayed (interval,
51                                                &do_speedup,
52                                                NULL);
53 }
54
55
56 /**
57  * Start task that may speed up our system clock artificially
58  *
59  * @param cfg configuration to use
60  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured
61  */
62 int
63 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg)
64 {
65   GNUNET_assert (NULL == speedup_task);
66   if (GNUNET_OK !=
67       GNUNET_CONFIGURATION_get_value_time (cfg,
68                                            "testing",
69                                            "SPEEDUP_INTERVAL",
70                                            &interval))
71     return GNUNET_SYSERR;
72   if (GNUNET_OK !=
73       GNUNET_CONFIGURATION_get_value_time (cfg,
74                                            "testing",
75                                            "SPEEDUP_DELTA",
76                                            &delta))
77     return GNUNET_SYSERR;
78
79   if ( (0 == interval.rel_value_us) ||
80        (0 == delta.rel_value_us) )
81   {
82     LOG (GNUNET_ERROR_TYPE_DEBUG,
83          "Speed up disabled\n");
84     return GNUNET_OK;
85   }
86   LOG (GNUNET_ERROR_TYPE_DEBUG,
87        "Speed up execution by %s\n",
88        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
89   LOG (GNUNET_ERROR_TYPE_DEBUG,
90        "Speed up executed every %s\n",
91        GNUNET_STRINGS_relative_time_to_string (interval, GNUNET_NO));
92   speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
93                                                          &do_speedup,
94                                                          NULL);
95   return GNUNET_OK;
96 }
97
98
99 /**
100  * Stop tasks that modify clock behavior.
101  */
102 void
103 GNUNET_SPEEDUP_stop_ ()
104 {
105   if (NULL != speedup_task)
106   {
107     GNUNET_SCHEDULER_cancel (speedup_task);
108     speedup_task = NULL;
109   }
110   if ( (0 != interval.rel_value_us) &&
111        (0 != delta.rel_value_us) )
112     LOG (GNUNET_ERROR_TYPE_DEBUG,
113          "Stopped execution speed up\n");
114 }
115
116 /* end of speedup.c */