Skip to content

Commit

Permalink
Enable Spotless (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 11, 2023
1 parent 0fae108 commit 6d166c2
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 302 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<revision>1.13</revision>
<changelist>-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/extras-${project.artifactId}</gitHubRepo>
<spotless.check.skip>false</spotless.check.skip>
</properties>

<dependencies>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/jvnet/hudson/AbstractMemoryMonitorImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,26 +30,26 @@ abstract class AbstractMemoryMonitorImpl extends MemoryMonitor {
protected long parse(String token) {
token = token.toLowerCase().trim();
long multiplier = 1;
if(token.endsWith("b"))
if (token.endsWith("b")) {
token = cutTail(token);
if(token.endsWith("k")) {
}
if (token.endsWith("k")) {
multiplier = 1024L;
token = cutTail(token);
}
if(token.endsWith("m")) {
multiplier = 1024L*1024;
if (token.endsWith("m")) {
multiplier = 1024L * 1024;
token = cutTail(token);
}
if(token.endsWith("g")) {
multiplier = 1024L*1024*1024;
if (token.endsWith("g")) {
multiplier = 1024L * 1024 * 1024;
token = cutTail(token);
}

return (long)(Float.parseFloat(token)*multiplier);
return (long) (Float.parseFloat(token) * multiplier);
}

protected String cutTail(String token) {
return token.substring(0,token.length()-1);
return token.substring(0, token.length() - 1);
}

}
44 changes: 23 additions & 21 deletions src/main/java/org/jvnet/hudson/Aix.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

/**
* AIX
*
*
* @author qxodream@gmail.com
*/
public class Aix extends AbstractMemoryMonitorImpl {
Expand All @@ -52,16 +52,17 @@ public static void main(String[] args) throws IOException {
* Returns total/availablae.
*/
private long[] getSwap() throws IOException {
long[] v = new long[] { -1, -1 };
long[] v = new long[] {-1, -1};
Process proc = startProcess("lsps", "-s");
/*
$ lsps -s
Total Paging Space Percent Used
45568MB 17%
$ lsps -s
Total Paging Space Percent Used
45568MB 17%
*/
*/

try (BufferedReader r = new BufferedReader(new InputStreamReader(proc.getInputStream(), Charset.defaultCharset()))) {
try (BufferedReader r =
new BufferedReader(new InputStreamReader(proc.getInputStream(), Charset.defaultCharset()))) {
String line;
while ((line = r.readLine()) != null) {
Matcher m = SWAP.matcher(line);
Expand All @@ -73,7 +74,7 @@ private long[] getSwap() throws IOException {
v[0] = totalSwap;
if (used > 0) {
v[1] = (totalSwap / 100) * (100 - used);
}
}
break;
}
}
Expand All @@ -91,39 +92,40 @@ public long getSize(long totalSwap, String unit) {
}

private long[] getMemUsed() throws IOException {
long[] v = new long[] { -1, -1 };
long[] v = new long[] {-1, -1};
Process proc = startProcess("vmstat");
/*
$ vmstat
$ vmstat
System configuration: lcpu=16 mem=25920MB
System configuration: lcpu=16 mem=25920MB
kthr memory page faults cpu
----- ----------- ------------------------ ------------ -----------
r b avm fre re pi po fr sr cy in sy cs us sy id wa
1 1 4986615 96970 0 0 0 0 12 0 34 4619 4005 2 1 98 0
kthr memory page faults cpu
----- ----------- ------------------------ ------------ -----------
r b avm fre re pi po fr sr cy in sy cs us sy id wa
1 1 4986615 96970 0 0 0 0 12 0 34 4619 4005 2 1 98 0
*/
try (BufferedReader r = new BufferedReader(new InputStreamReader(proc.getInputStream(), Charset.defaultCharset()))) {
*/
try (BufferedReader r =
new BufferedReader(new InputStreamReader(proc.getInputStream(), Charset.defaultCharset()))) {
String line;
while ((line = r.readLine()) != null) {
Matcher m = MEM_TOTAL.matcher(line);
if (m.find()) {
long mem = Long.parseLong(m.group(1));
String unit = m.group(2);
mem = getSize(mem, unit);
v[0]=mem;
v[0] = mem;
continue;
}
m = MEM_USED.matcher(line);
if (m.find()) {
//long used = Long.parseLong(m.group(1));
// long used = Long.parseLong(m.group(1));
long free = Long.parseLong(m.group(2));
v[1] = free * 4096;//v[0] == -1 ? free * 4096 : ( v[0] - used*4096);
v[1] = free * 4096; // v[0] == -1 ? free * 4096 : ( v[0] - used*4096);
break;
}
}
return v;
return v;
}
}

Expand Down
33 changes: 18 additions & 15 deletions src/main/java/org/jvnet/hudson/MemoryMonitor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,15 +24,14 @@
package org.jvnet.hudson;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.File;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Encapsulates how to compute {@link MemoryUsage}.
* Encapsulates how to compute {@link MemoryUsage}.
*
* @author Kohsuke Kawaguchi
*/
Expand All @@ -55,19 +54,22 @@ public abstract class MemoryMonitor {
* if no applicable implementation is found.
*/
public static MemoryMonitor get() throws IOException {
if(INSTANCE==null)
if (INSTANCE == null) {
INSTANCE = obtain();
}
return INSTANCE;
}

private static MemoryMonitor obtain() throws IOException {
if(File.pathSeparatorChar==';')
if (File.pathSeparatorChar == ';') {
return new Windows();
}

if(new File("/proc/meminfo").exists())
return new ProcMemInfo(); // Linux has this. Exactly since when, I don't know.
if (new File("/proc/meminfo").exists()) {
return new ProcMemInfo(); // Linux has this. Exactly since when, I don't know.
}
final String osName = System.getProperty("os.name");
if("AIX".equals(osName)){
if ("AIX".equals(osName)) {
Aix aix = new Aix();
aix.monitor();
return aix;
Expand All @@ -86,20 +88,21 @@ private static MemoryMonitor obtain() throws IOException {
Solaris solaris = new Solaris();
solaris.monitor();
return solaris;
} catch(Throwable t) {
} catch (Throwable t) {
// next
}

throw new IOException(String.format("No suitable implementation found: os.name=%s os.arch=%s sun.arch.data.model=%s",
System.getProperty("os.name"),System.getProperty("os.arch"),System.getProperty("sun.arch.data.model")));
throw new IOException(String.format(
"No suitable implementation found: os.name=%s os.arch=%s sun.arch.data.model=%s",
System.getProperty("os.name"),
System.getProperty("os.arch"),
System.getProperty("sun.arch.data.model")));
}

/**
* Main for test
*/
@SuppressFBWarnings(
value = "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE",
justification = "Only used in tests")
@SuppressFBWarnings(value = "LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE", justification = "Only used in tests")
public static void main(String[] args) throws IOException {
Logger l = Logger.getLogger(MemoryMonitor.class.getPackage().getName());
l.setLevel(Level.FINE);
Expand All @@ -108,7 +111,7 @@ public static void main(String[] args) throws IOException {
l.addHandler(h);

MemoryMonitor t = get();
System.out.println("implementation is "+t.getClass().getName());
System.out.println("implementation is " + t.getClass().getName());
System.out.println(t.monitor());
}

Expand Down
30 changes: 18 additions & 12 deletions src/main/java/org/jvnet/hudson/MemoryUsage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -56,35 +56,41 @@ public class MemoryUsage implements Serializable {
*/
public final long availableSwapSpace;

public MemoryUsage(long totalPhysicalMemory, long availablePhysicalMemory, long totalSwapSpace, long availableSwapSpace) {
public MemoryUsage(
long totalPhysicalMemory, long availablePhysicalMemory, long totalSwapSpace, long availableSwapSpace) {
this.totalPhysicalMemory = totalPhysicalMemory;
this.availablePhysicalMemory = availablePhysicalMemory;
this.totalSwapSpace = totalSwapSpace;
this.availableSwapSpace = availableSwapSpace;
}

MemoryUsage(long[] v) throws IOException {
this(v[0],v[1],v[2],v[3]);
if(!hasData(v))
this(v[0], v[1], v[2], v[3]);
if (!hasData(v)) {
throw new IOException("No data available");
}
}

@Override
public String toString() {
return String.format("Memory:%d/%dMB Swap:%d/%dMB",
toMB(availablePhysicalMemory),
toMB(totalPhysicalMemory),
toMB(availableSwapSpace),
toMB(totalSwapSpace));
return String.format(
"Memory:%d/%dMB Swap:%d/%dMB",
toMB(availablePhysicalMemory),
toMB(totalPhysicalMemory),
toMB(availableSwapSpace),
toMB(totalSwapSpace));
}

private static long toMB(long l) {
return l/(1024*1024);
return l / (1024 * 1024);
}

/*package*/ static boolean hasData(long[] values) {
for (long v : values)
if(v!=-1) return true;
for (long v : values) {
if (v != -1) {
return true;
}
}
return false;
}

Expand Down
39 changes: 16 additions & 23 deletions src/main/java/org/jvnet/hudson/ProcMemInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Copyright (c) 2008-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,7 +24,6 @@
package org.jvnet.hudson;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -40,23 +39,23 @@ final class ProcMemInfo extends MemoryMonitor {
public MemoryUsage monitor() throws IOException {
try (BufferedReader r = new BufferedReader(new FileReader("/proc/meminfo"))) {
long[] values = new long[4];
Arrays.fill(values,-1);
Arrays.fill(values, -1);

String line;
while((line=r.readLine())!=null) {
for( int i=0; i<HEADERS.length; i++ ) {
if(line.startsWith(HEADERS[i])) {
while ((line = r.readLine()) != null) {
for (int i = 0; i < HEADERS.length; i++) {
if (line.startsWith(HEADERS[i])) {
// found a line that we care about
String s = line.substring(HEADERS[i].length()).trim();

// trim off the suffix, if any
Suffix suffix = Suffix.find(s);
s = s.substring(0,s.length()-suffix.name.length()).trim();
s = s.substring(0, s.length() - suffix.name.length()).trim();

try {
values[i] = Long.parseLong(s)*suffix.multiplier;
values[i] = Long.parseLong(s) * suffix.multiplier;
} catch (NumberFormatException e) {
throw new IOException("Failed to parse: '"+s+"' out of '"+line+"'");
throw new IOException("Failed to parse: '" + s + "' out of '" + line + "'");
}
break;
}
Expand All @@ -70,23 +69,15 @@ public MemoryUsage monitor() throws IOException {
/**
* Lines in <tt>/proc/meminfo</tt> that we care about.
*/
private static final String[] HEADERS = new String[] {
"MemTotal:",
"MemFree:",
"SwapTotal:",
"SwapFree:"
};
private static final String[] HEADERS = new String[] {"MemTotal:", "MemFree:", "SwapTotal:", "SwapFree:"};

/**
* I've only seen "1234 kB" notation, but just to be safe.
*/
private static final Suffix[] SUFFIXES = new Suffix[] {
new Suffix("KB",1024),
new Suffix("MB",1024*1024),
new Suffix("GB",1024*1024*1024)
};
private static final Suffix[] SUFFIXES =
new Suffix[] {new Suffix("KB", 1024), new Suffix("MB", 1024 * 1024), new Suffix("GB", 1024 * 1024 * 1024)};

private static final Suffix NONE = new Suffix("",1);
private static final Suffix NONE = new Suffix("", 1);

private static class Suffix {
final String name;
Expand All @@ -98,9 +89,11 @@ private Suffix(String name, long multiplier) {
}

static Suffix find(String line) {
for (Suffix s : SUFFIXES)
if( line.substring(line.length()-s.name.length()).equalsIgnoreCase(s.name) )
for (Suffix s : SUFFIXES) {
if (line.substring(line.length() - s.name.length()).equalsIgnoreCase(s.name)) {
return s;
}
}
return NONE;
}
}
Expand Down

0 comments on commit 6d166c2

Please sign in to comment.