birq.c 11 KB

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