birq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /* Signal handlers */
  35. static volatile int sigterm = 0; /* Exit if 1 */
  36. static void sighandler(int signo);
  37. static void help(int status, const char *argv0);
  38. static struct options *opts_init(void);
  39. static void opts_free(struct options *opts);
  40. static int opts_parse(int argc, char *argv[], struct options *opts);
  41. /* Command line options */
  42. struct options {
  43. char *pidfile;
  44. int debug; /* Don't daemonize in debug mode */
  45. int log_facility;
  46. float threshold;
  47. int verbose;
  48. int ht;
  49. unsigned int long_interval;
  50. unsigned int short_interval;
  51. };
  52. /*--------------------------------------------------------- */
  53. int main(int argc, char **argv)
  54. {
  55. int retval = -1;
  56. struct options *opts = NULL;
  57. int pidfd = -1;
  58. unsigned int 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. /* Set period */
  115. interval = opts->short_interval;
  116. /* Main loop */
  117. while (!sigterm) {
  118. lub_list_node_t *node;
  119. char outstr[10];
  120. time_t t;
  121. struct tm *tmp;
  122. t = time(NULL);
  123. tmp = localtime(&t);
  124. strftime(outstr, sizeof(outstr), "%H:%M:%S", tmp);
  125. printf("----[ %s ]----------------------------------------------------------------\n", outstr);
  126. /* Rescan PCI devices for new IRQs. */
  127. scan_irqs(irqs, balance_irqs);
  128. if (opts->verbose)
  129. irq_list_show(irqs);
  130. /* Gather statistics on CPU load and number of interrupts. */
  131. gather_statistics(cpus, irqs);
  132. show_statistics(cpus, opts->verbose);
  133. /* Choose IRQ to move to another CPU.
  134. Don't choose IRQ if we already have new IRQs to balance */
  135. if (lub_list_len(balance_irqs) == 0) {
  136. choose_irqs_to_move(cpus, balance_irqs,
  137. opts->threshold);
  138. }
  139. /* If nothing to balance */
  140. if (lub_list_len(balance_irqs) != 0) {
  141. /* Set short interval to make balancing faster. */
  142. interval = opts->short_interval;
  143. /* Choose new CPU for IRQs need to be balanced. */
  144. balance(cpus, balance_irqs, opts->threshold);
  145. /* Write new values to /proc/irq/<IRQ>/smp_affinity */
  146. apply_affinity(balance_irqs);
  147. /* Free list of balanced IRQs */
  148. while ((node = lub_list__get_tail(balance_irqs))) {
  149. lub_list_del(balance_irqs, node);
  150. lub_list_node_free(node);
  151. }
  152. } else {
  153. /* If nothing to balance */
  154. interval = opts->long_interval;
  155. }
  156. /* Wait before nex iteration */
  157. sleep(interval);
  158. }
  159. /* Free data structures */
  160. irq_list_free(irqs);
  161. lub_list_free(balance_irqs);
  162. cpu_list_free(cpus);
  163. retval = 0;
  164. err:
  165. /* Remove pidfile */
  166. if (pidfd >= 0) {
  167. if (unlink(opts->pidfile) < 0) {
  168. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  169. opts->pidfile, strerror(errno));
  170. }
  171. }
  172. /* Free command line options */
  173. opts_free(opts);
  174. syslog(LOG_ERR, "Stop daemon.\n");
  175. return retval;
  176. }
  177. /*--------------------------------------------------------- */
  178. /*
  179. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  180. */
  181. static void sighandler(int signo)
  182. {
  183. sigterm = 1;
  184. }
  185. /*--------------------------------------------------------- */
  186. /* Initialize option structure by defaults */
  187. static struct options *opts_init(void)
  188. {
  189. struct options *opts = NULL;
  190. opts = malloc(sizeof(*opts));
  191. assert(opts);
  192. opts->debug = 0; /* daemonize by default */
  193. opts->pidfile = strdup(BIRQ_PIDFILE);
  194. opts->log_facility = LOG_DAEMON;
  195. opts->threshold = BIRQ_DEFAULT_THRESHOLD;
  196. opts->verbose = 0;
  197. opts->ht = 0;
  198. opts->long_interval = BIRQ_LONG_INTERVAL;
  199. opts->short_interval = BIRQ_SHORT_INTERVAL;
  200. return opts;
  201. }
  202. /*--------------------------------------------------------- */
  203. /* Free option structure */
  204. static void opts_free(struct options *opts)
  205. {
  206. if (opts->pidfile)
  207. free(opts->pidfile);
  208. free(opts);
  209. }
  210. /*--------------------------------------------------------- */
  211. /* Parse command line options */
  212. static int opts_parse(int argc, char *argv[], struct options *opts)
  213. {
  214. static const char *shortopts = "hp:dO:t:vri:I:";
  215. #ifdef HAVE_GETOPT_H
  216. static const struct option longopts[] = {
  217. {"help", 0, NULL, 'h'},
  218. {"pid", 1, NULL, 'p'},
  219. {"debug", 0, NULL, 'd'},
  220. {"facility", 1, NULL, 'O'},
  221. {"threshold", 1, NULL, 't'},
  222. {"verbose", 0, NULL, 'v'},
  223. {"ht", 0, NULL, 'r'},
  224. {"short-interval", 1, NULL, 'i'},
  225. {"long-interval", 1, NULL, 'i'},
  226. {NULL, 0, NULL, 0}
  227. };
  228. #endif
  229. optind = 1;
  230. while(1) {
  231. int opt;
  232. #ifdef HAVE_GETOPT_H
  233. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  234. #else
  235. opt = getopt(argc, argv, shortopts);
  236. #endif
  237. if (-1 == opt)
  238. break;
  239. switch (opt) {
  240. case 'p':
  241. if (opts->pidfile)
  242. free(opts->pidfile);
  243. opts->pidfile = strdup(optarg);
  244. break;
  245. case 'd':
  246. opts->debug = 1;
  247. break;
  248. case 'v':
  249. opts->verbose = 1;
  250. break;
  251. case 'r':
  252. opts->ht = 1;
  253. break;
  254. case 'O':
  255. if (lub_log_facility(optarg, &(opts->log_facility))) {
  256. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  257. help(-1, argv[0]);
  258. exit(-1);
  259. }
  260. break;
  261. case 't':
  262. {
  263. char *endptr;
  264. float thresh;
  265. thresh = strtof(optarg, &endptr);
  266. if (endptr == optarg)
  267. thresh = opts->threshold;
  268. opts->threshold = thresh;
  269. if (thresh > 100.00) {
  270. fprintf(stderr, "Error: Illegal threshold value %s.\n", optarg);
  271. help(-1, argv[0]);
  272. exit(-1);
  273. }
  274. }
  275. break;
  276. case 'i':
  277. {
  278. char *endptr;
  279. unsigned long int val;
  280. val = strtoul(optarg, &endptr, 10);
  281. if (endptr != optarg)
  282. opts->short_interval = val;
  283. }
  284. break;
  285. case 'I':
  286. {
  287. char *endptr;
  288. unsigned long int val;
  289. val = strtoul(optarg, &endptr, 10);
  290. if (endptr != optarg)
  291. opts->long_interval = val;
  292. }
  293. break;
  294. case 'h':
  295. help(0, argv[0]);
  296. exit(0);
  297. break;
  298. default:
  299. help(-1, argv[0]);
  300. exit(-1);
  301. break;
  302. }
  303. }
  304. return 0;
  305. }
  306. /*--------------------------------------------------------- */
  307. /* Print help message */
  308. static void help(int status, const char *argv0)
  309. {
  310. const char *name = NULL;
  311. if (!argv0)
  312. return;
  313. /* Find the basename */
  314. name = strrchr(argv0, '/');
  315. if (name)
  316. name++;
  317. else
  318. name = argv0;
  319. if (status != 0) {
  320. fprintf(stderr, "Try `%s -h' for more information.\n",
  321. name);
  322. } else {
  323. printf("Version : %s\n", VERSION);
  324. printf("Usage : %s [options]\n", name);
  325. printf("Daemon to store user configuration (i.e. commands). "
  326. "The part of the klish project.\n");
  327. printf("Options :\n");
  328. printf("\t-h, --help\tPrint this help.\n");
  329. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  330. printf("\t-v, --verbose\tBe verbose.\n");
  331. printf("\t-r, --ht\tEnable hyper-threading. Not recommended.\n");
  332. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  333. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  334. printf("\t-t <float>, --threshold=<float>\tThreshold to consider CPU is overloaded, in percents.\n");
  335. printf("\t-i <sec>, --short-interval=<sec>\tShort iteration interval.\n");
  336. printf("\t-I <sec>, --long-interval=<sec>\tLong iteration interval.\n");
  337. }
  338. }