birq.c 14 KB

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