route.cpp 7.7 KB

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