|
1 | 1 | package setup
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
5 | 6 | "strings"
|
6 | 7 |
|
@@ -83,3 +84,126 @@ func extractJSON(outputStr string) (string, error) {
|
83 | 84 | }
|
84 | 85 | return "", fmt.Errorf("failed to extract JSON from output: %s", outputStr)
|
85 | 86 | }
|
| 87 | + |
| 88 | +// DeviceInfo represents information about a device |
| 89 | +type DeviceInfo struct { |
| 90 | + Type string `json:"type"` |
| 91 | + BDF string `json:"bdf"` |
| 92 | + Vendor string `json:"vendor"` |
| 93 | + Device string `json:"device"` |
| 94 | + NUMA string `json:"numa"` |
| 95 | + Driver string `json:"driver"` |
| 96 | + DeviceName string `json:"device_name"` |
| 97 | + BlockDevices string `json:"block_devices"` |
| 98 | +} |
| 99 | + |
| 100 | +// HugepagesInfo represents information about Hugepages |
| 101 | +type HugepagesInfo struct { |
| 102 | + Node string `json:"node"` |
| 103 | + Hugesize string `json:"hugesize"` |
| 104 | + Free int `json:"free"` |
| 105 | + Total int `json:"total"` |
| 106 | +} |
| 107 | + |
| 108 | +// StatusInfo represents the complete status information |
| 109 | +type StatusInfo struct { |
| 110 | + Hugepages []HugepagesInfo `json:"hugepages"` |
| 111 | + DeviceInfos []DeviceInfo `json:"device_infos"` |
| 112 | +} |
| 113 | + |
| 114 | +func GetStatus(executor *commonNs.Executor) (string, error) { |
| 115 | + cmdArgs := []string{ |
| 116 | + spdkSetupPath, |
| 117 | + "status", |
| 118 | + } |
| 119 | + |
| 120 | + outputStr, err := executor.Execute(nil, "bash", cmdArgs, types.ExecuteTimeout) |
| 121 | + if err != nil { |
| 122 | + return "", err |
| 123 | + } |
| 124 | + |
| 125 | + status := parseStatus(outputStr) |
| 126 | + jsonString, err := json.Marshal(status) |
| 127 | + if err != nil { |
| 128 | + return "", err |
| 129 | + } |
| 130 | + |
| 131 | + return string(jsonString), nil |
| 132 | +} |
| 133 | + |
| 134 | +func parseStatus(output string) StatusInfo { |
| 135 | + var status StatusInfo |
| 136 | + lines := strings.Split(output, "\n") |
| 137 | + |
| 138 | + hugepagesIndex := -1 |
| 139 | + deviceInfoIndex := -1 |
| 140 | + |
| 141 | + for i, line := range lines { |
| 142 | + if line == "Hugepages" { |
| 143 | + hugepagesIndex = i |
| 144 | + } else if line == "Type BDF Vendor Device NUMA Driver Device Block devices" { |
| 145 | + deviceInfoIndex = i |
| 146 | + break |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if hugepagesIndex != -1 { |
| 151 | + status.Hugepages = parseHugepages(lines[hugepagesIndex+2:]) |
| 152 | + } |
| 153 | + |
| 154 | + if deviceInfoIndex != -1 { |
| 155 | + status.DeviceInfos = parseDeviceInfos(lines[deviceInfoIndex+1:]) |
| 156 | + } |
| 157 | + |
| 158 | + return status |
| 159 | +} |
| 160 | + |
| 161 | +func parseHugepages(lines []string) []HugepagesInfo { |
| 162 | + var hugepages []HugepagesInfo |
| 163 | + |
| 164 | + for _, line := range lines { |
| 165 | + fields := strings.Fields(line) |
| 166 | + if len(fields) != 5 { |
| 167 | + continue |
| 168 | + } |
| 169 | + |
| 170 | + hugepages = append(hugepages, HugepagesInfo{ |
| 171 | + Node: fields[0], |
| 172 | + Hugesize: fields[1], |
| 173 | + Free: parseInteger(fields[3]), |
| 174 | + Total: parseInteger(fields[5]), |
| 175 | + }) |
| 176 | + } |
| 177 | + |
| 178 | + return hugepages |
| 179 | +} |
| 180 | + |
| 181 | +func parseDeviceInfos(lines []string) []DeviceInfo { |
| 182 | + var deviceInfos []DeviceInfo |
| 183 | + |
| 184 | + for _, line := range lines { |
| 185 | + fields := strings.Fields(line) |
| 186 | + if len(fields) != 8 { |
| 187 | + continue |
| 188 | + } |
| 189 | + |
| 190 | + deviceInfos = append(deviceInfos, DeviceInfo{ |
| 191 | + Type: fields[0], |
| 192 | + BDF: fields[1], |
| 193 | + Vendor: fields[2], |
| 194 | + Device: fields[3], |
| 195 | + NUMA: fields[4], |
| 196 | + Driver: fields[5], |
| 197 | + DeviceName: fields[6], |
| 198 | + BlockDevices: fields[7], |
| 199 | + }) |
| 200 | + } |
| 201 | + |
| 202 | + return deviceInfos |
| 203 | +} |
| 204 | + |
| 205 | +func parseInteger(s string) int { |
| 206 | + var num int |
| 207 | + fmt.Sscanf(s, "%d", &num) |
| 208 | + return num |
| 209 | +} |
0 commit comments