Skip to content

Commit 371677b

Browse files
committed
refactor: extract ProcessRecord
1 parent 048b00d commit 371677b

File tree

2 files changed

+98
-96
lines changed

2 files changed

+98
-96
lines changed

backend/src/main/java/net/laprun/sustainability/power/sensors/macos/powermetrics/MacOSPowermetricsSensor.java

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -97,102 +97,6 @@ public SensorMetadata metadata() {
9797
return cpu.metadata();
9898
}
9999

100-
private static class ProcessRecord {
101-
final double cpu;
102-
final double gpu;
103-
final String pid;
104-
105-
public ProcessRecord(String line) throws IllegalArgumentException {
106-
// Expected normal output:
107-
//Name ID CPU ms/s samp ms/s User% Deadlines (<2 ms, 2-5 ms) Wakeups (Intr, Pkg idle) GPU ms/s
108-
//iTerm2 1008 46.66 46.91 83.94 0.00 0.00 30.46 0.00 0.00
109-
// Expected summary output:
110-
//Name ID CPU ms/s samp ms/s [total] User% Deadlines/s [total] (<2 ms, 2-5 ms) Wakeups/s [total] (Intr, Pkg idle) Dead GPU ms/s
111-
//WindowServer 406 493.74 493.96 [5165.88 ] 64.82 65.95 [690 ] 0.00 [0 ] 656.62 [6870 ] 4.21 [44 ] N 0.00
112-
113-
try {
114-
// Trim leading/trailing whitespace
115-
line = line.trim();
116-
117-
// Find first whitespace block after process name (marks start of ID)
118-
int idStart = findFirstWhitespace(line);
119-
if (idStart == -1) {
120-
throw new IllegalArgumentException("Cannot find ID in line: " + line);
121-
}
122-
123-
// Skip whitespace to get to ID
124-
idStart = skipWhitespace(line, idStart);
125-
int idEnd = findNextWhitespace(line, idStart);
126-
pid = RegisteredPID.prepare(line.substring(idStart, idEnd));
127-
128-
// Skip CPU ms/s column (skip whitespace, then number, then whitespace)
129-
int pos = skipWhitespace(line, idEnd);
130-
pos = skipNumber(line, pos);
131-
132-
// Now at samp ms/s
133-
pos = skipWhitespace(line, pos);
134-
int sampStart = pos;
135-
int sampEnd = skipNumber(line, sampStart);
136-
cpu = Double.parseDouble(line.substring(sampStart, sampEnd));
137-
138-
// Skip to end and work backwards to find GPU ms/s
139-
// The GPU value is the last numeric value on the line
140-
int lastNumEnd = line.length();
141-
while (lastNumEnd > 0 && Character.isWhitespace(line.charAt(lastNumEnd - 1))) {
142-
lastNumEnd--;
143-
}
144-
145-
int lastNumStart = lastNumEnd;
146-
while (lastNumStart > 0 && isNumberChar(line.charAt(lastNumStart - 1))) {
147-
lastNumStart--;
148-
}
149-
150-
if (lastNumStart < lastNumEnd) {
151-
gpu = Double.parseDouble(line.substring(lastNumStart, lastNumEnd));
152-
} else {
153-
throw new IllegalArgumentException("Cannot find GPU value in line: " + line);
154-
}
155-
156-
} catch (Exception e) {
157-
throw new IllegalArgumentException("Received line doesn't conform to expected format: " + line, e);
158-
}
159-
}
160-
161-
private static int findFirstWhitespace(String line) {
162-
for (int i = 0; i < line.length(); i++) {
163-
if (Character.isWhitespace(line.charAt(i))) {
164-
return i;
165-
}
166-
}
167-
return -1;
168-
}
169-
170-
private static int skipWhitespace(String line, int pos) {
171-
while (pos < line.length() && Character.isWhitespace(line.charAt(pos))) {
172-
pos++;
173-
}
174-
return pos;
175-
}
176-
177-
private static int findNextWhitespace(String line, int pos) {
178-
while (pos < line.length() && !Character.isWhitespace(line.charAt(pos))) {
179-
pos++;
180-
}
181-
return pos;
182-
}
183-
184-
private static int skipNumber(String line, int pos) {
185-
while (pos < line.length() && isNumberChar(line.charAt(pos))) {
186-
pos++;
187-
}
188-
return pos;
189-
}
190-
191-
private static boolean isNumberChar(char c) {
192-
return Character.isDigit(c) || c == '.' || c == '-' || c == 'e' || c == 'E';
193-
}
194-
}
195-
196100
Measures extractPowerMeasure(InputStream powerMeasureInput, Long tick) {
197101
final long start = System.currentTimeMillis();
198102
try {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package net.laprun.sustainability.power.sensors.macos.powermetrics;
2+
3+
import net.laprun.sustainability.power.sensors.RegisteredPID;
4+
5+
class ProcessRecord {
6+
final double cpu;
7+
final double gpu;
8+
final String pid;
9+
10+
public ProcessRecord(String line) throws IllegalArgumentException {
11+
// Expected normal output:
12+
//Name ID CPU ms/s samp ms/s User% Deadlines (<2 ms, 2-5 ms) Wakeups (Intr, Pkg idle) GPU ms/s
13+
//iTerm2 1008 46.66 46.91 83.94 0.00 0.00 30.46 0.00 0.00
14+
// Expected summary output:
15+
//Name ID CPU ms/s samp ms/s [total] User% Deadlines/s [total] (<2 ms, 2-5 ms) Wakeups/s [total] (Intr, Pkg idle) Dead GPU ms/s
16+
//WindowServer 406 493.74 493.96 [5165.88 ] 64.82 65.95 [690 ] 0.00 [0 ] 656.62 [6870 ] 4.21 [44 ] N 0.00
17+
try {
18+
// Trim leading/trailing whitespace
19+
line = line.trim();
20+
21+
// Find first whitespace block after process name (marks start of ID)
22+
int idStart = findFirstWhitespace(line);
23+
if (idStart == -1) {
24+
throw new IllegalArgumentException("Cannot find ID in line: " + line);
25+
}
26+
27+
// Skip whitespace to get to ID
28+
idStart = skipWhitespace(line, idStart);
29+
int idEnd = findNextWhitespace(line, idStart);
30+
pid = RegisteredPID.prepare(line.substring(idStart, idEnd));
31+
32+
// Skip CPU ms/s column (skip whitespace, then number, then whitespace)
33+
int pos = skipWhitespace(line, idEnd);
34+
pos = skipNumber(line, pos);
35+
36+
// Now at samp ms/s
37+
pos = skipWhitespace(line, pos);
38+
int sampStart = pos;
39+
int sampEnd = skipNumber(line, sampStart);
40+
cpu = Double.parseDouble(line.substring(sampStart, sampEnd));
41+
42+
// Skip to end and work backwards to find GPU ms/s
43+
// The GPU value is the last numeric value on the line
44+
int lastNumEnd = line.length();
45+
while (lastNumEnd > 0 && Character.isWhitespace(line.charAt(lastNumEnd - 1))) {
46+
lastNumEnd--;
47+
}
48+
49+
int lastNumStart = lastNumEnd;
50+
while (lastNumStart > 0 && isNumberChar(line.charAt(lastNumStart - 1))) {
51+
lastNumStart--;
52+
}
53+
54+
if (lastNumStart < lastNumEnd) {
55+
gpu = Double.parseDouble(line.substring(lastNumStart, lastNumEnd));
56+
} else {
57+
throw new IllegalArgumentException("Cannot find GPU value in line: " + line);
58+
}
59+
60+
} catch (Exception e) {
61+
throw new IllegalArgumentException("Received line doesn't conform to expected format: " + line, e);
62+
}
63+
}
64+
65+
private static int findFirstWhitespace(String line) {
66+
for (int i = 0; i < line.length(); i++) {
67+
if (Character.isWhitespace(line.charAt(i))) {
68+
return i;
69+
}
70+
}
71+
return -1;
72+
}
73+
74+
private static int skipWhitespace(String line, int pos) {
75+
while (pos < line.length() && Character.isWhitespace(line.charAt(pos))) {
76+
pos++;
77+
}
78+
return pos;
79+
}
80+
81+
private static int findNextWhitespace(String line, int pos) {
82+
while (pos < line.length() && !Character.isWhitespace(line.charAt(pos))) {
83+
pos++;
84+
}
85+
return pos;
86+
}
87+
88+
private static int skipNumber(String line, int pos) {
89+
while (pos < line.length() && isNumberChar(line.charAt(pos))) {
90+
pos++;
91+
}
92+
return pos;
93+
}
94+
95+
private static boolean isNumberChar(char c) {
96+
return Character.isDigit(c) || c == '.' || c == '-' || c == 'e' || c == 'E';
97+
}
98+
}

0 commit comments

Comments
 (0)