corosync-fplay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #include <config.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <errno.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <arpa/inet.h>
  16. #include <corosync/engine/logsys.h>
  17. uint32_t flt_data_size;
  18. uint32_t *flt_data;
  19. #define FDHEAD_INDEX (flt_data_size)
  20. #define FDTAIL_INDEX (flt_data_size + 1)
  21. #define TOTEMIP_ADDRLEN (sizeof(struct in6_addr))
  22. struct totem_ip_address {
  23. unsigned int nodeid;
  24. unsigned short family;
  25. unsigned char addr[TOTEMIP_ADDRLEN];
  26. } __attribute__((packed));
  27. struct memb_ring_id {
  28. struct totem_ip_address rep;
  29. unsigned long long seq;
  30. } __attribute__((packed));
  31. static const char *totemip_print(const struct totem_ip_address *addr)
  32. {
  33. static char buf[INET6_ADDRSTRLEN];
  34. return inet_ntop(addr->family, addr->addr, buf, sizeof(buf));
  35. }
  36. static char *print_string_len (const unsigned char *str, unsigned int len)
  37. {
  38. unsigned int i;
  39. static char buf[1024];
  40. memset (buf, 0, sizeof (buf));
  41. for (i = 0; i < len; i++) {
  42. buf[i] = str[i];
  43. }
  44. return (buf);
  45. }
  46. static void sync_printer_confchg_set_sync (const void **record)
  47. {
  48. const unsigned int *my_should_sync = record[0];
  49. printf ("Setting my_should_sync to %d\n", *my_should_sync);
  50. }
  51. static void sync_printer_set_sync_state (const void **record)
  52. {
  53. const unsigned int *my_sync_state = record[0];
  54. printf ("Setting my_sync_state to %d\n", *my_sync_state);
  55. }
  56. static void sync_printer_process_currentstate (const void **record)
  57. {
  58. const unsigned int *my_sync_state = record[0];
  59. printf ("Retrieving my_sync_state %d\n", *my_sync_state);
  60. }
  61. static void sync_printer_process_get_shouldsync (const void **record)
  62. {
  63. const unsigned int *my_should_sync = record[0];
  64. printf ("Getting my_should_sync %d\n", *my_should_sync);
  65. }
  66. static void sync_printer_checkpoint_release (const void **record)
  67. {
  68. const unsigned char *name = record[0];
  69. const uint16_t *name_len = record[1];
  70. const unsigned int *ckpt_id = record[2];
  71. const unsigned int *from = record[3];
  72. printf ("Checkpoint release name=[%s] id=[%d] from=[%d] len=[%d]\n",
  73. print_string_len (name, *name_len),
  74. *ckpt_id,
  75. *from,
  76. *name_len);
  77. }
  78. static void sync_printer_checkpoint_transmit (const void **record)
  79. {
  80. const unsigned char *name = record[0];
  81. const uint16_t *name_len = record[1];
  82. const unsigned int *ckpt_id = record[2];
  83. const unsigned int *xmit_id = record[3];
  84. printf ("xmit_id=[%d] Checkpoint transmit name=[%s] id=[%d]\n",
  85. *xmit_id, print_string_len (name, *name_len),
  86. *ckpt_id);
  87. }
  88. static void sync_printer_section_transmit (const void **record)
  89. {
  90. const unsigned char *ckpt_name = record[0];
  91. const uint16_t *name_len = record[1];
  92. const unsigned int *ckpt_id = record[2];
  93. const unsigned int *xmit_id = record[3];
  94. const unsigned char *section_name = record[4];
  95. const uint16_t *section_name_len = record[5];
  96. printf ("xmit_id=[%d] Section transmit checkpoint name=[%s] id=[%d] ",
  97. *xmit_id, print_string_len (ckpt_name, *name_len),
  98. *ckpt_id);
  99. printf ("section=[%s]\n",
  100. print_string_len (section_name, *section_name_len));
  101. }
  102. static void sync_printer_checkpoint_receive (const void **record)
  103. {
  104. const unsigned char *ckpt_name = record[0];
  105. const uint16_t *name_len = record[1];
  106. const unsigned int *ckpt_id = record[2];
  107. const unsigned int *xmit_id = record[3];
  108. printf ("xmit_id=[%d] Checkpoint receive checkpoint name=[%s] id=[%d]\n",
  109. *xmit_id, print_string_len (ckpt_name, *name_len), *ckpt_id);
  110. }
  111. static void sync_printer_section_receive (const void **record)
  112. {
  113. const unsigned char *ckpt_name = record[0];
  114. const uint16_t *name_len = record[1];
  115. const unsigned int *ckpt_id = record[2];
  116. const unsigned int *xmit_id = record[3];
  117. const unsigned char *section_name = record[4];
  118. const unsigned int *section_name_len = record[5];
  119. printf ("xmit_id=[%d] Section receive checkpoint name=[%s] id=[%d] ",
  120. *xmit_id, print_string_len (ckpt_name, *name_len),
  121. *ckpt_id);
  122. printf ("section=[%s]\n",
  123. print_string_len (section_name, *section_name_len));
  124. }
  125. static void sync_printer_confchg_fn (const void **record)
  126. {
  127. unsigned int i;
  128. const unsigned int *members = record[0];
  129. const unsigned int *member_count = record[1];
  130. const struct memb_ring_id *ring_id = record[2];
  131. struct in_addr addr;
  132. printf ("sync confchg fn ringid [ip=%s seq=%lld]\n",
  133. totemip_print (&ring_id->rep),
  134. ring_id->seq);
  135. printf ("members [%d]:\n", *member_count);
  136. for (i = 0; i < *member_count; i++) {
  137. addr.s_addr = members[i];
  138. printf ("\tmember [%s]\n", inet_ntoa (addr));
  139. }
  140. }
  141. static void printer_totemsrp_mcast (const void **record)
  142. {
  143. const unsigned int *msgid = record[0];
  144. printf ("totemsrp_mcast %d\n", *msgid);
  145. }
  146. static void printer_totemsrp_delv (const void **record)
  147. {
  148. const unsigned int *msgid = record[0];
  149. printf ("totemsrp_delv %d\n", *msgid);
  150. }
  151. static void printer_totempg_mcast_fits (const void **record)
  152. {
  153. const unsigned int *idx = record[0];
  154. const unsigned int *iov_len = record[1];
  155. const unsigned int *copy_len = record[2];
  156. const unsigned int *fragment_size = record[3];
  157. const unsigned int *max_packet_size = record[4];
  158. const unsigned int *copy_base = record[5];
  159. const unsigned char *next_fragment = record[6];
  160. printf ("totempg_mcast index=[%d] iov_len=[%d] copy_len=[%d] fragment_size=[%d] max_packet_size=[%d] copy_base=[%d] next_fragment[%d]\n",
  161. *idx, *iov_len, *copy_len, *fragment_size, *max_packet_size, *copy_base, *next_fragment);
  162. }
  163. static void sync_printer_service_process (const void **record)
  164. {
  165. const struct memb_ring_id *ring_id = record[0];
  166. const struct memb_ring_id *sync_ring_id = record[1];
  167. printf ("sync service process callback ringid [ip=%s seq=%lld] ",
  168. totemip_print (&ring_id->rep),
  169. ring_id->seq);
  170. printf ("sync ringid [ip=%s seq=%lld]\n",
  171. totemip_print (&sync_ring_id->rep),
  172. sync_ring_id->seq);
  173. }
  174. struct printer_subsys_record_print {
  175. int ident;
  176. void (*print_fn)(const void **record);
  177. int record_length;
  178. };
  179. struct printer_subsys {
  180. const char *subsys;
  181. struct printer_subsys_record_print *record_printers;
  182. int record_printers_count;
  183. };
  184. #define LOGREC_ID_SYNC_CONFCHG_FN 0
  185. #define LOGREC_ID_SYNC_SERVICE_PROCESS 1
  186. /*
  187. * CKPT subsystem
  188. */
  189. #define LOGREC_ID_CONFCHG_SETSYNC 0
  190. #define LOGREC_ID_SETSYNCSTATE 1
  191. #define LOGREC_ID_SYNC_PROCESS_CURRENTSTATE 2
  192. #define LOGREC_ID_SYNC_PROCESS_GETSHOULDSYNC 3
  193. #define LOGREC_ID_SYNC_CHECKPOINT_TRANSMIT 4
  194. #define LOGREC_ID_SYNC_SECTION_TRANSMIT 5
  195. #define LOGREC_ID_SYNC_CHECKPOINT_RECEIVE 6
  196. #define LOGREC_ID_SYNC_SECTION_RECEIVE 7
  197. #define LOGREC_ID_SYNC_CHECKPOINT_RELEASE 8
  198. #define LOGREC_ID_TOTEMSRP_MCAST 0
  199. #define LOGREC_ID_TOTEMSRP_DELV 1
  200. #define LOGREC_ID_TOTEMPG_MCAST_FITS 2
  201. static struct printer_subsys_record_print record_print_sync[] = {
  202. {
  203. .ident = LOGREC_ID_SYNC_CONFCHG_FN,
  204. .print_fn = sync_printer_confchg_fn,
  205. .record_length = 28
  206. },
  207. {
  208. .ident = LOGREC_ID_SYNC_SERVICE_PROCESS,
  209. .print_fn = sync_printer_service_process,
  210. .record_length = 28
  211. }
  212. };
  213. static struct printer_subsys_record_print record_print_ckpt[] = {
  214. {
  215. .ident = LOGREC_ID_CONFCHG_SETSYNC,
  216. .print_fn = sync_printer_confchg_set_sync,
  217. .record_length = 28
  218. },
  219. {
  220. .ident = LOGREC_ID_SETSYNCSTATE,
  221. .print_fn = sync_printer_set_sync_state,
  222. .record_length = 28
  223. },
  224. {
  225. .ident = LOGREC_ID_SYNC_PROCESS_CURRENTSTATE,
  226. .print_fn = sync_printer_process_currentstate,
  227. .record_length = 28
  228. },
  229. {
  230. .ident = LOGREC_ID_SYNC_PROCESS_GETSHOULDSYNC,
  231. .print_fn = sync_printer_process_get_shouldsync,
  232. .record_length = 28
  233. },
  234. {
  235. .ident = LOGREC_ID_SYNC_CHECKPOINT_TRANSMIT,
  236. .print_fn = sync_printer_checkpoint_transmit,
  237. .record_length = 28
  238. },
  239. {
  240. .ident = LOGREC_ID_SYNC_SECTION_TRANSMIT,
  241. .print_fn = sync_printer_section_transmit,
  242. .record_length = 28
  243. },
  244. {
  245. .ident = LOGREC_ID_SYNC_CHECKPOINT_RECEIVE,
  246. .print_fn = sync_printer_checkpoint_receive,
  247. .record_length = 28
  248. },
  249. {
  250. .ident = LOGREC_ID_SYNC_SECTION_RECEIVE,
  251. .print_fn = sync_printer_section_receive,
  252. .record_length = 28
  253. },
  254. {
  255. .ident = LOGREC_ID_SYNC_CHECKPOINT_RELEASE,
  256. .print_fn = sync_printer_checkpoint_release,
  257. .record_length = 28
  258. }
  259. };
  260. static struct printer_subsys_record_print record_print_totem[] = {
  261. {
  262. .ident = LOGREC_ID_TOTEMSRP_MCAST,
  263. .print_fn = printer_totemsrp_mcast,
  264. .record_length = 28
  265. },
  266. {
  267. .ident = LOGREC_ID_TOTEMSRP_DELV,
  268. .print_fn = printer_totemsrp_delv,
  269. .record_length = 28
  270. },
  271. {
  272. .ident = LOGREC_ID_TOTEMPG_MCAST_FITS,
  273. .print_fn = printer_totempg_mcast_fits,
  274. .record_length = 28
  275. }
  276. };
  277. static struct printer_subsys printer_subsystems[] = {
  278. {
  279. .subsys = "SYNC",
  280. .record_printers = record_print_sync,
  281. .record_printers_count = sizeof (record_print_sync) / sizeof (struct printer_subsys_record_print)
  282. },
  283. {
  284. .subsys = "CKPT",
  285. .record_printers = record_print_ckpt,
  286. .record_printers_count = sizeof (record_print_ckpt) / sizeof (struct printer_subsys_record_print)
  287. },
  288. {
  289. .subsys = "TOTEM",
  290. .record_printers = record_print_totem,
  291. .record_printers_count = sizeof (record_print_totem) / sizeof (struct printer_subsys_record_print)
  292. }
  293. };
  294. static unsigned int printer_subsys_count =
  295. sizeof (printer_subsystems) / sizeof (struct printer_subsys);
  296. #define G_RECORD_SIZE 10000
  297. static uint32_t g_record[G_RECORD_SIZE];
  298. /*
  299. * Copy record, dealing with wrapping
  300. */
  301. static int logsys_rec_get (int rec_idx) {
  302. uint32_t rec_size;
  303. int firstcopy, secondcopy;
  304. rec_size = flt_data[rec_idx];
  305. firstcopy = rec_size;
  306. secondcopy = 0;
  307. if (rec_size > G_RECORD_SIZE || rec_size > flt_data_size) {
  308. fprintf (stderr, "rec_size too large. Input file is probably corrupted.\n");
  309. exit (EXIT_FAILURE);
  310. }
  311. if (firstcopy + rec_idx > flt_data_size) {
  312. firstcopy = flt_data_size - rec_idx;
  313. secondcopy -= firstcopy - rec_size;
  314. }
  315. memcpy (&g_record[0], &flt_data[rec_idx], firstcopy * sizeof(uint32_t));
  316. if (secondcopy) {
  317. memcpy (&g_record[firstcopy], &flt_data[0], secondcopy * sizeof(uint32_t));
  318. }
  319. return ((rec_idx + rec_size) % flt_data_size);
  320. }
  321. static void logsys_rec_print (const void *record)
  322. {
  323. const uint32_t *buf_uint32t = record;
  324. uint32_t rec_size;
  325. uint32_t rec_ident;
  326. uint32_t level;
  327. uint32_t line;
  328. uint32_t arg_size_idx;
  329. unsigned int i;
  330. unsigned int j;
  331. unsigned int rec_idx = 0;
  332. uint32_t record_number;
  333. unsigned int words_processed;
  334. unsigned int found;
  335. const char *arguments[64];
  336. int arg_count = 0;
  337. rec_size = buf_uint32t[rec_idx];
  338. rec_ident = buf_uint32t[rec_idx+1];
  339. line = buf_uint32t[rec_idx+2];
  340. record_number = buf_uint32t[rec_idx+3];
  341. level = LOGSYS_DECODE_LEVEL(rec_ident);
  342. printf ("rec=[%d] ", record_number);
  343. arg_size_idx = rec_idx + 4;
  344. words_processed = 4;
  345. for (i = 0; words_processed < rec_size; i++) {
  346. arguments[arg_count++] =
  347. (const char *)&buf_uint32t[arg_size_idx + 1];
  348. words_processed += buf_uint32t[arg_size_idx] + 1;
  349. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  350. }
  351. found = 0;
  352. for (i = 0; i < printer_subsys_count; i++) {
  353. if (strcmp (arguments[0], printer_subsystems[i].subsys) == 0) {
  354. for (j = 0; j < printer_subsystems[i].record_printers_count; j++) {
  355. if (rec_ident == printer_subsystems[i].record_printers[j].ident) {
  356. printer_subsystems[i].record_printers[j].print_fn ((const void **)&arguments[3]);
  357. return;
  358. }
  359. }
  360. }
  361. }
  362. switch(LOGSYS_DECODE_RECID(rec_ident)) {
  363. case LOGSYS_RECID_LOG:
  364. printf ("Log Message=%s\n", arguments[3]);
  365. break;
  366. case LOGSYS_RECID_ENTER:
  367. printf ("ENTERING function [%s] line [%d]\n", arguments[2], line);
  368. break;
  369. case LOGSYS_RECID_LEAVE:
  370. printf ("LEAVING function [%s] line [%d]\n", arguments[2], line);
  371. break;
  372. case LOGSYS_RECID_TRACE1:
  373. printf ("Tracing(1) Messsage=%s\n", arguments[3]);
  374. break;
  375. case LOGSYS_RECID_TRACE2:
  376. printf ("Tracing(2) Messsage=%s\n", arguments[3]);
  377. break;
  378. case LOGSYS_RECID_TRACE3:
  379. printf ("Tracing(3) Messsage=%s\n", arguments[3]);
  380. break;
  381. case LOGSYS_RECID_TRACE4:
  382. printf ("Tracing(4) Messsage=%s\n", arguments[3]);
  383. break;
  384. case LOGSYS_RECID_TRACE5:
  385. printf ("Tracing(5) Messsage=%s\n", arguments[3]);
  386. break;
  387. case LOGSYS_RECID_TRACE6:
  388. printf ("Tracing(6) Messsage=%s\n", arguments[3]);
  389. break;
  390. case LOGSYS_RECID_TRACE7:
  391. printf ("Tracing(7) Messsage=%s\n", arguments[3]);
  392. break;
  393. case LOGSYS_RECID_TRACE8:
  394. printf ("Tracing(8) Messsage=%s\n", arguments[3]);
  395. break;
  396. default:
  397. printf ("Unknown record type found subsys=[%s] ident=[%d]\n",
  398. arguments[0], LOGSYS_DECODE_RECID(rec_ident));
  399. break;
  400. }
  401. #ifdef COMPILE_OUT
  402. printf ("\n");
  403. #endif
  404. }
  405. int main (void)
  406. {
  407. int fd;
  408. int rec_idx;
  409. int end_rec;
  410. int record_count = 1;
  411. ssize_t n_read;
  412. const char *data_file = LOCALSTATEDIR "/lib/corosync/fdata";
  413. size_t n_required;
  414. if ((fd = open (data_file, O_RDONLY)) < 0) {
  415. fprintf (stderr, "failed to open %s: %s\n",
  416. data_file, strerror (errno));
  417. return EXIT_FAILURE;
  418. }
  419. n_required = sizeof (uint32_t);
  420. n_read = read (fd, &flt_data_size, n_required);
  421. if (n_read != n_required) {
  422. fprintf (stderr, "Unable to read fdata header\n");
  423. return EXIT_FAILURE;
  424. }
  425. n_required = ((flt_data_size + 2) * sizeof(uint32_t));
  426. if ((flt_data = malloc (n_required)) == NULL) {
  427. fprintf (stderr, "exhausted virtual memory\n");
  428. return EXIT_FAILURE;
  429. }
  430. n_read = read (fd, flt_data, n_required);
  431. close (fd);
  432. if (n_read < 0) {
  433. fprintf (stderr, "reading %s failed: %s\n",
  434. data_file, strerror (errno));
  435. return EXIT_FAILURE;
  436. }
  437. if (n_read != n_required) {
  438. printf ("Warning: read %zd bytes, but expected %zu\n",
  439. n_read, n_required);
  440. }
  441. rec_idx = flt_data[FDTAIL_INDEX];
  442. end_rec = flt_data[FDHEAD_INDEX];
  443. printf ("Starting replay: head [%d] tail [%d]\n",
  444. flt_data[FDHEAD_INDEX],
  445. flt_data[FDTAIL_INDEX]);
  446. for (;;) {
  447. rec_idx = logsys_rec_get (rec_idx);
  448. logsys_rec_print (g_record);
  449. if (rec_idx == end_rec) {
  450. break;
  451. }
  452. record_count += 1;
  453. }
  454. printf ("Finishing replay: records found [%d]\n", record_count);
  455. return (0);
  456. }