birq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /* Gather statistics on CPU load and number of interrupts. */
  155. gather_statistics(cpus, irqs);
  156. show_statistics(cpus, opts->verbose);
  157. /* Choose IRQ to move to another CPU.
  158. Don't choose IRQ if we already have new IRQs to balance */
  159. if (lub_list_len(balance_irqs) == 0) {
  160. choose_irqs_to_move(cpus, balance_irqs,
  161. opts->threshold, opts->strategy);
  162. }
  163. /* If nothing to balance */
  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->threshold);
  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->verbose = 0;
  225. opts->ht = 0;
  226. opts->long_interval = BIRQ_LONG_INTERVAL;
  227. opts->short_interval = BIRQ_SHORT_INTERVAL;
  228. opts->strategy = BIRQ_CHOOSE_RND;
  229. return opts;
  230. }
  231. /*--------------------------------------------------------- */
  232. /* Free option structure */
  233. static void opts_free(struct options *opts)
  234. {
  235. if (opts->pidfile)
  236. free(opts->pidfile);
  237. if (opts->pxm)
  238. free(opts->pxm);
  239. free(opts);
  240. }
  241. /*--------------------------------------------------------- */
  242. /* Parse command line options */
  243. static int opts_parse(int argc, char *argv[], struct options *opts)
  244. {
  245. static const char *shortopts = "hp:dO:t:vri:I:s:x:";
  246. #ifdef HAVE_GETOPT_H
  247. static const struct option longopts[] = {
  248. {"help", 0, NULL, 'h'},
  249. {"pid", 1, NULL, 'p'},
  250. {"debug", 0, NULL, 'd'},
  251. {"facility", 1, NULL, 'O'},
  252. {"threshold", 1, NULL, 't'},
  253. {"verbose", 0, NULL, 'v'},
  254. {"ht", 0, NULL, 'r'},
  255. {"short-interval", 1, NULL, 'i'},
  256. {"long-interval", 1, NULL, 'i'},
  257. {"strategy", 1, NULL, 's'},
  258. {"pxm", 1, NULL, 'x'},
  259. {NULL, 0, NULL, 0}
  260. };
  261. #endif
  262. optind = 1;
  263. while(1) {
  264. int opt;
  265. #ifdef HAVE_GETOPT_H
  266. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  267. #else
  268. opt = getopt(argc, argv, shortopts);
  269. #endif
  270. if (-1 == opt)
  271. break;
  272. switch (opt) {
  273. case 'p':
  274. if (opts->pidfile)
  275. free(opts->pidfile);
  276. opts->pidfile = strdup(optarg);
  277. break;
  278. case 'x':
  279. if (opts->pxm)
  280. free(opts->pxm);
  281. opts->pxm = strdup(optarg);
  282. break;
  283. case 'd':
  284. opts->debug = 1;
  285. break;
  286. case 'v':
  287. opts->verbose = 1;
  288. break;
  289. case 'r':
  290. opts->ht = 1;
  291. break;
  292. case 'O':
  293. if (lub_log_facility(optarg, &(opts->log_facility))) {
  294. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  295. help(-1, argv[0]);
  296. exit(-1);
  297. }
  298. break;
  299. case 't':
  300. {
  301. char *endptr;
  302. float thresh;
  303. thresh = strtof(optarg, &endptr);
  304. if (endptr == optarg)
  305. thresh = opts->threshold;
  306. opts->threshold = thresh;
  307. if (thresh > 100.00) {
  308. fprintf(stderr, "Error: Illegal threshold value %s.\n", optarg);
  309. help(-1, argv[0]);
  310. exit(-1);
  311. }
  312. }
  313. break;
  314. case 'i':
  315. {
  316. char *endptr;
  317. unsigned long int val;
  318. val = strtoul(optarg, &endptr, 10);
  319. if (endptr != optarg)
  320. opts->short_interval = val;
  321. }
  322. break;
  323. case 'I':
  324. {
  325. char *endptr;
  326. unsigned long int val;
  327. val = strtoul(optarg, &endptr, 10);
  328. if (endptr != optarg)
  329. opts->long_interval = val;
  330. }
  331. break;
  332. case 'c':
  333. if (!strcmp(optarg, "max"))
  334. opts->strategy = BIRQ_CHOOSE_MAX;
  335. else if (!strcmp(optarg, "min"))
  336. opts->strategy = BIRQ_CHOOSE_MIN;
  337. else if (!strcmp(optarg, "rnd"))
  338. opts->strategy = BIRQ_CHOOSE_RND;
  339. else {
  340. fprintf(stderr, "Error: Illegal strategy value %s.\n", optarg);
  341. help(-1, argv[0]);
  342. exit(-1);
  343. }
  344. break;
  345. case 'h':
  346. help(0, argv[0]);
  347. exit(0);
  348. break;
  349. default:
  350. help(-1, argv[0]);
  351. exit(-1);
  352. break;
  353. }
  354. }
  355. return 0;
  356. }
  357. /*--------------------------------------------------------- */
  358. /* Print help message */
  359. static void help(int status, const char *argv0)
  360. {
  361. const char *name = NULL;
  362. if (!argv0)
  363. return;
  364. /* Find the basename */
  365. name = strrchr(argv0, '/');
  366. if (name)
  367. name++;
  368. else
  369. name = argv0;
  370. if (status != 0) {
  371. fprintf(stderr, "Try `%s -h' for more information.\n",
  372. name);
  373. } else {
  374. printf("Version : %s\n", VERSION);
  375. printf("Usage : %s [options]\n", name);
  376. printf("Daemon to balance IRQs.\n");
  377. printf("Options :\n");
  378. printf("\t-h, --help\tPrint this help.\n");
  379. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  380. printf("\t-v, --verbose\tBe verbose.\n");
  381. printf("\t-r, --ht\tEnable hyper-threading. Not recommended.\n");
  382. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  383. printf("\t-x <path>, --pxm=<path>\tProximity config file.\n");
  384. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  385. printf("\t-t <float>, --threshold=<float>\tThreshold to consider CPU is overloaded, in percents.\n");
  386. printf("\t-i <sec>, --short-interval=<sec>\tShort iteration interval.\n");
  387. printf("\t-I <sec>, --long-interval=<sec>\tLong iteration interval.\n");
  388. printf("\t-c <strategy>, --choose=<strategy>\tStrategy to choose IRQ to move (min/max/rnd).\n");
  389. }
  390. }