birq.c 7.6 KB

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