ats-testing-traffic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2013, 2016 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-traffic.c
  18. * @brief ats benchmark: traffic 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 TrafficGenerator *tg_head;
  26. static struct TrafficGenerator *tg_tail;
  27. extern struct GNUNET_ATS_TEST_Topology *top;
  28. static struct GNUNET_TIME_Relative
  29. get_delay (struct TrafficGenerator *tg)
  30. {
  31. struct GNUNET_TIME_Relative delay;
  32. struct GNUNET_TIME_Relative time_delta;
  33. long long int cur_rate;
  34. long long int delta_rate;
  35. delay.rel_value_us = 0;
  36. /* Calculate the current transmission rate based on the type of traffic */
  37. switch (tg->type) {
  38. case GNUNET_ATS_TEST_TG_CONSTANT:
  39. if (UINT32_MAX == tg->base_rate)
  40. return GNUNET_TIME_UNIT_ZERO;
  41. cur_rate = tg->base_rate;
  42. break;
  43. case GNUNET_ATS_TEST_TG_LINEAR:
  44. time_delta = GNUNET_TIME_absolute_get_duration(tg->time_start);
  45. /* Calculate point of time in the current period */
  46. time_delta.rel_value_us = time_delta.rel_value_us % tg->duration_period.rel_value_us;
  47. delta_rate = ((double) time_delta.rel_value_us / tg->duration_period.rel_value_us) *
  48. (tg->max_rate - tg->base_rate);
  49. if ((tg->max_rate < tg->base_rate) && ((tg->max_rate - tg->base_rate) > tg->base_rate))
  50. {
  51. /* This will cause an underflow */
  52. GNUNET_break (0);
  53. }
  54. cur_rate = tg->base_rate + delta_rate;
  55. break;
  56. case GNUNET_ATS_TEST_TG_RANDOM:
  57. cur_rate = tg->base_rate + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  58. tg->max_rate - tg->base_rate);
  59. break;
  60. case GNUNET_ATS_TEST_TG_SINUS:
  61. time_delta = GNUNET_TIME_absolute_get_duration(tg->time_start);
  62. /* Calculate point of time in the current period */
  63. time_delta.rel_value_us = time_delta.rel_value_us % tg->duration_period.rel_value_us;
  64. if ((tg->max_rate - tg->base_rate) > tg->base_rate)
  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_rate = (tg->max_rate - tg->base_rate) *
  71. sin ( (2 * M_PI) / ((double) tg->duration_period.rel_value_us) * time_delta.rel_value_us);
  72. cur_rate = tg->base_rate + delta_rate;
  73. break;
  74. default:
  75. return delay;
  76. break;
  77. }
  78. if (cur_rate < 0)
  79. {
  80. cur_rate = 1;
  81. }
  82. /* Calculate the delay for the next message based on the current delay */
  83. delay.rel_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us * TEST_MESSAGE_SIZE / cur_rate;
  84. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  85. "Current rate is %lld, calculated delay is %llu\n",
  86. cur_rate,
  87. (unsigned long long) delay.rel_value_us);
  88. return delay;
  89. }
  90. static void
  91. update_ping_data (void *cls)
  92. {
  93. struct BenchmarkPartner *p = cls;
  94. struct GNUNET_TIME_Relative delay;
  95. p->messages_sent++;
  96. p->bytes_sent += TEST_MESSAGE_SIZE;
  97. p->me->total_messages_sent++;
  98. p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
  99. if (NULL == p->tg)
  100. {
  101. GNUNET_break (0);
  102. return;
  103. }
  104. delay = get_delay (p->tg);
  105. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  106. "Delay for next transmission %s\n",
  107. GNUNET_STRINGS_relative_time_to_string (delay,
  108. GNUNET_YES));
  109. p->tg->next_ping_transmission
  110. = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
  111. delay);
  112. }
  113. static void
  114. comm_schedule_send (void *cls)
  115. {
  116. struct BenchmarkPartner *p = cls;
  117. struct TestMessage *msg;
  118. struct GNUNET_MQ_Envelope *env;
  119. p->tg->send_task = NULL;
  120. p->last_message_sent = GNUNET_TIME_absolute_get();
  121. env = GNUNET_MQ_msg (msg,
  122. TEST_MESSAGE_TYPE_PING);
  123. memset (msg->padding,
  124. 'a',
  125. sizeof (msg->padding));
  126. GNUNET_MQ_notify_sent (env,
  127. &update_ping_data,
  128. p);
  129. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  130. "Master [%u]: Sending PING to [%u]\n",
  131. p->me->no,
  132. p->dest->no);
  133. GNUNET_MQ_send (p->mq,
  134. env);
  135. }
  136. static void
  137. update_pong_data (void *cls)
  138. {
  139. struct BenchmarkPartner *p = cls;
  140. p->messages_sent++;
  141. p->bytes_sent += TEST_MESSAGE_SIZE;
  142. p->me->total_messages_sent++;
  143. p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
  144. }
  145. void
  146. GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p)
  147. {
  148. struct TestMessage *msg;
  149. struct GNUNET_MQ_Envelope *env;
  150. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  151. "Slave [%u]: Received PING from [%u], sending PONG\n",
  152. p->me->no,
  153. p->dest->no);
  154. p->messages_received++;
  155. p->bytes_received += TEST_MESSAGE_SIZE;
  156. p->me->total_messages_received++;
  157. p->me->total_bytes_received += TEST_MESSAGE_SIZE;
  158. env = GNUNET_MQ_msg (msg,
  159. TEST_MESSAGE_TYPE_PING);
  160. memset (msg->padding,
  161. 'a',
  162. sizeof (msg->padding));
  163. GNUNET_MQ_notify_sent (env,
  164. &update_pong_data,
  165. p);
  166. GNUNET_MQ_send (p->mq,
  167. env);
  168. }
  169. void
  170. GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p)
  171. {
  172. struct GNUNET_TIME_Relative left;
  173. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  174. "Master [%u]: Received PONG from [%u], next message\n",
  175. p->me->no,
  176. p->dest->no);
  177. p->messages_received++;
  178. p->bytes_received += TEST_MESSAGE_SIZE;
  179. p->me->total_messages_received++;
  180. p->me->total_bytes_received += TEST_MESSAGE_SIZE;
  181. p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
  182. GNUNET_TIME_absolute_get()).rel_value_us;
  183. /* Schedule next send event */
  184. if (NULL == p->tg)
  185. return;
  186. left = GNUNET_TIME_absolute_get_remaining(p->tg->next_ping_transmission);
  187. if (UINT32_MAX == p->tg->base_rate)
  188. {
  189. p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
  190. }
  191. else if (0 == left.rel_value_us)
  192. {
  193. p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
  194. }
  195. else
  196. {
  197. /* Enforce minimum transmission rate 1 msg / sec */
  198. if (GNUNET_TIME_UNIT_SECONDS.rel_value_us == (left = GNUNET_TIME_relative_min (left, GNUNET_TIME_UNIT_SECONDS)).rel_value_us)
  199. GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
  200. "Enforcing minimum send rate between master [%u] and slave [%u]\n",
  201. p->me->no, p->dest->no);
  202. p->tg->send_task = GNUNET_SCHEDULER_add_delayed (left,
  203. &comm_schedule_send, p);
  204. }
  205. }
  206. /**
  207. * Generate between the source master and the partner and send traffic with a
  208. * maximum rate.
  209. *
  210. * @param src traffic source
  211. * @param dest traffic partner
  212. * @param type type of traffic to generate
  213. * @param base_rate traffic base rate to send data with
  214. * @param max_rate traffic maximum rate to send data with
  215. * @param period duration of a period of traffic generation (~ 1/frequency)
  216. * @param duration how long to generate traffic
  217. * @return the traffic generator
  218. */
  219. struct TrafficGenerator *
  220. GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
  221. struct BenchmarkPartner *dest,
  222. enum GeneratorType type,
  223. unsigned int base_rate,
  224. unsigned int max_rate,
  225. struct GNUNET_TIME_Relative period,
  226. struct GNUNET_TIME_Relative duration)
  227. {
  228. struct TrafficGenerator *tg;
  229. if (NULL != dest->tg)
  230. {
  231. GNUNET_break (0);
  232. return NULL;
  233. }
  234. tg = GNUNET_new (struct TrafficGenerator);
  235. GNUNET_CONTAINER_DLL_insert (tg_head,
  236. tg_tail,
  237. tg);
  238. tg->type = type;
  239. tg->src = src;
  240. tg->dest = dest;
  241. tg->base_rate = base_rate;
  242. tg->max_rate = max_rate;
  243. tg->duration_period = period;
  244. tg->time_start = GNUNET_TIME_absolute_get();
  245. tg->next_ping_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
  246. switch (type) {
  247. case GNUNET_ATS_TEST_TG_CONSTANT:
  248. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  249. "Setting up constant traffic generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
  250. dest->me->no,
  251. GNUNET_i2s (&dest->me->id),
  252. dest->dest->no,
  253. GNUNET_i2s (&dest->dest->id),
  254. base_rate);
  255. break;
  256. case GNUNET_ATS_TEST_TG_LINEAR:
  257. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  258. "Setting up linear traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bips\n",
  259. dest->me->no,
  260. GNUNET_i2s (&dest->me->id),
  261. dest->dest->no,
  262. GNUNET_i2s (&dest->dest->id),
  263. base_rate,
  264. max_rate);
  265. break;
  266. case GNUNET_ATS_TEST_TG_SINUS:
  267. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  268. "Setting up sinus traffic generator master[%u] `%s' and slave [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
  269. dest->me->no,
  270. GNUNET_i2s (&dest->me->id),
  271. dest->dest->no,
  272. GNUNET_i2s (&dest->dest->id),
  273. base_rate,
  274. max_rate);
  275. break;
  276. case GNUNET_ATS_TEST_TG_RANDOM:
  277. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  278. "Setting up random traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bps\n",
  279. dest->me->no,
  280. GNUNET_i2s (&dest->me->id),
  281. dest->dest->no,
  282. GNUNET_i2s (&dest->dest->id),
  283. base_rate,
  284. max_rate);
  285. break;
  286. default:
  287. break;
  288. }
  289. dest->tg = tg;
  290. tg->send_task
  291. = GNUNET_SCHEDULER_add_now (&comm_schedule_send,
  292. dest);
  293. return tg;
  294. }
  295. void
  296. GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg)
  297. {
  298. GNUNET_CONTAINER_DLL_remove (tg_head,
  299. tg_tail,
  300. tg);
  301. tg->dest->tg = NULL;
  302. if (NULL != tg->send_task)
  303. {
  304. GNUNET_SCHEDULER_cancel (tg->send_task);
  305. tg->send_task = NULL;
  306. }
  307. GNUNET_free (tg);
  308. }
  309. /**
  310. * Stop all traffic generators
  311. */
  312. void
  313. GNUNET_ATS_TEST_generate_traffic_stop_all ()
  314. {
  315. struct TrafficGenerator *cur;
  316. struct TrafficGenerator *next;
  317. next = tg_head;
  318. for (cur = next; NULL != cur; cur = next)
  319. {
  320. next = cur->next;
  321. GNUNET_ATS_TEST_generate_traffic_stop(cur);
  322. }
  323. }
  324. /* end of file ats-testing-traffic.c */