birq.c 7.5 KB

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