route.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <pthread.h>
  2. #include "Enclave_t.h"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "sort.hpp"
  6. #include "route.hpp"
  7. #define PROFILE_ROUTING
  8. struct MsgBuffer {
  9. pthread_mutex_t mutex;
  10. uint8_t *buf;
  11. // The number of messages (not bytes) in, or on their way into, the
  12. // buffer
  13. uint32_t reserved;
  14. // The number of messages definitely in the buffer
  15. uint32_t inserted;
  16. MsgBuffer() : buf(NULL), reserved(0), inserted(0) {
  17. pthread_mutex_init(&mutex, NULL);
  18. }
  19. ~MsgBuffer() {
  20. delete[] buf;
  21. }
  22. // The number passed is messages, not bytes
  23. void alloc(uint32_t msgs) {
  24. delete[] buf;
  25. buf = NULL;
  26. reserved = 0;
  27. inserted = 0;
  28. // This may throw bad_alloc, but we'll catch it higher up
  29. buf = new uint8_t[size_t(msgs) * g_teems_config.msg_size];
  30. }
  31. // Reset the contents of the buffer
  32. void reset() {
  33. memset(buf, 0, inserted * g_teems_config.msg_size);
  34. reserved = 0;
  35. inserted = 0;
  36. }
  37. // You can't copy a MsgBuffer
  38. MsgBuffer(const MsgBuffer&) = delete;
  39. MsgBuffer &operator=(const MsgBuffer&) = delete;
  40. };
  41. enum RouteStep {
  42. ROUTE_NOT_STARTED,
  43. ROUTE_ROUND_1,
  44. ROUTE_ROUND_2
  45. };
  46. // The round1 MsgBuffer stores messages we ingest while waiting for
  47. // round 1 to start, which will be sorted and sent out in round 1. The
  48. // round2 MsgBuffer stores messages we receive in round 1, which will be
  49. // padded, sorted, and sent out in round 2.
  50. static struct RouteState {
  51. MsgBuffer round1;
  52. MsgBuffer round2;
  53. RouteStep step;
  54. uint32_t tot_msg_per_ing;
  55. uint32_t max_msg_to_each_str;
  56. uint32_t max_round2_msgs;
  57. } route_state;
  58. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  59. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  60. // Call this near the end of ecall_config_load, but before
  61. // comms_init_nodestate. Returns true on success, false on failure.
  62. bool route_init()
  63. {
  64. // Compute the maximum number of messages we could receive by direct
  65. // ingestion
  66. // Each ingestion node will have at most
  67. // ceil(user_count/num_ingestion_nodes) users, and each user will
  68. // send at most m_priv_out messages.
  69. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  70. g_teems_config.num_ingestion_nodes);
  71. uint32_t tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  72. // Compute the maximum number of messages we could receive in round 1
  73. // Each ingestion node will send us an our_weight/tot_weight
  74. // fraction of the messages they hold
  75. uint32_t max_msg_from_each_ing = CEILDIV(tot_msg_per_ing,
  76. g_teems_config.tot_weight) * g_teems_config.my_weight;
  77. // And the maximum number we can receive in total is that times the
  78. // number of ingestion nodes
  79. uint32_t max_round1_msgs = max_msg_from_each_ing *
  80. g_teems_config.num_ingestion_nodes;
  81. // Compute the maximum number of messages we could send in round 2
  82. // Each storage node has at most this many users
  83. uint32_t users_per_str = CEILDIV(g_teems_config.user_count,
  84. g_teems_config.num_storage_nodes);
  85. // And so can receive at most this many messages
  86. uint32_t tot_msg_per_str = users_per_str *
  87. g_teems_config.m_priv_in;
  88. // Which will be at most this many from us
  89. uint32_t max_msg_to_each_str = CEILDIV(tot_msg_per_str,
  90. g_teems_config.tot_weight) * g_teems_config.my_weight;
  91. // But we can't send more messages to each storage server than we
  92. // could receive in total
  93. if (max_msg_to_each_str > max_round1_msgs) {
  94. max_msg_to_each_str = max_round1_msgs;
  95. }
  96. // And the max total number of outgoing messages in round 2 is then
  97. uint32_t max_round2_msgs = max_msg_to_each_str *
  98. g_teems_config.num_storage_nodes;
  99. // In case we have a weird configuration where users can send more
  100. // messages per epoch than they can receive, ensure the round 2
  101. // buffer is large enough to hold the incoming messages as well
  102. if (max_round2_msgs < max_round1_msgs) {
  103. max_round2_msgs = max_round1_msgs;
  104. }
  105. /*
  106. printf("round1_msgs = %u, round2_msgs = %u\n",
  107. max_round1_msgs, max_round2_msgs);
  108. */
  109. // Create the route state
  110. try {
  111. route_state.round1.alloc(tot_msg_per_ing);
  112. route_state.round2.alloc(max_round2_msgs);
  113. } catch (std::bad_alloc&) {
  114. printf("Memory allocation failed in route_init\n");
  115. return false;
  116. }
  117. route_state.step = ROUTE_NOT_STARTED;
  118. route_state.tot_msg_per_ing = tot_msg_per_ing;
  119. route_state.max_msg_to_each_str = max_msg_to_each_str;
  120. route_state.max_round2_msgs = max_round2_msgs;
  121. threadid_t nthreads = g_teems_config.nthreads;
  122. #ifdef PROFILE_ROUTING
  123. unsigned long start = printf_with_rtclock("begin precompute evalplans (%u,%hu) (%u,%hu)\n", tot_msg_per_ing, nthreads, max_round2_msgs, nthreads);
  124. #endif
  125. sort_precompute_evalplan(tot_msg_per_ing, nthreads);
  126. sort_precompute_evalplan(max_round2_msgs, nthreads);
  127. #ifdef PROFILE_ROUTING
  128. printf_with_rtclock_diff(start, "end precompute evalplans\n");
  129. #endif
  130. return true;
  131. }
  132. // Precompute the WaksmanNetworks needed for the sorts. If you pass -1,
  133. // it will return the number of different sizes it needs. If you pass
  134. // [0,sizes-1], it will compute one WaksmanNetwork with that size index
  135. // and return the number of available WaksmanNetworks of that size.
  136. size_t ecall_precompute_sort(int sizeidx)
  137. {
  138. size_t ret = 0;
  139. switch(sizeidx) {
  140. case 0:
  141. #ifdef PROFILE_ROUTING
  142. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.tot_msg_per_ing);
  143. #endif
  144. ret = sort_precompute(route_state.tot_msg_per_ing);
  145. #ifdef PROFILE_ROUTING
  146. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.tot_msg_per_ing);}
  147. #endif
  148. break;
  149. case 1:
  150. #ifdef PROFILE_ROUTING
  151. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.max_round2_msgs);
  152. #endif
  153. ret = sort_precompute(route_state.max_round2_msgs);
  154. #ifdef PROFILE_ROUTING
  155. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.max_round2_msgs);}
  156. #endif
  157. break;
  158. default:
  159. ret = 2;
  160. break;
  161. }
  162. return ret;
  163. }
  164. // Directly ingest a buffer of num_msgs messages into the round1 buffer.
  165. // Return true on success, false on failure.
  166. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  167. {
  168. uint16_t msg_size = g_teems_config.msg_size;
  169. MsgBuffer &round1 = route_state.round1;
  170. pthread_mutex_lock(&round1.mutex);
  171. uint32_t start = round1.reserved;
  172. if (start + num_msgs > route_state.tot_msg_per_ing) {
  173. pthread_mutex_unlock(&round1.mutex);
  174. printf("Max %u messages exceeded\n",
  175. route_state.tot_msg_per_ing);
  176. return false;
  177. }
  178. round1.reserved += num_msgs;
  179. pthread_mutex_unlock(&round1.mutex);
  180. memmove(round1.buf + start * msg_size,
  181. msgs, num_msgs * msg_size);
  182. pthread_mutex_lock(&round1.mutex);
  183. round1.inserted += num_msgs;
  184. pthread_mutex_unlock(&round1.mutex);
  185. return true;
  186. }
  187. // Send the round 1 messages
  188. static void send_round1_msgs(const uint8_t *msgs, const uint64_t *indices,
  189. uint32_t N)
  190. {
  191. uint16_t msg_size = g_teems_config.msg_size;
  192. uint16_t tot_weight = g_teems_config.tot_weight;
  193. /*
  194. for (uint32_t i=0;i<N;++i) {
  195. const uint8_t *msg = msgs + indices[i]*msg_size;
  196. for (uint16_t j=0;j<msg_size/4;++j) {
  197. printf("%08x ", ((const uint32_t*)msg)[j]);
  198. }
  199. printf("\n");
  200. }
  201. */
  202. }
  203. // Perform the next round of routing. The callback pointer will be
  204. // passed to ocall_routing_round_complete when the round is complete.
  205. void ecall_routing_proceed(void *cbpointer)
  206. {
  207. if (route_state.step == ROUTE_NOT_STARTED) {
  208. MsgBuffer &round1 = route_state.round1;
  209. pthread_mutex_lock(&round1.mutex);
  210. // Ensure there are no pending messages currently being inserted
  211. // into the buffer
  212. while (round1.reserved != round1.inserted) {
  213. pthread_mutex_unlock(&round1.mutex);
  214. pthread_mutex_lock(&round1.mutex);
  215. }
  216. // Sort the messages we've received
  217. #ifdef PROFILE_ROUTING
  218. uint32_t inserted = round1.inserted;
  219. unsigned long start = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  220. #endif
  221. sort_mtobliv(g_teems_config.nthreads, round1.buf,
  222. g_teems_config.msg_size, round1.inserted,
  223. route_state.tot_msg_per_ing, send_round1_msgs);
  224. round1.reset();
  225. #ifdef PROFILE_ROUTING
  226. printf_with_rtclock_diff(start, "end oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  227. #endif
  228. route_state.step = ROUTE_ROUND_1;
  229. ocall_routing_round_complete(cbpointer, 1);
  230. }
  231. }