4
0

pload.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2008-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Authors: Steven Dake (sdake@redhat.com)
  7. * Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <qb/qblist.h>
  37. #include <qb/qbutil.h>
  38. #include <qb/qbipc_common.h>
  39. #include <corosync/swab.h>
  40. #include <corosync/corodefs.h>
  41. #include <corosync/coroapi.h>
  42. #include <corosync/icmap.h>
  43. #include <corosync/logsys.h>
  44. #include "service.h"
  45. #include "util.h"
  46. LOGSYS_DECLARE_SUBSYS ("PLOAD");
  47. /*
  48. * Service Interfaces required by service_message_handler struct
  49. */
  50. static struct corosync_api_v1 *api;
  51. static char *pload_exec_init_fn (struct corosync_api_v1 *corosync_api);
  52. /*
  53. * on wire / network bits
  54. */
  55. enum pload_exec_message_req_types {
  56. MESSAGE_REQ_EXEC_PLOAD_START = 0,
  57. MESSAGE_REQ_EXEC_PLOAD_MCAST = 1
  58. };
  59. struct req_exec_pload_start {
  60. struct qb_ipc_request_header header;
  61. uint32_t msg_count;
  62. uint32_t msg_size;
  63. };
  64. struct req_exec_pload_mcast {
  65. struct qb_ipc_request_header header;
  66. };
  67. static void message_handler_req_exec_pload_start (const void *msg,
  68. unsigned int nodeid);
  69. static void req_exec_pload_start_endian_convert (void *msg);
  70. static void message_handler_req_exec_pload_mcast (const void *msg,
  71. unsigned int nodeid);
  72. static void req_exec_pload_mcast_endian_convert (void *msg);
  73. static struct corosync_exec_handler pload_exec_engine[] =
  74. {
  75. {
  76. .exec_handler_fn = message_handler_req_exec_pload_start,
  77. .exec_endian_convert_fn = req_exec_pload_start_endian_convert
  78. },
  79. {
  80. .exec_handler_fn = message_handler_req_exec_pload_mcast,
  81. .exec_endian_convert_fn = req_exec_pload_mcast_endian_convert
  82. }
  83. };
  84. /*
  85. * internal bits and pieces
  86. */
  87. /*
  88. * really unused buffer but we need to give something to iovec
  89. */
  90. static char *buffer = NULL;
  91. /*
  92. * wanted/size come from config
  93. * sent/delivered track the runtime status
  94. */
  95. static uint32_t msgs_wanted = 0;
  96. static uint32_t msg_size = 0;
  97. static uint32_t msgs_sent = 0;
  98. static uint32_t msgs_delivered = 0;
  99. /*
  100. * bit flip to track if we are running or not and avoid multiple instances
  101. */
  102. static uint8_t pload_started = 0;
  103. /*
  104. * handle for scheduler
  105. */
  106. static hdb_handle_t start_mcasting_handle;
  107. /*
  108. * timing/profiling
  109. */
  110. static unsigned long long int tv1;
  111. static unsigned long long int tv2;
  112. static unsigned long long int tv_elapsed;
  113. /*
  114. * Service engine hooks
  115. */
  116. struct corosync_service_engine pload_service_engine = {
  117. .name = "corosync profile loading service",
  118. .id = PLOAD_SERVICE,
  119. .priority = 1,
  120. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED,
  121. .exec_engine = pload_exec_engine,
  122. .exec_engine_count = sizeof (pload_exec_engine) / sizeof (struct corosync_exec_handler),
  123. .exec_init_fn = pload_exec_init_fn
  124. };
  125. struct corosync_service_engine *pload_get_service_engine_ver0 (void)
  126. {
  127. return (&pload_service_engine);
  128. }
  129. /*
  130. * internal use only functions
  131. */
  132. /*
  133. * not all architectures / OSes define timersub in sys/time.h or time.h
  134. */
  135. #ifndef timersub
  136. #warning Using internal timersub definition. Check your include header files
  137. #define timersub(a, b, result) \
  138. do { \
  139. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  140. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  141. if ((result)->tv_usec < 0) { \
  142. --(result)->tv_sec; \
  143. (result)->tv_usec += 1000000; \
  144. } \
  145. } while (0)
  146. #endif /* timersub */
  147. /*
  148. * tell all cluster nodes to start mcasting
  149. */
  150. static void pload_send_start (uint32_t count, uint32_t size)
  151. {
  152. struct req_exec_pload_start req_exec_pload_start;
  153. struct iovec iov;
  154. req_exec_pload_start.header.id = SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_START);
  155. req_exec_pload_start.msg_count = count;
  156. req_exec_pload_start.msg_size = size;
  157. iov.iov_base = (void *)&req_exec_pload_start;
  158. iov.iov_len = sizeof (struct req_exec_pload_start);
  159. api->totem_mcast (&iov, 1, TOTEM_AGREED);
  160. }
  161. /*
  162. * send N empty data messages of size X
  163. */
  164. static int pload_send_message (const void *arg)
  165. {
  166. struct req_exec_pload_mcast req_exec_pload_mcast;
  167. struct iovec iov[2];
  168. unsigned int res;
  169. unsigned int iov_len = 1;
  170. req_exec_pload_mcast.header.id = SERVICE_ID_MAKE (PLOAD_SERVICE, MESSAGE_REQ_EXEC_PLOAD_MCAST);
  171. req_exec_pload_mcast.header.size = sizeof (struct req_exec_pload_mcast) + msg_size;
  172. iov[0].iov_base = (void *)&req_exec_pload_mcast;
  173. iov[0].iov_len = sizeof (struct req_exec_pload_mcast);
  174. if (msg_size > sizeof (req_exec_pload_mcast)) {
  175. iov[1].iov_base = &buffer;
  176. iov[1].iov_len = msg_size - sizeof (req_exec_pload_mcast);
  177. iov_len = 2;
  178. }
  179. do {
  180. res = api->totem_mcast (iov, iov_len, TOTEM_AGREED);
  181. if (res == -1) {
  182. break;
  183. } else {
  184. msgs_sent++;
  185. }
  186. } while (msgs_sent < msgs_wanted);
  187. if (msgs_sent == msgs_wanted) {
  188. return (0);
  189. } else {
  190. return (-1);
  191. }
  192. }
  193. /*
  194. * hook into icmap to read config at runtime
  195. * we do NOT start by default, ever!
  196. */
  197. static void pload_read_config(
  198. int32_t event,
  199. const char *key_name,
  200. struct icmap_notify_value new_val,
  201. struct icmap_notify_value old_val,
  202. void *user_data)
  203. {
  204. uint32_t pload_count = 1500000;
  205. uint32_t pload_size = 300;
  206. char *pload_start = NULL;
  207. icmap_get_uint32("pload.count", &pload_count);
  208. icmap_get_uint32("pload.size", &pload_size);
  209. if (pload_size > MESSAGE_SIZE_MAX) {
  210. pload_size = MESSAGE_SIZE_MAX;
  211. log_printf(LOGSYS_LEVEL_WARNING, "pload size limited to %u", pload_size);
  212. }
  213. if ((!pload_started) &&
  214. (icmap_get_string("pload.start", &pload_start) == CS_OK)) {
  215. if (!strcmp(pload_start,
  216. "i_totally_understand_pload_will_crash_my_cluster_and_kill_corosync_on_exit")) {
  217. buffer = malloc(pload_size);
  218. if (buffer) {
  219. log_printf(LOGSYS_LEVEL_WARNING, "Starting pload!");
  220. pload_send_start(pload_count, pload_size);
  221. } else {
  222. log_printf(LOGSYS_LEVEL_WARNING,
  223. "Unable to allocate pload buffer!");
  224. }
  225. }
  226. free(pload_start);
  227. }
  228. }
  229. /*
  230. * exec functions
  231. */
  232. static char *pload_exec_init_fn (struct corosync_api_v1 *corosync_api)
  233. {
  234. icmap_track_t pload_track = NULL;
  235. api = corosync_api;
  236. /*
  237. * track changes to pload config and start only on demand
  238. */
  239. if (icmap_track_add("pload.",
  240. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  241. pload_read_config,
  242. NULL,
  243. &pload_track) != CS_OK) {
  244. return (char *)"Unable to setup pload config tracking!\n";
  245. }
  246. return NULL;
  247. }
  248. /*
  249. * network messages/onwire handlers
  250. */
  251. static void req_exec_pload_start_endian_convert (void *msg)
  252. {
  253. struct req_exec_pload_start *req_exec_pload_start = msg;
  254. req_exec_pload_start->msg_count = swab32(req_exec_pload_start->msg_count);
  255. req_exec_pload_start->msg_size = swab32(req_exec_pload_start->msg_size);
  256. }
  257. static void message_handler_req_exec_pload_start (
  258. const void *msg,
  259. unsigned int nodeid)
  260. {
  261. const struct req_exec_pload_start *req_exec_pload_start = msg;
  262. /*
  263. * don't start multiple instances
  264. */
  265. if (pload_started) {
  266. return;
  267. }
  268. pload_started = 1;
  269. msgs_wanted = req_exec_pload_start->msg_count;
  270. msg_size = req_exec_pload_start->msg_size;
  271. api->schedwrk_create (
  272. &start_mcasting_handle,
  273. pload_send_message,
  274. &start_mcasting_handle);
  275. }
  276. static void req_exec_pload_mcast_endian_convert (void *msg)
  277. {
  278. }
  279. static void message_handler_req_exec_pload_mcast (
  280. const void *msg,
  281. unsigned int nodeid)
  282. {
  283. char log_buffer[1024];
  284. if (msgs_delivered == 0) {
  285. tv1 = qb_util_nano_current_get ();
  286. }
  287. msgs_delivered += 1;
  288. if (msgs_delivered == msgs_wanted) {
  289. tv2 = qb_util_nano_current_get ();
  290. tv_elapsed = tv2 - tv1;
  291. sprintf (log_buffer, "%5d Writes %d bytes per write %7.3f seconds runtime, %9.3f TP/S, %9.3f MB/S.",
  292. msgs_delivered,
  293. msg_size,
  294. (tv_elapsed / 1000000000.0),
  295. ((float)msgs_delivered) / (tv_elapsed / 1000000000.0),
  296. (((float)msgs_delivered) * ((float)msg_size) /
  297. (tv_elapsed / 1000000000.0)) / (1024.0 * 1024.0));
  298. log_printf (LOGSYS_LEVEL_NOTICE, "%s", log_buffer);
  299. log_printf (LOGSYS_LEVEL_WARNING, "Stopping corosync the hard way");
  300. if (buffer) {
  301. free(buffer);
  302. buffer = NULL;
  303. }
  304. exit(COROSYNC_DONE_PLOAD);
  305. }
  306. }