GpuDeviceInformationParser { public synchronized GpuDeviceInformation parseXml(String xmlContent) throws YarnException { InputSource inputSource = new InputSource(new StringReader(xmlContent)); SAXSource source = new SAXSource(xmlReader, inputSource); try { return (GpuDeviceInformation) unmarshaller.unmarshal(source); } catch (JAXBException e) { String msg = "Failed to parse XML output of " + GPU_SCRIPT_REFERENCE + "!"; LOG.error(msg, e); throw new YarnException(msg, e); } } GpuDeviceInformationParser(); synchronized GpuDeviceInformation parseXml(String xmlContent); static final String GPU_SCRIPT_REFERENCE; }
@Test public void testParse() throws IOException, YarnException { File f = new File("src/test/resources/nvidia-smi-sample-output.xml"); String s = FileUtils.readFileToString(f, UTF_8); GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); GpuDeviceInformation info = parser.parseXml(s); assertEquals("375.66", info.getDriverVersion()); assertEquals(2, info.getGpus().size()); assertFirstGpu(info.getGpus().get(0)); assertSecondGpu(info.getGpus().get(1)); }
@Test public void testParseExcerpt() throws IOException, YarnException { File f = new File("src/test/resources/nvidia-smi-output-excerpt.xml"); String s = FileUtils.readFileToString(f, UTF_8); GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); GpuDeviceInformation info = parser.parseXml(s); assertEquals("375.66", info.getDriverVersion()); assertEquals(2, info.getGpus().size()); assertFirstGpu(info.getGpus().get(0)); assertSecondGpu(info.getGpus().get(1)); }
@Test public void testParseConsecutivelyWithSameParser() throws IOException, YarnException { File f = new File("src/test/resources/nvidia-smi-sample-output.xml"); String s = FileUtils.readFileToString(f, UTF_8); for (int i = 0; i < 3; i++) { GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); GpuDeviceInformation info = parser.parseXml(s); assertEquals("375.66", info.getDriverVersion()); assertEquals(2, info.getGpus().size()); assertFirstGpu(info.getGpus().get(0)); assertSecondGpu(info.getGpus().get(1)); } }
@Test public void testParseEmptyString() throws YarnException { expected.expect(YarnException.class); GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); parser.parseXml(""); }
@Test public void testParseInvalidRootElement() throws YarnException { expected.expect(YarnException.class); GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); parser.parseXml("<nvidia_smiiiii></nvidia_smiiiii"); }
@Test public void testParseMissingTags() throws IOException, YarnException { File f = new File("src/test/resources/nvidia-smi-output-missing-tags.xml"); String s = FileUtils.readFileToString(f, UTF_8); GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); GpuDeviceInformation info = parser.parseXml(s); assertEquals("375.66", info.getDriverVersion()); assertEquals(1, info.getGpus().size()); PerGpuDeviceInformation gpu = info.getGpus().get(0); assertEquals("N/A", gpu.getProductName()); assertEquals("N/A", gpu.getUuid()); assertEquals(-1, gpu.getMinorNumber()); assertNull(gpu.getGpuMemoryUsage()); assertNull(gpu.getTemperature()); assertNull(gpu.getGpuUtilizations()); }
@Test public void testParseMissingInnerTags() throws IOException, YarnException { File f =new File("src/test/resources/nvidia-smi-output-missing-tags2.xml"); String s = FileUtils.readFileToString(f, UTF_8); GpuDeviceInformationParser parser = new GpuDeviceInformationParser(); GpuDeviceInformation info = parser.parseXml(s); assertEquals("375.66", info.getDriverVersion()); assertEquals(2, info.getGpus().size()); PerGpuDeviceInformation gpu = info.getGpus().get(0); assertEquals("Tesla P100-PCIE-12GB", gpu.getProductName()); assertEquals("GPU-28604e81-21ec-cc48-6759-bf2648b22e16", gpu.getUuid()); assertEquals(0, gpu.getMinorNumber()); assertEquals(-1, gpu.getGpuMemoryUsage().getTotalMemoryMiB()); assertEquals(-1, (long) gpu.getGpuMemoryUsage().getUsedMemoryMiB()); assertEquals(-1, (long) gpu.getGpuMemoryUsage().getAvailMemoryMiB()); assertEquals(0f, gpu.getGpuUtilizations().getOverallGpuUtilization(), DELTA); assertEquals(Float.MIN_VALUE, gpu.getTemperature().getCurrentGpuTemp(), DELTA); assertEquals(Float.MIN_VALUE, gpu.getTemperature().getMaxGpuTemp(), DELTA); assertEquals(Float.MIN_VALUE, gpu.getTemperature().getSlowThresholdGpuTemp(), DELTA); assertSecondGpu(info.getGpus().get(1)); }