corosync-fplay.c 13 KB

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