|
| 1 | +// +build solaris |
| 2 | + |
1 | 3 | package mem
|
2 | 4 |
|
3 | 5 | import (
|
@@ -119,3 +121,85 @@ func nonGlobalZoneMemoryCapacity() (uint64, error) {
|
119 | 121 |
|
120 | 122 | return memSizeBytes, nil
|
121 | 123 | }
|
| 124 | + |
| 125 | +const swapsCommand = "swap" |
| 126 | + |
| 127 | +// The blockSize as reported by `swap -l`. See https://docs.oracle.com/cd/E23824_01/html/821-1459/fsswap-52195.html |
| 128 | +const blockSize = 512 |
| 129 | + |
| 130 | +// swapctl column indexes |
| 131 | +const ( |
| 132 | + nameCol = 0 |
| 133 | + // devCol = 1 |
| 134 | + // swaploCol = 2 |
| 135 | + totalBlocksCol = 3 |
| 136 | + freeBlocksCol = 4 |
| 137 | +) |
| 138 | + |
| 139 | +func SwapDevices() ([]*SwapDevice, error) { |
| 140 | + return SwapDevicesWithContext(context.Background()) |
| 141 | +} |
| 142 | + |
| 143 | +func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) { |
| 144 | + swapsCommandPath, err := exec.LookPath(swapsCommand) |
| 145 | + if err != nil { |
| 146 | + return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err) |
| 147 | + } |
| 148 | + output, err := invoke.CommandWithContext(swapsCommandPath, "-l") |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("could not execute %q: %w", swapsCommand, err) |
| 151 | + } |
| 152 | + |
| 153 | + return parseSwapsCommandOutput(string(output)) |
| 154 | +} |
| 155 | + |
| 156 | +func parseSwapsCommandOutput(output string) ([]*SwapDevice, error) { |
| 157 | + lines := strings.Split(output, "\n") |
| 158 | + if len(lines) == 0 { |
| 159 | + return nil, fmt.Errorf("could not parse output of %q: no lines in %q", swapsCommand, output) |
| 160 | + } |
| 161 | + |
| 162 | + // Check header headerFields are as expected. |
| 163 | + headerFields := strings.Fields(lines[0]) |
| 164 | + if len(headerFields) < freeBlocksCol { |
| 165 | + return nil, fmt.Errorf("couldn't parse %q: too few fields in header %q", swapsCommand, lines[0]) |
| 166 | + } |
| 167 | + if headerFields[nameCol] != "swapfile" { |
| 168 | + return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsCommand, headerFields[nameCol], "swapfile") |
| 169 | + } |
| 170 | + if headerFields[totalBlocksCol] != "blocks" { |
| 171 | + return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsCommand, headerFields[totalBlocksCol], "blocks") |
| 172 | + } |
| 173 | + if headerFields[freeBlocksCol] != "free" { |
| 174 | + return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsCommand, headerFields[freeBlocksCol], "free") |
| 175 | + } |
| 176 | + |
| 177 | + var swapDevices []*SwapDevice |
| 178 | + for _, line := range lines[1:] { |
| 179 | + if line == "" { |
| 180 | + continue // the terminal line is typically empty |
| 181 | + } |
| 182 | + fields := strings.Fields(line) |
| 183 | + if len(fields) < freeBlocksCol { |
| 184 | + return nil, fmt.Errorf("couldn't parse %q: too few fields", swapsCommand) |
| 185 | + } |
| 186 | + |
| 187 | + totalBlocks, err := strconv.ParseUint(fields[totalBlocksCol], 10, 64) |
| 188 | + if err != nil { |
| 189 | + return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapsCommand, err) |
| 190 | + } |
| 191 | + |
| 192 | + freeBlocks, err := strconv.ParseUint(fields[freeBlocksCol], 10, 64) |
| 193 | + if err != nil { |
| 194 | + return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapsCommand, err) |
| 195 | + } |
| 196 | + |
| 197 | + swapDevices = append(swapDevices, &SwapDevice{ |
| 198 | + Name: fields[nameCol], |
| 199 | + UsedBytes: (totalBlocks - freeBlocks) * blockSize, |
| 200 | + FreeBytes: freeBlocks * blockSize, |
| 201 | + }) |
| 202 | + } |
| 203 | + |
| 204 | + return swapDevices, nil |
| 205 | +} |
0 commit comments