corosync-fplay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. unsigned int flt_data_size;
  18. unsigned int *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. static unsigned int g_record[10000];
  297. /*
  298. * Copy record, dealing with wrapping
  299. */
  300. static int logsys_rec_get (int rec_idx) {
  301. unsigned int rec_size;
  302. int firstcopy, secondcopy;
  303. rec_size = flt_data[rec_idx];
  304. firstcopy = rec_size;
  305. secondcopy = 0;
  306. if (firstcopy + rec_idx > flt_data_size) {
  307. firstcopy = flt_data_size - rec_idx;
  308. secondcopy -= firstcopy - rec_size;
  309. }
  310. memcpy (&g_record[0], &flt_data[rec_idx], firstcopy<<2);
  311. if (secondcopy) {
  312. memcpy (&g_record[firstcopy], &flt_data[0], secondcopy<<2);
  313. }
  314. return ((rec_idx + rec_size) % flt_data_size);
  315. }
  316. static void logsys_rec_print (const void *record)
  317. {
  318. const unsigned int *buf_uint32t = record;
  319. unsigned int rec_size;
  320. unsigned int rec_ident;
  321. unsigned int level;
  322. unsigned int line;
  323. unsigned int arg_size_idx;
  324. unsigned int i;
  325. unsigned int j;
  326. unsigned int rec_idx = 0;
  327. unsigned int record_number;
  328. unsigned int words_processed;
  329. unsigned int found;
  330. const char *arguments[64];
  331. int arg_count = 0;
  332. rec_size = buf_uint32t[rec_idx];
  333. rec_ident = buf_uint32t[rec_idx+1];
  334. line = buf_uint32t[rec_idx+2];
  335. record_number = buf_uint32t[rec_idx+3];
  336. level = LOGSYS_DECODE_LEVEL(rec_ident);
  337. printf ("rec=[%d] ", record_number);
  338. arg_size_idx = rec_idx + 4;
  339. words_processed = 4;
  340. for (i = 0; words_processed < rec_size; i++) {
  341. arguments[arg_count++] =
  342. (const char *)&buf_uint32t[arg_size_idx + 1];
  343. words_processed += buf_uint32t[arg_size_idx] + 1;
  344. arg_size_idx += buf_uint32t[arg_size_idx] + 1;
  345. }
  346. found = 0;
  347. for (i = 0; i < printer_subsys_count; i++) {
  348. if (strcmp (arguments[0], printer_subsystems[i].subsys) == 0) {
  349. for (j = 0; j < printer_subsystems[i].record_printers_count; j++) {
  350. if (rec_ident == printer_subsystems[i].record_printers[j].ident) {
  351. printer_subsystems[i].record_printers[j].print_fn ((const void **)&arguments[3]);
  352. return;
  353. }
  354. }
  355. }
  356. }
  357. switch(LOGSYS_DECODE_RECID(rec_ident)) {
  358. case LOGSYS_RECID_LOG:
  359. printf ("Log Message=%s\n", arguments[3]);
  360. break;
  361. case LOGSYS_RECID_ENTER:
  362. printf ("ENTERING function [%s] line [%d]\n", arguments[2], line);
  363. break;
  364. case LOGSYS_RECID_LEAVE:
  365. printf ("LEAVING function [%s] line [%d]\n", arguments[2], line);
  366. break;
  367. case LOGSYS_RECID_TRACE1:
  368. printf ("Tracing(1) Messsage=%s\n", arguments[3]);
  369. break;
  370. case LOGSYS_RECID_TRACE2:
  371. printf ("Tracing(2) Messsage=%s\n", arguments[3]);
  372. break;
  373. case LOGSYS_RECID_TRACE3:
  374. printf ("Tracing(3) Messsage=%s\n", arguments[3]);
  375. break;
  376. case LOGSYS_RECID_TRACE4:
  377. printf ("Tracing(4) Messsage=%s\n", arguments[3]);
  378. break;
  379. case LOGSYS_RECID_TRACE5:
  380. printf ("Tracing(5) Messsage=%s\n", arguments[3]);
  381. break;
  382. case LOGSYS_RECID_TRACE6:
  383. printf ("Tracing(6) Messsage=%s\n", arguments[3]);
  384. break;
  385. case LOGSYS_RECID_TRACE7:
  386. printf ("Tracing(7) Messsage=%s\n", arguments[3]);
  387. break;
  388. case LOGSYS_RECID_TRACE8:
  389. printf ("Tracing(8) Messsage=%s\n", arguments[3]);
  390. break;
  391. default:
  392. printf ("Unknown record type found subsys=[%s] ident=[%d]\n",
  393. arguments[0], LOGSYS_DECODE_RECID(rec_ident));
  394. break;
  395. }
  396. #ifdef COMPILE_OUT
  397. printf ("\n");
  398. #endif
  399. }
  400. int main (void)
  401. {
  402. unsigned int fd;
  403. int rec_idx;
  404. int end_rec;
  405. int record_count = 1;
  406. ssize_t n_read;
  407. const char *data_file = LOCALSTATEDIR "/lib/corosync/fdata";
  408. size_t n_required;
  409. if ((fd = open (data_file, O_RDONLY)) < 0) {
  410. fprintf (stderr, "failed to open %s: %s\n",
  411. data_file, strerror (errno));
  412. return EXIT_FAILURE;
  413. }
  414. n_required = sizeof (unsigned int);
  415. n_read = read (fd, &flt_data_size, n_required);
  416. if (n_read != n_required) {
  417. fprintf (stderr, "Unable to read fdata header\n");
  418. return EXIT_FAILURE;
  419. }
  420. n_required = ((flt_data_size + 2) * sizeof(unsigned int));
  421. if ((flt_data = malloc (n_required)) == NULL) {
  422. fprintf (stderr, "exhausted virtual memory\n");
  423. return EXIT_FAILURE;
  424. }
  425. n_read = read (fd, flt_data, n_required);
  426. close (fd);
  427. if (n_read < 0) {
  428. fprintf (stderr, "reading %s failed: %s\n",
  429. data_file, strerror (errno));
  430. return EXIT_FAILURE;
  431. }
  432. if (n_read != n_required) {
  433. printf ("Warning: read %lu bytes, but expected %lu\n",
  434. (unsigned long) n_read, (unsigned long) n_required);
  435. }
  436. rec_idx = flt_data[FDTAIL_INDEX];
  437. end_rec = flt_data[FDHEAD_INDEX];
  438. printf ("Starting replay: head [%d] tail [%d]\n",
  439. flt_data[FDHEAD_INDEX],
  440. flt_data[FDTAIL_INDEX]);
  441. for (;;) {
  442. rec_idx = logsys_rec_get (rec_idx);
  443. logsys_rec_print (g_record);
  444. if (rec_idx == end_rec) {
  445. break;
  446. }
  447. record_count += 1;
  448. }
  449. printf ("Finishing replay: records found [%d]\n", record_count);
  450. return (0);
  451. }