mq_perf_tests.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * This application is Copyright 2012 Red Hat, Inc.
  3. * Doug Ledford <dledford@redhat.com>
  4. *
  5. * mq_perf_tests is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * mq_perf_tests is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * For the full text of the license, see <http://www.gnu.org/licenses/>.
  15. *
  16. * mq_perf_tests.c
  17. * Tests various types of message queue workloads, concentrating on those
  18. * situations that invole large message sizes, large message queue depths,
  19. * or both, and reports back useful metrics about kernel message queue
  20. * performance.
  21. *
  22. */
  23. #define _GNU_SOURCE
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <limits.h>
  30. #include <errno.h>
  31. #include <signal.h>
  32. #include <pthread.h>
  33. #include <sched.h>
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #include <sys/resource.h>
  37. #include <sys/stat.h>
  38. #include <sys/param.h>
  39. #include <mqueue.h>
  40. #include <popt.h>
  41. #include <error.h>
  42. #include "kselftest.h"
  43. static char *usage =
  44. "Usage:\n"
  45. " %s [-c #[,#..] -f] path\n"
  46. "\n"
  47. " -c # Skip most tests and go straight to a high queue depth test\n"
  48. " and then run that test continuously (useful for running at\n"
  49. " the same time as some other workload to see how much the\n"
  50. " cache thrashing caused by adding messages to a very deep\n"
  51. " queue impacts the performance of other programs). The number\n"
  52. " indicates which CPU core we should bind the process to during\n"
  53. " the run. If you have more than one physical CPU, then you\n"
  54. " will need one copy per physical CPU package, and you should\n"
  55. " specify the CPU cores to pin ourself to via a comma separated\n"
  56. " list of CPU values.\n"
  57. " -f Only usable with continuous mode. Pin ourself to the CPUs\n"
  58. " as requested, then instead of looping doing a high mq\n"
  59. " workload, just busy loop. This will allow us to lock up a\n"
  60. " single CPU just like we normally would, but without actually\n"
  61. " thrashing the CPU cache. This is to make it easier to get\n"
  62. " comparable numbers from some other workload running on the\n"
  63. " other CPUs. One set of numbers with # CPUs locked up running\n"
  64. " an mq workload, and another set of numbers with those same\n"
  65. " CPUs locked away from the test workload, but not doing\n"
  66. " anything to trash the cache like the mq workload might.\n"
  67. " path Path name of the message queue to create\n"
  68. "\n"
  69. " Note: this program must be run as root in order to enable all tests\n"
  70. "\n";
  71. char *MAX_MSGS = "/proc/sys/fs/mqueue/msg_max";
  72. char *MAX_MSGSIZE = "/proc/sys/fs/mqueue/msgsize_max";
  73. #define MAX_CPUS 64
  74. char *cpu_option_string;
  75. int cpus_to_pin[MAX_CPUS];
  76. int num_cpus_to_pin;
  77. pthread_t cpu_threads[MAX_CPUS];
  78. pthread_t main_thread;
  79. cpu_set_t *cpu_set;
  80. int cpu_set_size;
  81. int cpus_online;
  82. #define MSG_SIZE 16
  83. #define TEST1_LOOPS 10000000
  84. #define TEST2_LOOPS 100000
  85. int continuous_mode;
  86. int continuous_mode_fake;
  87. struct rlimit saved_limits, cur_limits;
  88. int saved_max_msgs, saved_max_msgsize;
  89. int cur_max_msgs, cur_max_msgsize;
  90. FILE *max_msgs, *max_msgsize;
  91. int cur_nice;
  92. char *queue_path = "/mq_perf_tests";
  93. mqd_t queue = -1;
  94. struct mq_attr result;
  95. int mq_prio_max;
  96. const struct poptOption options[] = {
  97. {
  98. .longName = "continuous",
  99. .shortName = 'c',
  100. .argInfo = POPT_ARG_STRING,
  101. .arg = &cpu_option_string,
  102. .val = 'c',
  103. .descrip = "Run continuous tests at a high queue depth in "
  104. "order to test the effects of cache thrashing on "
  105. "other tasks on the system. This test is intended "
  106. "to be run on one core of each physical CPU while "
  107. "some other CPU intensive task is run on all the other "
  108. "cores of that same physical CPU and the other task "
  109. "is timed. It is assumed that the process of adding "
  110. "messages to the message queue in a tight loop will "
  111. "impact that other task to some degree. Once the "
  112. "tests are performed in this way, you should then "
  113. "re-run the tests using fake mode in order to check "
  114. "the difference in time required to perform the CPU "
  115. "intensive task",
  116. .argDescrip = "cpu[,cpu]",
  117. },
  118. {
  119. .longName = "fake",
  120. .shortName = 'f',
  121. .argInfo = POPT_ARG_NONE,
  122. .arg = &continuous_mode_fake,
  123. .val = 0,
  124. .descrip = "Tie up the CPUs that we would normally tie up in"
  125. "continuous mode, but don't actually do any mq stuff, "
  126. "just keep the CPU busy so it can't be used to process "
  127. "system level tasks as this would free up resources on "
  128. "the other CPU cores and skew the comparison between "
  129. "the no-mqueue work and mqueue work tests",
  130. .argDescrip = NULL,
  131. },
  132. {
  133. .longName = "path",
  134. .shortName = 'p',
  135. .argInfo = POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT,
  136. .arg = &queue_path,
  137. .val = 'p',
  138. .descrip = "The name of the path to use in the mqueue "
  139. "filesystem for our tests",
  140. .argDescrip = "pathname",
  141. },
  142. POPT_AUTOHELP
  143. POPT_TABLEEND
  144. };
  145. static inline void __set(FILE *stream, int value, char *err_msg);
  146. void shutdown(int exit_val, char *err_cause, int line_no);
  147. void sig_action_SIGUSR1(int signum, siginfo_t *info, void *context);
  148. void sig_action(int signum, siginfo_t *info, void *context);
  149. static inline int get(FILE *stream);
  150. static inline void set(FILE *stream, int value);
  151. static inline int try_set(FILE *stream, int value);
  152. static inline void getr(int type, struct rlimit *rlim);
  153. static inline void setr(int type, struct rlimit *rlim);
  154. static inline void open_queue(struct mq_attr *attr);
  155. void increase_limits(void);
  156. static inline void __set(FILE *stream, int value, char *err_msg)
  157. {
  158. rewind(stream);
  159. if (fprintf(stream, "%d", value) < 0)
  160. perror(err_msg);
  161. }
  162. void shutdown(int exit_val, char *err_cause, int line_no)
  163. {
  164. static int in_shutdown = 0;
  165. int errno_at_shutdown = errno;
  166. int i;
  167. /* In case we get called by multiple threads or from an sighandler */
  168. if (in_shutdown++)
  169. return;
  170. /* Free the cpu_set allocated using CPU_ALLOC in main function */
  171. CPU_FREE(cpu_set);
  172. for (i = 0; i < num_cpus_to_pin; i++)
  173. if (cpu_threads[i]) {
  174. pthread_kill(cpu_threads[i], SIGUSR1);
  175. pthread_join(cpu_threads[i], NULL);
  176. }
  177. if (queue != -1)
  178. if (mq_close(queue))
  179. perror("mq_close() during shutdown");
  180. if (queue_path)
  181. /*
  182. * Be silent if this fails, if we cleaned up already it's
  183. * expected to fail
  184. */
  185. mq_unlink(queue_path);
  186. if (saved_max_msgs)
  187. __set(max_msgs, saved_max_msgs,
  188. "failed to restore saved_max_msgs");
  189. if (saved_max_msgsize)
  190. __set(max_msgsize, saved_max_msgsize,
  191. "failed to restore saved_max_msgsize");
  192. if (exit_val)
  193. error(exit_val, errno_at_shutdown, "%s at %d",
  194. err_cause, line_no);
  195. exit(0);
  196. }
  197. void sig_action_SIGUSR1(int signum, siginfo_t *info, void *context)
  198. {
  199. if (pthread_self() != main_thread)
  200. pthread_exit(0);
  201. else {
  202. fprintf(stderr, "Caught signal %d in SIGUSR1 handler, "
  203. "exiting\n", signum);
  204. shutdown(0, "", 0);
  205. fprintf(stderr, "\n\nReturned from shutdown?!?!\n\n");
  206. exit(0);
  207. }
  208. }
  209. void sig_action(int signum, siginfo_t *info, void *context)
  210. {
  211. if (pthread_self() != main_thread)
  212. pthread_kill(main_thread, signum);
  213. else {
  214. fprintf(stderr, "Caught signal %d, exiting\n", signum);
  215. shutdown(0, "", 0);
  216. fprintf(stderr, "\n\nReturned from shutdown?!?!\n\n");
  217. exit(0);
  218. }
  219. }
  220. static inline int get(FILE *stream)
  221. {
  222. int value;
  223. rewind(stream);
  224. if (fscanf(stream, "%d", &value) != 1)
  225. shutdown(4, "Error reading /proc entry", __LINE__);
  226. return value;
  227. }
  228. static inline void set(FILE *stream, int value)
  229. {
  230. int new_value;
  231. rewind(stream);
  232. if (fprintf(stream, "%d", value) < 0)
  233. return shutdown(5, "Failed writing to /proc file", __LINE__);
  234. new_value = get(stream);
  235. if (new_value != value)
  236. return shutdown(5, "We didn't get what we wrote to /proc back",
  237. __LINE__);
  238. }
  239. static inline int try_set(FILE *stream, int value)
  240. {
  241. int new_value;
  242. rewind(stream);
  243. fprintf(stream, "%d", value);
  244. new_value = get(stream);
  245. return new_value == value;
  246. }
  247. static inline void getr(int type, struct rlimit *rlim)
  248. {
  249. if (getrlimit(type, rlim))
  250. shutdown(6, "getrlimit()", __LINE__);
  251. }
  252. static inline void setr(int type, struct rlimit *rlim)
  253. {
  254. if (setrlimit(type, rlim))
  255. shutdown(7, "setrlimit()", __LINE__);
  256. }
  257. /**
  258. * open_queue - open the global queue for testing
  259. * @attr - An attr struct specifying the desired queue traits
  260. * @result - An attr struct that lists the actual traits the queue has
  261. *
  262. * This open is not allowed to fail, failure will result in an orderly
  263. * shutdown of the program. The global queue_path is used to set what
  264. * queue to open, the queue descriptor is saved in the global queue
  265. * variable.
  266. */
  267. static inline void open_queue(struct mq_attr *attr)
  268. {
  269. int flags = O_RDWR | O_EXCL | O_CREAT | O_NONBLOCK;
  270. int perms = DEFFILEMODE;
  271. queue = mq_open(queue_path, flags, perms, attr);
  272. if (queue == -1)
  273. shutdown(1, "mq_open()", __LINE__);
  274. if (mq_getattr(queue, &result))
  275. shutdown(1, "mq_getattr()", __LINE__);
  276. printf("\n\tQueue %s created:\n", queue_path);
  277. printf("\t\tmq_flags:\t\t\t%s\n", result.mq_flags & O_NONBLOCK ?
  278. "O_NONBLOCK" : "(null)");
  279. printf("\t\tmq_maxmsg:\t\t\t%lu\n", result.mq_maxmsg);
  280. printf("\t\tmq_msgsize:\t\t\t%lu\n", result.mq_msgsize);
  281. printf("\t\tmq_curmsgs:\t\t\t%lu\n", result.mq_curmsgs);
  282. }
  283. void *fake_cont_thread(void *arg)
  284. {
  285. int i;
  286. for (i = 0; i < num_cpus_to_pin; i++)
  287. if (cpu_threads[i] == pthread_self())
  288. break;
  289. printf("\tStarted fake continuous mode thread %d on CPU %d\n", i,
  290. cpus_to_pin[i]);
  291. while (1)
  292. ;
  293. }
  294. void *cont_thread(void *arg)
  295. {
  296. char buff[MSG_SIZE];
  297. int i;
  298. unsigned int priority;
  299. for (i = 0; i < num_cpus_to_pin; i++)
  300. if (cpu_threads[i] == pthread_self())
  301. break;
  302. printf("\tStarted continuous mode thread %d on CPU %d\n", i,
  303. cpus_to_pin[i]);
  304. while (1) {
  305. while (mq_send(queue, buff, sizeof(buff), 0) == 0)
  306. ;
  307. mq_receive(queue, buff, sizeof(buff), &priority);
  308. }
  309. }
  310. #define drain_queue() \
  311. while (mq_receive(queue, buff, MSG_SIZE, &prio_in) == MSG_SIZE)
  312. #define do_untimed_send() \
  313. do { \
  314. if (mq_send(queue, buff, MSG_SIZE, prio_out)) \
  315. shutdown(3, "Test send failure", __LINE__); \
  316. } while (0)
  317. #define do_send_recv() \
  318. do { \
  319. clock_gettime(clock, &start); \
  320. if (mq_send(queue, buff, MSG_SIZE, prio_out)) \
  321. shutdown(3, "Test send failure", __LINE__); \
  322. clock_gettime(clock, &middle); \
  323. if (mq_receive(queue, buff, MSG_SIZE, &prio_in) != MSG_SIZE) \
  324. shutdown(3, "Test receive failure", __LINE__); \
  325. clock_gettime(clock, &end); \
  326. nsec = ((middle.tv_sec - start.tv_sec) * 1000000000) + \
  327. (middle.tv_nsec - start.tv_nsec); \
  328. send_total.tv_nsec += nsec; \
  329. if (send_total.tv_nsec >= 1000000000) { \
  330. send_total.tv_sec++; \
  331. send_total.tv_nsec -= 1000000000; \
  332. } \
  333. nsec = ((end.tv_sec - middle.tv_sec) * 1000000000) + \
  334. (end.tv_nsec - middle.tv_nsec); \
  335. recv_total.tv_nsec += nsec; \
  336. if (recv_total.tv_nsec >= 1000000000) { \
  337. recv_total.tv_sec++; \
  338. recv_total.tv_nsec -= 1000000000; \
  339. } \
  340. } while (0)
  341. struct test {
  342. char *desc;
  343. void (*func)(int *);
  344. };
  345. void const_prio(int *prio)
  346. {
  347. return;
  348. }
  349. void inc_prio(int *prio)
  350. {
  351. if (++*prio == mq_prio_max)
  352. *prio = 0;
  353. }
  354. void dec_prio(int *prio)
  355. {
  356. if (--*prio < 0)
  357. *prio = mq_prio_max - 1;
  358. }
  359. void random_prio(int *prio)
  360. {
  361. *prio = random() % mq_prio_max;
  362. }
  363. struct test test2[] = {
  364. {"\n\tTest #2a: Time send/recv message, queue full, constant prio\n",
  365. const_prio},
  366. {"\n\tTest #2b: Time send/recv message, queue full, increasing prio\n",
  367. inc_prio},
  368. {"\n\tTest #2c: Time send/recv message, queue full, decreasing prio\n",
  369. dec_prio},
  370. {"\n\tTest #2d: Time send/recv message, queue full, random prio\n",
  371. random_prio},
  372. {NULL, NULL}
  373. };
  374. /**
  375. * Tests to perform (all done with MSG_SIZE messages):
  376. *
  377. * 1) Time to add/remove message with 0 messages on queue
  378. * 1a) with constant prio
  379. * 2) Time to add/remove message when queue close to capacity:
  380. * 2a) with constant prio
  381. * 2b) with increasing prio
  382. * 2c) with decreasing prio
  383. * 2d) with random prio
  384. * 3) Test limits of priorities honored (double check _SC_MQ_PRIO_MAX)
  385. */
  386. void *perf_test_thread(void *arg)
  387. {
  388. char buff[MSG_SIZE];
  389. int prio_out;
  390. unsigned int prio_in;
  391. int i;
  392. clockid_t clock;
  393. pthread_t *t;
  394. struct timespec res, start, middle, end, send_total, recv_total;
  395. unsigned long long nsec;
  396. struct test *cur_test;
  397. t = &cpu_threads[0];
  398. printf("\n\tStarted mqueue performance test thread on CPU %d\n",
  399. cpus_to_pin[0]);
  400. mq_prio_max = sysconf(_SC_MQ_PRIO_MAX);
  401. if (mq_prio_max == -1)
  402. shutdown(2, "sysconf(_SC_MQ_PRIO_MAX)", __LINE__);
  403. if (pthread_getcpuclockid(cpu_threads[0], &clock) != 0)
  404. shutdown(2, "pthread_getcpuclockid", __LINE__);
  405. if (clock_getres(clock, &res))
  406. shutdown(2, "clock_getres()", __LINE__);
  407. printf("\t\tMax priorities:\t\t\t%d\n", mq_prio_max);
  408. printf("\t\tClock resolution:\t\t%lu nsec%s\n", res.tv_nsec,
  409. res.tv_nsec > 1 ? "s" : "");
  410. printf("\n\tTest #1: Time send/recv message, queue empty\n");
  411. printf("\t\t(%d iterations)\n", TEST1_LOOPS);
  412. prio_out = 0;
  413. send_total.tv_sec = 0;
  414. send_total.tv_nsec = 0;
  415. recv_total.tv_sec = 0;
  416. recv_total.tv_nsec = 0;
  417. for (i = 0; i < TEST1_LOOPS; i++)
  418. do_send_recv();
  419. printf("\t\tSend msg:\t\t\t%ld.%lus total time\n",
  420. send_total.tv_sec, send_total.tv_nsec);
  421. nsec = ((unsigned long long)send_total.tv_sec * 1000000000 +
  422. send_total.tv_nsec) / TEST1_LOOPS;
  423. printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
  424. printf("\t\tRecv msg:\t\t\t%ld.%lus total time\n",
  425. recv_total.tv_sec, recv_total.tv_nsec);
  426. nsec = ((unsigned long long)recv_total.tv_sec * 1000000000 +
  427. recv_total.tv_nsec) / TEST1_LOOPS;
  428. printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
  429. for (cur_test = test2; cur_test->desc != NULL; cur_test++) {
  430. printf("%s:\n", cur_test->desc);
  431. printf("\t\t(%d iterations)\n", TEST2_LOOPS);
  432. prio_out = 0;
  433. send_total.tv_sec = 0;
  434. send_total.tv_nsec = 0;
  435. recv_total.tv_sec = 0;
  436. recv_total.tv_nsec = 0;
  437. printf("\t\tFilling queue...");
  438. fflush(stdout);
  439. clock_gettime(clock, &start);
  440. for (i = 0; i < result.mq_maxmsg - 1; i++) {
  441. do_untimed_send();
  442. cur_test->func(&prio_out);
  443. }
  444. clock_gettime(clock, &end);
  445. nsec = ((unsigned long long)(end.tv_sec - start.tv_sec) *
  446. 1000000000) + (end.tv_nsec - start.tv_nsec);
  447. printf("done.\t\t%lld.%llds\n", nsec / 1000000000,
  448. nsec % 1000000000);
  449. printf("\t\tTesting...");
  450. fflush(stdout);
  451. for (i = 0; i < TEST2_LOOPS; i++) {
  452. do_send_recv();
  453. cur_test->func(&prio_out);
  454. }
  455. printf("done.\n");
  456. printf("\t\tSend msg:\t\t\t%ld.%lus total time\n",
  457. send_total.tv_sec, send_total.tv_nsec);
  458. nsec = ((unsigned long long)send_total.tv_sec * 1000000000 +
  459. send_total.tv_nsec) / TEST2_LOOPS;
  460. printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
  461. printf("\t\tRecv msg:\t\t\t%ld.%lus total time\n",
  462. recv_total.tv_sec, recv_total.tv_nsec);
  463. nsec = ((unsigned long long)recv_total.tv_sec * 1000000000 +
  464. recv_total.tv_nsec) / TEST2_LOOPS;
  465. printf("\t\t\t\t\t\t%lld nsec/msg\n", nsec);
  466. printf("\t\tDraining queue...");
  467. fflush(stdout);
  468. clock_gettime(clock, &start);
  469. drain_queue();
  470. clock_gettime(clock, &end);
  471. nsec = ((unsigned long long)(end.tv_sec - start.tv_sec) *
  472. 1000000000) + (end.tv_nsec - start.tv_nsec);
  473. printf("done.\t\t%lld.%llds\n", nsec / 1000000000,
  474. nsec % 1000000000);
  475. }
  476. return 0;
  477. }
  478. void increase_limits(void)
  479. {
  480. cur_limits.rlim_cur = RLIM_INFINITY;
  481. cur_limits.rlim_max = RLIM_INFINITY;
  482. setr(RLIMIT_MSGQUEUE, &cur_limits);
  483. while (try_set(max_msgs, cur_max_msgs += 10))
  484. ;
  485. cur_max_msgs = get(max_msgs);
  486. while (try_set(max_msgsize, cur_max_msgsize += 1024))
  487. ;
  488. cur_max_msgsize = get(max_msgsize);
  489. if (setpriority(PRIO_PROCESS, 0, -20) != 0)
  490. shutdown(2, "setpriority()", __LINE__);
  491. cur_nice = -20;
  492. }
  493. int main(int argc, char *argv[])
  494. {
  495. struct mq_attr attr;
  496. char *option, *next_option;
  497. int i, cpu, rc;
  498. struct sigaction sa;
  499. poptContext popt_context;
  500. void *retval;
  501. main_thread = pthread_self();
  502. num_cpus_to_pin = 0;
  503. if (sysconf(_SC_NPROCESSORS_ONLN) == -1) {
  504. perror("sysconf(_SC_NPROCESSORS_ONLN)");
  505. exit(1);
  506. }
  507. if (getuid() != 0)
  508. ksft_exit_skip("Not running as root, but almost all tests "
  509. "require root in order to modify\nsystem settings. "
  510. "Exiting.\n");
  511. cpus_online = MIN(MAX_CPUS, sysconf(_SC_NPROCESSORS_ONLN));
  512. cpu_set = CPU_ALLOC(cpus_online);
  513. if (cpu_set == NULL) {
  514. perror("CPU_ALLOC()");
  515. exit(1);
  516. }
  517. cpu_set_size = CPU_ALLOC_SIZE(cpus_online);
  518. CPU_ZERO_S(cpu_set_size, cpu_set);
  519. popt_context = poptGetContext(NULL, argc, (const char **)argv,
  520. options, 0);
  521. while ((rc = poptGetNextOpt(popt_context)) > 0) {
  522. switch (rc) {
  523. case 'c':
  524. continuous_mode = 1;
  525. option = cpu_option_string;
  526. do {
  527. next_option = strchr(option, ',');
  528. if (next_option)
  529. *next_option = '\0';
  530. cpu = atoi(option);
  531. if (cpu >= cpus_online)
  532. fprintf(stderr, "CPU %d exceeds "
  533. "cpus online, ignoring.\n",
  534. cpu);
  535. else
  536. cpus_to_pin[num_cpus_to_pin++] = cpu;
  537. if (next_option)
  538. option = ++next_option;
  539. } while (next_option && num_cpus_to_pin < MAX_CPUS);
  540. /* Double check that they didn't give us the same CPU
  541. * more than once */
  542. for (cpu = 0; cpu < num_cpus_to_pin; cpu++) {
  543. if (CPU_ISSET_S(cpus_to_pin[cpu], cpu_set_size,
  544. cpu_set)) {
  545. fprintf(stderr, "Any given CPU may "
  546. "only be given once.\n");
  547. goto err_code;
  548. } else
  549. CPU_SET_S(cpus_to_pin[cpu],
  550. cpu_set_size, cpu_set);
  551. }
  552. break;
  553. case 'p':
  554. /*
  555. * Although we can create a msg queue with a
  556. * non-absolute path name, unlink will fail. So,
  557. * if the name doesn't start with a /, add one
  558. * when we save it.
  559. */
  560. option = queue_path;
  561. if (*option != '/') {
  562. queue_path = malloc(strlen(option) + 2);
  563. if (!queue_path) {
  564. perror("malloc()");
  565. goto err_code;
  566. }
  567. queue_path[0] = '/';
  568. queue_path[1] = 0;
  569. strcat(queue_path, option);
  570. free(option);
  571. }
  572. break;
  573. }
  574. }
  575. if (continuous_mode && num_cpus_to_pin == 0) {
  576. fprintf(stderr, "Must pass at least one CPU to continuous "
  577. "mode.\n");
  578. poptPrintUsage(popt_context, stderr, 0);
  579. goto err_code;
  580. } else if (!continuous_mode) {
  581. num_cpus_to_pin = 1;
  582. cpus_to_pin[0] = cpus_online - 1;
  583. }
  584. max_msgs = fopen(MAX_MSGS, "r+");
  585. max_msgsize = fopen(MAX_MSGSIZE, "r+");
  586. if (!max_msgs)
  587. shutdown(2, "Failed to open msg_max", __LINE__);
  588. if (!max_msgsize)
  589. shutdown(2, "Failed to open msgsize_max", __LINE__);
  590. /* Load up the current system values for everything we can */
  591. getr(RLIMIT_MSGQUEUE, &saved_limits);
  592. cur_limits = saved_limits;
  593. saved_max_msgs = cur_max_msgs = get(max_msgs);
  594. saved_max_msgsize = cur_max_msgsize = get(max_msgsize);
  595. errno = 0;
  596. cur_nice = getpriority(PRIO_PROCESS, 0);
  597. if (errno)
  598. shutdown(2, "getpriority()", __LINE__);
  599. /* Tell the user our initial state */
  600. printf("\nInitial system state:\n");
  601. printf("\tUsing queue path:\t\t\t%s\n", queue_path);
  602. printf("\tRLIMIT_MSGQUEUE(soft):\t\t\t%ld\n",
  603. (long) saved_limits.rlim_cur);
  604. printf("\tRLIMIT_MSGQUEUE(hard):\t\t\t%ld\n",
  605. (long) saved_limits.rlim_max);
  606. printf("\tMaximum Message Size:\t\t\t%d\n", saved_max_msgsize);
  607. printf("\tMaximum Queue Size:\t\t\t%d\n", saved_max_msgs);
  608. printf("\tNice value:\t\t\t\t%d\n", cur_nice);
  609. printf("\n");
  610. increase_limits();
  611. printf("Adjusted system state for testing:\n");
  612. if (cur_limits.rlim_cur == RLIM_INFINITY) {
  613. printf("\tRLIMIT_MSGQUEUE(soft):\t\t\t(unlimited)\n");
  614. printf("\tRLIMIT_MSGQUEUE(hard):\t\t\t(unlimited)\n");
  615. } else {
  616. printf("\tRLIMIT_MSGQUEUE(soft):\t\t\t%ld\n",
  617. (long) cur_limits.rlim_cur);
  618. printf("\tRLIMIT_MSGQUEUE(hard):\t\t\t%ld\n",
  619. (long) cur_limits.rlim_max);
  620. }
  621. printf("\tMaximum Message Size:\t\t\t%d\n", cur_max_msgsize);
  622. printf("\tMaximum Queue Size:\t\t\t%d\n", cur_max_msgs);
  623. printf("\tNice value:\t\t\t\t%d\n", cur_nice);
  624. printf("\tContinuous mode:\t\t\t(%s)\n", continuous_mode ?
  625. (continuous_mode_fake ? "fake mode" : "enabled") :
  626. "disabled");
  627. printf("\tCPUs to pin:\t\t\t\t%d", cpus_to_pin[0]);
  628. for (cpu = 1; cpu < num_cpus_to_pin; cpu++)
  629. printf(",%d", cpus_to_pin[cpu]);
  630. printf("\n");
  631. sa.sa_sigaction = sig_action_SIGUSR1;
  632. sigemptyset(&sa.sa_mask);
  633. sigaddset(&sa.sa_mask, SIGHUP);
  634. sigaddset(&sa.sa_mask, SIGINT);
  635. sigaddset(&sa.sa_mask, SIGQUIT);
  636. sigaddset(&sa.sa_mask, SIGTERM);
  637. sa.sa_flags = SA_SIGINFO;
  638. if (sigaction(SIGUSR1, &sa, NULL) == -1)
  639. shutdown(1, "sigaction(SIGUSR1)", __LINE__);
  640. sa.sa_sigaction = sig_action;
  641. if (sigaction(SIGHUP, &sa, NULL) == -1)
  642. shutdown(1, "sigaction(SIGHUP)", __LINE__);
  643. if (sigaction(SIGINT, &sa, NULL) == -1)
  644. shutdown(1, "sigaction(SIGINT)", __LINE__);
  645. if (sigaction(SIGQUIT, &sa, NULL) == -1)
  646. shutdown(1, "sigaction(SIGQUIT)", __LINE__);
  647. if (sigaction(SIGTERM, &sa, NULL) == -1)
  648. shutdown(1, "sigaction(SIGTERM)", __LINE__);
  649. if (!continuous_mode_fake) {
  650. attr.mq_flags = O_NONBLOCK;
  651. attr.mq_maxmsg = cur_max_msgs;
  652. attr.mq_msgsize = MSG_SIZE;
  653. open_queue(&attr);
  654. }
  655. for (i = 0; i < num_cpus_to_pin; i++) {
  656. pthread_attr_t thread_attr;
  657. void *thread_func;
  658. if (continuous_mode_fake)
  659. thread_func = &fake_cont_thread;
  660. else if (continuous_mode)
  661. thread_func = &cont_thread;
  662. else
  663. thread_func = &perf_test_thread;
  664. CPU_ZERO_S(cpu_set_size, cpu_set);
  665. CPU_SET_S(cpus_to_pin[i], cpu_set_size, cpu_set);
  666. pthread_attr_init(&thread_attr);
  667. pthread_attr_setaffinity_np(&thread_attr, cpu_set_size,
  668. cpu_set);
  669. if (pthread_create(&cpu_threads[i], &thread_attr, thread_func,
  670. NULL))
  671. shutdown(1, "pthread_create()", __LINE__);
  672. pthread_attr_destroy(&thread_attr);
  673. }
  674. if (!continuous_mode) {
  675. pthread_join(cpu_threads[0], &retval);
  676. shutdown((long)retval, "perf_test_thread()", __LINE__);
  677. } else {
  678. while (1)
  679. sleep(1);
  680. }
  681. shutdown(0, "", 0);
  682. err_code:
  683. CPU_FREE(cpu_set);
  684. exit(1);
  685. }