birq.c 14 KB

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