birq.c 8.1 KB

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