You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

421 lines
11 KiB

  1. Revert upstream commit d1993bcde2a524346a9508754671f096ec129ad1
  2. to avoid glib dependency. That commit polluted the main code
  3. with glib types.
  4. --- a/irqbalance.h
  5. +++ b/irqbalance.h
  6. @@ -8,7 +8,6 @@
  7. #include <stdint.h>
  8. #include <glib.h>
  9. -#include <glib-unix.h>
  10. #include <syslog.h>
  11. #include <limits.h>
  12. @@ -63,7 +62,6 @@ extern GList *packages;
  13. extern GList *cache_domains;
  14. extern GList *cpus;
  15. extern int numa_avail;
  16. -extern GList *cl_banned_irqs;
  17. extern int debug_mode;
  18. extern int journal_logging;
  19. @@ -171,7 +169,5 @@ extern unsigned int log_mask;
  20. }while(0)
  21. #endif /* HAVE_LIBSYSTEMD */
  22. -#define SOCKET_PATH "irqbalance"
  23. -
  24. #endif /* __INCLUDE_GUARD_IRQBALANCE_H_ */
  25. --- a/irqbalance.c
  26. +++ b/irqbalance.c
  27. @@ -31,8 +31,6 @@
  28. #include <time.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. -#include <sys/socket.h>
  32. -#include <sys/un.h>
  33. #include <fcntl.h>
  34. #ifdef HAVE_GETOPT_LONG
  35. #include <getopt.h>
  36. @@ -44,7 +42,6 @@
  37. #include "irqbalance.h"
  38. volatile int keep_going = 1;
  39. -int socket_fd;
  40. int one_shot_mode;
  41. int debug_mode;
  42. int foreground_mode;
  43. @@ -61,9 +58,6 @@ char *banscript = NULL;
  44. char *polscript = NULL;
  45. long HZ;
  46. int sleep_interval = SLEEP_INTERVAL;
  47. -GMainLoop *main_loop;
  48. -
  49. -char *banned_cpumask_from_ui = NULL;
  50. static void sleep_approx(int seconds)
  51. {
  52. @@ -236,224 +230,22 @@ static void force_rebalance_irq(struct i
  53. info->assigned_obj = NULL;
  54. }
  55. -gboolean handler(gpointer data __attribute__((unused)))
  56. +static void handler(int signum)
  57. {
  58. + (void)signum;
  59. keep_going = 0;
  60. - g_main_loop_quit(main_loop);
  61. - return TRUE;
  62. }
  63. -gboolean force_rescan(gpointer data __attribute__((unused)))
  64. +static void force_rescan(int signum)
  65. {
  66. + (void)signum;
  67. if (cycle_count)
  68. need_rescan = 1;
  69. - return TRUE;
  70. -}
  71. -
  72. -gboolean scan(gpointer data)
  73. -{
  74. - log(TO_CONSOLE, LOG_INFO, "\n\n\n-----------------------------------------------------------------------------\n");
  75. - clear_work_stats();
  76. - parse_proc_interrupts();
  77. - parse_proc_stat();
  78. -
  79. -
  80. - /* cope with cpu hotplug -- detected during /proc/interrupts parsing */
  81. - if (need_rescan) {
  82. - need_rescan = 0;
  83. - cycle_count = 0;
  84. - log(TO_CONSOLE, LOG_INFO, "Rescanning cpu topology \n");
  85. - clear_work_stats();
  86. -
  87. - free_object_tree();
  88. - build_object_tree();
  89. - for_each_irq(NULL, force_rebalance_irq, NULL);
  90. - parse_proc_interrupts();
  91. - parse_proc_stat();
  92. - sleep_approx(sleep_interval);
  93. - clear_work_stats();
  94. - parse_proc_interrupts();
  95. - parse_proc_stat();
  96. - }
  97. -
  98. - if (cycle_count)
  99. - update_migration_status();
  100. -
  101. - calculate_placement();
  102. - activate_mappings();
  103. -
  104. - if (debug_mode)
  105. - dump_tree();
  106. - if (one_shot_mode)
  107. - keep_going = 0;
  108. - cycle_count++;
  109. -
  110. - if (data != &sleep_interval) {
  111. - data = &sleep_interval;
  112. - g_timeout_add_seconds(sleep_interval, scan, data);
  113. - return FALSE;
  114. - }
  115. -
  116. - if (keep_going)
  117. - return TRUE;
  118. - else
  119. - return FALSE;
  120. -}
  121. -
  122. -void get_irq_data(struct irq_info *irq, void *data)
  123. -{
  124. - sprintf(data + strlen(data),
  125. - "IRQ %d LOAD %lu DIFF %lu CLASS %d ", irq->irq, irq->load,
  126. - (irq->irq_count - irq->last_irq_count), irq->class);
  127. -}
  128. -
  129. -void get_object_stat(struct topo_obj *object, void *data)
  130. -{
  131. - char irq_data[1024] = "\0";
  132. -
  133. - if (g_list_length(object->interrupts) > 0) {
  134. - for_each_irq(object->interrupts, get_irq_data, irq_data);
  135. - }
  136. - sprintf(data + strlen(data), "TYPE %d NUMBER %d LOAD %lu SAVE_MODE %d %s",
  137. - object->obj_type, object->number, object->load,
  138. - object->powersave_mode, irq_data);
  139. - if (object->obj_type != OBJ_TYPE_CPU) {
  140. - for_each_object(object->children, get_object_stat, data);
  141. - }
  142. -}
  143. -
  144. -gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attribute__((unused)))
  145. -{
  146. - char buff[500];
  147. - int sock;
  148. - int recv_size = 0;
  149. - int valid_user = 0;
  150. -
  151. - struct iovec iov = { buff, 500 };
  152. - struct msghdr msg = { NULL, 0, &iov, 1, NULL, 0, 0 };
  153. - msg.msg_control = malloc(CMSG_SPACE(sizeof(struct ucred)));
  154. - msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
  155. -
  156. - struct cmsghdr *cmsg;
  157. -
  158. - if (condition == G_IO_IN) {
  159. - sock = accept(fd, NULL, NULL);
  160. - if (sock < 0) {
  161. - log(TO_ALL, LOG_WARNING, "Connection couldn't be accepted.\n");
  162. - return TRUE;
  163. - }
  164. - if ((recv_size = recvmsg(sock, &msg, 0)) < 0) {
  165. - log(TO_ALL, LOG_WARNING, "Error while receiving data.\n");
  166. - return TRUE;
  167. - }
  168. - cmsg = CMSG_FIRSTHDR(&msg);
  169. - if ((cmsg->cmsg_level == SOL_SOCKET) &&
  170. - (cmsg->cmsg_type == SCM_CREDENTIALS)) {
  171. - struct ucred *credentials = (struct ucred *) CMSG_DATA(cmsg);
  172. - if (!credentials->uid) {
  173. - valid_user = 1;
  174. - }
  175. - }
  176. - if (!valid_user) {
  177. - log(TO_ALL, LOG_INFO, "Permission denied for user to connect to socket.\n");
  178. - return TRUE;
  179. - }
  180. -
  181. - if (!strncmp(buff, "stats", strlen("stats"))) {
  182. - char stats[2048] = "\0";
  183. - for_each_object(numa_nodes, get_object_stat, stats);
  184. - send(sock, stats, strlen(stats), 0);
  185. - }
  186. - if (!strncmp(buff, "settings ", strlen("settings "))) {
  187. - if (!(strncmp(buff + strlen("settings "), "sleep ",
  188. - strlen("sleep ")))) {
  189. - char *sleep_string = malloc(
  190. - sizeof(char) * (recv_size - strlen("settings sleep ")));
  191. - strncpy(sleep_string, buff + strlen("settings sleep "),
  192. - recv_size - strlen("settings sleep "));
  193. - int new_iterval = strtoul(sleep_string, NULL, 10);
  194. - if (new_iterval >= 1) {
  195. - sleep_interval = new_iterval;
  196. - }
  197. - } else if (!(strncmp(buff + strlen("settings "), "ban irqs ",
  198. - strlen("ban irqs ")))) {
  199. - char *end;
  200. - char *irq_string = malloc(
  201. - sizeof(char) * (recv_size - strlen("settings ban irqs ")));
  202. - strncpy(irq_string, buff + strlen("settings ban irqs "),
  203. - recv_size - strlen("settings ban irqs "));
  204. - g_list_free_full(cl_banned_irqs, free);
  205. - cl_banned_irqs = NULL;
  206. - need_rescan = 1;
  207. - if (!strncmp(irq_string, "NONE", strlen("NONE"))) {
  208. - return TRUE;
  209. - }
  210. - int irq = strtoul(irq_string, &end, 10);
  211. - do {
  212. - add_cl_banned_irq(irq);
  213. - } while((irq = strtoul(end, &end, 10)));
  214. - } else if (!(strncmp(buff + strlen("settings "), "cpus ",
  215. - strlen("cpus")))) {
  216. - char *cpu_ban_string = malloc(
  217. - sizeof(char) * (recv_size - strlen("settings cpus ")));
  218. - strncpy(cpu_ban_string, buff + strlen("settings cpus "),
  219. - recv_size - strlen("settings cpus "));
  220. - banned_cpumask_from_ui = strtok(cpu_ban_string, " ");
  221. - if (!strncmp(banned_cpumask_from_ui, "NULL", strlen("NULL"))) {
  222. - banned_cpumask_from_ui = NULL;
  223. - }
  224. - need_rescan = 1;
  225. - }
  226. - }
  227. - if (!strncmp(buff, "setup", strlen("setup"))) {
  228. - char setup[2048] = "\0";
  229. - snprintf(setup, 2048, "SLEEP %d ", sleep_interval);
  230. - if(g_list_length(cl_banned_irqs) > 0) {
  231. - for_each_irq(cl_banned_irqs, get_irq_data, setup);
  232. - }
  233. - char banned[512];
  234. - cpumask_scnprintf(banned, 512, banned_cpus);
  235. - snprintf(setup + strlen(setup), 2048 - strlen(setup),
  236. - "BANNED %s", banned);
  237. - send(sock, setup, strlen(setup), 0);
  238. - }
  239. -
  240. - close(sock);
  241. - }
  242. - return TRUE;
  243. -}
  244. -
  245. -int init_socket(char *socket_name)
  246. -{
  247. - struct sockaddr_un addr;
  248. - memset(&addr, 0, sizeof(struct sockaddr_un));
  249. -
  250. - socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  251. - if (socket_fd < 0) {
  252. - log(TO_ALL, LOG_WARNING, "Socket couldn't be created.\n");
  253. - return 1;
  254. - }
  255. -
  256. - addr.sun_family = AF_UNIX;
  257. - addr.sun_path[0] = '\0';
  258. - strncpy(addr.sun_path + 1, socket_name, strlen(socket_name));
  259. - if (bind(socket_fd, (struct sockaddr *)&addr,
  260. - sizeof(sa_family_t) + strlen(socket_name) + 1) < 0) {
  261. - log(TO_ALL, LOG_WARNING, "Daemon couldn't be bound to the socket.\n");
  262. - return 1;
  263. - }
  264. - int optval = 1;
  265. - if (setsockopt(socket_fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) < 0) {
  266. - log(TO_ALL, LOG_WARNING, "Unable to set socket options.\n");
  267. - return 1;
  268. - }
  269. - listen(socket_fd, 1);
  270. - g_unix_fd_add(socket_fd, G_IO_IN, sock_handle, NULL);
  271. - return 0;
  272. }
  273. int main(int argc, char** argv)
  274. {
  275. + struct sigaction action, hupaction;
  276. sigset_t sigset, old_sigset;
  277. sigemptyset(&sigset);
  278. @@ -553,11 +345,19 @@ int main(int argc, char** argv)
  279. }
  280. }
  281. - g_unix_signal_add(SIGINT, handler, NULL);
  282. - g_unix_signal_add(SIGTERM, handler, NULL);
  283. - g_unix_signal_add(SIGUSR1, handler, NULL);
  284. - g_unix_signal_add(SIGUSR2, handler, NULL);
  285. - g_unix_signal_add(SIGHUP, force_rescan, NULL);
  286. + action.sa_handler = handler;
  287. + sigemptyset(&action.sa_mask);
  288. + action.sa_flags = 0;
  289. + sigaction(SIGINT, &action, NULL);
  290. + sigaction(SIGTERM, &action, NULL);
  291. + sigaction(SIGUSR1, &action, NULL);
  292. + sigaction(SIGUSR2, &action, NULL);
  293. +
  294. + hupaction.sa_handler = force_rescan;
  295. + sigemptyset(&hupaction.sa_mask);
  296. + hupaction.sa_flags = 0;
  297. + sigaction(SIGHUP, &hupaction, NULL);
  298. +
  299. sigprocmask(SIG_SETMASK, &old_sigset, NULL);
  300. #ifdef HAVE_LIBCAP_NG
  301. @@ -566,32 +366,58 @@ int main(int argc, char** argv)
  302. capng_lock();
  303. capng_apply(CAPNG_SELECT_BOTH);
  304. #endif
  305. +
  306. for_each_irq(NULL, force_rebalance_irq, NULL);
  307. parse_proc_interrupts();
  308. parse_proc_stat();
  309. - char socket_name[64];
  310. - snprintf(socket_name, 64, "%s%d.sock", SOCKET_PATH, getpid());
  311. + while (keep_going) {
  312. + sleep_approx(sleep_interval);
  313. + log(TO_CONSOLE, LOG_INFO, "\n\n\n-----------------------------------------------------------------------------\n");
  314. - if (init_socket(socket_name)) {
  315. - return EXIT_FAILURE;
  316. - }
  317. - main_loop = g_main_loop_new(NULL, FALSE);
  318. - int *last_interval = &sleep_interval;
  319. - g_timeout_add_seconds(sleep_interval, scan, last_interval);
  320. - g_main_loop_run(main_loop);
  321. - g_main_loop_quit(main_loop);
  322. + clear_work_stats();
  323. + parse_proc_interrupts();
  324. + parse_proc_stat();
  325. +
  326. + /* cope with cpu hotplug -- detected during /proc/interrupts parsing */
  327. + if (need_rescan) {
  328. + need_rescan = 0;
  329. + cycle_count = 0;
  330. + log(TO_CONSOLE, LOG_INFO, "Rescanning cpu topology \n");
  331. + clear_work_stats();
  332. +
  333. + free_object_tree();
  334. + build_object_tree();
  335. + for_each_irq(NULL, force_rebalance_irq, NULL);
  336. + parse_proc_interrupts();
  337. + parse_proc_stat();
  338. + sleep_approx(sleep_interval);
  339. + clear_work_stats();
  340. + parse_proc_interrupts();
  341. + parse_proc_stat();
  342. + }
  343. +
  344. + if (cycle_count)
  345. + update_migration_status();
  346. +
  347. + calculate_placement();
  348. + activate_mappings();
  349. + if (debug_mode)
  350. + dump_tree();
  351. + if (one_shot_mode)
  352. + keep_going = 0;
  353. + cycle_count++;
  354. +
  355. + }
  356. free_object_tree();
  357. free_cl_opts();
  358. /* Remove pidfile */
  359. if (!foreground_mode && pidfile)
  360. unlink(pidfile);
  361. - /* Remove socket */
  362. - close(socket_fd);
  363. return EXIT_SUCCESS;
  364. }
  365. --- a/cputree.c
  366. +++ b/cputree.c
  367. @@ -38,7 +38,6 @@
  368. #include "irqbalance.h"
  369. -extern char *banned_cpumask_from_ui;
  370. GList *cpus;
  371. GList *cache_domains;
  372. @@ -77,15 +76,11 @@ static void setup_banned_cpus(void)
  373. cpus_clear(nohz_full);
  374. /* A manually specified cpumask overrides auto-detection. */
  375. - if (banned_cpumask_from_ui != NULL) {
  376. - cpulist_parse(banned_cpumask_from_ui,
  377. - strlen(banned_cpumask_from_ui), banned_cpus);
  378. - goto out;
  379. - }
  380. if (getenv("IRQBALANCE_BANNED_CPUS")) {
  381. cpumask_parse_user(getenv("IRQBALANCE_BANNED_CPUS"), strlen(getenv("IRQBALANCE_BANNED_CPUS")), banned_cpus);
  382. goto out;
  383. }
  384. +
  385. file = fopen("/sys/devices/system/cpu/isolated", "r");
  386. if (file) {
  387. if (getline(&line, &size, file) > 0) {
  388. @@ -117,8 +112,6 @@ out:
  389. log(TO_CONSOLE, LOG_INFO, "Isolated CPUs: %s\n", buffer);
  390. cpumask_scnprintf(buffer, 4096, nohz_full);
  391. log(TO_CONSOLE, LOG_INFO, "Adaptive-ticks CPUs: %s\n", buffer);
  392. - cpumask_scnprintf(buffer, 4096, banned_cpus);
  393. - log(TO_CONSOLE, LOG_INFO, "Banned CPUs: %s\n", buffer);
  394. }
  395. static struct topo_obj* add_cache_domain_to_package(struct topo_obj *cache,