start.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include <condition_variable>
  2. #include <mutex>
  3. #include <stdlib.h>
  4. #include "Untrusted.hpp"
  5. #include "start.hpp"
  6. // Default 4 epochs
  7. int num_epochs = 4;
  8. // Default epoch_duration of 5 seconds
  9. int epoch_duration = 5;
  10. // Default of 12 Waksman Networks (3 per private_route for 4 epochs)
  11. int num_WN_to_precompute = 12;
  12. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  13. class Epoch {
  14. NetIO &netio;
  15. uint32_t epoch_num;
  16. std::mutex m;
  17. std::condition_variable cv;
  18. bool epoch_complete;
  19. void round_cb(uint32_t round_num) {
  20. if (round_num) {
  21. printf("Round %u complete\n", round_num);
  22. boost::asio::post(netio.io_context(), [this]{
  23. proceed();
  24. });
  25. } else {
  26. printf("Epoch %u complete\n", epoch_num);
  27. {
  28. std::lock_guard lk(m);
  29. epoch_complete = true;
  30. }
  31. const NodeConfig &my_conf = netio.myconfig();
  32. if(my_conf.roles & ROLE_STORAGE) {
  33. boost::asio::post(netio.io_context(), [this]{
  34. netio.send_client_mailbox();
  35. });
  36. }
  37. cv.notify_one();
  38. }
  39. }
  40. public:
  41. Epoch(NetIO &netio_obj, uint32_t ep_num):
  42. netio(netio_obj), epoch_num(ep_num),
  43. epoch_complete(false) {}
  44. void proceed() {
  45. ecall_routing_proceed([this](uint32_t round_num) {
  46. round_cb(round_num);
  47. });
  48. }
  49. void wait() {
  50. std::unique_lock lk(m);
  51. cv.wait(lk, [this]{ return epoch_complete; });
  52. }
  53. };
  54. static void epoch(NetIO &netio, char **args) {
  55. static uint32_t epoch_num = 1;
  56. uint16_t num_nodes = netio.num_nodes;
  57. uint32_t num_tokens[num_nodes];
  58. uint32_t tot_tokens = 0;
  59. for (nodenum_t j=0;j<num_nodes;++j) {
  60. num_tokens[j] = atoi(args[netio.me*num_nodes+j]);
  61. tot_tokens += num_tokens[j];
  62. }
  63. const Config &config = netio.config();
  64. uint16_t msg_size = config.msg_size;
  65. nodenum_t my_node_num = config.my_node_num;
  66. uint8_t my_roles = config.nodes[my_node_num].roles;
  67. if (my_roles & ROLE_INGESTION) {
  68. uint8_t *msgs = new uint8_t[tot_tokens * msg_size];
  69. uint8_t *nextmsg = msgs;
  70. uint32_t dest_uid_mask = (1 << DEST_UID_BITS) - 1;
  71. uint32_t rem_tokens = tot_tokens;
  72. while (rem_tokens > 0) {
  73. // Pick a random remaining token
  74. uint32_t r = uint32_t(lrand48()) % rem_tokens;
  75. for (nodenum_t j=0;j<num_nodes;++j) {
  76. if (r < num_tokens[j]) {
  77. // Use a token from node j
  78. *((uint32_t*)nextmsg) =
  79. (j << DEST_UID_BITS) +
  80. ((rem_tokens-1) & dest_uid_mask);
  81. // Put a bunch of copies of r as the message body
  82. for (uint16_t i=1;i<msg_size/4;++i) {
  83. ((uint32_t*)nextmsg)[i] = r;
  84. }
  85. num_tokens[j] -= 1;
  86. rem_tokens -= 1;
  87. nextmsg += msg_size;
  88. } else {
  89. r -= num_tokens[j];
  90. }
  91. }
  92. }
  93. /*
  94. for (uint32_t i=0;i<tot_tokens;++i) {
  95. for(uint16_t j=0;j<msg_size/4;++j) {
  96. printf("%08x ", ((uint32_t*)msgs)[i*msg_size/4+j]);
  97. }
  98. printf("\n");
  99. }
  100. */
  101. if (!ecall_ingest_raw(msgs, tot_tokens)) {
  102. printf("Ingestion failed\n");
  103. return;
  104. }
  105. }
  106. Epoch epoch(netio, epoch_num);
  107. epoch.proceed();
  108. epoch.wait();
  109. // Launch threads to refill the precomputed Waksman networks we
  110. // used, but just let them run in the background.
  111. size_t num_sizes = ecall_precompute_sort(-1);
  112. for (int i=0;i<int(num_sizes);++i) {
  113. boost::thread t([i] {
  114. ecall_precompute_sort(i);
  115. });
  116. t.detach();
  117. }
  118. ++epoch_num;
  119. }
  120. static void epoch_clients(NetIO &netio) {
  121. static uint32_t epoch_num = 1;
  122. Epoch epoch(netio, epoch_num);
  123. epoch.proceed();
  124. epoch.wait();
  125. // Launch threads to refill the precomputed Waksman networks we
  126. // used, but just let them run in the background.
  127. size_t num_sizes = ecall_precompute_sort(-1);
  128. for (int i=0;i<int(num_sizes);++i) {
  129. boost::thread t([i] {
  130. ecall_precompute_sort(i);
  131. });
  132. t.detach();
  133. }
  134. ++epoch_num;
  135. }
  136. static void route_clients_test(NetIO &netio)
  137. {
  138. // Default epoch_interval is 5 sec
  139. size_t epoch_interval = epoch_duration * 1000000;
  140. printf("Epoch duration = %d\n", epoch_duration);
  141. // Sleep one epoch_interval for clients to connect
  142. usleep(epoch_interval);
  143. // Precompute some WaksmanNetworks
  144. size_t num_sizes = ecall_precompute_sort(-2);
  145. printf("Precompute num_sizes = %ld\n", num_sizes);
  146. int num_WN_per_size = int(CEILDIV(num_WN_to_precompute, num_sizes));
  147. if(num_WN_per_size <2) {
  148. num_WN_per_size = 2;
  149. }
  150. for (int i=0;i<int(num_sizes);++i) {
  151. std::vector<boost::thread> ts;
  152. // num_WN_to_precompute = num_sizes * ceil(precompute_WN/epoch_time)
  153. for (int j=0; j<num_WN_per_size; ++j) {
  154. ts.emplace_back([i] {
  155. ecall_precompute_sort(i);
  156. });
  157. }
  158. for (auto& t: ts) {
  159. t.join();
  160. }
  161. }
  162. // Run epoch
  163. for (int i=1; i<=5; ++i) {
  164. struct timespec tp;
  165. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  166. unsigned long start = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  167. epoch_clients(netio);
  168. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  169. unsigned long end = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  170. unsigned long diff = end - start;
  171. printf("client_count = %ld\n", client_count);
  172. printf("bytes_sent = %ld\n", netio.reset_bytes_sent());
  173. printf("Epoch %d time: %lu.%06lu s\n", i, diff/1000000, diff%1000000);
  174. // Sleep for the rest of the epoch interval
  175. if (diff < epoch_interval) {
  176. usleep(epoch_interval - (useconds_t) diff);
  177. }
  178. }
  179. netio.close();
  180. exit(0);
  181. }
  182. static void route_test(NetIO &netio, char **args)
  183. {
  184. // Count the number of arguments
  185. size_t nargs = 0;
  186. while (args[nargs]) {
  187. ++nargs;
  188. }
  189. uint16_t num_nodes = netio.num_nodes;
  190. size_t sq_nodes = num_nodes;
  191. sq_nodes *= sq_nodes;
  192. if (nargs != sq_nodes) {
  193. printf("Expecting %lu arguments, found %lu\n", sq_nodes, nargs);
  194. return;
  195. }
  196. // The arguments are num_nodes sets of num_nodes values. The jth
  197. // value in the ith set is the number of private routing tokens
  198. // ingestion node i holds for storage node j.
  199. // We are node i = netio.me, so ignore the other sets of values.
  200. // Precompute some WaksmanNetworks
  201. const Config &config = netio.config();
  202. size_t num_sizes = ecall_precompute_sort(-2);
  203. for (int i=0;i<int(num_sizes);++i) {
  204. std::vector<boost::thread> ts;
  205. for (int j=0; j<config.nthreads; ++j) {
  206. ts.emplace_back([i] {
  207. ecall_precompute_sort(i);
  208. });
  209. }
  210. for (auto& t: ts) {
  211. t.join();
  212. }
  213. }
  214. // The epoch interval, in microseconds
  215. uint32_t epoch_interval_us = 1000000;
  216. // Run 10 epochs
  217. for (int i=0; i<10; ++i) {
  218. struct timespec tp;
  219. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  220. unsigned long start = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  221. epoch(netio, args);
  222. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  223. unsigned long end = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  224. unsigned long diff = end - start;
  225. printf("Epoch time: %lu.%06lu s\n", diff/1000000, diff%1000000);
  226. // Sleep for the rest of the epoch interval
  227. if (diff < epoch_interval_us) {
  228. usleep(epoch_interval_us - (useconds_t)diff);
  229. }
  230. }
  231. netio.close();
  232. }
  233. // Once all the networking is set up, start doing whatever we were asked
  234. // to do on the command line
  235. void start(NetIO &netio, char **args)
  236. {
  237. if (*args && !strcmp(*args, "route")) {
  238. ++args;
  239. route_test(netio, args);
  240. return;
  241. }
  242. if (*args && !strcmp(*args, "route_clients")) {
  243. ++args;
  244. printf("num_epochs = %d, epoch_duration = %d, num_WN_to_precompute = %d\n",
  245. num_epochs, epoch_duration, num_WN_to_precompute);
  246. route_clients_test(netio);
  247. return;
  248. }
  249. }