Browse Source

增加一区GPU服务器参数读取module:<hostparam>

chenminghua 1 year ago
parent
commit
08dac4ca5c

+ 95 - 0
hostparam/pom.xml

@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>sis-background</artifactId>
+        <groupId>com.gyee</groupId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hostparam</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
+        <dependency>
+            <groupId>net.java.dev.jna</groupId>
+            <artifactId>jna</artifactId>
+            <version>5.11.0</version>
+        </dependency>
+
+        <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
+        <dependency>
+            <groupId>net.java.dev.jna</groupId>
+            <artifactId>jna-platform</artifactId>
+            <version>5.11.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>net.java.dev.jna</groupId>
+            <artifactId>jna</artifactId>
+            <version>5.11.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.17</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.0.M4</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.github.votoanthuan</groupId>
+            <artifactId>sigar</artifactId>
+            <version>1.6.4</version>
+        </dependency>
+
+        <!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core -->
+        <dependency>
+            <groupId>com.github.oshi</groupId>
+            <artifactId>oshi-core</artifactId>
+            <version>6.1.6</version>
+        </dependency>
+    </dependencies>
+
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <includeSystemScope>true</includeSystemScope>
+                    <excludes>
+                        <exclude>
+                            <groupId>org.projectlombok</groupId>
+                            <artifactId>lombok</artifactId>
+                        </exclude>
+                    </excludes>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
hostparam/src/main/java/com/gyee/host/HostparamApplication.java

@@ -0,0 +1,13 @@
+package com.gyee.host;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class HostparamApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(HostparamApplication.class, args);
+    }
+
+}

+ 115 - 0
hostparam/src/main/java/com/gyee/host/comm/AjaxResult.java

@@ -0,0 +1,115 @@
+package com.gyee.host.comm;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import java.util.HashMap;
+
+/**
+* @ClassName: AjaxResult
+* @Description: TODO(ajax操作消息提醒)
+* @author gyee
+* @date 2018年8月18日
+*
+ */
+public class AjaxResult extends HashMap<String, Object>
+{
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 初始化一个新创建的 Message 对象
+     */
+
+    public AjaxResult()
+    {
+    }
+
+    /**
+     * 返回错误消息
+     *
+     * @return 错误消息
+     */
+    public static AjaxResult error()
+    {
+        return error(1, "操作失败");
+    }
+
+    /**
+     * 返回错误消息
+     *
+     * @param msg 内容
+     * @return 错误消息
+     */
+    public static AjaxResult error(String msg)
+    {
+        return error(500, msg);
+    }
+
+    /**
+     * 返回错误消息
+     *
+     * @param code 错误码
+     * @param msg 内容
+     * @return 错误消息
+     */
+    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
+    public static AjaxResult error(int code, String msg)
+    {
+        AjaxResult json = new AjaxResult();
+        json.put("code", code);
+        json.put("msg", msg);
+        return json;
+    }
+
+    /**
+     * 返回成功消息
+     *
+     * @param msg 内容
+     * @return 成功消息
+     */
+    public static AjaxResult success(String msg)
+    {
+        AjaxResult json = new AjaxResult();
+        json.put("msg", msg);
+        json.put("code", 200);
+        return json;
+    }
+
+    /**
+     * 返回成功消息
+     *
+     * @return 成功消息
+     */
+    public static AjaxResult success()
+    {
+        return AjaxResult.success("操作成功");
+    }
+
+
+    public static AjaxResult successData(Object value){
+        AjaxResult json = new AjaxResult();
+        json.put("code", 200);
+        json.put("data", value);
+        return json;
+    }
+
+    public static AjaxResult successData(int code, Object value){
+        AjaxResult json = new AjaxResult();
+        json.put("code", code);
+        json.put("data", value);
+        return json;
+    }
+
+
+    /**
+     * 返回成功消息
+     *
+     * @param key 键值
+     * @param value 内容
+     * @return 成功消息
+     */
+    @Override
+    public AjaxResult put(String key, Object value)
+    {
+        super.put(key, value);
+        return this;
+    }
+}

+ 44 - 0
hostparam/src/main/java/com/gyee/host/controller/HostparamController.java

@@ -0,0 +1,44 @@
+package com.gyee.host.controller;
+
+import com.gyee.host.comm.AjaxResult;
+import com.gyee.host.mode.GpuInfo;
+import com.gyee.host.service.HostparamService;
+import com.gyee.host.service.ShellService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/hostparam")
+public class HostparamController {
+
+    @Autowired
+    private HostparamService hostparamService;
+
+    @Autowired
+    private ShellService shellService;
+
+    @GetMapping(value = "getparam")
+    public AjaxResult getParam() throws Exception{
+        Map map = hostparamService.getHostParam();
+        List<GpuInfo> gpuInfoList = hostparamService.getGpuInfos();
+        map.put("gpu",gpuInfoList);
+
+        return AjaxResult.successData(map);
+    }
+
+
+    @GetMapping(value = "gpu")
+    public AjaxResult getGPU() throws IOException {
+        List<GpuInfo> gpuInfoList = hostparamService.getGpuInfos();
+        return AjaxResult.successData(gpuInfoList);
+    }
+}
+
+
+

+ 19 - 0
hostparam/src/main/java/com/gyee/host/mode/CpuInfo.java

@@ -0,0 +1,19 @@
+package com.gyee.host.mode;
+
+import lombok.Data;
+
+@Data
+public class CpuInfo {
+
+    //cpu名称
+    private String cpuName;
+
+    //cpu数量
+    private  int  cpuNum;
+
+    //cpu核心数量
+    private  int cpuCoreNum;
+
+    //cpu线程数
+    private  int cpuhreads;
+}

+ 18 - 0
hostparam/src/main/java/com/gyee/host/mode/DiskInfo.java

@@ -0,0 +1,18 @@
+package com.gyee.host.mode;
+
+import lombok.Data;
+
+@Data
+public class DiskInfo {
+
+
+    private String diskId;
+    //磁盘名称
+    private String diskName;
+
+    //型号
+    private String diskMode;
+
+    //大小
+    private String diskSize;
+}

+ 13 - 0
hostparam/src/main/java/com/gyee/host/mode/GpuInfo.java

@@ -0,0 +1,13 @@
+package com.gyee.host.mode;
+
+import lombok.Data;
+
+@Data
+public class GpuInfo {
+    //("GPU编号")
+    private Integer number;
+
+    //("显卡名称")
+    private String name;
+
+}

+ 21 - 0
hostparam/src/main/java/com/gyee/host/mode/Memory.java

@@ -0,0 +1,21 @@
+package com.gyee.host.mode;
+
+
+import lombok.Data;
+
+@Data
+public class Memory {
+
+
+    //("总内存")
+    private String totalMemory;
+
+    //("使用内存")
+    //private String usedMemory;
+
+    //("可用内存")
+    private String useableMemory;
+
+    //("使用率")
+    //private Double usageRate;
+}

+ 215 - 0
hostparam/src/main/java/com/gyee/host/service/HostparamService.java

@@ -0,0 +1,215 @@
+package com.gyee.host.service;
+
+import cn.hutool.core.lang.Console;
+import cn.hutool.system.OsInfo;
+import cn.hutool.system.SystemUtil;
+import com.gyee.host.mode.CpuInfo;
+import com.gyee.host.mode.DiskInfo;
+import com.gyee.host.mode.GpuInfo;
+import com.gyee.host.mode.Memory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import oshi.SystemInfo;
+import oshi.hardware.*;
+import oshi.software.os.OperatingSystem;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+@Service
+public class HostparamService {
+
+    private static final Logger logger = LoggerFactory.getLogger(HostparamService.class);
+
+
+    @Autowired
+    private ShellService shellService;
+
+    public Map getHostParam(){
+//        Console.log("======= 操作系统信息 ======");
+        OsInfo osInfo = SystemUtil.getOsInfo();
+        Console.log("操作系统:{}", osInfo.getName());
+        Console.log("系统版本:{}", osInfo.getVersion());
+//        Console.log("系统架构:{}", osInfo.getArch());
+//        Console.log("JVM总内存:{}", format(SystemUtil.getTotalMemory()));
+//        Console.log("JVM剩余内存:{}", format(SystemUtil.getFreeMemory()));
+
+
+        Map map =new HashMap();
+        SystemInfo systemInfo = new SystemInfo();
+        HardwareAbstractionLayer hardware = systemInfo.getHardware();
+        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
+
+        Console.log("======= CPU信息 ======");
+        CentralProcessor cpu = hardware.getProcessor();
+        CentralProcessor.ProcessorIdentifier cpuInfo = cpu.getProcessorIdentifier();
+        CpuInfo info = new CpuInfo();
+//        Console.log("cpu数量:{}", cpu.getPhysicalPackageCount());
+        info.setCpuNum(cpu.getPhysicalPackageCount());
+//        Console.log("cpu核心数:{}", cpu.getPhysicalProcessorCount());
+        info.setCpuCoreNum(cpu.getPhysicalProcessorCount());
+//        Console.log("cpu线程数:{}", cpu.getLogicalProcessorCount());
+        info.setCpuhreads(cpu.getLogicalProcessorCount());
+//        Console.log("cpuID:{}", cpuInfo.getProcessorID());
+//        Console.log("cpu名称:{}", cpuInfo.getName());
+        info.setCpuName(cpuInfo.getName());
+//        Console.log("cpu标识:{}", cpuInfo.getIdentifier());
+        map.put("cpu",info);
+
+//        Console.log("======= 主板信息 ======");
+//        ComputerSystem computer = hardware.getComputerSystem();
+//        Console.log("主板型号:{}", computer.getModel());
+//        Console.log("主板序列号:{}", computer.getSerialNumber());
+
+        Console.log("======= 内存信息 ======");
+        Memory m = new Memory();
+        GlobalMemory memory = hardware.getMemory();
+//        Console.log("内存大小:{}", format(memory.getTotal()));
+//        Console.log("可用内存:{}", format(memory.getAvailable()));
+        m.setTotalMemory(format(memory.getTotal()));
+        m.setUseableMemory(format(memory.getAvailable()));
+        map.put("memory",m);
+        Console.log("==========");
+        List<PhysicalMemory> memoryList = memory.getPhysicalMemory();
+//        memoryList.forEach((item) -> {
+//            Console.log("内存型号:{}", item.getManufacturer());
+//            Console.log("内存规格:{}", item.getMemoryType());
+//            Console.log("内存主频:{}", format(item.getClockSpeed()));
+//            Console.log("内存大小:{}", format(item.getCapacity()));
+//            Console.log("==========");
+//        });
+
+        Console.log("======= 物理磁盘信息 ======");
+        List<HWDiskStore> diskList = hardware.getDiskStores();
+
+        List<DiskInfo> diskInfos = new ArrayList<>();
+        diskList.forEach((disk) -> {
+            DiskInfo diskInfo = new DiskInfo();
+//            Console.log("名称:{}", disk.getName());
+            diskInfo.setDiskName(disk.getName());
+//            Console.log("型号:{}", disk.getModel());
+            diskInfo.setDiskMode(disk.getModel());
+//            Console.log("序号:{}", disk.getSerial());
+            diskInfo.setDiskId(disk.getSerial());
+//            Console.log("大小:{}", format(disk.getSize()));
+            diskInfo.setDiskSize(format(disk.getSize()));
+//            Console.log("==========");
+            diskInfos.add(diskInfo);
+        });
+
+        map.put("disk",diskInfos);
+
+//        Console.log("======= 逻辑磁盘信息 ======");
+//        FileSystem fileSystem = operatingSystem.getFileSystem();
+//        List<OSFileStore> fileList = fileSystem.getFileStores(true);
+//        fileList.forEach((file) -> {
+//            Console.log("名称:{}", file.getName());
+//            Console.log("UUID:{}", file.getUUID());
+//            Console.log("盘符:{}", file.getMount());
+//            Console.log("文件类型:{}", file.getType());
+//            Console.log("总大小:{}", format(file.getTotalSpace()));
+//            Console.log("剩余大小:{}", format(file.getUsableSpace()));
+//            Console.log("==========");
+//        });
+        return  map;
+    }
+
+
+    private static String format(long size) {
+        if (size < 1024) {
+            return size + "B";
+        } else {
+            size = size / 1024;
+        }
+        if (size < 1024) {
+            return size + "KB";
+        } else {
+            size = size / 1024;
+        }
+        if (size < 1024) {
+            size = size * 100;
+            return size / 100 + "." + size % 100 + "MB";
+        } else {
+            size = size * 100 / 1024;
+            return size / 100 + "." + size % 100 + "GB";
+        }
+    }
+
+
+
+    public List<GpuInfo> getGpuInfos() throws IOException {
+        String gpus = null;
+
+        gpus = shellService.getGPU();
+        //命令行调用后获取的信息
+        /*String gpus = "Mon Jun  1 10:47:16 2020       \n" +
+                "+-----------------------------------------------------------------------------+\n" +
+                "| NVIDIA-SMI 418.87.01    Driver Version: 418.87.01    CUDA Version: 10.1     |\n" +
+                "|-------------------------------+----------------------+----------------------+\n" +
+                "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n" +
+                "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n" +
+                "|===============================+======================+======================|\n" +
+                "|   0  TITAN V             Off  | 00000000:2D:00.0  On |                  N/A |\n" +
+                "| 29%   43C    P8    27W / 250W |   1123MiB / 12035MiB |      0%      Default |\n" +
+                "+-------------------------------+----------------------+----------------------+\n" +
+                "|   1  GeForce RTX 208...  Off  | 00000000:99:00.0 Off |                  N/A |\n" +
+                "|  0%   29C    P8    20W / 260W |     11MiB / 10989MiB |      0%      Default |\n" +
+                "+-------------------------------+----------------------+----------------------+\n" +
+                "                                                                               \n" +
+                "+-----------------------------------------------------------------------------+\n" +
+                "| Processes:                                                       GPU Memory |\n" +
+                "|  GPU       PID   Type   Process name                             Usage      |\n" +
+                "|=============================================================================|\n" +
+                "|    0     16841      C   inference_worker                            1077MiB |\n" +
+                "|    0     19996      G   /usr/lib/xorg/Xorg                            33MiB |\n" +
+                "+-----------------------------------------------------------------------------+\n";*/
+        //System.out.println("命令行获取的结果: " + gpus);
+        //分割废物信息
+        String[] split = gpus.split("\\|===============================\\+======================\\+======================\\|");
+        String[] gpusInfo = split[1].split("                                                                               ");
+        // 分割多个gpu
+        String[] gpuInfo = gpusInfo[0].split("\\+-------------------------------\\+----------------------\\+----------------------\\+");
+        //System.out.println("000000000000000000000000000000000");
+        List<GpuInfo> gpuInfoList = new ArrayList<>();
+        for (int i = 0; i < gpuInfo.length - 1; i++) {
+            GpuInfo gpuInfo1 = new GpuInfo();
+            String[] nameAndInfo = gpuInfo[i].split("\n");
+            //只要第二块的数据
+            /*0
+             *TITAN
+             *V
+             *Off
+             * */
+            String[] split1 = nameAndInfo[1].split("\\|")[1] // 0  TITAN V             Off
+                    .split("\\s+");//去空格
+
+            gpuInfo1.setNumber(Integer.parseInt(split1[1]));
+            StringBuffer name = new StringBuffer();
+            for (int j = 0; j < split1.length - 1; j++) {
+                if (j > 1 && j != split1.length) {
+                    name.append(split1[j] + " ");
+                }
+            }
+            gpuInfo1.setName(name.toString());
+
+            String[] info = nameAndInfo[2].split("\\|")[2].split("\\s+");
+            /* System.out.println("biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~");*/
+//            gpuInfo1.setUsedMemory(info[1]);
+//            gpuInfo1.setTotalMemory(info[3]);
+//            int useable = Integer.parseInt(gpuInfo1.getTotalMemory().split("MiB")[0]) - Integer.parseInt(gpuInfo1.getUsedMemory().split("MiB")[0]);
+//            gpuInfo1.setUseableMemory(useable + "MiB");
+//            Double usageRate = Integer.parseInt(gpuInfo1.getUsedMemory().split("MiB")[0]) * 100.00 / Integer.parseInt(gpuInfo1.getTotalMemory().split("MiB")[0]);
+//            gpuInfo1.setUsageRate(usageRate);
+            gpuInfoList.add(gpuInfo1);
+
+        }
+        return gpuInfoList;
+    }
+
+}

+ 42 - 0
hostparam/src/main/java/com/gyee/host/service/ShellService.java

@@ -0,0 +1,42 @@
+package com.gyee.host.service;
+
+import com.sun.jna.Platform;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+
+@Service
+public class ShellService {
+
+    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();
+          return null;
+        }
+
+        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.toString();
+    }
+}

+ 7 - 0
hostparam/src/main/resources/application.yml

@@ -0,0 +1,7 @@
+server:
+  port: 85200
+  servlet:
+   encoding:
+    charset: UTF-8
+    enabled: true
+    force: true

+ 1 - 0
pom.xml

@@ -25,6 +25,7 @@
         <module>gyee-sample-impala</module>
         <module>power-fitting</module>
         <module>power-fitting-JN</module>
+        <module>hostparam</module>
     </modules>
 
 

+ 9 - 2
power-fitting/src/main/java/com/gyee/power/fitting/service/custom/ratedpower/RatedPowerService.java

@@ -1,6 +1,7 @@
 package com.gyee.power.fitting.service.custom.ratedpower;
 
 import com.gyee.power.fitting.common.spring.InitialRunner;
+import com.gyee.power.fitting.common.util.DateUtil;
 import com.gyee.power.fitting.common.util.FileUtil;
 import com.gyee.power.fitting.model.Equipmentmodel;
 import com.gyee.power.fitting.model.Powerfittinganalysis;
@@ -14,6 +15,7 @@ import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 额定功率分析
@@ -34,7 +36,7 @@ public class RatedPowerService {
             return result;
 
         List<PowerPointData> all = new ArrayList<>();
-        TreeMap<String, Double> map = new TreeMap<>();
+        TreeMap<String, Object> map = new TreeMap<>();
         for (Powerfittinganalysis obj : list){
             String[] spid = obj.getProcessid().split(",");
             for (String id : spid){
@@ -43,6 +45,9 @@ public class RatedPowerService {
                 all.addAll(ls);
                 double avg = ls.size() > 0 ? ls.stream().mapToDouble(PowerPointData::getPower).average().getAsDouble() : 0;
                 map.put(item.getWindturbine(), new BigDecimal(avg).setScale(0, RoundingMode.CEILING).doubleValue());
+                Map<String, Integer> mp = new HashMap<>();
+                ls.stream().collect(Collectors.groupingBy(PowerPointData::getTime)).forEach((k, v) -> mp.put(k, v.size()));
+                map.put("count", mp);
             }
         }
         double avg = all.size() > 0 ? all.stream().mapToDouble(PowerPointData::getPower).average().getAsDouble() : 0;
@@ -66,8 +71,10 @@ public class RatedPowerService {
         for (int i = 1; i < content.size(); i++){
             String[] split = content.get(i).split(",");
             PowerPointData data = new PowerPointData(split, false);
-            if (data.getSpeed() >= 12.0)
+            if (data.getSpeed() >= 12.0){
+                data.setTime(DateUtil.format(data.getTime(), DateUtil.DATE_PATTERN));
                 list.add(data);
+            }
         }
 
         return list;