- Allocate buffer large enough to contain UNIX_PATH_MAX size pathnames in case of...
[oweals/gnunet.git] / src / util / speedup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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
29 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
30
31
32 static struct GNUNET_TIME_Relative interval;
33
34 static struct GNUNET_TIME_Relative delta;
35
36 static GNUNET_SCHEDULER_TaskIdentifier speedup_task;
37
38
39 static void
40 do_speedup (void *cls,
41             const struct GNUNET_SCHEDULER_TaskContext *tc)
42 {
43   static long long current_offset;
44
45   speedup_task = GNUNET_SCHEDULER_NO_TASK;
46   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
47     return;
48   current_offset += delta.rel_value_us;
49   GNUNET_TIME_set_offset (current_offset);
50   LOG (GNUNET_ERROR_TYPE_DEBUG,
51        "Speeding up execution time by %s\n",
52        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
53   speedup_task = GNUNET_SCHEDULER_add_delayed (interval, &do_speedup, NULL);
54 }
55
56
57 /**
58  * Start task that may speed up our system clock artificially
59  *
60  * @param cfg configuration to use
61  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the speedup was not configured
62  */
63 int
64 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg)
65 {
66   if (GNUNET_OK !=
67       GNUNET_CONFIGURATION_get_value_time (cfg, "testing",
68                                            "SPEEDUP_INTERVAL", &interval))
69     return GNUNET_SYSERR;
70   if (GNUNET_OK !=
71       GNUNET_CONFIGURATION_get_value_time (cfg, "testing",
72                                            "SPEEDUP_DELTA", &delta))
73     return GNUNET_SYSERR;
74
75   if ((0 == interval.rel_value_us) || (0 == delta.rel_value_us))
76   {
77     LOG (GNUNET_ERROR_TYPE_DEBUG,
78          "Speed up disabled\n");
79     return GNUNET_OK;
80   }
81   LOG (GNUNET_ERROR_TYPE_DEBUG,
82        "Speed up execution by %s\n",
83        GNUNET_STRINGS_relative_time_to_string (delta, GNUNET_NO));
84   LOG (GNUNET_ERROR_TYPE_DEBUG,
85        "Speed up executed every %s\n",
86        GNUNET_STRINGS_relative_time_to_string (interval, GNUNET_NO));
87   speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
88                                                          &do_speedup, NULL);
89   return GNUNET_OK;
90 }
91
92
93 /**
94  * Stop tasks that modify clock behavior.
95  */
96 void
97 GNUNET_SPEEDUP_stop_ ()
98 {
99   if (GNUNET_SCHEDULER_NO_TASK != speedup_task)
100   {
101     GNUNET_SCHEDULER_cancel (speedup_task);
102     speedup_task = GNUNET_SCHEDULER_NO_TASK;
103   }
104   if ( (0 != interval.rel_value_us) &&
105        (0 != delta.rel_value_us) )
106     LOG (GNUNET_ERROR_TYPE_DEBUG,
107          "Stopped execution speed up\n");
108 }
109
110
111
112 /* end of speedup.c */