birq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. /* Write new values to /proc/irq/<IRQ>/smp_affinity */
  208. apply_affinity(balance_irqs);
  209. /* Free list of balanced IRQs */
  210. while ((node = lub_list__get_tail(balance_irqs))) {
  211. lub_list_del(balance_irqs, node);
  212. lub_list_node_free(node);
  213. }
  214. } else {
  215. /* If nothing to balance */
  216. interval = opts->long_interval;
  217. }
  218. /* Wait before next iteration */
  219. sleep(interval);
  220. }
  221. /* Free data structures */
  222. irq_list_free(irqs);
  223. lub_list_free(balance_irqs);
  224. cpu_list_free(cpus);
  225. numa_list_free(numas);
  226. pxm_list_free(pxms);
  227. retval = 0;
  228. err:
  229. /* Remove pidfile */
  230. if (pidfd >= 0) {
  231. if (unlink(opts->pidfile) < 0) {
  232. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  233. opts->pidfile, strerror(errno));
  234. }
  235. }
  236. /* Free command line options */
  237. opts_free(opts);
  238. syslog(LOG_ERR, "Stop daemon.\n");
  239. return retval;
  240. }
  241. /*--------------------------------------------------------- */
  242. /* Signal handler for temination signals (like SIGTERM, SIGINT, ...) */
  243. static void sighandler(int signo)
  244. {
  245. sigterm = 1;
  246. signo = signo; /* Happy compiler */
  247. }
  248. /*--------------------------------------------------------- */
  249. /* Re-read config file on SIGHUP */
  250. static void sighup_handler(int signo)
  251. {
  252. sighup = 1;
  253. signo = signo; /* Happy compiler */
  254. }
  255. /*--------------------------------------------------------- */
  256. /* Initialize option structure by defaults */
  257. static struct options *opts_init(void)
  258. {
  259. struct options *opts = NULL;
  260. opts = malloc(sizeof(*opts));
  261. assert(opts);
  262. opts->debug = 0; /* daemonize by default */
  263. opts->pidfile = strdup(BIRQ_PIDFILE);
  264. opts->cfgfile = strdup(BIRQ_CFGFILE);
  265. opts->cfgfile_userdefined = 0;
  266. opts->pxm = NULL;
  267. opts->log_facility = LOG_DAEMON;
  268. opts->threshold = BIRQ_DEFAULT_THRESHOLD;
  269. opts->load_limit = BIRQ_DEFAULT_LOAD_LIMIT;
  270. opts->verbose = 0;
  271. opts->ht = 0;
  272. opts->long_interval = BIRQ_LONG_INTERVAL;
  273. opts->short_interval = BIRQ_SHORT_INTERVAL;
  274. opts->strategy = BIRQ_CHOOSE_RND;
  275. cpus_init(opts->exclude_cpus);
  276. cpus_clear(opts->exclude_cpus);
  277. return opts;
  278. }
  279. /*--------------------------------------------------------- */
  280. /* Free option structure */
  281. static void opts_free(struct options *opts)
  282. {
  283. if (opts->pidfile)
  284. free(opts->pidfile);
  285. if (opts->cfgfile)
  286. free(opts->cfgfile);
  287. if (opts->pxm)
  288. free(opts->pxm);
  289. cpus_free(opts->exclude_cpus);
  290. free(opts);
  291. }
  292. /* Parse 'strategy' option */
  293. static int opt_parse_strategy(const char *optarg, birq_choose_strategy_e *strategy)
  294. {
  295. assert(optarg);
  296. assert(strategy);
  297. if (!strcmp(optarg, "max"))
  298. *strategy = BIRQ_CHOOSE_MAX;
  299. else if (!strcmp(optarg, "min"))
  300. *strategy = BIRQ_CHOOSE_MIN;
  301. else if (!strcmp(optarg, "rnd"))
  302. *strategy = BIRQ_CHOOSE_RND;
  303. else {
  304. fprintf(stderr, "Error: Illegal strategy value %s.\n", optarg);
  305. return -1;
  306. }
  307. return 0;
  308. }
  309. /* Parse 'threshold' and 'load-limit' options */
  310. static int opt_parse_threshold(const char *optarg, float *threshold)
  311. {
  312. char *endptr;
  313. float thresh;
  314. assert(optarg);
  315. assert(threshold);
  316. thresh = strtof(optarg, &endptr);
  317. if (endptr == optarg) {
  318. fprintf(stderr, "Error: Illegal threshold/load-limit value %s.\n", optarg);
  319. return -1;
  320. }
  321. if (thresh > 100.00) {
  322. fprintf(stderr, "Error: The threshold/load-limit value %s > 100.\n", optarg);
  323. return -1;
  324. }
  325. *threshold = thresh;
  326. return 0;
  327. }
  328. /* Parse 'short-interval' and 'long-interval' options */
  329. static int opt_parse_interval(const char *optarg, unsigned int *interval)
  330. {
  331. char *endptr;
  332. unsigned long int val;
  333. assert(optarg);
  334. assert(interval);
  335. val = strtoul(optarg, &endptr, 10);
  336. if (endptr == optarg) {
  337. fprintf(stderr, "Error: Illegal interval value %s.\n", optarg);
  338. return -1;
  339. }
  340. *interval = val;
  341. return 0;
  342. }
  343. /*--------------------------------------------------------- */
  344. /* Parse command line options */
  345. static int opts_parse(int argc, char *argv[], struct options *opts)
  346. {
  347. static const char *shortopts = "hp:c:dO:t:l:vri:I:s:x:";
  348. #ifdef HAVE_GETOPT_H
  349. static const struct option longopts[] = {
  350. {"help", 0, NULL, 'h'},
  351. {"pid", 1, NULL, 'p'},
  352. {"conf", 1, NULL, 'c'},
  353. {"debug", 0, NULL, 'd'},
  354. {"facility", 1, NULL, 'O'},
  355. {"threshold", 1, NULL, 't'},
  356. {"load-limit", 1, NULL, 't'},
  357. {"verbose", 0, NULL, 'v'},
  358. {"ht", 0, NULL, 'r'},
  359. {"short-interval", 1, NULL, 'i'},
  360. {"long-interval", 1, NULL, 'I'},
  361. {"strategy", 1, NULL, 's'},
  362. {"pxm", 1, NULL, 'x'},
  363. {NULL, 0, NULL, 0}
  364. };
  365. #endif
  366. optind = 1;
  367. while(1) {
  368. int opt;
  369. #ifdef HAVE_GETOPT_H
  370. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  371. #else
  372. opt = getopt(argc, argv, shortopts);
  373. #endif
  374. if (-1 == opt)
  375. break;
  376. switch (opt) {
  377. case 'p':
  378. if (opts->pidfile)
  379. free(opts->pidfile);
  380. opts->pidfile = strdup(optarg);
  381. break;
  382. case 'c':
  383. if (opts->cfgfile)
  384. free(opts->cfgfile);
  385. opts->cfgfile = strdup(optarg);
  386. opts->cfgfile_userdefined = 1;
  387. break;
  388. case 'x':
  389. if (opts->pxm)
  390. free(opts->pxm);
  391. opts->pxm = strdup(optarg);
  392. break;
  393. case 'd':
  394. opts->debug = 1;
  395. break;
  396. case 'v':
  397. opts->verbose = 1;
  398. break;
  399. case 'r':
  400. opts->ht = 1;
  401. break;
  402. case 'O':
  403. if (lub_log_facility(optarg, &(opts->log_facility))) {
  404. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  405. exit(-1);
  406. }
  407. break;
  408. case 't':
  409. if (opt_parse_threshold(optarg, &opts->threshold))
  410. exit(-1);
  411. break;
  412. case 'l':
  413. if (opt_parse_threshold(optarg, &opts->load_limit))
  414. exit(-1);
  415. break;
  416. case 'i':
  417. if (opt_parse_interval(optarg, &opts->short_interval))
  418. exit(-1);
  419. break;
  420. case 'I':
  421. if (opt_parse_interval(optarg, &opts->long_interval))
  422. exit(-1);
  423. break;
  424. case 's':
  425. if (opt_parse_strategy(optarg, &opts->strategy) < 0)
  426. exit(-1);
  427. break;
  428. case 'h':
  429. help(0, argv[0]);
  430. exit(0);
  431. break;
  432. default:
  433. help(-1, argv[0]);
  434. exit(-1);
  435. break;
  436. }
  437. }
  438. return 0;
  439. }
  440. /*--------------------------------------------------------- */
  441. /* Print help message */
  442. static void help(int status, const char *argv0)
  443. {
  444. const char *name = NULL;
  445. if (!argv0)
  446. return;
  447. /* Find the basename */
  448. name = strrchr(argv0, '/');
  449. if (name)
  450. name++;
  451. else
  452. name = argv0;
  453. if (status != 0) {
  454. fprintf(stderr, "Try `%s -h' for more information.\n",
  455. name);
  456. } else {
  457. printf("Version : %s\n", VERSION);
  458. printf("Usage : %s [options]\n", name);
  459. printf("Daemon to balance IRQs.\n");
  460. printf("Options :\n");
  461. printf("\t-h, --help Print this help.\n");
  462. printf("\t-d, --debug Debug mode. Don't daemonize.\n");
  463. printf("\t-v, --verbose Be verbose.\n");
  464. printf("\t-r, --ht Enable Hyper Threading.\n");
  465. printf("\t-p <path>, --pid=<path> File to save daemon's PID to (" BIRQ_PIDFILE ").\n");
  466. printf("\t-c <path>, --conf=<path> Config file (" BIRQ_CFGFILE ").\n");
  467. printf("\t-x <path>, --pxm=<path> Proximity config file.\n");
  468. printf("\t-O, --facility Syslog facility (DAEMON).\n");
  469. printf("\t-t <float>, --threshold=<float> Threshold to consider CPU is overloaded, in percents. Default threhold is %.2f.\n",
  470. BIRQ_DEFAULT_THRESHOLD);
  471. 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",
  472. BIRQ_DEFAULT_LOAD_LIMIT);
  473. printf("\t-i <sec>, --short-interval=<sec> Short iteration interval.\n");
  474. printf("\t-I <sec>, --long-interval=<sec> Long iteration interval.\n");
  475. printf("\t-s <strategy>, --strategy=<strategy> Strategy to choose IRQ to move (min/max/rnd).\n");
  476. }
  477. }
  478. /*--------------------------------------------------------- */
  479. /* Parse config file */
  480. static int parse_config(const char *fname, struct options *opts)
  481. {
  482. lub_ini_t *ini;
  483. const char *tmp = NULL;
  484. ini = lub_ini_new();
  485. if (lub_ini_parse_file(ini, opts->cfgfile)) {
  486. lub_ini_free(ini);
  487. return -1;
  488. }
  489. if ((tmp = lub_ini_find(ini, "strategy")))
  490. if (opt_parse_strategy(tmp, &opts->strategy) < 0)
  491. goto err;
  492. if ((tmp = lub_ini_find(ini, "threshold")))
  493. if (opt_parse_threshold(tmp, &opts->threshold))
  494. goto err;
  495. if ((tmp = lub_ini_find(ini, "load-limit")))
  496. if (opt_parse_threshold(tmp, &opts->load_limit))
  497. goto err;
  498. if ((tmp = lub_ini_find(ini, "short-interval")))
  499. if (opt_parse_interval(tmp, &opts->short_interval))
  500. goto err;
  501. if ((tmp = lub_ini_find(ini, "long-interval")))
  502. if (opt_parse_interval(tmp, &opts->long_interval))
  503. goto err;
  504. if ((tmp = lub_ini_find(ini, "exclude-cpus")))
  505. if (cpumask_parse_user(tmp, strlen(tmp), opts->exclude_cpus)) {
  506. fprintf(stderr, "Error: Can't parse exclude-cpu option \"%s\".\n", tmp);
  507. goto err;
  508. }
  509. return 0;
  510. err:
  511. lub_ini_free(ini);
  512. return -1;
  513. }