birq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. int verbose;
  51. int ht;
  52. unsigned int long_interval;
  53. unsigned int short_interval;
  54. birq_choose_strategy_e strategy;
  55. };
  56. /*--------------------------------------------------------- */
  57. int main(int argc, char **argv)
  58. {
  59. int retval = -1;
  60. struct options *opts = NULL;
  61. int pidfd = -1;
  62. unsigned int interval;
  63. /* Signal vars */
  64. struct sigaction sig_act;
  65. sigset_t sig_set;
  66. /* IRQ list. It contain all found IRQs. */
  67. lub_list_t *irqs;
  68. /* IRQs need to be balanced */
  69. lub_list_t *balance_irqs;
  70. /* CPU list. It contain all found CPUs. */
  71. lub_list_t *cpus;
  72. /* NUMA list. It contain all found NUMA nodes. */
  73. lub_list_t *numas;
  74. /* Proximity list. */
  75. lub_list_t *pxms;
  76. /* Parse command line options */
  77. opts = opts_init();
  78. if (opts_parse(argc, argv, opts))
  79. goto err;
  80. /* Initialize syslog */
  81. openlog(argv[0], LOG_CONS, opts->log_facility);
  82. syslog(LOG_ERR, "Start daemon.\n");
  83. /* Fork the daemon */
  84. if (!opts->debug) {
  85. /* Daemonize */
  86. if (daemon(0, 0) < 0) {
  87. syslog(LOG_ERR, "Can't daemonize\n");
  88. goto err;
  89. }
  90. /* Write pidfile */
  91. if ((pidfd = open(opts->pidfile,
  92. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  93. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  94. syslog(LOG_WARNING, "Can't open pidfile %s: %s",
  95. opts->pidfile, strerror(errno));
  96. } else {
  97. char str[20];
  98. snprintf(str, sizeof(str), "%u\n", getpid());
  99. str[sizeof(str) - 1] = '\0';
  100. if (write(pidfd, str, strlen(str)) < 0)
  101. syslog(LOG_WARNING, "Can't write to %s: %s",
  102. opts->pidfile, strerror(errno));
  103. close(pidfd);
  104. }
  105. }
  106. /* Set signal handler */
  107. sigemptyset(&sig_set);
  108. sigaddset(&sig_set, SIGTERM);
  109. sigaddset(&sig_set, SIGINT);
  110. sigaddset(&sig_set, SIGQUIT);
  111. sig_act.sa_flags = 0;
  112. sig_act.sa_mask = sig_set;
  113. sig_act.sa_handler = &sighandler;
  114. sigaction(SIGTERM, &sig_act, NULL);
  115. sigaction(SIGINT, &sig_act, NULL);
  116. sigaction(SIGQUIT, &sig_act, NULL);
  117. /* Randomize */
  118. srand(time(NULL));
  119. /* Scan NUMA nodes */
  120. numas = lub_list_new(numa_list_compare);
  121. scan_numas(numas);
  122. if (opts->verbose)
  123. show_numas(numas);
  124. /* Scan CPUs */
  125. cpus = lub_list_new(cpu_list_compare);
  126. scan_cpus(cpus, opts->ht);
  127. if (opts->verbose)
  128. show_cpus(cpus);
  129. /* Prepare data structures */
  130. irqs = lub_list_new(irq_list_compare);
  131. balance_irqs = lub_list_new(irq_list_compare);
  132. /* Parse proximity file */
  133. pxms = lub_list_new(NULL);
  134. if (opts->pxm)
  135. parse_pxm_config(opts->pxm, pxms, numas);
  136. if (opts->verbose)
  137. show_pxms(pxms);
  138. /* Main loop */
  139. while (!sigterm) {
  140. lub_list_node_t *node;
  141. char outstr[10];
  142. time_t t;
  143. struct tm *tmp;
  144. t = time(NULL);
  145. tmp = localtime(&t);
  146. if (tmp) {
  147. strftime(outstr, sizeof(outstr), "%H:%M:%S", tmp);
  148. printf("----[ %s ]----------------------------------------------------------------\n", outstr);
  149. }
  150. /* Rescan PCI devices for new IRQs. */
  151. scan_irqs(irqs, balance_irqs, pxms);
  152. if (opts->verbose)
  153. irq_list_show(irqs);
  154. /* Link IRQs to CPUs due to real current smp affinity. */
  155. link_irqs_to_cpus(cpus, irqs);
  156. /* Gather statistics on CPU load and number of interrupts. */
  157. gather_statistics(cpus, irqs);
  158. show_statistics(cpus, opts->verbose);
  159. /* Choose IRQ to move to another CPU.
  160. Don't choose IRQ if we already have new IRQs to balance */
  161. if (lub_list_len(balance_irqs) == 0) {
  162. choose_irqs_to_move(cpus, balance_irqs,
  163. opts->threshold, opts->strategy);
  164. }
  165. /* If nothing to balance */
  166. if (lub_list_len(balance_irqs) != 0) {
  167. /* Set short interval to make balancing faster. */
  168. interval = opts->short_interval;
  169. /* Choose new CPU for IRQs need to be balanced. */
  170. balance(cpus, balance_irqs, opts->threshold);
  171. /* Write new values to /proc/irq/<IRQ>/smp_affinity */
  172. apply_affinity(balance_irqs);
  173. /* Free list of balanced IRQs */
  174. while ((node = lub_list__get_tail(balance_irqs))) {
  175. lub_list_del(balance_irqs, node);
  176. lub_list_node_free(node);
  177. }
  178. } else {
  179. /* If nothing to balance */
  180. interval = opts->long_interval;
  181. }
  182. /* Wait before next iteration */
  183. sleep(interval);
  184. }
  185. /* Free data structures */
  186. irq_list_free(irqs);
  187. lub_list_free(balance_irqs);
  188. cpu_list_free(cpus);
  189. numa_list_free(numas);
  190. pxm_list_free(pxms);
  191. retval = 0;
  192. err:
  193. /* Remove pidfile */
  194. if (pidfd >= 0) {
  195. if (unlink(opts->pidfile) < 0) {
  196. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  197. opts->pidfile, strerror(errno));
  198. }
  199. }
  200. /* Free command line options */
  201. opts_free(opts);
  202. syslog(LOG_ERR, "Stop daemon.\n");
  203. return retval;
  204. }
  205. /*--------------------------------------------------------- */
  206. /*
  207. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  208. */
  209. static void sighandler(int signo)
  210. {
  211. sigterm = 1;
  212. signo = signo; /* Happy compiler */
  213. }
  214. /*--------------------------------------------------------- */
  215. /* Initialize option structure by defaults */
  216. static struct options *opts_init(void)
  217. {
  218. struct options *opts = NULL;
  219. opts = malloc(sizeof(*opts));
  220. assert(opts);
  221. opts->debug = 0; /* daemonize by default */
  222. opts->pidfile = strdup(BIRQ_PIDFILE);
  223. opts->pxm = NULL;
  224. opts->log_facility = LOG_DAEMON;
  225. opts->threshold = BIRQ_DEFAULT_THRESHOLD;
  226. opts->verbose = 0;
  227. opts->ht = 0;
  228. opts->long_interval = BIRQ_LONG_INTERVAL;
  229. opts->short_interval = BIRQ_SHORT_INTERVAL;
  230. opts->strategy = BIRQ_CHOOSE_RND;
  231. return opts;
  232. }
  233. /*--------------------------------------------------------- */
  234. /* Free option structure */
  235. static void opts_free(struct options *opts)
  236. {
  237. if (opts->pidfile)
  238. free(opts->pidfile);
  239. if (opts->pxm)
  240. free(opts->pxm);
  241. free(opts);
  242. }
  243. /*--------------------------------------------------------- */
  244. /* Parse command line options */
  245. static int opts_parse(int argc, char *argv[], struct options *opts)
  246. {
  247. static const char *shortopts = "hp:dO:t:vri:I:s:x:";
  248. #ifdef HAVE_GETOPT_H
  249. static const struct option longopts[] = {
  250. {"help", 0, NULL, 'h'},
  251. {"pid", 1, NULL, 'p'},
  252. {"debug", 0, NULL, 'd'},
  253. {"facility", 1, NULL, 'O'},
  254. {"threshold", 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 'i':
  317. {
  318. char *endptr;
  319. unsigned long int val;
  320. val = strtoul(optarg, &endptr, 10);
  321. if (endptr != optarg)
  322. opts->short_interval = val;
  323. }
  324. break;
  325. case 'I':
  326. {
  327. char *endptr;
  328. unsigned long int val;
  329. val = strtoul(optarg, &endptr, 10);
  330. if (endptr != optarg)
  331. opts->long_interval = val;
  332. }
  333. break;
  334. case 's':
  335. if (!strcmp(optarg, "max"))
  336. opts->strategy = BIRQ_CHOOSE_MAX;
  337. else if (!strcmp(optarg, "min"))
  338. opts->strategy = BIRQ_CHOOSE_MIN;
  339. else if (!strcmp(optarg, "rnd"))
  340. opts->strategy = BIRQ_CHOOSE_RND;
  341. else {
  342. fprintf(stderr, "Error: Illegal strategy value %s.\n", optarg);
  343. help(-1, argv[0]);
  344. exit(-1);
  345. }
  346. break;
  347. case 'h':
  348. help(0, argv[0]);
  349. exit(0);
  350. break;
  351. default:
  352. help(-1, argv[0]);
  353. exit(-1);
  354. break;
  355. }
  356. }
  357. return 0;
  358. }
  359. /*--------------------------------------------------------- */
  360. /* Print help message */
  361. static void help(int status, const char *argv0)
  362. {
  363. const char *name = NULL;
  364. if (!argv0)
  365. return;
  366. /* Find the basename */
  367. name = strrchr(argv0, '/');
  368. if (name)
  369. name++;
  370. else
  371. name = argv0;
  372. if (status != 0) {
  373. fprintf(stderr, "Try `%s -h' for more information.\n",
  374. name);
  375. } else {
  376. printf("Version : %s\n", VERSION);
  377. printf("Usage : %s [options]\n", name);
  378. printf("Daemon to balance IRQs.\n");
  379. printf("Options :\n");
  380. printf("\t-h, --help\tPrint this help.\n");
  381. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  382. printf("\t-v, --verbose\tBe verbose.\n");
  383. printf("\t-r, --ht\tEnable hyper-threading. Not recommended.\n");
  384. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  385. printf("\t-x <path>, --pxm=<path>\tProximity config file.\n");
  386. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  387. printf("\t-t <float>, --threshold=<float>\tThreshold to consider CPU is overloaded, in percents.\n");
  388. printf("\t-i <sec>, --short-interval=<sec>\tShort iteration interval.\n");
  389. printf("\t-I <sec>, --long-interval=<sec>\tLong iteration interval.\n");
  390. printf("\t-s <strategy>, --strategy=<strategy>\tStrategy to choose IRQ to move (min/max/rnd).\n");
  391. }
  392. }