birq.c 8.0 KB

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