mq_perf_tests.c 21 KB

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