2 This file is part of GNUnet.
3 (C) 2010-2013 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.
21 * @file ats-tests/ats-testing-preferences.c
22 * @brief ats benchmark: preference generator
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "ats-testing.h"
30 static struct PreferenceGenerator *pg_head;
31 static struct PreferenceGenerator *pg_tail;
33 extern struct GNUNET_ATS_TEST_Topology *top;
36 get_preference (struct PreferenceGenerator *pg)
38 struct GNUNET_TIME_Relative time_delta;
42 /* Calculate the current preference value */
44 case GNUNET_ATS_TEST_TG_CONSTANT:
45 pref_value = pg->base_value;
47 case GNUNET_ATS_TEST_TG_LINEAR:
48 time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
49 /* Calculate point of time in the current period */
50 time_delta.rel_value_us = time_delta.rel_value_us %
51 pg->duration_period.rel_value_us;
52 delta_value = ((double) time_delta.rel_value_us /
53 pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
54 if ((pg->max_value < pg->base_value) &&
55 ((pg->max_value - pg->base_value) > pg->base_value))
57 /* This will cause an underflow */
60 pref_value = pg->base_value + delta_value;
62 case GNUNET_ATS_TEST_TG_RANDOM:
63 delta_value = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
64 10000 * (pg->max_value - pg->base_value)) / 10000;
65 pref_value = pg->base_value + delta_value;
67 case GNUNET_ATS_TEST_TG_SINUS:
68 time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
69 /* Calculate point of time in the current period */
70 time_delta.rel_value_us = time_delta.rel_value_us %
71 pg->duration_period.rel_value_us;
72 if ((pg->max_value - pg->base_value) > pg->base_value)
74 /* This will cause an underflow for second half of sinus period,
75 * will be detected in general when experiments are loaded */
78 delta_value = (pg->max_value - pg->base_value) *
79 sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
80 time_delta.rel_value_us);
81 pref_value = pg->base_value + delta_value;
87 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current preference value is %f\n",
94 set_pref_task (void *cls,
95 const struct GNUNET_SCHEDULER_TaskContext *tc)
97 struct BenchmarkPartner *p = cls;
99 p->pg->set_task = GNUNET_SCHEDULER_NO_TASK;
101 pref_value = get_preference (p->pg);
103 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
104 "Setting preference for master [%u] and slave [%u] for %s to %f\n",
105 p->me->no, p->dest->no,
106 GNUNET_ATS_print_preference_type (p->pg->kind), pref_value);
108 GNUNET_ATS_performance_change_preference(p->me->ats_perf_handle,
109 &p->dest->id, p->pg->kind, pref_value, GNUNET_ATS_PREFERENCE_END);
111 switch (p->pg->kind) {
112 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
113 p->pref_bandwidth = pref_value;
115 case GNUNET_ATS_PREFERENCE_LATENCY:
116 p->pref_delay = pref_value;
122 p->pg->set_task = GNUNET_SCHEDULER_add_delayed (p->pg->frequency,
129 * Generate between the source master and the partner and set preferences with a
130 * value depending on the generator.
133 * @param dest partner
134 * @param type type of preferences to generate
135 * @param base_rate traffic base rate to send data with
136 * @param max_rate traffic maximum rate to send data with
137 * @param period duration of a period of traffic generation (~ 1/frequency)
138 * @param duration how long to generate traffic
139 * @return the traffic generator
141 struct PreferenceGenerator *
142 GNUNET_ATS_TEST_generate_preferences_start (struct BenchmarkPeer *src,
143 struct BenchmarkPartner *dest,
144 enum GeneratorType type,
147 struct GNUNET_TIME_Relative period,
148 struct GNUNET_TIME_Relative frequency,
149 enum GNUNET_ATS_PreferenceKind kind)
151 struct PreferenceGenerator *pg;
153 if (NULL != dest->pg)
159 pg = GNUNET_new (struct PreferenceGenerator);
160 GNUNET_CONTAINER_DLL_insert (pg_head, pg_tail, pg);
165 pg->base_value = base_value;
166 pg->max_value = value_rate;
167 pg->duration_period = period;
168 pg->frequency = frequency;
169 pg->time_start = GNUNET_TIME_absolute_get();
172 case GNUNET_ATS_TEST_TG_CONSTANT:
173 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
174 "Setting up constant preference generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
175 dest->me->no, GNUNET_i2s (&dest->me->id),
176 dest->dest->no, GNUNET_i2s (&dest->dest->id),
179 case GNUNET_ATS_TEST_TG_LINEAR:
180 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
181 "Setting up linear preference generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bips\n",
182 dest->me->no, GNUNET_i2s (&dest->me->id),
183 dest->dest->no, GNUNET_i2s (&dest->dest->id),
184 base_value, value_rate);
186 case GNUNET_ATS_TEST_TG_SINUS:
187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188 "Setting up sinus preference generator master[%u] `%s' and slave [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
189 dest->me->no, GNUNET_i2s (&dest->me->id),
190 dest->dest->no, GNUNET_i2s (&dest->dest->id),
191 base_value, value_rate);
193 case GNUNET_ATS_TEST_TG_RANDOM:
194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
195 "Setting up random preference generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bps\n",
196 dest->me->no, GNUNET_i2s (&dest->me->id),
197 dest->dest->no, GNUNET_i2s (&dest->dest->id),
198 base_value, value_rate);
205 pg->set_task = GNUNET_SCHEDULER_add_now (&set_pref_task, dest);
211 GNUNET_ATS_TEST_generate_preferences_stop (struct PreferenceGenerator *pg)
213 GNUNET_CONTAINER_DLL_remove (pg_head, pg_tail, pg);
216 if (GNUNET_SCHEDULER_NO_TASK != pg->set_task)
218 GNUNET_SCHEDULER_cancel (pg->set_task);
219 pg->set_task = GNUNET_SCHEDULER_NO_TASK;
227 * Stop all preferences generators
230 GNUNET_ATS_TEST_generate_preferences_stop_all ()
232 struct PreferenceGenerator *cur;
233 struct PreferenceGenerator *next;
235 for (cur = next; NULL != cur; cur = next)
238 GNUNET_ATS_TEST_generate_preferences_stop(cur);
242 /* end of file ats-testing-preferences.c */