birq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "lub/log.h"
  28. #include "lub/list.h"
  29. #include "irq.h"
  30. #include "cpu.h"
  31. #include "nl.h"
  32. #include "statistics.h"
  33. #include "balance.h"
  34. #define BIRQ_PIDFILE "/var/run/birq.pid"
  35. #define BIRQ_INTERVAL 3 /* in seconds */
  36. #ifndef VERSION
  37. #define VERSION 1.0.0
  38. #endif
  39. #define QUOTE(t) #t
  40. #define version(v) printf("%s\n", v)
  41. static volatile int sigterm = 0; /* Exit if 1 */
  42. static volatile int rescan = 1; /* IRQ rescan is needed */
  43. /* Signal handlers */
  44. static void sighandler(int signo);
  45. static void rescan_sighandler(int signo);
  46. static void help(int status, const char *argv0);
  47. static struct options *opts_init(void);
  48. static void opts_free(struct options *opts);
  49. static int opts_parse(int argc, char *argv[], struct options *opts);
  50. /* Command line options */
  51. struct options {
  52. char *pidfile;
  53. int debug; /* Don't daemonize in debug mode */
  54. int log_facility;
  55. };
  56. /*--------------------------------------------------------- */
  57. int main(int argc, char **argv)
  58. {
  59. int retval = -1;
  60. struct options *opts = NULL;
  61. int pidfd = -1;
  62. /* Signal vars */
  63. struct sigaction sig_act;
  64. sigset_t sig_set;
  65. /* NetLink vars */
  66. nl_fds_t *nl_fds = NULL; /* NetLink socket */
  67. /* IRQ list. It contain all found irqs. */
  68. lub_list_t *irqs;
  69. /* IRQs to balance */
  70. lub_list_t *balance_irqs;
  71. /* CPU list. It contain all found CPUs. */
  72. lub_list_t *cpus;
  73. /* Parse command line options */
  74. opts = opts_init();
  75. if (opts_parse(argc, argv, opts))
  76. goto err;
  77. /* Initialize syslog */
  78. openlog(argv[0], LOG_CONS, opts->log_facility);
  79. syslog(LOG_ERR, "Start daemon.\n");
  80. /* Init NetLink socket */
  81. if (!(nl_fds = nl_init()))
  82. goto err;
  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. if (write(pidfd, str, strlen(str)) < 0)
  100. syslog(LOG_WARNING, "Can't write to %s: %s",
  101. opts->pidfile, strerror(errno));
  102. close(pidfd);
  103. }
  104. }
  105. /* Set signal handler */
  106. sigemptyset(&sig_set);
  107. sigaddset(&sig_set, SIGTERM);
  108. sigaddset(&sig_set, SIGINT);
  109. sigaddset(&sig_set, SIGQUIT);
  110. sig_act.sa_flags = 0;
  111. sig_act.sa_mask = sig_set;
  112. sig_act.sa_handler = &sighandler;
  113. sigaction(SIGTERM, &sig_act, NULL);
  114. sigaction(SIGINT, &sig_act, NULL);
  115. sigaction(SIGQUIT, &sig_act, NULL);
  116. /* Set rescan signal handler */
  117. sigemptyset(&sig_set);
  118. sigaddset(&sig_set, SIGUSR1);
  119. sig_act.sa_flags = 0;
  120. sig_act.sa_mask = sig_set;
  121. sig_act.sa_handler = &rescan_sighandler;
  122. sigaction(SIGUSR1, &sig_act, NULL);
  123. /* Scan CPUs */
  124. cpus = lub_list_new(cpu_list_compare);
  125. if (opts->debug)
  126. fprintf(stdout, "Scanning CPUs...\n");
  127. scan_cpus(cpus);
  128. if (opts->debug)
  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. /* Main loop */
  134. while (!sigterm) {
  135. lub_list_node_t *node;
  136. int n;
  137. if (rescan) {
  138. if (opts->debug)
  139. fprintf(stdout, "Scanning hardware...\n");
  140. rescan = 0;
  141. irq_list_populate(irqs, balance_irqs);
  142. if (opts->debug)
  143. irq_list_show(irqs);
  144. }
  145. /* Timeout and poll for new devices */
  146. while ((n = nl_poll(nl_fds, BIRQ_INTERVAL)) != 0) {
  147. if (-1 == n) {
  148. fprintf(stderr,
  149. "Error: Broken NetLink socket.\n");
  150. goto end;
  151. }
  152. if (-2 == n) /* EINTR */
  153. break;
  154. if (n > 0) {
  155. rescan = 1;
  156. continue;
  157. }
  158. }
  159. if (opts->debug)
  160. printf("Some balancing...\n");
  161. parse_proc_stat(cpus, irqs);
  162. show_statistics(cpus);
  163. balance(cpus, balance_irqs);
  164. apply_affinity(balance_irqs);
  165. while ((node = lub_list__get_tail(balance_irqs))) {
  166. lub_list_del(balance_irqs, node);
  167. lub_list_node_free(node);
  168. }
  169. }
  170. end:
  171. /* Free data structures */
  172. irq_list_free(irqs);
  173. lub_list_free(balance_irqs);
  174. cpu_list_free(cpus);
  175. retval = 0;
  176. err:
  177. /* Close NetLink socket */
  178. nl_close(nl_fds);
  179. /* Remove pidfile */
  180. if (pidfd >= 0) {
  181. if (unlink(opts->pidfile) < 0) {
  182. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  183. opts->pidfile, strerror(errno));
  184. }
  185. }
  186. /* Free command line options */
  187. opts_free(opts);
  188. syslog(LOG_ERR, "Stop daemon.\n");
  189. return retval;
  190. }
  191. /*--------------------------------------------------------- */
  192. /*
  193. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  194. */
  195. static void sighandler(int signo)
  196. {
  197. sigterm = 1;
  198. }
  199. /*--------------------------------------------------------- */
  200. /*
  201. * Force IRQ rescan (SIGUSR1)
  202. */
  203. static void rescan_sighandler(int signo)
  204. {
  205. rescan = 1;
  206. }
  207. /*--------------------------------------------------------- */
  208. /* Initialize option structure by defaults */
  209. static struct options *opts_init(void)
  210. {
  211. struct options *opts = NULL;
  212. opts = malloc(sizeof(*opts));
  213. assert(opts);
  214. opts->debug = 0; /* daemonize by default */
  215. opts->pidfile = strdup(BIRQ_PIDFILE);
  216. opts->log_facility = LOG_DAEMON;
  217. return opts;
  218. }
  219. /*--------------------------------------------------------- */
  220. /* Free option structure */
  221. static void opts_free(struct options *opts)
  222. {
  223. if (opts->pidfile)
  224. free(opts->pidfile);
  225. free(opts);
  226. }
  227. /*--------------------------------------------------------- */
  228. /* Parse command line options */
  229. static int opts_parse(int argc, char *argv[], struct options *opts)
  230. {
  231. static const char *shortopts = "hvp:dO:";
  232. #ifdef HAVE_GETOPT_H
  233. static const struct option longopts[] = {
  234. {"help", 0, NULL, 'h'},
  235. {"version", 0, NULL, 'v'},
  236. {"pid", 1, NULL, 'p'},
  237. {"debug", 0, NULL, 'd'},
  238. {"facility", 1, NULL, 'O'},
  239. {NULL, 0, NULL, 0}
  240. };
  241. #endif
  242. optind = 1;
  243. while(1) {
  244. int opt;
  245. #ifdef HAVE_GETOPT_H
  246. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  247. #else
  248. opt = getopt(argc, argv, shortopts);
  249. #endif
  250. if (-1 == opt)
  251. break;
  252. switch (opt) {
  253. case 'p':
  254. if (opts->pidfile)
  255. free(opts->pidfile);
  256. opts->pidfile = strdup(optarg);
  257. break;
  258. case 'd':
  259. opts->debug = 1;
  260. break;
  261. case 'O':
  262. if (lub_log_facility(optarg, &(opts->log_facility))) {
  263. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  264. help(-1, argv[0]);
  265. exit(-1);
  266. }
  267. break;
  268. case 'h':
  269. help(0, argv[0]);
  270. exit(0);
  271. break;
  272. case 'v':
  273. version(VERSION);
  274. exit(0);
  275. break;
  276. default:
  277. help(-1, argv[0]);
  278. exit(-1);
  279. break;
  280. }
  281. }
  282. return 0;
  283. }
  284. /*--------------------------------------------------------- */
  285. /* Print help message */
  286. static void help(int status, const char *argv0)
  287. {
  288. const char *name = NULL;
  289. if (!argv0)
  290. return;
  291. /* Find the basename */
  292. name = strrchr(argv0, '/');
  293. if (name)
  294. name++;
  295. else
  296. name = argv0;
  297. if (status != 0) {
  298. fprintf(stderr, "Try `%s -h' for more information.\n",
  299. name);
  300. } else {
  301. printf("Usage: %s [options]\n", name);
  302. printf("Daemon to store user configuration (i.e. commands). "
  303. "The part of the klish project.\n");
  304. printf("Options:\n");
  305. printf("\t-v, --version\tPrint version.\n");
  306. printf("\t-h, --help\tPrint this help.\n");
  307. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  308. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  309. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  310. }
  311. }