udpsha.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "udplib.hpp"
  14. #include "udpsha.hpp"
  15. #include "jsocket.hpp"
  16. #include "jlog.hpp"
  17. #include "roxie.hpp"
  18. #include "roxiemem.hpp"
  19. #ifdef _WIN32
  20. #include <winsock2.h>
  21. #else
  22. #include <sys/socket.h>
  23. #endif
  24. using roxiemem::DataBuffer;
  25. using roxiemem::IDataBufferManager;
  26. IDataBufferManager *bufferManager;
  27. unsigned udpTraceLevel = 0;
  28. unsigned udpTraceCategories = (unsigned) -1;
  29. unsigned udpFlowSocketsSize = 131072;
  30. unsigned udpLocalWriteSocketSize = 1024000;
  31. unsigned udpSnifferReadThreadPriority = 3;
  32. unsigned udpSnifferSendThreadPriority = 3;
  33. unsigned multicastTTL = 1;
  34. MODULE_INIT(INIT_PRIORITY_STANDARD)
  35. {
  36. bufferManager = roxiemem::createDataBufferManager(roxiemem::DATA_ALIGNMENT_SIZE);
  37. return true;
  38. }
  39. MODULE_EXIT()
  40. {
  41. bufferManager->Release();
  42. }
  43. const IpAddress &ServerIdentifier::getNodeAddress() const
  44. {
  45. return serverIp;
  46. }
  47. ServerIdentifier myNode;
  48. //---------------------------------------------------------------------------------------------
  49. void queue_t::set_queue_size(unsigned int queue_s)
  50. {
  51. queue_size = queue_s;
  52. element_count = queue_size;
  53. elements = new queue_element[queue_size];
  54. free_space.signal(queue_size);
  55. active_buffers = 0;
  56. first = 0;
  57. last = 0;
  58. }
  59. queue_t::queue_t(unsigned int queue_s)
  60. {
  61. set_queue_size(queue_s);
  62. signal_free_sl = 0;
  63. }
  64. queue_t::queue_t()
  65. {
  66. signal_free_sl = 0;
  67. queue_size = 0;
  68. element_count = 0;
  69. elements = nullptr;
  70. active_buffers = 0;
  71. first = 0;
  72. last = 0;
  73. }
  74. queue_t::~queue_t()
  75. {
  76. delete [] elements;
  77. }
  78. int queue_t::free_slots()
  79. {
  80. int res=0;
  81. while (!res)
  82. {
  83. c_region.enter();
  84. res = queue_size - active_buffers;
  85. if (!res)
  86. signal_free_sl++;
  87. c_region.leave();
  88. if (!res)
  89. {
  90. while (!free_sl.wait(3000))
  91. {
  92. if (udpTraceLevel >= 1)
  93. DBGLOG("queue_t::free_slots blocked for 3 seconds waiting for free_sl semaphore");
  94. }
  95. }
  96. }
  97. return res;
  98. }
  99. void queue_t::interrupt()
  100. {
  101. data_avail.interrupt();
  102. }
  103. void queue_t::pushOwn(DataBuffer *buf)
  104. {
  105. while (!free_space.wait(3000))
  106. {
  107. if (udpTraceLevel >= 1)
  108. DBGLOG("queue_t::pushOwn blocked for 3 seconds waiting for free_space semaphore, activeBuffers == %d", active_buffers);
  109. }
  110. c_region.enter();
  111. int next = (last + 1) % element_count;
  112. elements[last].data = buf;
  113. last = next;
  114. active_buffers++;
  115. c_region.leave();
  116. data_avail.signal();
  117. }
  118. DataBuffer *queue_t::pop(bool block)
  119. {
  120. if (!data_avail.wait(block ? INFINITE : 0))
  121. return nullptr;
  122. DataBuffer *ret = NULL;
  123. bool must_signal;
  124. {
  125. CriticalBlock b(c_region);
  126. if (!active_buffers)
  127. return NULL;
  128. ret = elements[first].data;
  129. first = (first + 1) % element_count;
  130. active_buffers--;
  131. must_signal = signal_free_sl>0;
  132. if (must_signal)
  133. signal_free_sl--;
  134. }
  135. free_space.signal();
  136. if (must_signal)
  137. free_sl.signal();
  138. return ret;
  139. }
  140. unsigned queue_t::removeData(const void *key, PKT_CMP_FUN pkCmpFn)
  141. {
  142. unsigned removed = 0;
  143. unsigned signalFree = 0;
  144. unsigned signalFreeSlots = 0;
  145. {
  146. CriticalBlock b(c_region);
  147. if (active_buffers)
  148. {
  149. unsigned destix = first;
  150. unsigned ix = first;
  151. for (;;)
  152. {
  153. if (elements[ix].data && (!key || !pkCmpFn || pkCmpFn((const void*) elements[ix].data, key)))
  154. {
  155. ::Release(elements[ix].data);
  156. signalFree++;
  157. active_buffers--;
  158. removed++;
  159. }
  160. else
  161. elements[destix++] = elements[ix];
  162. ix++;
  163. if (ix==element_count)
  164. ix = 0;
  165. if (destix==element_count)
  166. destix = 0;
  167. if (ix == last)
  168. break;
  169. }
  170. if (signalFree && signal_free_sl)
  171. {
  172. signal_free_sl--;
  173. signalFreeSlots++;
  174. }
  175. last = destix;
  176. }
  177. }
  178. if (signalFree)
  179. free_space.signal(signalFree);
  180. if (signalFreeSlots)
  181. free_sl.signal(signalFreeSlots);
  182. return removed;
  183. }
  184. bool queue_t::dataQueued(const void *key, PKT_CMP_FUN pkCmpFn)
  185. {
  186. bool ret = false;
  187. CriticalBlock b(c_region);
  188. if (active_buffers)
  189. {
  190. unsigned ix = first;
  191. for (;;)
  192. {
  193. if (elements[ix].data && pkCmpFn((const void*) elements[ix].data, key))
  194. {
  195. ret = true;
  196. break;
  197. }
  198. ix++;
  199. if (ix==element_count)
  200. ix = 0;
  201. if (ix==last)
  202. break;
  203. }
  204. }
  205. return ret;
  206. }
  207. #ifndef _WIN32
  208. #define HOSTENT hostent
  209. #include <netdb.h>
  210. #endif
  211. int check_set(const char *path, int value)
  212. {
  213. #ifdef __linux__
  214. FILE *f = fopen(path,"r");
  215. char res[32];
  216. char *r = 0;
  217. int si = 0;
  218. if (f) {
  219. r = fgets(res, sizeof(res), f);
  220. fclose(f);
  221. }
  222. if (r)
  223. si = atoi(r);
  224. if (!si)
  225. {
  226. OWARNLOG("WARNING: Failed to read value for %s", path);
  227. return 0;
  228. }
  229. else if (si<value)
  230. return -1;
  231. #endif
  232. return 0;
  233. }
  234. int check_max_socket_read_buffer(int size) {
  235. return check_set("/proc/sys/net/core/rmem_max", size);
  236. }
  237. int check_max_socket_write_buffer(int size) {
  238. return check_set("/proc/sys/net/core/wmem_max", size);
  239. }
  240. #if defined( __linux__) || defined(__APPLE__)
  241. void setLinuxThreadPriority(int level)
  242. {
  243. pthread_t self = pthread_self();
  244. int policy;
  245. sched_param param;
  246. int rc;
  247. if (( rc = pthread_getschedparam(self, &policy, &param)) != 0)
  248. DBGLOG("pthread_getschedparam error: %d", rc);
  249. if (level < 0)
  250. UNIMPLEMENTED;
  251. else if (!level)
  252. {
  253. param.sched_priority = 0;
  254. policy = SCHED_OTHER;
  255. }
  256. else
  257. {
  258. policy = SCHED_RR;
  259. param.sched_priority = level;
  260. }
  261. if(( rc = pthread_setschedparam(self, policy, &param)) != 0)
  262. DBGLOG("pthread_setschedparam error: %d policy=%i pr=%i id=%" I64F "i TID=%i", rc, policy, param.sched_priority, (unsigned __int64) self, threadLogID());
  263. else
  264. DBGLOG("priority set id=%" I64F "i policy=%i pri=%i TID=%i", (unsigned __int64) self, policy, param.sched_priority, threadLogID());
  265. }
  266. #endif
  267. extern UDPLIB_API void queryMemoryPoolStats(StringBuffer &memStats)
  268. {
  269. if (bufferManager)
  270. bufferManager->poolStats(memStats);
  271. }
  272. /*
  273. Crazy thoughts on network-wide flow control
  274. Avoid sending data that clashes with other outbound or inbound data
  275. is outbound really an issue?
  276. if only inbound, should be easier
  277. can have each inbound node police its own, for a start
  278. udplib already tries to do this
  279. when sending permission to send, best to pick someone that is not sending to anyone else
  280. udplib already tries to do this
  281. but it can still lead to idleness - id node 1 sending to node 2, and node2 to node 1, node3 can't find anyone idle.
  282. If you do need global:
  283. Every bit of data getting sent (perhaps over a certain size threshold?) gets permission from central traffic cop
  284. Outbound packet says source node, target node size
  285. Reply says source,target,size
  286. Cop allows immediately if nothing inflight between those pairs
  287. Cop assumes completion
  288. Cop redundancy
  289. - a backup cop is listening in?
  290. - use multicast for requests and replies?
  291. - no reply implies what?
  292. - backup cop just needs heartbeat from active cop
  293. - permission expires
  294. - multiple cops for blocks of targets?
  295. - but I want global view of who is sending
  296. */