ats-testing-preferences.c 8.0 KB

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