birq.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #ifdef HAVE_GETOPT_H
  21. #include <getopt.h>
  22. #endif
  23. #include <sys/poll.h>
  24. #include <sys/socket.h>
  25. #include <linux/types.h>
  26. #include <linux/netlink.h>
  27. #include "birq.h"
  28. #include "lub/log.h"
  29. #include "lub/list.h"
  30. #include "irq.h"
  31. #include "cpu.h"
  32. #include "nl.h"
  33. #include "statistics.h"
  34. #include "balance.h"
  35. #ifndef VERSION
  36. #define VERSION 1.0.0
  37. #endif
  38. #define QUOTE(t) #t
  39. #define version(v) printf("%s\n", v)
  40. static volatile int sigterm = 0; /* Exit if 1 */
  41. static volatile int rescan = 1; /* IRQ rescan is needed */
  42. /* Signal handlers */
  43. static void sighandler(int signo);
  44. static void rescan_sighandler(int signo);
  45. static void help(int status, const char *argv0);
  46. static struct options *opts_init(void);
  47. static void opts_free(struct options *opts);
  48. static int opts_parse(int argc, char *argv[], struct options *opts);
  49. /* Command line options */
  50. struct options {
  51. char *pidfile;
  52. int debug; /* Don't daemonize in debug mode */
  53. int log_facility;
  54. float threshold;
  55. };
  56. /*--------------------------------------------------------- */
  57. int main(int argc, char **argv)
  58. {
  59. int retval = -1;
  60. struct options *opts = NULL;
  61. int pidfd = -1;
  62. int interval = BIRQ_SHORT_INTERVAL;
  63. /* Signal vars */
  64. struct sigaction sig_act;
  65. sigset_t sig_set;
  66. /* NetLink vars */
  67. nl_fds_t *nl_fds = NULL; /* NetLink socket */
  68. /* IRQ list. It contain all found IRQs. */
  69. lub_list_t *irqs;
  70. /* IRQs need to be balanced */
  71. lub_list_t *balance_irqs;
  72. /* CPU list. It contain all found CPUs. */
  73. lub_list_t *cpus;
  74. /* Parse command line options */
  75. opts = opts_init();
  76. if (opts_parse(argc, argv, opts))
  77. goto err;
  78. /* Initialize syslog */
  79. openlog(argv[0], LOG_CONS, opts->log_facility);
  80. syslog(LOG_ERR, "Start daemon.\n");
  81. /* Init NetLink socket */
  82. if (!(nl_fds = nl_init()))
  83. goto err;
  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. 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. /* Set rescan signal handler */
  118. sigemptyset(&sig_set);
  119. sigaddset(&sig_set, SIGUSR1);
  120. sig_act.sa_flags = 0;
  121. sig_act.sa_mask = sig_set;
  122. sig_act.sa_handler = &rescan_sighandler;
  123. sigaction(SIGUSR1, &sig_act, NULL);
  124. /* Scan CPUs */
  125. cpus = lub_list_new(cpu_list_compare);
  126. scan_cpus(cpus);
  127. /* Prepare data structures */
  128. irqs = lub_list_new(irq_list_compare);
  129. balance_irqs = lub_list_new(irq_list_compare);
  130. /* Main loop */
  131. while (!sigterm) {
  132. lub_list_node_t *node;
  133. int n;
  134. if (rescan) {
  135. if (opts->debug)
  136. fprintf(stdout, "Scanning hardware...\n");
  137. rescan = 0;
  138. scan_irqs(irqs, balance_irqs);
  139. if (opts->debug)
  140. irq_list_show(irqs);
  141. }
  142. /* Timeout and poll for new devices */
  143. while ((n = nl_poll(nl_fds, interval)) != 0) {
  144. if (-1 == n) {
  145. fprintf(stderr,
  146. "Error: Broken NetLink socket.\n");
  147. goto end;
  148. }
  149. if (-2 == n) /* EINTR */
  150. break;
  151. if (n > 0) {
  152. rescan = 1;
  153. continue;
  154. }
  155. }
  156. /* Gather statistics on CPU load and number of interrupts. */
  157. gather_statistics(cpus, irqs);
  158. show_statistics(cpus);
  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);
  164. }
  165. /* If nothing to balance */
  166. if (lub_list_len(balance_irqs) == 0) {
  167. interval = BIRQ_LONG_INTERVAL;
  168. continue;
  169. }
  170. /* Set short interval to make balancing faster. */
  171. interval = BIRQ_SHORT_INTERVAL;
  172. /* Choose new CPU for IRQs need to be balanced. */
  173. balance(cpus, balance_irqs, opts->threshold);
  174. /* Write new values to /proc/irq/<IRQ>/smp_affinity */
  175. apply_affinity(balance_irqs);
  176. /* Free list of balanced IRQs */
  177. while ((node = lub_list__get_tail(balance_irqs))) {
  178. lub_list_del(balance_irqs, node);
  179. lub_list_node_free(node);
  180. }
  181. }
  182. end:
  183. /* Free data structures */
  184. irq_list_free(irqs);
  185. lub_list_free(balance_irqs);
  186. cpu_list_free(cpus);
  187. retval = 0;
  188. err:
  189. /* Close NetLink socket */
  190. nl_close(nl_fds);
  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. }
  211. /*--------------------------------------------------------- */
  212. /*
  213. * Force IRQ rescan (SIGUSR1)
  214. */
  215. static void rescan_sighandler(int signo)
  216. {
  217. rescan = 1;
  218. }
  219. /*--------------------------------------------------------- */
  220. /* Initialize option structure by defaults */
  221. static struct options *opts_init(void)
  222. {
  223. struct options *opts = NULL;
  224. opts = malloc(sizeof(*opts));
  225. assert(opts);
  226. opts->debug = 0; /* daemonize by default */
  227. opts->pidfile = strdup(BIRQ_PIDFILE);
  228. opts->log_facility = LOG_DAEMON;
  229. opts->threshold = BIRQ_DEFAULT_THRESHOLD;
  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. free(opts);
  239. }
  240. /*--------------------------------------------------------- */
  241. /* Parse command line options */
  242. static int opts_parse(int argc, char *argv[], struct options *opts)
  243. {
  244. static const char *shortopts = "hvp:dO:t:";
  245. #ifdef HAVE_GETOPT_H
  246. static const struct option longopts[] = {
  247. {"help", 0, NULL, 'h'},
  248. {"version", 0, NULL, 'v'},
  249. {"pid", 1, NULL, 'p'},
  250. {"debug", 0, NULL, 'd'},
  251. {"facility", 1, NULL, 'O'},
  252. {"threshold", 1, NULL, 't'},
  253. {NULL, 0, NULL, 0}
  254. };
  255. #endif
  256. optind = 1;
  257. while(1) {
  258. int opt;
  259. #ifdef HAVE_GETOPT_H
  260. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  261. #else
  262. opt = getopt(argc, argv, shortopts);
  263. #endif
  264. if (-1 == opt)
  265. break;
  266. switch (opt) {
  267. case 'p':
  268. if (opts->pidfile)
  269. free(opts->pidfile);
  270. opts->pidfile = strdup(optarg);
  271. break;
  272. case 'd':
  273. opts->debug = 1;
  274. break;
  275. case 'O':
  276. if (lub_log_facility(optarg, &(opts->log_facility))) {
  277. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  278. help(-1, argv[0]);
  279. exit(-1);
  280. }
  281. break;
  282. case 't':
  283. {
  284. char *endptr;
  285. float thresh;
  286. thresh = strtof(optarg, &endptr);
  287. if (endptr == optarg)
  288. thresh = opts->threshold;
  289. opts->threshold = thresh;
  290. if (thresh > 100.00) {
  291. fprintf(stderr, "Error: Illegal threshold value %s.\n", optarg);
  292. help(-1, argv[0]);
  293. exit(-1);
  294. }
  295. }
  296. break;
  297. case 'h':
  298. help(0, argv[0]);
  299. exit(0);
  300. break;
  301. case 'v':
  302. version(VERSION);
  303. exit(0);
  304. break;
  305. default:
  306. help(-1, argv[0]);
  307. exit(-1);
  308. break;
  309. }
  310. }
  311. return 0;
  312. }
  313. /*--------------------------------------------------------- */
  314. /* Print help message */
  315. static void help(int status, const char *argv0)
  316. {
  317. const char *name = NULL;
  318. if (!argv0)
  319. return;
  320. /* Find the basename */
  321. name = strrchr(argv0, '/');
  322. if (name)
  323. name++;
  324. else
  325. name = argv0;
  326. if (status != 0) {
  327. fprintf(stderr, "Try `%s -h' for more information.\n",
  328. name);
  329. } else {
  330. printf("Usage: %s [options]\n", name);
  331. printf("Daemon to store user configuration (i.e. commands). "
  332. "The part of the klish project.\n");
  333. printf("Options:\n");
  334. printf("\t-v, --version\tPrint version.\n");
  335. printf("\t-h, --help\tPrint this help.\n");
  336. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  337. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  338. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  339. printf("\t-t <float>, --threshold=<float>\tThreshold to consider CPU is overloaded, in percents.\n");
  340. }
  341. }