birq.c 7.7 KB

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