corosync-fplay.c 14 KB

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