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