|
| 1 | +package cpuscaling |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// CPU directory path |
| 12 | +const cpuPath = "/sys/devices/system/cpu/" |
| 13 | + |
| 14 | +type CPUSysFsStateScaler struct { |
| 15 | +} |
| 16 | + |
| 17 | +func (c *CPUSysFsStateScaler) EnsureOnlineCPUs(X int) error { |
| 18 | + cpus, err := getAllCPUs() |
| 19 | + if err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + |
| 23 | + onlineCount, err := c.GetActiveCPUsCount() |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + if onlineCount < uint32(X) { |
| 29 | + for _, cpu := range cpus { |
| 30 | + if cpu == 0 { |
| 31 | + // Skip CPU 0 as it is always online and can't be toggled |
| 32 | + continue |
| 33 | + } |
| 34 | + |
| 35 | + online, err := isCPUOnline(cpu) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + if !online { |
| 41 | + // Mark CPU as online |
| 42 | + err := setCPUOnline(cpu, true) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + onlineCount++ |
| 47 | + } |
| 48 | + |
| 49 | + // Stop when we reach the target count |
| 50 | + if onlineCount == uint32(X) { |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + } else if onlineCount > uint32(X) { |
| 55 | + // Remove CPUs if there are more than X online |
| 56 | + for i := len(cpus) - 1; i >= 0; i-- { |
| 57 | + cpu := cpus[i] |
| 58 | + if cpu == 0 { |
| 59 | + // Skip CPU 0 as it cannot be taken offline |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + online, err := isCPUOnline(cpu) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + if online { |
| 69 | + // Mark CPU as offline |
| 70 | + err := setCPUOnline(cpu, false) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + onlineCount-- |
| 75 | + } |
| 76 | + |
| 77 | + // Stop when we reach the target count |
| 78 | + if onlineCount == uint32(X) { |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if onlineCount != uint32(X) { |
| 85 | + return fmt.Errorf("failed to ensure %d CPUs are online, current online CPUs: %d", X, onlineCount) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// GetActiveCPUsCount() returns the count of online CPUs. |
| 92 | +func (c *CPUSysFsStateScaler) GetActiveCPUsCount() (uint32, error) { |
| 93 | + cpus, err := getAllCPUs() |
| 94 | + if err != nil { |
| 95 | + return 0, err |
| 96 | + } |
| 97 | + |
| 98 | + var onlineCount uint32 |
| 99 | + for _, cpu := range cpus { |
| 100 | + online, err := isCPUOnline(cpu) |
| 101 | + if err != nil { |
| 102 | + return 0, err |
| 103 | + } |
| 104 | + if online { |
| 105 | + onlineCount++ |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return onlineCount, nil |
| 110 | +} |
| 111 | + |
| 112 | +// GetTotalCPUsCount returns the total number of CPUs (online + offline). |
| 113 | +func (c *CPUSysFsStateScaler) GetTotalCPUsCount() (uint32, error) { |
| 114 | + cpus, err := getAllCPUs() |
| 115 | + if err != nil { |
| 116 | + return 0, err |
| 117 | + } |
| 118 | + |
| 119 | + return uint32(len(cpus)), nil |
| 120 | +} |
| 121 | + |
| 122 | +// Helper functions |
| 123 | + |
| 124 | +// getAllCPUs returns a list of all CPUs that are physically present in the system. |
| 125 | +func getAllCPUs() ([]int, error) { |
| 126 | + data, err := os.ReadFile(filepath.Join(cpuPath, "possible")) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + return parseCPURange(string(data)) |
| 132 | +} |
| 133 | + |
| 134 | +// parseCPURange parses the CPU range string (e.g., "0-3") and returns a list of CPUs. |
| 135 | +func parseCPURange(cpuRange string) ([]int, error) { |
| 136 | + var cpus []int |
| 137 | + cpuRange = strings.TrimSpace(cpuRange) |
| 138 | + parts := strings.Split(cpuRange, "-") |
| 139 | + |
| 140 | + if len(parts) == 1 { |
| 141 | + // Single CPU case, e.g., "0" |
| 142 | + cpu, err := strconv.Atoi(parts[0]) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + cpus = append(cpus, cpu) |
| 147 | + } else if len(parts) == 2 { |
| 148 | + // Range case, e.g., "0-3" |
| 149 | + start, err := strconv.Atoi(parts[0]) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + end, err := strconv.Atoi(parts[1]) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + for i := start; i <= end; i++ { |
| 158 | + cpus = append(cpus, i) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return cpus, nil |
| 163 | +} |
| 164 | + |
| 165 | +// isCPUOnline checks if a given CPU is online. |
| 166 | +func isCPUOnline(cpu int) (bool, error) { |
| 167 | + data, err := os.ReadFile(filepath.Join(cpuPath, fmt.Sprintf("cpu%d/online", cpu))) |
| 168 | + if os.IsNotExist(err) { |
| 169 | + // If the file doesn't exist for CPU 0, assume it's online |
| 170 | + if cpu == 0 { |
| 171 | + return true, nil |
| 172 | + } |
| 173 | + return false, err |
| 174 | + } |
| 175 | + if err != nil { |
| 176 | + return false, err |
| 177 | + } |
| 178 | + |
| 179 | + online := strings.TrimSpace(string(data)) |
| 180 | + return online == "1", nil |
| 181 | +} |
| 182 | + |
| 183 | +// setCPUOnline sets the given CPU as online (true) or offline (false). |
| 184 | +func setCPUOnline(cpu int, online bool) error { |
| 185 | + state := "0" |
| 186 | + if online { |
| 187 | + state = "1" |
| 188 | + } |
| 189 | + |
| 190 | + err := os.WriteFile(filepath.Join(cpuPath, fmt.Sprintf("cpu%d/online", cpu)), []byte(state), 0644) |
| 191 | + if err != nil { |
| 192 | + return fmt.Errorf("failed to set CPU %d online status: %w", cpu, err) |
| 193 | + } |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
0 commit comments