-w32 bugfix -- compiler warning too
[oweals/gnunet.git] / src / util / speedup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2006, 2009 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 2, 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_time_lib.h"
28 #include "gnunet_scheduler_lib.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 GNUNET_SCHEDULER_TaskIdentifier speedup_task;
38
39
40 static void
41 do_speedup (void *cls, 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;
49   GNUNET_TIME_set_offset (current_offset);
50   LOG (GNUNET_ERROR_TYPE_DEBUG, 
51        "Speeding up execution time by %llu ms\n", delta.rel_value);
52   speedup_task = GNUNET_SCHEDULER_add_delayed (interval, &do_speedup, 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   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "SPEEDUP_INTERVAL", &interval))
66     return GNUNET_SYSERR;
67   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "SPEEDUP_DELTA", &delta))
68     return GNUNET_SYSERR;
69
70   if ((0 == interval.rel_value) || (0 == delta.rel_value))
71   {
72     LOG (GNUNET_ERROR_TYPE_DEBUG,
73          "Speed up disabled\n");
74     return GNUNET_OK;
75   }
76   LOG (GNUNET_ERROR_TYPE_DEBUG,
77        "Speed up execution time %llu ms every %llu ms\n",
78        delta.rel_value, interval.rel_value);
79   speedup_task = GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO, &do_speedup, NULL);
80   return GNUNET_OK;
81 }
82
83
84 /**
85  * Stop tasks that modify clock behavior.
86  */
87 void
88 GNUNET_SPEEDUP_stop_ ( )
89 {
90   if (GNUNET_SCHEDULER_NO_TASK != speedup_task)
91   {
92     GNUNET_SCHEDULER_cancel (speedup_task);
93     speedup_task = GNUNET_SCHEDULER_NO_TASK;
94   }
95   if ((0 != interval.rel_value) && (0 != delta.rel_value))
96     LOG (GNUNET_ERROR_TYPE_DEBUG,
97          "Stopped execution speed up\n");
98 }
99
100
101
102 /* end of speedup.c */