pload.c 10 KB

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