cpghum.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * Copyright (c) 2015-2017 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <errno.h>
  40. #include <time.h>
  41. #include <limits.h>
  42. #include <ctype.h>
  43. #include <syslog.h>
  44. #include <stdarg.h>
  45. #include <sys/time.h>
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #include <sys/select.h>
  49. #include <sys/uio.h>
  50. #include <sys/un.h>
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <pthread.h>
  54. #include <zlib.h>
  55. #include <libgen.h>
  56. #include <getopt.h>
  57. #include <corosync/corotypes.h>
  58. #include <corosync/cpg.h>
  59. static cpg_handle_t handle;
  60. static pthread_t thread;
  61. #ifndef timersub
  62. #define timersub(a, b, result) \
  63. do { \
  64. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  65. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  66. if ((result)->tv_usec < 0) { \
  67. --(result)->tv_sec; \
  68. (result)->tv_usec += 1000000; \
  69. } \
  70. } while (0)
  71. #endif /* timersub */
  72. static int alarm_notice;
  73. #define MAX_NODEID 65536
  74. #define ONE_MEG 1048576
  75. #define DATASIZE (ONE_MEG*20)
  76. static char data[DATASIZE];
  77. static int send_counter = 0;
  78. static int do_syslog = 0;
  79. static int quiet = 0;
  80. static int report_rtt = 0;
  81. static int abort_on_error = 0;
  82. static int machine_readable = 0;
  83. static char delimiter = ',';
  84. static int to_stderr = 0;
  85. static unsigned int g_our_nodeid;
  86. static volatile int stopped;
  87. static unsigned int flood_start = 64;
  88. static unsigned int flood_multiplier = 5;
  89. static unsigned long flood_max = (ONE_MEG - 100);
  90. // stats
  91. static unsigned int length_errors=0;
  92. static unsigned int crc_errors=0;
  93. static unsigned int sequence_errors=0;
  94. static unsigned int packets_sent=0;
  95. static unsigned int packets_recvd=0;
  96. static unsigned int packets_recvd1=0; /* For flood intermediates */
  97. static unsigned int send_retries=0;
  98. static unsigned int send_fails=0;
  99. static unsigned long avg_rtt=0;
  100. static unsigned long max_rtt=0;
  101. static unsigned long min_rtt=LONG_MAX;
  102. static unsigned long interim_avg_rtt=0;
  103. static unsigned long interim_max_rtt=0;
  104. static unsigned long interim_min_rtt=LONG_MAX;
  105. struct cpghum_header {
  106. unsigned int counter;
  107. unsigned int crc;
  108. unsigned int size;
  109. struct timeval timestamp;
  110. };
  111. static void cpg_bm_confchg_fn (
  112. cpg_handle_t handle_in,
  113. const struct cpg_name *group_name,
  114. const struct cpg_address *member_list, size_t member_list_entries,
  115. const struct cpg_address *left_list, size_t left_list_entries,
  116. const struct cpg_address *joined_list, size_t joined_list_entries)
  117. {
  118. }
  119. static unsigned int g_recv_count;
  120. static unsigned int g_recv_length;
  121. static int g_recv_start[MAX_NODEID+1];
  122. static int g_recv_counter[MAX_NODEID+1];
  123. static int g_recv_size[MAX_NODEID+1];
  124. static int g_log_mask = 0xFFFF;
  125. typedef enum
  126. {
  127. CPGH_LOG_INFO = 1,
  128. CPGH_LOG_PERF = 2,
  129. CPGH_LOG_RTT = 4,
  130. CPGH_LOG_STATS = 8,
  131. CPGH_LOG_ERR = 16
  132. } log_type_t;
  133. static void cpgh_print_message(int syslog_level, const char *facility_name, const char *format, va_list ap)
  134. {
  135. char msg[1024];
  136. int start = 0;
  137. if (machine_readable) {
  138. snprintf(msg, sizeof(msg), "%s%c", facility_name, delimiter);
  139. start = strlen(msg);
  140. }
  141. vsnprintf(msg+start, sizeof(msg)-start, format, ap);
  142. if (to_stderr || (syslog_level <= LOG_ERR)) {
  143. fprintf(stderr, "%s", msg);
  144. }
  145. else {
  146. printf("%s", msg);
  147. }
  148. if (do_syslog) {
  149. syslog(syslog_level, "%s", msg);
  150. }
  151. }
  152. static void cpgh_log_printf(log_type_t type, const char *format, ...)
  153. {
  154. va_list ap;
  155. if (!(type & g_log_mask)) {
  156. return;
  157. }
  158. va_start(ap, format);
  159. switch (type) {
  160. case CPGH_LOG_INFO:
  161. cpgh_print_message(LOG_INFO, "[Info]", format, ap);
  162. break;
  163. case CPGH_LOG_PERF:
  164. cpgh_print_message(LOG_INFO, "[Perf]", format, ap);
  165. break;
  166. case CPGH_LOG_RTT:
  167. cpgh_print_message(LOG_INFO, "[RTT]", format, ap);
  168. break;
  169. case CPGH_LOG_STATS:
  170. cpgh_print_message(LOG_INFO, "[Stats]", format, ap);
  171. break;
  172. case CPGH_LOG_ERR:
  173. cpgh_print_message(LOG_ERR, "[Err]", format, ap);
  174. break;
  175. default:
  176. break;
  177. }
  178. va_end(ap);
  179. }
  180. static unsigned long update_rtt(struct timeval *header_timestamp, int packet_count,
  181. unsigned long *rtt_min, unsigned long *rtt_avg, unsigned long *rtt_max)
  182. {
  183. struct timeval tv1;
  184. struct timeval rtt;
  185. unsigned long rtt_usecs;
  186. gettimeofday (&tv1, NULL);
  187. timersub(&tv1, header_timestamp, &rtt);
  188. rtt_usecs = rtt.tv_usec + rtt.tv_sec*1000000;
  189. if (rtt_usecs > *rtt_max) {
  190. *rtt_max = rtt_usecs;
  191. }
  192. if (rtt_usecs < *rtt_min) {
  193. *rtt_min = rtt_usecs;
  194. }
  195. /* Don't start the average with 0 */
  196. if (*rtt_avg == 0) {
  197. *rtt_avg = rtt_usecs;
  198. }
  199. else {
  200. *rtt_avg = ((*rtt_avg * packet_count) + rtt_usecs) / (packet_count+1);
  201. }
  202. return rtt_usecs;
  203. }
  204. static void cpg_bm_deliver_fn (
  205. cpg_handle_t handle_in,
  206. const struct cpg_name *group_name,
  207. uint32_t nodeid,
  208. uint32_t pid,
  209. void *msg,
  210. size_t msg_len)
  211. {
  212. uLong crc=0;
  213. struct cpghum_header *header = (struct cpghum_header *)msg;
  214. uLong recv_crc = header->crc & 0xFFFFFFFF;
  215. unsigned int *dataint = (unsigned int *)((char*)msg + sizeof(struct cpghum_header));
  216. unsigned int datalen;
  217. if (nodeid > MAX_NODEID) {
  218. cpgh_log_printf(CPGH_LOG_ERR, "Got message from invalid nodeid %d (too high for us). Quitting\n", nodeid);
  219. exit(1);
  220. }
  221. packets_recvd++;
  222. packets_recvd1++;
  223. g_recv_length = msg_len;
  224. datalen = header->size - sizeof(struct cpghum_header);
  225. // Report RTT first in case abort_on_error is set
  226. if (nodeid == g_our_nodeid) {
  227. unsigned long rtt_usecs;
  228. // For flood
  229. update_rtt(&header->timestamp, packets_recvd1, &interim_min_rtt, &interim_avg_rtt, &interim_max_rtt);
  230. rtt_usecs = update_rtt(&header->timestamp, g_recv_counter[nodeid], &min_rtt, &avg_rtt, &max_rtt);
  231. if (report_rtt) {
  232. if (machine_readable) {
  233. cpgh_log_printf(CPGH_LOG_RTT, "%ld%c%ld%c%ld%c%ld\n", rtt_usecs, delimiter, min_rtt, delimiter, avg_rtt, delimiter, max_rtt);
  234. }
  235. else {
  236. cpgh_log_printf(CPGH_LOG_RTT, "%s: RTT %ld uS (min/avg/max): %ld/%ld/%ld\n", group_name->value, rtt_usecs, min_rtt, avg_rtt, max_rtt);
  237. }
  238. }
  239. }
  240. // Basic check, packets should all be the right size
  241. if (msg_len != header->size) {
  242. length_errors++;
  243. cpgh_log_printf(CPGH_LOG_ERR, "%s: message sizes don't match. got %zu, expected %u from node %d\n", group_name->value, msg_len, header->size, nodeid);
  244. if (abort_on_error) {
  245. exit(2);
  246. }
  247. }
  248. g_recv_size[nodeid] = msg_len;
  249. // Sequence counters are incrementing in step?
  250. if (header->counter != g_recv_counter[nodeid]) {
  251. /* Don't report the first mismatch or a newly restarted sender, we're just catching up */
  252. if (g_recv_counter[nodeid] && header->counter) {
  253. sequence_errors++;
  254. cpgh_log_printf(CPGH_LOG_ERR, "%s: counters don't match. got %d, expected %d from node %d\n", group_name->value, header->counter, g_recv_counter[nodeid], nodeid);
  255. if (abort_on_error) {
  256. exit(2);
  257. }
  258. }
  259. else {
  260. g_recv_start[nodeid] = header->counter;
  261. }
  262. /* Catch up or we'll be printing errors for ever */
  263. g_recv_counter[nodeid] = header->counter+1;
  264. }
  265. else {
  266. g_recv_counter[nodeid]++;
  267. }
  268. /* Check crc */
  269. crc = crc32(0, NULL, 0);
  270. crc = crc32(crc, (Bytef *)dataint, datalen) & 0xFFFFFFFF;
  271. if (crc != recv_crc) {
  272. crc_errors++;
  273. cpgh_log_printf(CPGH_LOG_ERR, "%s: CRCs don't match. got %lx, expected %lx from nodeid %d\n", group_name->value, recv_crc, crc, nodeid);
  274. if (abort_on_error) {
  275. exit(2);
  276. }
  277. }
  278. g_recv_count++;
  279. }
  280. static cpg_model_v1_data_t model1_data = {
  281. .cpg_deliver_fn = cpg_bm_deliver_fn,
  282. .cpg_confchg_fn = cpg_bm_confchg_fn,
  283. };
  284. static cpg_callbacks_t callbacks = {
  285. .cpg_deliver_fn = cpg_bm_deliver_fn,
  286. .cpg_confchg_fn = cpg_bm_confchg_fn
  287. };
  288. static struct cpg_name group_name = {
  289. .value = "cpghum",
  290. .length = 7
  291. };
  292. static void set_packet(int write_size, int counter)
  293. {
  294. struct cpghum_header *header = (struct cpghum_header *)data;
  295. int i;
  296. unsigned int *dataint = (unsigned int *)(data + sizeof(struct cpghum_header));
  297. unsigned int datalen = write_size - sizeof(struct cpghum_header);
  298. struct timeval tv1;
  299. uLong crc;
  300. header->counter = counter;
  301. for (i=0; i<(datalen/4); i++) {
  302. dataint[i] = rand();
  303. }
  304. crc = crc32(0, NULL, 0);
  305. header->crc = crc32(crc, (Bytef*)&dataint[0], datalen);
  306. header->size = write_size;
  307. gettimeofday (&tv1, NULL);
  308. memcpy(&header->timestamp, &tv1, sizeof(struct timeval));
  309. }
  310. /* Basically this is cpgbench.c */
  311. static void cpg_flood (
  312. cpg_handle_t handle_in,
  313. int write_size)
  314. {
  315. struct timeval tv1, tv2, tv_elapsed;
  316. struct iovec iov;
  317. unsigned int res = CS_OK;
  318. alarm_notice = 0;
  319. iov.iov_base = data;
  320. iov.iov_len = write_size;
  321. alarm (10);
  322. packets_recvd1 = 0;
  323. interim_avg_rtt = 0;
  324. interim_max_rtt = 0;
  325. interim_min_rtt = LONG_MAX;
  326. gettimeofday (&tv1, NULL);
  327. do {
  328. if (res == CS_OK) {
  329. set_packet(write_size, send_counter);
  330. }
  331. res = cpg_mcast_joined (handle_in, CPG_TYPE_AGREED, &iov, 1);
  332. if (res == CS_OK) {
  333. /* Only increment the packet counter if it was sucessfully sent */
  334. packets_sent++;
  335. send_counter++;
  336. }
  337. else {
  338. if (res == CS_ERR_TRY_AGAIN) {
  339. send_retries++;
  340. }
  341. else {
  342. send_fails++;
  343. }
  344. }
  345. } while (!stopped && alarm_notice == 0 && (res == CS_OK || res == CS_ERR_TRY_AGAIN));
  346. gettimeofday (&tv2, NULL);
  347. timersub (&tv2, &tv1, &tv_elapsed);
  348. if (!quiet) {
  349. if (machine_readable) {
  350. cpgh_log_printf (CPGH_LOG_PERF, "%d%c%d%c%f%c%f%c%f%c%ld%c%ld%c%ld\n", packets_recvd1, delimiter, write_size, delimiter,
  351. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)), delimiter,
  352. ((float)packets_recvd1) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)), delimiter,
  353. ((float)packets_recvd1) * ((float)write_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0), delimiter,
  354. interim_min_rtt, delimiter, interim_avg_rtt, delimiter, interim_max_rtt);
  355. }
  356. else {
  357. cpgh_log_printf (CPGH_LOG_PERF, "%5d messages received ", packets_recvd1);
  358. cpgh_log_printf (CPGH_LOG_PERF, "%5d bytes per write ", write_size);
  359. cpgh_log_printf (CPGH_LOG_PERF, "%7.3f Seconds runtime ",
  360. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  361. cpgh_log_printf (CPGH_LOG_PERF, "%9.3f TP/s ",
  362. ((float)packets_recvd1) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  363. cpgh_log_printf (CPGH_LOG_PERF, "%7.3f MB/s ",
  364. ((float)packets_recvd1) * ((float)write_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
  365. cpgh_log_printf (CPGH_LOG_PERF, "RTT for this size (min/avg/max) %ld/%ld/%ld\n",
  366. interim_min_rtt, interim_avg_rtt, interim_max_rtt);
  367. }
  368. }
  369. }
  370. static void cpg_test (
  371. cpg_handle_t handle_in,
  372. int write_size,
  373. int delay_time,
  374. int print_time)
  375. {
  376. struct timeval tv1, tv2, tv_elapsed;
  377. struct iovec iov;
  378. unsigned int res;
  379. alarm_notice = 0;
  380. iov.iov_base = data;
  381. iov.iov_len = write_size;
  382. g_recv_count = 0;
  383. alarm (print_time);
  384. do {
  385. send_counter++;
  386. resend:
  387. set_packet(write_size, send_counter);
  388. res = cpg_mcast_joined (handle_in, CPG_TYPE_AGREED, &iov, 1);
  389. if (res == CS_ERR_TRY_AGAIN) {
  390. usleep(10000);
  391. send_retries++;
  392. goto resend;
  393. }
  394. if (res != CS_OK) {
  395. cpgh_log_printf(CPGH_LOG_ERR, "send failed: %d\n", res);
  396. send_fails++;
  397. }
  398. else {
  399. packets_sent++;
  400. }
  401. usleep(delay_time*1000);
  402. } while (alarm_notice == 0 && (res == CS_OK || res == CS_ERR_TRY_AGAIN) && stopped == 0);
  403. gettimeofday (&tv2, NULL);
  404. timersub (&tv2, &tv1, &tv_elapsed);
  405. if (!quiet) {
  406. if (machine_readable) {
  407. cpgh_log_printf(CPGH_LOG_RTT, "%d%c%ld%c%ld%c%ld\n", 0, delimiter, min_rtt, delimiter, avg_rtt, delimiter, max_rtt);
  408. }
  409. else {
  410. cpgh_log_printf(CPGH_LOG_PERF, "%s: %5d message%s received, ", group_name.value, g_recv_count, g_recv_count==1?"":"s");
  411. cpgh_log_printf(CPGH_LOG_PERF, "%5d bytes per write. ", write_size);
  412. cpgh_log_printf(CPGH_LOG_RTT, "RTT min/avg/max: %ld/%ld/%ld\n", min_rtt, avg_rtt, max_rtt);
  413. }
  414. }
  415. }
  416. static void sigalrm_handler (int num)
  417. {
  418. alarm_notice = 1;
  419. }
  420. static void sigint_handler (int num)
  421. {
  422. stopped = 1;
  423. }
  424. static void* dispatch_thread (void *arg)
  425. {
  426. cpg_dispatch (handle, CS_DISPATCH_BLOCKING);
  427. return NULL;
  428. }
  429. static void usage(char *cmd)
  430. {
  431. fprintf(stderr, "%s [OPTIONS]\n", cmd);
  432. fprintf(stderr, "\n");
  433. fprintf(stderr, "%s sends CPG messages to all registered users of the CPG.\n", cmd);
  434. fprintf(stderr, "The messages have a sequence number and a CRC so that missing or\n");
  435. fprintf(stderr, "corrupted messages will be detected and reported.\n");
  436. fprintf(stderr, "\n");
  437. fprintf(stderr, "%s can also be asked to simply listen for (and check) packets\n", cmd);
  438. fprintf(stderr, "so that there is another node in the cluster connected to the CPG.\n");
  439. fprintf(stderr, "\n");
  440. fprintf(stderr, "Multiple copies, in different CPGs, can also be run on the same or\n");
  441. fprintf(stderr, "different nodes by using the -n option.\n");
  442. fprintf(stderr, "\n");
  443. fprintf(stderr, "%s can handle more than 1 sender in the same CPG provided they are on\n", cmd);
  444. fprintf(stderr, "different nodes.\n");
  445. fprintf(stderr, "\n");
  446. fprintf(stderr, " -w<num>, --size-bytes Write size in Kbytes, default 4\n");
  447. fprintf(stderr, " -W<num>, --size-kb Write size in bytes, default 4096\n");
  448. fprintf(stderr, " -n<name>, --name CPG name to use, default 'cpghum'\n");
  449. fprintf(stderr, " -M Write machine-readable results\n");
  450. fprintf(stderr, " -D<char> Delimiter for machine-readable results (default ',')\n");
  451. fprintf(stderr, " -E Send normal output to stderr instead of stdout\n");
  452. fprintf(stderr, " -d<num>, --delay Delay between sending packets (mS), default 1000\n");
  453. fprintf(stderr, " -r<num> Number of repetitions, default 100\n");
  454. fprintf(stderr, " -p<num> Delay between printing output (seconds), default 10s\n");
  455. fprintf(stderr, " -l, --listen Listen and check CRCs only, don't send (^C to quit)\n");
  456. fprintf(stderr, " -t, --rtt Report Round Trip Times for each packet.\n");
  457. fprintf(stderr, " -m<num> cpg_initialise() model. Default 1.\n");
  458. fprintf(stderr, " -s Also send errors to syslog.\n");
  459. fprintf(stderr, " -f, --flood Flood test CPG (cpgbench). see --flood-* long options\n");
  460. fprintf(stderr, " -a Abort on crc/length/sequence error\n");
  461. fprintf(stderr, " -q, --quiet Quiet. Don't print messages every 10s (see also -p)\n");
  462. fprintf(stderr, " -qq Very quiet. Don't print stats at the end\n");
  463. fprintf(stderr, " --flood-start=bytes Start value for --flood\n");
  464. fprintf(stderr, " --flood-mult=value Packet size multiplier value for --flood\n");
  465. fprintf(stderr, " --flood-max=bytes Maximum packet size for --flood\n");
  466. fprintf(stderr, "\n");
  467. fprintf(stderr, " values for --flood* and -W can have K or M suffixes to indicate\n");
  468. fprintf(stderr, " Kilobytes or Megabytes\n");
  469. fprintf(stderr, "\n");
  470. fprintf(stderr, "%s exit code is 0 if no error happened, 1 on generic error and 2 on\n", cmd);
  471. fprintf(stderr, "send/crc/length/sequence error");
  472. fprintf(stderr, "\n");
  473. }
  474. /* Parse a size, optionally ending in 'K', 'M' */
  475. static long parse_bytes(const char *valstring)
  476. {
  477. unsigned int value;
  478. int multiplier = 1;
  479. char suffix = '\0';
  480. int have_suffix = 0;
  481. /* Suffix is optional */
  482. if (sscanf(valstring, "%u%c", &value, &suffix) == 0) {
  483. return 0;
  484. }
  485. if (toupper(suffix) == 'M') {
  486. multiplier = 1024*1024;
  487. have_suffix = 1;
  488. }
  489. if (toupper(suffix) == 'K') {
  490. multiplier = 1024;
  491. have_suffix = 1;
  492. }
  493. if (!have_suffix && suffix != '\0') {
  494. fprintf(stderr, "Invalid suffix '%c', only K or M supported\n", suffix);
  495. return 0;
  496. }
  497. return value * multiplier;
  498. }
  499. int main (int argc, char *argv[]) {
  500. int i;
  501. unsigned int res;
  502. uint32_t maxsize;
  503. int opt;
  504. int bs;
  505. int write_size = 4096;
  506. int delay_time = 1000;
  507. int repetitions = 100;
  508. int print_time = 10;
  509. int have_size = 0;
  510. int listen_only = 0;
  511. int flood = 0;
  512. int model = 1;
  513. int option_index = 0;
  514. struct option long_options[] = {
  515. {"flood-start", required_argument, 0, 0 },
  516. {"flood-mult", required_argument, 0, 0 },
  517. {"flood-max", required_argument, 0, 0 },
  518. {"size-kb", required_argument, 0, 'w' },
  519. {"size-bytes", required_argument, 0, 'W' },
  520. {"name", required_argument, 0, 'n' },
  521. {"rtt", no_argument, 0, 't' },
  522. {"flood", no_argument, 0, 'f' },
  523. {"quiet", no_argument, 0, 'q' },
  524. {"listen", no_argument, 0, 'l' },
  525. {"help", no_argument, 0, '?' },
  526. {0, 0, 0, 0 }
  527. };
  528. while ( (opt = getopt_long(argc, argv, "qlstafMEn:d:r:p:m:w:W:D:",
  529. long_options, &option_index)) != -1 ) {
  530. switch (opt) {
  531. case 0: // Long-only options
  532. if (strcmp(long_options[option_index].name, "flood-start") == 0) {
  533. flood_start = parse_bytes(optarg);
  534. if (flood_start == 0) {
  535. fprintf(stderr, "flood-start value invalid\n");
  536. exit(1);
  537. }
  538. }
  539. if (strcmp(long_options[option_index].name, "flood-mult") == 0) {
  540. flood_multiplier = parse_bytes(optarg);
  541. if (flood_multiplier == 0) {
  542. fprintf(stderr, "flood-mult value invalid\n");
  543. exit(1);
  544. }
  545. }
  546. if (strcmp(long_options[option_index].name, "flood-max") == 0) {
  547. flood_max = parse_bytes(optarg);
  548. if (flood_max == 0) {
  549. fprintf(stderr, "flood-max value invalid\n");
  550. exit(1);
  551. }
  552. }
  553. break;
  554. case 'w': // Write size in K
  555. bs = atoi(optarg);
  556. if (bs > 0) {
  557. write_size = bs*1024;
  558. have_size = 1;
  559. }
  560. break;
  561. case 'W': // Write size in bytes (or with a suffix)
  562. bs = parse_bytes(optarg);
  563. if (bs > 0) {
  564. write_size = bs;
  565. have_size = 1;
  566. }
  567. break;
  568. case 'n':
  569. if (strlen(optarg) >= CPG_MAX_NAME_LENGTH) {
  570. fprintf(stderr, "CPG name too long\n");
  571. exit(1);
  572. }
  573. strcpy(group_name.value, optarg);
  574. group_name.length = strlen(group_name.value);
  575. break;
  576. case 't':
  577. report_rtt = 1;
  578. break;
  579. case 'E':
  580. to_stderr = 1;
  581. break;
  582. case 'M':
  583. machine_readable = 1;
  584. break;
  585. case 'f':
  586. flood = 1;
  587. break;
  588. case 'a':
  589. abort_on_error = 1;
  590. break;
  591. case 'd':
  592. delay_time = atoi(optarg);
  593. break;
  594. case 'D':
  595. delimiter = optarg[0];
  596. break;
  597. case 'r':
  598. repetitions = atoi(optarg);
  599. break;
  600. case 'p':
  601. print_time = atoi(optarg);
  602. break;
  603. case 'l':
  604. listen_only = 1;
  605. break;
  606. case 's':
  607. do_syslog = 1;
  608. break;
  609. case 'q':
  610. quiet++;
  611. break;
  612. case 'm':
  613. model = atoi(optarg);
  614. if (model < 0 || model > 1) {
  615. fprintf(stderr, "%s: Model must be 0-1\n", argv[0]);
  616. exit(1);
  617. }
  618. break;
  619. case '?':
  620. usage(basename(argv[0]));
  621. exit(1);
  622. }
  623. }
  624. if (!have_size && flood) {
  625. write_size = flood_start;
  626. }
  627. signal (SIGALRM, sigalrm_handler);
  628. signal (SIGINT, sigint_handler);
  629. switch (model) {
  630. case 0:
  631. res = cpg_initialize (&handle, &callbacks);
  632. break;
  633. case 1:
  634. res = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model1_data, NULL);
  635. break;
  636. default:
  637. res=999; // can't get here but it keeps the compiler happy
  638. break;
  639. }
  640. if (res != CS_OK) {
  641. cpgh_log_printf(CPGH_LOG_ERR, "cpg_initialize failed with result %d\n", res);
  642. exit (1);
  643. }
  644. cpg_local_get(handle, &g_our_nodeid);
  645. pthread_create (&thread, NULL, dispatch_thread, NULL);
  646. res = cpg_join (handle, &group_name);
  647. if (res != CS_OK) {
  648. cpgh_log_printf(CPGH_LOG_ERR, "cpg_join failed with result %d\n", res);
  649. exit (1);
  650. }
  651. if (listen_only) {
  652. int secs = 0;
  653. while (!stopped) {
  654. sleep(1);
  655. if (++secs > print_time && !quiet) {
  656. int nodes_printed = 0;
  657. if (!machine_readable) {
  658. for (i=1; i<MAX_NODEID; i++) {
  659. if (g_recv_counter[i]) {
  660. cpgh_log_printf(CPGH_LOG_INFO, "%s: %5d message%s of %d bytes received from node %d\n",
  661. group_name.value, g_recv_counter[i] - g_recv_start[i],
  662. g_recv_counter[i]==1?"":"s",
  663. g_recv_size[i], i);
  664. nodes_printed++;
  665. }
  666. }
  667. }
  668. /* Separate list of nodes if more than one */
  669. if (nodes_printed > 1) {
  670. cpgh_log_printf(CPGH_LOG_INFO, "\n");
  671. }
  672. secs = 0;
  673. }
  674. }
  675. }
  676. else {
  677. cpg_max_atomic_msgsize_get (handle, &maxsize);
  678. if (write_size > maxsize) {
  679. fprintf(stderr, "INFO: packet size (%d) is larger than the maximum atomic size (%d), libcpg will fragment\n",
  680. write_size, maxsize);
  681. }
  682. /* The main job starts here */
  683. if (flood) {
  684. for (i = 0; i < 10; i++) { /* number of repetitions - up to 50k */
  685. cpg_flood (handle, write_size);
  686. signal (SIGALRM, sigalrm_handler);
  687. write_size *= flood_multiplier;
  688. if (write_size > flood_max) {
  689. break;
  690. }
  691. }
  692. }
  693. else {
  694. send_counter = -1; /* So we start from zero to allow listeners to sync */
  695. for (i = 0; i < repetitions && !stopped; i++) {
  696. cpg_test (handle, write_size, delay_time, print_time);
  697. signal (SIGALRM, sigalrm_handler);
  698. }
  699. }
  700. }
  701. res = cpg_finalize (handle);
  702. if (res != CS_OK) {
  703. cpgh_log_printf(CPGH_LOG_ERR, "cpg_finalize failed with result %d\n", res);
  704. exit (1);
  705. }
  706. if (quiet < 2) {
  707. /* Don't print LONG_MAX for min_rtt if we don't have a value */
  708. if (min_rtt == LONG_MAX) {
  709. min_rtt = 0L;
  710. }
  711. if (machine_readable) {
  712. cpgh_log_printf(CPGH_LOG_STATS, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%ld%c%ld%c%ld\n",
  713. packets_sent, delimiter,
  714. send_fails, delimiter,
  715. send_retries, delimiter,
  716. length_errors, delimiter,
  717. packets_recvd, delimiter,
  718. sequence_errors, delimiter,
  719. crc_errors, delimiter,
  720. min_rtt, delimiter,
  721. avg_rtt, delimiter,
  722. max_rtt);
  723. }
  724. else {
  725. cpgh_log_printf(CPGH_LOG_STATS, "\n");
  726. cpgh_log_printf(CPGH_LOG_STATS, "Stats:\n");
  727. if (!listen_only) {
  728. cpgh_log_printf(CPGH_LOG_STATS, " packets sent: %d\n", packets_sent);
  729. cpgh_log_printf(CPGH_LOG_STATS, " send failures: %d\n", send_fails);
  730. cpgh_log_printf(CPGH_LOG_STATS, " send retries: %d\n", send_retries);
  731. }
  732. cpgh_log_printf(CPGH_LOG_STATS, " length errors: %d\n", length_errors);
  733. cpgh_log_printf(CPGH_LOG_STATS, " packets recvd: %d\n", packets_recvd);
  734. cpgh_log_printf(CPGH_LOG_STATS, " sequence errors: %d\n", sequence_errors);
  735. cpgh_log_printf(CPGH_LOG_STATS, " crc errors: %d\n", crc_errors);
  736. if (!listen_only) {
  737. cpgh_log_printf(CPGH_LOG_STATS, " min RTT: %ld\n", min_rtt);
  738. cpgh_log_printf(CPGH_LOG_STATS, " max RTT: %ld\n", max_rtt);
  739. cpgh_log_printf(CPGH_LOG_STATS, " avg RTT: %ld\n", avg_rtt);
  740. }
  741. cpgh_log_printf(CPGH_LOG_STATS, "\n");
  742. }
  743. }
  744. res = 0;
  745. if (send_fails > 0 || (have_size && length_errors > 0) || sequence_errors > 0 || crc_errors > 0) {
  746. res = 2;
  747. }
  748. return (res);
  749. }