blob: b8ae5d89b558635ee735717563e9ac62f95be73b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.verification.spec.retrievers;
import com.yahoo.vespa.hosted.node.verification.commons.CommandExecutor;
import com.yahoo.vespa.hosted.node.verification.commons.parser.OutputParser;
import com.yahoo.vespa.hosted.node.verification.commons.parser.ParseInstructions;
import com.yahoo.vespa.hosted.node.verification.commons.parser.ParseResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Retrieves number of CPU cores, and stores the result in a HardwareInfo instance
*
* @author olaaun
* @author sgrostad
*/
public class CPURetriever implements HardwareRetriever {
private static final String CPU_INFO_COMMAND = "cat /proc/cpuinfo";
private static final String SEARCH_WORD = "cpu MHz";
private static final String REGEX_SPLIT = "\\s+:\\s";
private static final int SEARCH_ELEMENT_INDEX = 0;
private static final int RETURN_ELEMENT_INDEX = 1;
private static final Logger logger = Logger.getLogger(CPURetriever.class.getName());
private final HardwareInfo hardwareInfo;
private final CommandExecutor commandExecutor;
public CPURetriever(HardwareInfo hardwareInfo, CommandExecutor commandExecutor) {
this.hardwareInfo = hardwareInfo;
this.commandExecutor = commandExecutor;
}
@Override
public void updateInfo() {
try {
List<String> commandOutput = commandExecutor.executeCommand(CPU_INFO_COMMAND);
List<ParseResult> parseResults = parseCPUInfoFile(commandOutput);
setCpuCores(parseResults);
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to retrieve CPU info", e);
}
}
protected List<ParseResult> parseCPUInfoFile(List<String> commandOutput) {
List<String> searchWords = new ArrayList<>(Arrays.asList(SEARCH_WORD));
ParseInstructions parseInstructions = new ParseInstructions(SEARCH_ELEMENT_INDEX, RETURN_ELEMENT_INDEX, REGEX_SPLIT, searchWords);
List<ParseResult> parseResults = OutputParser.parseOutput(parseInstructions, commandOutput);
return parseResults;
}
protected void setCpuCores(List<ParseResult> parseResults) {
hardwareInfo.setMinCpuCores(countCpuCores(parseResults));
}
protected int countCpuCores(List<ParseResult> parseResults) {
return parseResults.size();
}
}
|