pload.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <assert.h>
  52. #include <qb/qblist.h>
  53. #include <qb/qbutil.h>
  54. #include <qb/qbipc_common.h>
  55. #include <corosync/swab.h>
  56. #include <corosync/corotypes.h>
  57. #include <corosync/corodefs.h>
  58. #include <corosync/mar_gen.h>
  59. #include <corosync/coroapi.h>
  60. #include <corosync/ipc_pload.h>
  61. #include <corosync/list.h>
  62. #include <corosync/logsys.h>
  63. #include "service.h"
  64. LOGSYS_DECLARE_SUBSYS ("PLOAD");
  65. enum pload_exec_message_req_types {
  66. MESSAGE_REQ_EXEC_PLOAD_START = 0,
  67. MESSAGE_REQ_EXEC_PLOAD_MCAST = 1
  68. };
  69. /*
  70. * Service Interfaces required by service_message_handler struct
  71. */
  72. static int pload_exec_init_fn (
  73. struct corosync_api_v1 *corosync_api);
  74. static void pload_confchg_fn (
  75. enum totem_configuration_type configuration_type,
  76. const unsigned int *member_list, size_t member_list_entries,
  77. const unsigned int *left_list, size_t left_list_entries,
  78. const unsigned int *joined_list, size_t joined_list_entries,
  79. const struct memb_ring_id *ring_id);
  80. static void message_handler_req_exec_pload_start (const void *msg,
  81. unsigned int nodeid);
  82. static void message_handler_req_exec_pload_mcast (const void *msg,
  83. unsigned int nodeid);
  84. static void req_exec_pload_start_endian_convert (void *msg);
  85. static void req_exec_pload_mcast_endian_convert (void *msg);
  86. static void message_handler_req_pload_start (void *conn, const void *msg);
  87. static int pload_lib_init_fn (void *conn);
  88. static int pload_lib_exit_fn (void *conn);
  89. static char buffer[1000000];
  90. static unsigned int msgs_delivered = 0;
  91. static unsigned int msgs_wanted = 0;
  92. static unsigned int msg_size = 0;
  93. static unsigned int msg_code = 1;
  94. static unsigned int msgs_sent = 0;
  95. static struct corosync_api_v1 *api;
  96. struct req_exec_pload_start {
  97. struct qb_ipc_request_header header;
  98. unsigned int msg_code;
  99. unsigned int msg_count;
  100. unsigned int msg_size;
  101. unsigned int time_interval;
  102. };
  103. struct req_exec_pload_mcast {
  104. struct qb_ipc_request_header header;
  105. unsigned int msg_code;
  106. };
  107. static struct corosync_lib_handler pload_lib_engine[] =
  108. {
  109. { /* 0 */
  110. .lib_handler_fn = message_handler_req_pload_start,
  111. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  112. }
  113. };
  114. static struct corosync_exec_handler pload_exec_engine[] =
  115. {
  116. {
  117. .exec_handler_fn = message_handler_req_exec_pload_start,
  118. .exec_endian_convert_fn = req_exec_pload_start_endian_convert
  119. },
  120. {
  121. .exec_handler_fn = message_handler_req_exec_pload_mcast,
  122. .exec_endian_convert_fn = req_exec_pload_mcast_endian_convert
  123. }
  124. };
  125. struct corosync_service_engine pload_service_engine = {
  126. .name = "corosync profile loading service",
  127. .id = PLOAD_SERVICE,
  128. .priority = 1,
  129. .private_data_size = 0,
  130. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  131. .lib_init_fn = pload_lib_init_fn,
  132. .lib_exit_fn = pload_lib_exit_fn,
  133. .lib_engine = pload_lib_engine,
  134. .lib_engine_count = sizeof (pload_lib_engine) / sizeof (struct corosync_lib_handler),
  135. .exec_engine = pload_exec_engine,
  136. .exec_engine_count = sizeof (pload_exec_engine) / sizeof (struct corosync_exec_handler),
  137. .confchg_fn = pload_confchg_fn,
  138. .exec_init_fn = pload_exec_init_fn,
  139. .exec_dump_fn = NULL,
  140. .sync_mode = CS_SYNC_V2
  141. };
  142. static DECLARE_LIST_INIT (confchg_notify);
  143. struct corosync_service_engine *pload_get_service_engine_ver0 (void)
  144. {
  145. return (&pload_service_engine);
  146. }
  147. static int pload_exec_init_fn (
  148. struct corosync_api_v1 *corosync_api)
  149. {
  150. #ifdef COROSYNC_SOLARIS
  151. logsys_subsys_init();
  152. #endif
  153. api = corosync_api;
  154. return 0;
  155. }
  156. static void pload_confchg_fn (
  157. enum totem_configuration_type configuration_type,
  158. const unsigned int *member_list, size_t member_list_entries,
  159. const unsigned int *left_list, size_t left_list_entries,
  160. const unsigned int *joined_list, size_t joined_list_entries,
  161. const struct memb_ring_id *ring_id)
  162. {
  163. }
  164. static int pload_lib_init_fn (void *conn)
  165. {
  166. return (0);
  167. }
  168. static int pload_lib_exit_fn (void *conn)
  169. {
  170. return (0);
  171. }
  172. static void message_handler_req_pload_start (void *conn, const void *msg)
  173. {
  174. const struct req_lib_pload_start *req_lib_pload_start = msg;
  175. struct req_exec_pload_start req_exec_pload_start;
  176. struct iovec iov;
  177. req_exec_pload_start.header.id =
  178. SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_START);
  179. req_exec_pload_start.msg_code = req_lib_pload_start->msg_code;
  180. req_exec_pload_start.msg_size = req_lib_pload_start->msg_size;
  181. req_exec_pload_start.msg_count = req_lib_pload_start->msg_count;
  182. req_exec_pload_start.time_interval = req_lib_pload_start->time_interval;
  183. iov.iov_base = (void *)&req_exec_pload_start;
  184. iov.iov_len = sizeof (struct req_exec_pload_start);
  185. msgs_delivered = 0;
  186. msgs_wanted = 0;
  187. msgs_sent = 0;
  188. api->totem_mcast (&iov, 1, TOTEM_AGREED);
  189. }
  190. static void req_exec_pload_start_endian_convert (void *msg)
  191. {
  192. }
  193. static void req_exec_pload_mcast_endian_convert (void *msg)
  194. {
  195. }
  196. static int send_message (const void *arg)
  197. {
  198. struct req_exec_pload_mcast req_exec_pload_mcast;
  199. struct iovec iov[2];
  200. unsigned int res;
  201. unsigned int iov_len = 1;
  202. req_exec_pload_mcast.header.id =
  203. SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_MCAST);
  204. req_exec_pload_mcast.header.size = sizeof (struct req_exec_pload_mcast) + msg_size;
  205. iov[0].iov_base = (void *)&req_exec_pload_mcast;
  206. iov[0].iov_len = sizeof (struct req_exec_pload_mcast);
  207. if (msg_size > sizeof (req_exec_pload_mcast)) {
  208. iov[1].iov_base = buffer;
  209. iov[1].iov_len = msg_size - sizeof (req_exec_pload_mcast);
  210. iov_len = 2;
  211. }
  212. do {
  213. res = api->totem_mcast (iov, iov_len, TOTEM_AGREED);
  214. if (res == -1) {
  215. break;
  216. } else {
  217. msgs_sent++;
  218. msg_code++;
  219. }
  220. } while (msgs_sent < msgs_wanted);
  221. if (msgs_sent == msgs_wanted) {
  222. return (0);
  223. } else {
  224. return (-1);
  225. }
  226. }
  227. hdb_handle_t start_mcasting_handle;
  228. static void start_mcasting (void)
  229. {
  230. api->schedwrk_create (
  231. &start_mcasting_handle,
  232. send_message,
  233. &start_mcasting_handle);
  234. }
  235. static void message_handler_req_exec_pload_start (
  236. const void *msg,
  237. unsigned int nodeid)
  238. {
  239. const struct req_exec_pload_start *req_exec_pload_start = msg;
  240. msgs_wanted = req_exec_pload_start->msg_count;
  241. msg_size = req_exec_pload_start->msg_size;
  242. msg_code = req_exec_pload_start->msg_code;
  243. start_mcasting ();
  244. }
  245. #ifndef timersub
  246. #define timersub(a, b, result) \
  247. do { \
  248. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  249. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  250. if ((result)->tv_usec < 0) { \
  251. --(result)->tv_sec; \
  252. (result)->tv_usec += 1000000; \
  253. } \
  254. } while (0)
  255. #endif /* timersub */
  256. unsigned long long int tv1;
  257. unsigned long long int tv2;
  258. unsigned long long int tv_elapsed;
  259. int last_msg_no = 0;
  260. static void message_handler_req_exec_pload_mcast (
  261. const void *msg,
  262. unsigned int nodeid)
  263. {
  264. const struct req_exec_pload_mcast *pload_mcast = msg;
  265. char log_buffer[1024];
  266. last_msg_no = pload_mcast->msg_code;
  267. if (msgs_delivered == 0) {
  268. tv1 = qb_util_nano_current_get ();
  269. }
  270. msgs_delivered += 1;
  271. if (msgs_delivered == msgs_wanted) {
  272. tv2 = qb_util_nano_current_get ();
  273. tv_elapsed = tv2 - tv1;
  274. sprintf (log_buffer, "%5d Writes %d bytes per write %7.3f seconds runtime, %9.3f TP/S, %9.3f MB/S.",
  275. msgs_delivered,
  276. msg_size,
  277. (tv_elapsed / 1000000000.0),
  278. ((float)msgs_delivered) / (tv_elapsed / 1000000000.0),
  279. (((float)msgs_delivered) * ((float)msg_size) /
  280. (tv_elapsed / 1000000000.0)) / (1024.0 * 1024.0));
  281. log_printf (LOGSYS_LEVEL_NOTICE, "%s", log_buffer);
  282. }
  283. }