1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Intel Speed Select -- Allow speed select to daemonize 4 * Copyright (c) 2022 Intel Corporation. 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <stdarg.h> 10 #include <string.h> 11 #include <unistd.h> 12 #include <fcntl.h> 13 #include <sys/file.h> 14 #include <sys/types.h> 15 #include <sys/stat.h> 16 #include <errno.h> 17 #include <getopt.h> 18 #include <signal.h> 19 #include <time.h> 20 21 #include "isst.h" 22 23 static int per_package_levels_info[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE]; 24 static time_t per_package_levels_tm[MAX_PACKAGE_COUNT][MAX_DIE_PER_PACKAGE][MAX_PUNIT_PER_DIE]; 25 26 static void init_levels(void) 27 { 28 int i, j, k; 29 30 for (i = 0; i < MAX_PACKAGE_COUNT; ++i) 31 for (j = 0; j < MAX_DIE_PER_PACKAGE; ++j) 32 for (k = 0; k < MAX_PUNIT_PER_DIE; ++k) 33 per_package_levels_info[i][j][k] = -1; 34 } 35 36 void process_level_change(struct isst_id *id) 37 { 38 struct isst_pkg_ctdp_level_info ctdp_level; 39 struct isst_pkg_ctdp pkg_dev; 40 time_t tm; 41 int ret; 42 43 if (id->pkg < 0 || id->die < 0 || id->punit < 0) { 44 debug_printf("Invalid package/die info for cpu:%d\n", id->cpu); 45 return; 46 } 47 48 tm = time(NULL); 49 if (tm - per_package_levels_tm[id->pkg][id->die][id->punit] < 2) 50 return; 51 52 per_package_levels_tm[id->pkg][id->die][id->punit] = tm; 53 54 ret = isst_get_ctdp_levels(id, &pkg_dev); 55 if (ret) { 56 debug_printf("Can't get tdp levels for cpu:%d\n", id->cpu); 57 return; 58 } 59 60 debug_printf("Get Config level %d pkg:%d die:%d current_level:%d\n", id->cpu, 61 id->pkg, id->die, pkg_dev.current_level); 62 63 if (pkg_dev.locked) { 64 debug_printf("config TDP s locked \n"); 65 return; 66 } 67 68 if (per_package_levels_info[id->pkg][id->die][id->punit] == pkg_dev.current_level) 69 return; 70 71 debug_printf("**Config level change for cpu:%d pkg:%d die:%d from %d to %d\n", 72 id->cpu, id->pkg, id->die, per_package_levels_info[id->pkg][id->die][id->punit], 73 pkg_dev.current_level); 74 75 per_package_levels_info[id->pkg][id->die][id->punit] = pkg_dev.current_level; 76 77 ctdp_level.core_cpumask_size = 78 alloc_cpu_set(&ctdp_level.core_cpumask); 79 ret = isst_get_coremask_info(id, pkg_dev.current_level, &ctdp_level); 80 if (ret) { 81 free_cpu_set(ctdp_level.core_cpumask); 82 debug_printf("Can't get core_mask:%d\n", id->cpu); 83 return; 84 } 85 86 if (use_cgroupv2()) { 87 int ret; 88 89 ret = enable_cpuset_controller(); 90 if (ret) 91 goto use_offline; 92 93 isolate_cpus(id, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask, 94 pkg_dev.current_level, 0); 95 96 goto free_mask; 97 } 98 99 use_offline: 100 if (ctdp_level.cpu_count) { 101 int i, max_cpus = get_topo_max_cpus(); 102 for (i = 0; i < max_cpus; ++i) { 103 if (!is_cpu_in_power_domain(i, id)) 104 continue; 105 if (CPU_ISSET_S(i, ctdp_level.core_cpumask_size, ctdp_level.core_cpumask)) { 106 fprintf(stderr, "online cpu %d\n", i); 107 set_cpu_online_offline(i, 1); 108 } else { 109 fprintf(stderr, "offline cpu %d\n", i); 110 set_cpu_online_offline(i, 0); 111 } 112 } 113 } 114 free_mask: 115 free_cpu_set(ctdp_level.core_cpumask); 116 } 117 118 static void _poll_for_config_change(struct isst_id *id, void *arg1, void *arg2, 119 void *arg3, void *arg4) 120 { 121 process_level_change(id); 122 } 123 124 static void poll_for_config_change(void) 125 { 126 for_each_online_power_domain_in_set(_poll_for_config_change, NULL, NULL, 127 NULL, NULL); 128 } 129 130 static int done = 0; 131 static int pid_file_handle; 132 133 static void signal_handler(int sig) 134 { 135 switch (sig) { 136 case SIGINT: 137 case SIGTERM: 138 done = 1; 139 hfi_exit(); 140 exit(0); 141 break; 142 default: 143 break; 144 } 145 } 146 147 static void daemonize(char *rundir, char *pidfile) 148 { 149 int pid, sid, i; 150 char str[10]; 151 struct stat st; 152 struct sigaction sig_actions; 153 sigset_t sig_set; 154 int ret; 155 156 if (getppid() == 1) 157 return; 158 159 sigemptyset(&sig_set); 160 sigaddset(&sig_set, SIGCHLD); 161 sigaddset(&sig_set, SIGTSTP); 162 sigaddset(&sig_set, SIGTTOU); 163 sigaddset(&sig_set, SIGTTIN); 164 sigprocmask(SIG_BLOCK, &sig_set, NULL); 165 166 sig_actions.sa_handler = signal_handler; 167 sigemptyset(&sig_actions.sa_mask); 168 sig_actions.sa_flags = 0; 169 170 sigaction(SIGHUP, &sig_actions, NULL); 171 sigaction(SIGTERM, &sig_actions, NULL); 172 sigaction(SIGINT, &sig_actions, NULL); 173 174 pid = fork(); 175 if (pid < 0) { 176 /* Could not fork */ 177 exit(EXIT_FAILURE); 178 } 179 if (pid > 0) 180 exit(EXIT_SUCCESS); 181 182 umask(027); 183 184 sid = setsid(); 185 if (sid < 0) 186 exit(EXIT_FAILURE); 187 188 /* close all descriptors */ 189 for (i = getdtablesize(); i >= 0; --i) 190 close(i); 191 192 i = open("/dev/null", O_RDWR); 193 if (i < 0) 194 exit(EXIT_FAILURE); 195 196 ret = dup(i); 197 if (ret == -1) 198 exit(EXIT_FAILURE); 199 200 ret = chdir(rundir); 201 if (ret == -1) 202 exit(EXIT_FAILURE); 203 204 pid_file_handle = open(pidfile, O_RDWR | O_CREAT | O_NOFOLLOW, 0600); 205 if (pid_file_handle == -1) { 206 /* Couldn't open lock file */ 207 exit(1); 208 } 209 210 if (fstat(pid_file_handle, &st) == -1) 211 exit(1); 212 213 if (!S_ISREG(st.st_mode)) 214 exit(1); 215 /* Try to lock file */ 216 #ifdef LOCKF_SUPPORT 217 if (lockf(pid_file_handle, F_TLOCK, 0) == -1) { 218 #else 219 if (flock(pid_file_handle, LOCK_EX|LOCK_NB) < 0) { 220 #endif 221 /* Couldn't get lock on lock file */ 222 fprintf(stderr, "Couldn't get lock file %d\n", getpid()); 223 exit(1); 224 } 225 snprintf(str, sizeof(str), "%d\n", getpid()); 226 ret = write(pid_file_handle, str, strlen(str)); 227 if (ret == -1) 228 exit(EXIT_FAILURE); 229 230 close(i); 231 } 232 233 int isst_daemon(int debug_mode, int poll_interval, int no_daemon) 234 { 235 int ret; 236 237 if (!no_daemon && poll_interval < 0 && !debug_mode) { 238 fprintf(stderr, "OOB mode is enabled and will run as daemon\n"); 239 daemonize((char *) "/tmp/", 240 (char *)"/tmp/hfi-events.pid"); 241 } else { 242 signal(SIGINT, signal_handler); 243 } 244 245 init_levels(); 246 247 if (poll_interval < 0) { 248 ret = hfi_main(); 249 if (ret) { 250 fprintf(stderr, "HFI initialization failed\n"); 251 } 252 fprintf(stderr, "Must specify poll-interval\n"); 253 return ret; 254 } 255 256 debug_printf("Starting loop\n"); 257 while (!done) { 258 sleep(poll_interval); 259 poll_for_config_change(); 260 } 261 262 return 0; 263 } 264