start.cpp 7.2 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. ((rem_tokens-1) & 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(-2);
  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. // Run epoch
  140. for (int i=0; i<3; ++i) {
  141. if(i==0) {
  142. usleep(EPOCH_INTERVAL);
  143. }
  144. struct timespec tp;
  145. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  146. unsigned long start = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  147. epoch_clients(netio);
  148. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  149. unsigned long end = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  150. unsigned long diff = end - start;
  151. printf("Epoch time: %lu.%06lu s\n", diff/1000000, diff%1000000);
  152. // Sleep for the rest of the epoch interval
  153. if (diff < EPOCH_INTERVAL) {
  154. usleep(EPOCH_INTERVAL - (useconds_t) diff);
  155. }
  156. }
  157. netio.close();
  158. }
  159. static void route_test(NetIO &netio, char **args)
  160. {
  161. // Count the number of arguments
  162. size_t nargs = 0;
  163. while (args[nargs]) {
  164. ++nargs;
  165. }
  166. uint16_t num_nodes = netio.num_nodes;
  167. size_t sq_nodes = num_nodes;
  168. sq_nodes *= sq_nodes;
  169. if (nargs != sq_nodes) {
  170. printf("Expecting %lu arguments, found %lu\n", sq_nodes, nargs);
  171. return;
  172. }
  173. // The arguments are num_nodes sets of num_nodes values. The jth
  174. // value in the ith set is the number of private routing tokens
  175. // ingestion node i holds for storage node j.
  176. // We are node i = netio.me, so ignore the other sets of values.
  177. // Precompute some WaksmanNetworks
  178. const Config &config = netio.config();
  179. size_t num_sizes = ecall_precompute_sort(-2);
  180. for (int i=0;i<int(num_sizes);++i) {
  181. std::vector<boost::thread> ts;
  182. for (int j=0; j<config.nthreads; ++j) {
  183. ts.emplace_back([i] {
  184. ecall_precompute_sort(i);
  185. });
  186. }
  187. for (auto& t: ts) {
  188. t.join();
  189. }
  190. }
  191. // The epoch interval, in microseconds
  192. uint32_t epoch_interval_us = 1000000;
  193. // Run 10 epochs
  194. for (int i=0; i<10; ++i) {
  195. struct timespec tp;
  196. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  197. unsigned long start = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  198. epoch(netio, args);
  199. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  200. unsigned long end = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  201. unsigned long diff = end - start;
  202. printf("Epoch time: %lu.%06lu s\n", diff/1000000, diff%1000000);
  203. // Sleep for the rest of the epoch interval
  204. if (diff < epoch_interval_us) {
  205. usleep(epoch_interval_us - (useconds_t)diff);
  206. }
  207. }
  208. netio.close();
  209. }
  210. // Once all the networking is set up, start doing whatever we were asked
  211. // to do on the command line
  212. void start(NetIO &netio, char **args)
  213. {
  214. if (*args && !strcmp(*args, "route")) {
  215. ++args;
  216. route_test(netio, args);
  217. return;
  218. }
  219. if (*args && !strcmp(*args, "route_clients")) {
  220. ++args;
  221. route_clients_test(netio);
  222. return;
  223. }
  224. }