mq_perf_tests.c 21 KB

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