pload.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2008-2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/time.h>
  39. #include <sys/ioctl.h>
  40. #include <netinet/in.h>
  41. #include <sys/uio.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <time.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <string.h>
  51. #include <corosync/swab.h>
  52. #include <corosync/corotypes.h>
  53. #include <corosync/coroipc_types.h>
  54. #include <corosync/corodefs.h>
  55. #include <corosync/lcr/lcr_comp.h>
  56. #include <corosync/mar_gen.h>
  57. #include <corosync/engine/coroapi.h>
  58. #include <corosync/ipc_pload.h>
  59. #include <corosync/list.h>
  60. #include <corosync/engine/logsys.h>
  61. LOGSYS_DECLARE_SUBSYS ("PLOAD");
  62. enum pload_exec_message_req_types {
  63. MESSAGE_REQ_EXEC_PLOAD_START = 0,
  64. MESSAGE_REQ_EXEC_PLOAD_MCAST = 1
  65. };
  66. /*
  67. * Service Interfaces required by service_message_handler struct
  68. */
  69. static int pload_exec_init_fn (
  70. struct corosync_api_v1 *corosync_api);
  71. static void pload_confchg_fn (
  72. enum totem_configuration_type configuration_type,
  73. const unsigned int *member_list, size_t member_list_entries,
  74. const unsigned int *left_list, size_t left_list_entries,
  75. const unsigned int *joined_list, size_t joined_list_entries,
  76. const struct memb_ring_id *ring_id);
  77. static void message_handler_req_exec_pload_start (const void *msg,
  78. unsigned int nodeid);
  79. static void message_handler_req_exec_pload_mcast (const void *msg,
  80. unsigned int nodeid);
  81. static void req_exec_pload_start_endian_convert (void *msg);
  82. static void req_exec_pload_mcast_endian_convert (void *msg);
  83. static void message_handler_req_pload_start (void *conn, const void *msg);
  84. static int pload_lib_init_fn (void *conn);
  85. static int pload_lib_exit_fn (void *conn);
  86. static char buffer[1000000];
  87. static unsigned int msgs_delivered = 0;
  88. static unsigned int msgs_wanted = 0;
  89. static unsigned int msg_size = 0;
  90. static unsigned int msg_code = 1;
  91. static unsigned int msgs_sent = 0;
  92. static struct corosync_api_v1 *api;
  93. struct req_exec_pload_start {
  94. coroipc_request_header_t header;
  95. unsigned int msg_code;
  96. unsigned int msg_count;
  97. unsigned int msg_size;
  98. unsigned int time_interval;
  99. };
  100. struct req_exec_pload_mcast {
  101. coroipc_request_header_t header;
  102. unsigned int msg_code;
  103. };
  104. static struct corosync_lib_handler pload_lib_engine[] =
  105. {
  106. { /* 0 */
  107. .lib_handler_fn = message_handler_req_pload_start,
  108. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  109. }
  110. };
  111. static struct corosync_exec_handler pload_exec_engine[] =
  112. {
  113. {
  114. .exec_handler_fn = message_handler_req_exec_pload_start,
  115. .exec_endian_convert_fn = req_exec_pload_start_endian_convert
  116. },
  117. {
  118. .exec_handler_fn = message_handler_req_exec_pload_mcast,
  119. .exec_endian_convert_fn = req_exec_pload_mcast_endian_convert
  120. }
  121. };
  122. struct corosync_service_engine pload_service_engine = {
  123. .name = "corosync profile loading service",
  124. .id = PLOAD_SERVICE,
  125. .priority = 1,
  126. .private_data_size = 0,
  127. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  128. .lib_init_fn = pload_lib_init_fn,
  129. .lib_exit_fn = pload_lib_exit_fn,
  130. .lib_engine = pload_lib_engine,
  131. .lib_engine_count = sizeof (pload_lib_engine) / sizeof (struct corosync_lib_handler),
  132. .exec_engine = pload_exec_engine,
  133. .exec_engine_count = sizeof (pload_exec_engine) / sizeof (struct corosync_exec_handler),
  134. .confchg_fn = pload_confchg_fn,
  135. .exec_init_fn = pload_exec_init_fn,
  136. .exec_dump_fn = NULL
  137. };
  138. static DECLARE_LIST_INIT (confchg_notify);
  139. /*
  140. * Dynamic loading descriptor
  141. */
  142. static struct corosync_service_engine *pload_get_service_engine_ver0 (void);
  143. static struct corosync_service_engine_iface_ver0 pload_service_engine_iface = {
  144. .corosync_get_service_engine_ver0 = pload_get_service_engine_ver0
  145. };
  146. static struct lcr_iface corosync_pload_ver0[1] = {
  147. {
  148. .name = "corosync_pload",
  149. .version = 0,
  150. .versions_replace = 0,
  151. .versions_replace_count = 0,
  152. .dependencies = 0,
  153. .dependency_count = 0,
  154. .constructor = NULL,
  155. .destructor = NULL,
  156. .interfaces = NULL,
  157. }
  158. };
  159. static struct lcr_comp pload_comp_ver0 = {
  160. .iface_count = 1,
  161. .ifaces = corosync_pload_ver0
  162. };
  163. static struct corosync_service_engine *pload_get_service_engine_ver0 (void)
  164. {
  165. return (&pload_service_engine);
  166. }
  167. #ifdef COROSYNC_SOLARIS
  168. void corosync_lcr_component_register (void);
  169. void corosync_lcr_component_register (void) {
  170. #else
  171. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  172. #endif
  173. lcr_interfaces_set (&corosync_pload_ver0[0], &pload_service_engine_iface);
  174. lcr_component_register (&pload_comp_ver0);
  175. }
  176. static int pload_exec_init_fn (
  177. struct corosync_api_v1 *corosync_api)
  178. {
  179. #ifdef COROSYNC_SOLARIS
  180. logsys_subsys_init();
  181. #endif
  182. api = corosync_api;
  183. return 0;
  184. }
  185. static void pload_confchg_fn (
  186. enum totem_configuration_type configuration_type,
  187. const unsigned int *member_list, size_t member_list_entries,
  188. const unsigned int *left_list, size_t left_list_entries,
  189. const unsigned int *joined_list, size_t joined_list_entries,
  190. const struct memb_ring_id *ring_id)
  191. {
  192. }
  193. static int pload_lib_init_fn (void *conn)
  194. {
  195. return (0);
  196. }
  197. static int pload_lib_exit_fn (void *conn)
  198. {
  199. return (0);
  200. }
  201. static void message_handler_req_pload_start (void *conn, const void *msg)
  202. {
  203. const struct req_lib_pload_start *req_lib_pload_start = msg;
  204. struct req_exec_pload_start req_exec_pload_start;
  205. struct iovec iov;
  206. req_exec_pload_start.header.id =
  207. SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_START);
  208. req_exec_pload_start.msg_code = req_lib_pload_start->msg_code;
  209. req_exec_pload_start.msg_size = req_lib_pload_start->msg_size;
  210. req_exec_pload_start.msg_count = req_lib_pload_start->msg_count;
  211. req_exec_pload_start.time_interval = req_lib_pload_start->time_interval;
  212. iov.iov_base = (void *)&req_exec_pload_start;
  213. iov.iov_len = sizeof (struct req_exec_pload_start);
  214. msgs_delivered = 0;
  215. msgs_wanted = 0;
  216. msgs_sent = 0;
  217. api->totem_mcast (&iov, 1, TOTEM_AGREED);
  218. }
  219. static void req_exec_pload_start_endian_convert (void *msg)
  220. {
  221. }
  222. static void req_exec_pload_mcast_endian_convert (void *msg)
  223. {
  224. }
  225. static int send_message (const void *arg)
  226. {
  227. struct req_exec_pload_mcast req_exec_pload_mcast;
  228. struct iovec iov[2];
  229. unsigned int res;
  230. unsigned int iov_len = 1;
  231. req_exec_pload_mcast.header.id =
  232. SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_MCAST);
  233. req_exec_pload_mcast.header.size = sizeof (struct req_exec_pload_mcast) + msg_size;
  234. iov[0].iov_base = (void *)&req_exec_pload_mcast;
  235. iov[0].iov_len = sizeof (struct req_exec_pload_mcast);
  236. if (msg_size > sizeof (req_exec_pload_mcast)) {
  237. iov[1].iov_base = buffer;
  238. iov[1].iov_len = msg_size - sizeof (req_exec_pload_mcast);
  239. iov_len = 2;
  240. }
  241. do {
  242. res = api->totem_mcast (iov, iov_len, TOTEM_AGREED);
  243. if (res == -1) {
  244. break;
  245. } else {
  246. msgs_sent++;
  247. msg_code++;
  248. }
  249. } while (msgs_sent < msgs_wanted);
  250. if (msgs_sent == msgs_wanted) {
  251. return (0);
  252. } else {
  253. return (-1);
  254. }
  255. }
  256. hdb_handle_t start_mcasting_handle;
  257. static void start_mcasting (void)
  258. {
  259. api->schedwrk_create (
  260. &start_mcasting_handle,
  261. send_message,
  262. &start_mcasting_handle);
  263. }
  264. static void message_handler_req_exec_pload_start (
  265. const void *msg,
  266. unsigned int nodeid)
  267. {
  268. const struct req_exec_pload_start *req_exec_pload_start = msg;
  269. msgs_wanted = req_exec_pload_start->msg_count;
  270. msg_size = req_exec_pload_start->msg_size;
  271. msg_code = req_exec_pload_start->msg_code;
  272. start_mcasting ();
  273. }
  274. #ifndef timersub
  275. # define timersub(a, b, result) \
  276. do { \
  277. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  278. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  279. if ((result)->tv_usec < 0) { \
  280. --(result)->tv_sec; \
  281. (result)->tv_usec += 1000000; \
  282. } \
  283. } while (0)
  284. #endif
  285. struct timeval tv1;
  286. struct timeval tv2;
  287. struct timeval tv_elapsed;
  288. int last_msg_no = 0;
  289. static void message_handler_req_exec_pload_mcast (
  290. const void *msg,
  291. unsigned int nodeid)
  292. {
  293. const struct req_exec_pload_mcast *pload_mcast = msg;
  294. last_msg_no = pload_mcast->msg_code;
  295. if (msgs_delivered == 0) {
  296. gettimeofday (&tv1, NULL);
  297. }
  298. msgs_delivered += 1;
  299. if (msgs_delivered == msgs_wanted) {
  300. gettimeofday (&tv2, NULL);
  301. timersub (&tv2, &tv1, &tv_elapsed);
  302. printf ("%5d Writes ", msgs_delivered);
  303. printf ("%5d bytes per write ", msg_size);
  304. printf ("%7.3f Seconds runtime ",
  305. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  306. printf ("%9.3f TP/s ",
  307. ((float)msgs_delivered) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  308. printf ("%7.3f MB/s.\n",
  309. ((float)msgs_delivered) * ((float)msg_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
  310. }
  311. }