birq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * birq
  3. *
  4. * Balance IRQ
  5. *
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif /* HAVE_CONFIG_H */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <syslog.h>
  19. #include <fcntl.h>
  20. #include <time.h>
  21. #ifdef HAVE_GETOPT_H
  22. #include <getopt.h>
  23. #endif
  24. #include "birq.h"
  25. #include "lub/log.h"
  26. #include "lub/list.h"
  27. #include "lub/ini.h"
  28. #include "irq.h"
  29. #include "numa.h"
  30. #include "cpu.h"
  31. #include "statistics.h"
  32. #include "balance.h"
  33. #include "pxm.h"
  34. #ifndef VERSION
  35. #define VERSION "1.2.0"
  36. #endif
  37. /* Signal handlers */
  38. static volatile int sigterm = 0; /* Exit if 1 */
  39. static void sighandler(int signo);
  40. static volatile int sighup = 0; /* Re-read config file */
  41. static void sighup_handler(int signo);
  42. static void help(int status, const char *argv0);
  43. static struct options *opts_init(void);
  44. static void opts_free(struct options *opts);
  45. static int opts_parse(int argc, char *argv[], struct options *opts);
  46. static int parse_config(const char *fname, struct options *opts);
  47. /* Command line options */
  48. struct options {
  49. char *pidfile;
  50. char *cfgfile;
  51. int cfgfile_userdefined;
  52. char *pxm; /* Proximity config file */
  53. int debug; /* Don't daemonize in debug mode */
  54. int log_facility;
  55. float threshold;
  56. float load_limit;
  57. int verbose;
  58. int ht;
  59. int non_local_cpus;
  60. unsigned int long_interval;
  61. unsigned int short_interval;
  62. birq_choose_strategy_e strategy;
  63. cpumask_t exclude_cpus;
  64. };
  65. /*--------------------------------------------------------- */
  66. int main(int argc, char **argv)
  67. {
  68. int retval = -1;
  69. struct options *opts = NULL;
  70. int pidfd = -1;
  71. unsigned int interval;
  72. /* Signal vars */
  73. struct sigaction sig_act;
  74. sigset_t sig_set;
  75. /* IRQ list. It contain all found IRQs. */
  76. lub_list_t *irqs;
  77. /* IRQs need to be balanced */
  78. lub_list_t *balance_irqs;
  79. /* CPU list. It contain all found CPUs. */
  80. lub_list_t *cpus;
  81. /* NUMA list. It contain all found NUMA nodes. */
  82. lub_list_t *numas;
  83. /* Proximity list. */
  84. lub_list_t *pxms;
  85. /* Parse command line options */
  86. opts = opts_init();
  87. if (opts_parse(argc, argv, opts))
  88. goto err;
  89. /* Parse config file */
  90. if (!access(opts->cfgfile, R_OK)) {
  91. if (parse_config(opts->cfgfile, opts))
  92. goto err;
  93. } else if (opts->cfgfile_userdefined) {
  94. fprintf(stderr, "Error: Can't find config file %s\n",
  95. opts->cfgfile);
  96. goto err;
  97. }
  98. /* Validate threshold and load limit */
  99. if (opts->load_limit > opts->threshold) {
  100. fprintf(stderr, "Error: The load limit is greater than threshold.\n");
  101. goto err;
  102. }
  103. /* Initialize syslog */
  104. openlog(argv[0], LOG_CONS, opts->log_facility);
  105. syslog(LOG_ERR, "Start daemon.\n");
  106. /* Fork the daemon */
  107. if (!opts->debug) {
  108. /* Daemonize */
  109. if (daemon(0, 0) < 0) {
  110. syslog(LOG_ERR, "Can't daemonize\n");
  111. goto err;
  112. }
  113. /* Write pidfile */
  114. if ((pidfd = open(opts->pidfile,
  115. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  116. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  117. syslog(LOG_WARNING, "Can't open pidfile %s: %s\n",
  118. opts->pidfile, strerror(errno));
  119. } else {
  120. char str[20];
  121. snprintf(str, sizeof(str), "%u\n", getpid());
  122. str[sizeof(str) - 1] = '\0';
  123. if (write(pidfd, str, strlen(str)) < 0)
  124. syslog(LOG_WARNING, "Can't write to %s: %s\n",
  125. opts->pidfile, strerror(errno));
  126. close(pidfd);
  127. }
  128. }
  129. /* Set signal handler */
  130. sigemptyset(&sig_set);
  131. sigaddset(&sig_set, SIGTERM);
  132. sigaddset(&sig_set, SIGINT);
  133. sigaddset(&sig_set, SIGQUIT);
  134. sig_act.sa_flags = 0;
  135. sig_act.sa_mask = sig_set;
  136. sig_act.sa_handler = &sighandler;
  137. sigaction(SIGTERM, &sig_act, NULL);
  138. sigaction(SIGINT, &sig_act, NULL);
  139. sigaction(SIGQUIT, &sig_act, NULL);
  140. /* SIGHUP handler */
  141. sigemptyset(&sig_set);
  142. sigaddset(&sig_set, SIGHUP);
  143. sig_act.sa_flags = 0;
  144. sig_act.sa_mask = sig_set;
  145. sig_act.sa_handler = &sighup_handler;
  146. sigaction(SIGHUP, &sig_act, NULL);
  147. /* Randomize */
  148. srand(time(NULL));
  149. /* Scan NUMA nodes */
  150. numas = lub_list_new(numa_list_compare);
  151. scan_numas(numas);
  152. if (opts->verbose)
  153. show_numas(numas);
  154. /* Scan CPUs */
  155. cpus = lub_list_new(cpu_list_compare);
  156. scan_cpus(cpus, opts->ht);
  157. if (opts->verbose)
  158. show_cpus(cpus);
  159. /* Prepare data structures */
  160. irqs = lub_list_new(irq_list_compare);
  161. balance_irqs = lub_list_new(irq_list_compare);
  162. /* Parse proximity file */
  163. pxms = lub_list_new(NULL);
  164. if (opts->pxm)
  165. parse_pxm_config(opts->pxm, pxms, numas);
  166. if (opts->verbose)
  167. show_pxms(pxms);
  168. /* Main loop */
  169. while (!sigterm) {
  170. lub_list_node_t *node;
  171. char outstr[10];
  172. time_t t;
  173. struct tm *tmp;
  174. t = time(NULL);
  175. tmp = localtime(&t);
  176. if (tmp) {
  177. strftime(outstr, sizeof(outstr), "%H:%M:%S", tmp);
  178. printf("----[ %s ]----------------------------------------------------------------\n", outstr);
  179. }
  180. /* Re-read config file on SIGHUP */
  181. if (sighup) {
  182. if (!access(opts->cfgfile, R_OK)) {
  183. syslog(LOG_ERR, "Re-reading config file\n");
  184. if (parse_config(opts->cfgfile, opts))
  185. syslog(LOG_ERR, "Error while config file parsing.\n");
  186. } else if (opts->cfgfile_userdefined)
  187. syslog(LOG_ERR, "Can't find config file.\n");
  188. sighup = 0;
  189. }
  190. /* Rescan PCI devices for new IRQs. */
  191. scan_irqs(irqs, balance_irqs, pxms);
  192. if (opts->verbose)
  193. irq_list_show(irqs);
  194. /* Link IRQs to CPUs due to real current smp affinity. */
  195. link_irqs_to_cpus(cpus, irqs);
  196. /* Gather statistics on CPU load and number of interrupts. */
  197. gather_statistics(cpus, irqs);
  198. show_statistics(cpus, opts->verbose);
  199. /* Choose IRQ to move to another CPU. */
  200. choose_irqs_to_move(cpus, balance_irqs,
  201. opts->threshold, opts->strategy, &opts->exclude_cpus);
  202. /* Balance IRQs */
  203. if (lub_list_len(balance_irqs) != 0) {
  204. /* Set short interval to make balancing faster. */
  205. interval = opts->short_interval;
  206. /* Choose new CPU for IRQs need to be balanced. */
  207. balance(cpus, balance_irqs, opts->load_limit,
  208. &opts->exclude_cpus, opts->non_local_cpus);
  209. /* Write new values to /proc/irq/<IRQ>/smp_affinity */
  210. apply_affinity(balance_irqs);
  211. /* Free list of balanced IRQs */
  212. while ((node = lub_list__get_tail(balance_irqs))) {
  213. lub_list_del(balance_irqs, node);
  214. lub_list_node_free(node);
  215. }
  216. } else {
  217. /* If nothing to balance */
  218. interval = opts->long_interval;
  219. }
  220. /* Wait before next iteration */
  221. sleep(interval);
  222. }
  223. /* Free data structures */
  224. irq_list_free(irqs);
  225. lub_list_free(balance_irqs);
  226. cpu_list_free(cpus);
  227. numa_list_free(numas);
  228. pxm_list_free(pxms);
  229. retval = 0;
  230. err:
  231. /* Remove pidfile */
  232. if (pidfd >= 0) {
  233. if (unlink(opts->pidfile) < 0) {
  234. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  235. opts->pidfile, strerror(errno));
  236. }
  237. }
  238. /* Free command line options */
  239. opts_free(opts);
  240. syslog(LOG_ERR, "Stop daemon.\n");
  241. return retval;
  242. }
  243. /*--------------------------------------------------------- */
  244. /* Signal handler for temination signals (like SIGTERM, SIGINT, ...) */
  245. static void sighandler(int signo)
  246. {
  247. sigterm = 1;
  248. signo = signo; /* Happy compiler */
  249. }
  250. /*--------------------------------------------------------- */
  251. /* Re-read config file on SIGHUP */
  252. static void sighup_handler(int signo)
  253. {
  254. sighup = 1;
  255. signo = signo; /* Happy compiler */
  256. }
  257. /*--------------------------------------------------------- */
  258. /* Initialize option structure by defaults */
  259. static struct options *opts_init(void)
  260. {
  261. struct options *opts = NULL;
  262. opts = malloc(sizeof(*opts));
  263. assert(opts);
  264. opts->debug = 0; /* daemonize by default */
  265. opts->pidfile = strdup(BIRQ_PIDFILE);
  266. opts->cfgfile = strdup(BIRQ_CFGFILE);
  267. opts->cfgfile_userdefined = 0;
  268. opts->pxm = NULL;
  269. opts->log_facility = LOG_DAEMON;
  270. opts->threshold = BIRQ_DEFAULT_THRESHOLD;
  271. opts->load_limit = BIRQ_DEFAULT_LOAD_LIMIT;
  272. opts->verbose = 0;
  273. opts->ht = 1; /* It's 1 since 1.5.0 */
  274. opts->non_local_cpus = 0;
  275. opts->long_interval = BIRQ_LONG_INTERVAL;
  276. opts->short_interval = BIRQ_SHORT_INTERVAL;
  277. opts->strategy = BIRQ_CHOOSE_RND;
  278. cpus_init(opts->exclude_cpus);
  279. cpus_clear(opts->exclude_cpus);
  280. return opts;
  281. }
  282. /*--------------------------------------------------------- */
  283. /* Free option structure */
  284. static void opts_free(struct options *opts)
  285. {
  286. if (opts->pidfile)
  287. free(opts->pidfile);
  288. if (opts->cfgfile)
  289. free(opts->cfgfile);
  290. if (opts->pxm)
  291. free(opts->pxm);
  292. cpus_free(opts->exclude_cpus);
  293. free(opts);
  294. }
  295. /* Parse y/n options */
  296. static int opt_parse_y_n(const char *optarg, int *flag)
  297. {
  298. assert(optarg);
  299. assert(flag);
  300. if (!strcmp(optarg, "y"))
  301. *flag = 1;
  302. else if (!strcmp(optarg, "yes"))
  303. *flag = 1;
  304. else if (!strcmp(optarg, "n"))
  305. *flag = 0;
  306. else if (!strcmp(optarg, "no"))
  307. *flag = 0;
  308. else {
  309. fprintf(stderr, "Error: Illegal flag value %s.\n", optarg);
  310. return -1;
  311. }
  312. return 0;
  313. }
  314. /* Parse 'strategy' option */
  315. static int opt_parse_strategy(const char *optarg, birq_choose_strategy_e *strategy)
  316. {
  317. assert(optarg);
  318. assert(strategy);
  319. if (!strcmp(optarg, "max"))
  320. *strategy = BIRQ_CHOOSE_MAX;
  321. else if (!strcmp(optarg, "min"))
  322. *strategy = BIRQ_CHOOSE_MIN;
  323. else if (!strcmp(optarg, "rnd"))
  324. *strategy = BIRQ_CHOOSE_RND;
  325. else {
  326. fprintf(stderr, "Error: Illegal strategy value %s.\n", optarg);
  327. return -1;
  328. }
  329. return 0;
  330. }
  331. /* Parse 'threshold' and 'load-limit' options */
  332. static int opt_parse_threshold(const char *optarg, float *threshold)
  333. {
  334. char *endptr;
  335. float thresh;
  336. assert(optarg);
  337. assert(threshold);
  338. thresh = strtof(optarg, &endptr);
  339. if (endptr == optarg) {
  340. fprintf(stderr, "Error: Illegal threshold/load-limit value %s.\n", optarg);
  341. return -1;
  342. }
  343. if (thresh > 100.00) {
  344. fprintf(stderr, "Error: The threshold/load-limit value %s > 100.\n", optarg);
  345. return -1;
  346. }
  347. *threshold = thresh;
  348. return 0;
  349. }
  350. /* Parse 'short-interval' and 'long-interval' options */
  351. static int opt_parse_interval(const char *optarg, unsigned int *interval)
  352. {
  353. char *endptr;
  354. unsigned long int val;
  355. assert(optarg);
  356. assert(interval);
  357. val = strtoul(optarg, &endptr, 10);
  358. if (endptr == optarg) {
  359. fprintf(stderr, "Error: Illegal interval value %s.\n", optarg);
  360. return -1;
  361. }
  362. *interval = val;
  363. return 0;
  364. }
  365. /*--------------------------------------------------------- */
  366. /* Parse command line options */
  367. static int opts_parse(int argc, char *argv[], struct options *opts)
  368. {
  369. static const char *shortopts = "hp:c:dO:t:l:vri:I:s:x:";
  370. #ifdef HAVE_GETOPT_H
  371. static const struct option longopts[] = {
  372. {"help", 0, NULL, 'h'},
  373. {"pid", 1, NULL, 'p'},
  374. {"conf", 1, NULL, 'c'},
  375. {"debug", 0, NULL, 'd'},
  376. {"facility", 1, NULL, 'O'},
  377. {"threshold", 1, NULL, 't'},
  378. {"load-limit", 1, NULL, 't'},
  379. {"verbose", 0, NULL, 'v'},
  380. {"ht", 0, NULL, 'r'},
  381. {"short-interval", 1, NULL, 'i'},
  382. {"long-interval", 1, NULL, 'I'},
  383. {"strategy", 1, NULL, 's'},
  384. {"pxm", 1, NULL, 'x'},
  385. {NULL, 0, NULL, 0}
  386. };
  387. #endif
  388. optind = 1;
  389. while(1) {
  390. int opt;
  391. #ifdef HAVE_GETOPT_H
  392. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  393. #else
  394. opt = getopt(argc, argv, shortopts);
  395. #endif
  396. if (-1 == opt)
  397. break;
  398. switch (opt) {
  399. case 'p':
  400. if (opts->pidfile)
  401. free(opts->pidfile);
  402. opts->pidfile = strdup(optarg);
  403. break;
  404. case 'c':
  405. if (opts->cfgfile)
  406. free(opts->cfgfile);
  407. opts->cfgfile = strdup(optarg);
  408. opts->cfgfile_userdefined = 1;
  409. break;
  410. case 'x':
  411. if (opts->pxm)
  412. free(opts->pxm);
  413. opts->pxm = strdup(optarg);
  414. break;
  415. case 'd':
  416. opts->debug = 1;
  417. break;
  418. case 'v':
  419. opts->verbose = 1;
  420. break;
  421. case 'r':
  422. fprintf(stderr, "Warning: The -r option is obsoleted. The HT is enabled by default.\n");
  423. break;
  424. case 'O':
  425. if (lub_log_facility(optarg, &(opts->log_facility))) {
  426. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  427. exit(-1);
  428. }
  429. break;
  430. case 't':
  431. if (opt_parse_threshold(optarg, &opts->threshold))
  432. exit(-1);
  433. break;
  434. case 'l':
  435. if (opt_parse_threshold(optarg, &opts->load_limit))
  436. exit(-1);
  437. break;
  438. case 'i':
  439. if (opt_parse_interval(optarg, &opts->short_interval))
  440. exit(-1);
  441. break;
  442. case 'I':
  443. if (opt_parse_interval(optarg, &opts->long_interval))
  444. exit(-1);
  445. break;
  446. case 's':
  447. if (opt_parse_strategy(optarg, &opts->strategy) < 0)
  448. exit(-1);
  449. break;
  450. case 'h':
  451. help(0, argv[0]);
  452. exit(0);
  453. break;
  454. default:
  455. help(-1, argv[0]);
  456. exit(-1);
  457. break;
  458. }
  459. }
  460. return 0;
  461. }
  462. /*--------------------------------------------------------- */
  463. /* Print help message */
  464. static void help(int status, const char *argv0)
  465. {
  466. const char *name = NULL;
  467. if (!argv0)
  468. return;
  469. /* Find the basename */
  470. name = strrchr(argv0, '/');
  471. if (name)
  472. name++;
  473. else
  474. name = argv0;
  475. if (status != 0) {
  476. fprintf(stderr, "Try `%s -h' for more information.\n",
  477. name);
  478. } else {
  479. printf("Version : %s\n", VERSION);
  480. printf("Usage : %s [options]\n", name);
  481. printf("Daemon to balance IRQs.\n");
  482. printf("Options :\n");
  483. printf("\t-h, --help Print this help.\n");
  484. printf("\t-d, --debug Debug mode. Don't daemonize.\n");
  485. printf("\t-v, --verbose Be verbose.\n");
  486. printf("\t-r, --ht This option is obsoleted. The Hyper Threading is enabled by default.\n");
  487. printf("\t-p <path>, --pid=<path> File to save daemon's PID to (" BIRQ_PIDFILE ").\n");
  488. printf("\t-c <path>, --conf=<path> Config file (" BIRQ_CFGFILE ").\n");
  489. printf("\t-x <path>, --pxm=<path> Proximity config file.\n");
  490. printf("\t-O, --facility Syslog facility (DAEMON).\n");
  491. printf("\t-t <float>, --threshold=<float> Threshold to consider CPU is overloaded, in percents. Default threhold is %.2f.\n",
  492. BIRQ_DEFAULT_THRESHOLD);
  493. printf("\t-l <float>, --load-limit=<float> Don't move IRQs to CPUs loaded more than this limit, in percents. Default limit is %.2f.\n",
  494. BIRQ_DEFAULT_LOAD_LIMIT);
  495. printf("\t-i <sec>, --short-interval=<sec> Short iteration interval.\n");
  496. printf("\t-I <sec>, --long-interval=<sec> Long iteration interval.\n");
  497. printf("\t-s <strategy>, --strategy=<strategy> Strategy to choose IRQ to move (min/max/rnd).\n");
  498. }
  499. }
  500. /*--------------------------------------------------------- */
  501. /* Parse config file */
  502. static int parse_config(const char *fname, struct options *opts)
  503. {
  504. int ret = -1; /* Pessimistic retval */
  505. lub_ini_t *ini;
  506. const char *tmp = NULL;
  507. ini = lub_ini_new();
  508. if (lub_ini_parse_file(ini, opts->cfgfile)) {
  509. lub_ini_free(ini);
  510. return -1;
  511. }
  512. if ((tmp = lub_ini_find(ini, "strategy")))
  513. if (opt_parse_strategy(tmp, &opts->strategy) < 0)
  514. goto err;
  515. if ((tmp = lub_ini_find(ini, "threshold")))
  516. if (opt_parse_threshold(tmp, &opts->threshold))
  517. goto err;
  518. if ((tmp = lub_ini_find(ini, "load-limit")))
  519. if (opt_parse_threshold(tmp, &opts->load_limit))
  520. goto err;
  521. if ((tmp = lub_ini_find(ini, "short-interval")))
  522. if (opt_parse_interval(tmp, &opts->short_interval))
  523. goto err;
  524. if ((tmp = lub_ini_find(ini, "long-interval")))
  525. if (opt_parse_interval(tmp, &opts->long_interval))
  526. goto err;
  527. if ((tmp = lub_ini_find(ini, "exclude-cpus")))
  528. if (cpumask_parse_user(tmp, strlen(tmp), opts->exclude_cpus)) {
  529. fprintf(stderr, "Error: Can't parse exclude-cpu option \"%s\".\n", tmp);
  530. goto err;
  531. }
  532. if ((tmp = lub_ini_find(ini, "ht")))
  533. if (opt_parse_y_n(tmp, &opts->ht))
  534. goto err;
  535. if ((tmp = lub_ini_find(ini, "non-local-cpus")))
  536. if (opt_parse_y_n(tmp, &opts->non_local_cpus))
  537. goto err;
  538. ret = 0;
  539. err:
  540. lub_ini_free(ini);
  541. return ret;
  542. }