start.cpp 7.3 KB

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