birq.c 9.3 KB

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