ats-testing-preferences.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file ats-tests/ats-testing-preferences.c
  19. * @brief ats benchmark: preference generator
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "ats-testing.h"
  26. static struct PreferenceGenerator *pg_head;
  27. static struct PreferenceGenerator *pg_tail;
  28. extern struct GNUNET_ATS_TEST_Topology *top;
  29. static double
  30. get_preference (struct PreferenceGenerator *pg)
  31. {
  32. struct GNUNET_TIME_Relative time_delta;
  33. double delta_value;
  34. double pref_value;
  35. /* Calculate the current preference value */
  36. switch (pg->type) {
  37. case GNUNET_ATS_TEST_TG_CONSTANT:
  38. pref_value = pg->base_value;
  39. break;
  40. case GNUNET_ATS_TEST_TG_LINEAR:
  41. time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
  42. /* Calculate point of time in the current period */
  43. time_delta.rel_value_us = time_delta.rel_value_us %
  44. pg->duration_period.rel_value_us;
  45. delta_value = ((double) time_delta.rel_value_us /
  46. pg->duration_period.rel_value_us) * (pg->max_value - pg->base_value);
  47. if ((pg->max_value < pg->base_value) &&
  48. ((pg->max_value - pg->base_value) > pg->base_value))
  49. {
  50. /* This will cause an underflow */
  51. GNUNET_break (0);
  52. }
  53. pref_value = pg->base_value + delta_value;
  54. break;
  55. case GNUNET_ATS_TEST_TG_RANDOM:
  56. delta_value = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  57. 10000 * (pg->max_value - pg->base_value)) / 10000;
  58. pref_value = pg->base_value + delta_value;
  59. break;
  60. case GNUNET_ATS_TEST_TG_SINUS:
  61. time_delta = GNUNET_TIME_absolute_get_duration(pg->time_start);
  62. /* Calculate point of time in the current period */
  63. time_delta.rel_value_us = time_delta.rel_value_us %
  64. pg->duration_period.rel_value_us;
  65. if ((pg->max_value - pg->base_value) > pg->base_value)
  66. {
  67. /* This will cause an underflow for second half of sinus period,
  68. * will be detected in general when experiments are loaded */
  69. GNUNET_break (0);
  70. }
  71. delta_value = (pg->max_value - pg->base_value) *
  72. sin ( (2 * M_PI) / ((double) pg->duration_period.rel_value_us) *
  73. time_delta.rel_value_us);
  74. pref_value = pg->base_value + delta_value;
  75. break;
  76. default:
  77. pref_value = 0.0;
  78. break;
  79. }
  80. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current preference value is %f\n",
  81. pref_value);
  82. return pref_value;
  83. }
  84. static void
  85. set_pref_task (void *cls,
  86. const struct GNUNET_SCHEDULER_TaskContext *tc)
  87. {
  88. struct BenchmarkPartner *p = cls;
  89. double pref_value;
  90. p->pg->set_task = NULL;
  91. pref_value = get_preference (p->pg);
  92. GNUNET_log(GNUNET_ERROR_TYPE_INFO,
  93. "Setting preference for master [%u] and slave [%u] for %s to %f\n",
  94. p->me->no, p->dest->no,
  95. GNUNET_ATS_print_preference_type (p->pg->kind), pref_value);
  96. GNUNET_ATS_performance_change_preference(p->me->ats_perf_handle,
  97. &p->dest->id, p->pg->kind, pref_value, GNUNET_ATS_PREFERENCE_END);
  98. switch (p->pg->kind) {
  99. case GNUNET_ATS_PREFERENCE_BANDWIDTH:
  100. p->pref_bandwidth = pref_value;
  101. break;
  102. case GNUNET_ATS_PREFERENCE_LATENCY:
  103. p->pref_delay = pref_value;
  104. break;
  105. default:
  106. break;
  107. }
  108. p->pg->set_task = GNUNET_SCHEDULER_add_delayed (p->pg->frequency,
  109. set_pref_task, p);
  110. }
  111. /**
  112. * Generate between the source master and the partner and set preferences with a
  113. * value depending on the generator.
  114. *
  115. * @param src source
  116. * @param dest partner
  117. * @param type type of preferences to generate
  118. * @param base_value traffic base rate to send data with
  119. * @param value_rate traffic maximum rate to send data with
  120. * @param period duration of a period of preferences generation (~ 1/frequency)
  121. * @param frequency how long to generate preferences
  122. * @param kind ATS preference to generate
  123. * @return the preference generator
  124. */
  125. struct PreferenceGenerator *
  126. GNUNET_ATS_TEST_generate_preferences_start (struct BenchmarkPeer *src,
  127. struct BenchmarkPartner *dest,
  128. enum GeneratorType type,
  129. long int base_value,
  130. long int value_rate,
  131. struct GNUNET_TIME_Relative period,
  132. struct GNUNET_TIME_Relative frequency,
  133. enum GNUNET_ATS_PreferenceKind kind)
  134. {
  135. struct PreferenceGenerator *pg;
  136. if (NULL != dest->pg)
  137. {
  138. GNUNET_break (0);
  139. return NULL;
  140. }
  141. pg = GNUNET_new (struct PreferenceGenerator);
  142. GNUNET_CONTAINER_DLL_insert (pg_head, pg_tail, pg);
  143. pg->type = type;
  144. pg->src = src;
  145. pg->dest = dest;
  146. pg->kind = kind;
  147. pg->base_value = base_value;
  148. pg->max_value = value_rate;
  149. pg->duration_period = period;
  150. pg->frequency = frequency;
  151. pg->time_start = GNUNET_TIME_absolute_get();
  152. switch (type) {
  153. case GNUNET_ATS_TEST_TG_CONSTANT:
  154. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  155. "Setting up constant preference generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
  156. dest->me->no, GNUNET_i2s (&dest->me->id),
  157. dest->dest->no, GNUNET_i2s (&dest->dest->id),
  158. base_value);
  159. break;
  160. case GNUNET_ATS_TEST_TG_LINEAR:
  161. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  162. "Setting up linear preference generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bips\n",
  163. dest->me->no, GNUNET_i2s (&dest->me->id),
  164. dest->dest->no, GNUNET_i2s (&dest->dest->id),
  165. base_value, value_rate);
  166. break;
  167. case GNUNET_ATS_TEST_TG_SINUS:
  168. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  169. "Setting up sinus preference generator master[%u] `%s' and slave [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
  170. dest->me->no, GNUNET_i2s (&dest->me->id),
  171. dest->dest->no, GNUNET_i2s (&dest->dest->id),
  172. base_value, value_rate);
  173. break;
  174. case GNUNET_ATS_TEST_TG_RANDOM:
  175. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  176. "Setting up random preference generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bps\n",
  177. dest->me->no, GNUNET_i2s (&dest->me->id),
  178. dest->dest->no, GNUNET_i2s (&dest->dest->id),
  179. base_value, value_rate);
  180. break;
  181. default:
  182. break;
  183. }
  184. dest->pg = pg;
  185. pg->set_task = GNUNET_SCHEDULER_add_now (&set_pref_task, dest);
  186. return pg;
  187. }
  188. void
  189. GNUNET_ATS_TEST_generate_preferences_stop (struct PreferenceGenerator *pg)
  190. {
  191. GNUNET_CONTAINER_DLL_remove (pg_head, pg_tail, pg);
  192. pg->dest->pg = NULL;
  193. if (NULL != pg->set_task)
  194. {
  195. GNUNET_SCHEDULER_cancel (pg->set_task);
  196. pg->set_task = NULL;
  197. }
  198. GNUNET_free (pg);
  199. }
  200. /**
  201. * Stop all preferences generators
  202. */
  203. void
  204. GNUNET_ATS_TEST_generate_preferences_stop_all ()
  205. {
  206. struct PreferenceGenerator *cur;
  207. struct PreferenceGenerator *next;
  208. next = pg_head;
  209. for (cur = next; NULL != cur; cur = next)
  210. {
  211. next = cur->next;
  212. GNUNET_ATS_TEST_generate_preferences_stop(cur);
  213. }
  214. }
  215. /* end of file ats-testing-preferences.c */