birq.c 8.3 KB

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