pload.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2008 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 <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <sys/time.h>
  38. #include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. #include <sys/uio.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <signal.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/ipc_gen.h>
  54. #include <corosync/lcr/lcr_comp.h>
  55. #include <corosync/engine/coroapi.h>
  56. #include <corosync/ipc_pload.h>
  57. #include <corosync/list.h>
  58. #include <corosync/engine/logsys.h>
  59. LOGSYS_DECLARE_SUBSYS ("PLOAD", LOG_INFO);
  60. enum pload_exec_message_req_types {
  61. MESSAGE_REQ_EXEC_PLOAD_START = 0,
  62. MESSAGE_REQ_EXEC_PLOAD_MCAST = 1
  63. };
  64. /*
  65. * Service Interfaces required by service_message_handler struct
  66. */
  67. static int pload_exec_init_fn (
  68. struct corosync_api_v1 *corosync_api);
  69. static void pload_confchg_fn (
  70. enum totem_configuration_type configuration_type,
  71. unsigned int *member_list, int member_list_entries,
  72. unsigned int *left_list, int left_list_entries,
  73. unsigned int *joined_list, int joined_list_entries,
  74. struct memb_ring_id *ring_id);
  75. static void message_handler_req_exec_pload_start (void *msg, unsigned int nodeid);
  76. static void message_handler_req_exec_pload_mcast (void *msg, unsigned int nodeid);
  77. static void req_exec_pload_start_endian_convert (void *msg);
  78. static void req_exec_pload_mcast_endian_convert (void *msg);
  79. static void message_handler_req_pload_start (void *conn, void *msg);
  80. static int pload_lib_init_fn (void *conn);
  81. static int pload_lib_exit_fn (void *conn);
  82. static char buffer[1000000];
  83. static unsigned int msgs_delivered = 0;
  84. static unsigned int msgs_wanted = 0;
  85. static unsigned int msg_size = 0;
  86. static unsigned int msg_code = 1;
  87. static unsigned int msgs_sent = 0;
  88. static struct corosync_api_v1 *api;
  89. struct req_exec_pload_start {
  90. mar_req_header_t header;
  91. unsigned int msg_code;
  92. unsigned int msg_count;
  93. unsigned int msg_size;
  94. unsigned int time_interval;
  95. };
  96. struct req_exec_pload_mcast {
  97. mar_req_header_t header;
  98. unsigned int msg_code;
  99. };
  100. static struct corosync_lib_handler pload_lib_engine[] =
  101. {
  102. { /* 0 */
  103. .lib_handler_fn = message_handler_req_pload_start,
  104. .response_size = sizeof (struct res_lib_pload_start),
  105. .response_id = MESSAGE_RES_PLOAD_START,
  106. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  107. }
  108. };
  109. static struct corosync_exec_handler pload_exec_engine[] =
  110. {
  111. {
  112. .exec_handler_fn = message_handler_req_exec_pload_start,
  113. .exec_endian_convert_fn = req_exec_pload_start_endian_convert
  114. },
  115. {
  116. .exec_handler_fn = message_handler_req_exec_pload_mcast,
  117. .exec_endian_convert_fn = req_exec_pload_mcast_endian_convert
  118. }
  119. };
  120. struct corosync_service_engine pload_service_engine = {
  121. .name = "corosync profile loading service",
  122. .id = PLOAD_SERVICE,
  123. .private_data_size = 0,
  124. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  125. .lib_init_fn = pload_lib_init_fn,
  126. .lib_exit_fn = pload_lib_exit_fn,
  127. .lib_engine = pload_lib_engine,
  128. .lib_engine_count = sizeof (pload_lib_engine) / sizeof (struct corosync_lib_handler),
  129. .exec_engine = pload_exec_engine,
  130. .exec_engine_count = sizeof (pload_exec_engine) / sizeof (struct corosync_exec_handler),
  131. .confchg_fn = pload_confchg_fn,
  132. .exec_init_fn = pload_exec_init_fn,
  133. .exec_dump_fn = NULL
  134. };
  135. static DECLARE_LIST_INIT (confchg_notify);
  136. /*
  137. * Dynamic loading descriptor
  138. */
  139. static struct corosync_service_engine *pload_get_service_engine_ver0 (void);
  140. static struct corosync_service_engine_iface_ver0 pload_service_engine_iface = {
  141. .corosync_get_service_engine_ver0 = pload_get_service_engine_ver0
  142. };
  143. static struct lcr_iface corosync_pload_ver0[1] = {
  144. {
  145. .name = "corosync_pload",
  146. .version = 0,
  147. .versions_replace = 0,
  148. .versions_replace_count = 0,
  149. .dependencies = 0,
  150. .dependency_count = 0,
  151. .constructor = NULL,
  152. .destructor = NULL,
  153. .interfaces = NULL,
  154. }
  155. };
  156. static struct lcr_comp pload_comp_ver0 = {
  157. .iface_count = 1,
  158. .ifaces = corosync_pload_ver0
  159. };
  160. static struct corosync_service_engine *pload_get_service_engine_ver0 (void)
  161. {
  162. return (&pload_service_engine);
  163. }
  164. __attribute__ ((constructor)) static void pload_comp_register (void) {
  165. lcr_interfaces_set (&corosync_pload_ver0[0], &pload_service_engine_iface);
  166. lcr_component_register (&pload_comp_ver0);
  167. }
  168. static int pload_exec_init_fn (
  169. struct corosync_api_v1 *corosync_api)
  170. {
  171. api = corosync_api;
  172. return 0;
  173. }
  174. static void pload_confchg_fn (
  175. enum totem_configuration_type configuration_type,
  176. unsigned int *member_list, int member_list_entries,
  177. unsigned int *left_list, int left_list_entries,
  178. unsigned int *joined_list, int joined_list_entries,
  179. struct memb_ring_id *ring_id)
  180. {
  181. }
  182. static int pload_lib_init_fn (void *conn)
  183. {
  184. return (0);
  185. }
  186. static int pload_lib_exit_fn (void *conn)
  187. {
  188. return (0);
  189. }
  190. static void message_handler_req_pload_start (void *conn, void *msg)
  191. {
  192. struct req_lib_pload_start *req_lib_pload_start = (struct req_lib_pload_start *)msg;
  193. struct req_exec_pload_start req_exec_pload_start;
  194. struct iovec iov;
  195. req_exec_pload_start.header.id =
  196. SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_START);
  197. req_exec_pload_start.msg_code = req_lib_pload_start->msg_code;
  198. req_exec_pload_start.msg_size = req_lib_pload_start->msg_size;
  199. req_exec_pload_start.msg_count = req_lib_pload_start->msg_count;
  200. req_exec_pload_start.time_interval = req_lib_pload_start->time_interval;
  201. iov.iov_base = &req_exec_pload_start;
  202. iov.iov_len = sizeof (struct req_exec_pload_start);
  203. api->totem_mcast (&iov, 1, TOTEM_AGREED);
  204. }
  205. static void req_exec_pload_start_endian_convert (void *msg)
  206. {
  207. }
  208. static void req_exec_pload_mcast_endian_convert (void *msg)
  209. {
  210. }
  211. static int msg_no = 0;
  212. int send_message (enum totem_callback_token_type type, void *arg)
  213. {
  214. struct req_exec_pload_mcast req_exec_pload_mcast;
  215. struct iovec iov[2];
  216. unsigned int res;
  217. int iov_len = 2;
  218. req_exec_pload_mcast.header.id =
  219. SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_MCAST);
  220. req_exec_pload_mcast.header.size = sizeof (struct req_exec_pload_mcast) + msg_size;
  221. iov[0].iov_base = &req_exec_pload_mcast;
  222. iov[0].iov_len = sizeof (struct req_exec_pload_mcast);
  223. iov[1].iov_base = buffer;
  224. iov[1].iov_len = msg_size - sizeof (struct req_exec_pload_mcast);
  225. if (iov[1].iov_len < 0) {
  226. iov_len = 1;
  227. }
  228. do {
  229. res = api->totem_mcast (iov, iov_len, TOTEM_AGREED);
  230. if (res == -1) {
  231. break;
  232. } else {
  233. msgs_sent++;
  234. msg_code++;
  235. }
  236. } while (msgs_sent <= msgs_wanted);
  237. if (msgs_sent == msgs_wanted) {
  238. return (0);
  239. } else {
  240. return (-1);
  241. }
  242. }
  243. void *token_callback;
  244. void start_mcasting (void)
  245. {
  246. api->totem_callback_token_create (
  247. &token_callback,
  248. TOTEM_CALLBACK_TOKEN_RECEIVED,
  249. 1,
  250. send_message,
  251. &token_callback);
  252. }
  253. static void message_handler_req_exec_pload_start (
  254. void *msg,
  255. unsigned int nodeid)
  256. {
  257. struct req_exec_pload_start *req_exec_pload_start = (struct req_exec_pload_start *)msg;
  258. msgs_wanted = req_exec_pload_start->msg_count;
  259. msg_size = req_exec_pload_start->msg_size;
  260. msg_code = req_exec_pload_start->msg_code;
  261. start_mcasting ();
  262. }
  263. # define timersub(a, b, result) \
  264. do { \
  265. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  266. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  267. if ((result)->tv_usec < 0) { \
  268. --(result)->tv_sec; \
  269. (result)->tv_usec += 1000000; \
  270. } \
  271. } while (0)
  272. struct timeval tv1;
  273. struct timeval tv2;
  274. struct timeval tv_elapsed;
  275. int last_msg_no = 0;
  276. static void message_handler_req_exec_pload_mcast (
  277. void *msg,
  278. unsigned int nodeid)
  279. {
  280. struct req_exec_pload_mcast *pload_mcast = (struct req_exec_pload_mcast *)msg;
  281. assert (pload_mcast->msg_code - 1 == last_msg_no);
  282. last_msg_no = pload_mcast->msg_code;
  283. if (msgs_delivered == 0) {
  284. gettimeofday (&tv1, NULL);
  285. }
  286. msgs_delivered += 1;
  287. if (msgs_delivered == msgs_wanted) {
  288. gettimeofday (&tv2, NULL);
  289. timersub (&tv2, &tv1, &tv_elapsed);
  290. printf ("%5d Writes ", msgs_delivered);
  291. printf ("%5d bytes per write ", msg_size);
  292. printf ("%7.3f Seconds runtime ",
  293. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  294. printf ("%9.3f TP/s ",
  295. ((float)msgs_delivered) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  296. printf ("%7.3f MB/s.\n",
  297. ((float)msgs_delivered) * ((float)msg_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
  298. }
  299. }