方案一
获取已用显存,总显存,gpu 名称,使用率等等
命令获取gpu信息
public class ShellService {
@Value("${micronaut.server.port}")
private Integer myServerPort;
@Inject
private TrainModelRepository trainModelRepository;
private static HashMap<String, Process> processHashMap = new HashMap<>();
public String getGPU() throws IOException {
Process process = null;
try {
if (Platform.isWindows()) {
process = Runtime.getRuntime().exec("nvidia-smi.exe");
} else if (Platform.isLinux()) {
String[] shell = {"/bin/bash", "-c", "nvidia-smi"};
process = Runtime.getRuntime().exec(shell);
}
process.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
throw new IndaiException("显卡不存在或获取显卡信息失败");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = "";
while (null != (line = reader.readLine())) {
stringBuffer.append(line + "\n");
}
return stringBuffer.toStr