start.cpp 10 KB

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