start.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 route_test(NetIO &netio, char **args)
  108. {
  109. // Count the number of arguments
  110. size_t nargs = 0;
  111. while (args[nargs]) {
  112. ++nargs;
  113. }
  114. uint16_t num_nodes = netio.num_nodes;
  115. size_t sq_nodes = num_nodes;
  116. sq_nodes *= sq_nodes;
  117. if (nargs != sq_nodes) {
  118. printf("Expecting %lu arguments, found %lu\n", sq_nodes, nargs);
  119. return;
  120. }
  121. // The arguments are num_nodes sets of num_nodes values. The jth
  122. // value in the ith set is the number of private routing tokens
  123. // ingestion node i holds for storage node j.
  124. // We are node i = netio.me, so ignore the other sets of values.
  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. // The epoch interval, in microseconds
  140. uint32_t epoch_interval_us = 1000000;
  141. // Run 10 epochs
  142. for (int i=0; i<10; ++i) {
  143. struct timespec tp;
  144. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  145. unsigned long start = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  146. epoch(netio, args);
  147. clock_gettime(CLOCK_REALTIME_COARSE, &tp);
  148. unsigned long end = tp.tv_sec * 1000000 + tp.tv_nsec/1000;
  149. unsigned long diff = end - start;
  150. printf("Epoch time: %lu.%06lu s\n", diff/1000000, diff%1000000);
  151. // Sleep for the rest of the epoch interval
  152. if (diff < epoch_interval_us) {
  153. usleep(epoch_interval_us - (useconds_t)diff);
  154. }
  155. }
  156. netio.close();
  157. }
  158. // Once all the networking is set up, start doing whatever we were asked
  159. // to do on the command line
  160. void start(NetIO &netio, char **args)
  161. {
  162. if (*args && !strcmp(*args, "route")) {
  163. ++args;
  164. route_test(netio, args);
  165. return;
  166. }
  167. }