Files
sheepit-shadow-nabber/src/main/java/com/sheepit/client/RenderProcess.java

167 lines
4.4 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2015 Laurent CLOUET
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.sheepit.client;
import com.sheepit.client.os.OS;
import lombok.Data;
import java.util.Date;
2021-12-21 10:08:59 +00:00
import java.util.concurrent.atomic.AtomicLong;
2022-09-29 14:06:40 +00:00
import lombok.Setter;
2021-12-21 10:08:59 +00:00
import oshi.software.os.OSProcess;
2023-10-19 10:54:48 +00:00
/**
* Provides facilities to track how long a render process takes,
* how long different phases lasted
* how many resources it uses and has used
* and to end the process if necessary
*/
@Data public class RenderProcess {
private long startTime;
private long endTime;
2022-09-29 14:06:40 +00:00
@Setter
private int scenePrepDuration;
@Setter
private int renderDuration;
@Setter
private int postProcessingDuration;
private int remainingDuration; // in seconds
2021-12-21 10:08:59 +00:00
private AtomicLong memoryUsed; // in kB
2021-11-16 14:51:53 +00:00
private long peakMemoryUsed; // in kB
private int coresUsed;
private Process process;
2021-12-21 10:08:59 +00:00
private OSProcess osProcess;
private Log log;
2021-12-21 10:08:59 +00:00
public RenderProcess(Log _log) {
process = null;
2021-12-21 10:08:59 +00:00
osProcess = null;
startTime = -1;
endTime = -1;
2022-09-29 14:06:40 +00:00
scenePrepDuration = -1;
renderDuration = -1;
postProcessingDuration = -1;
2021-12-21 10:08:59 +00:00
memoryUsed = new AtomicLong(0);
2021-11-16 14:51:53 +00:00
peakMemoryUsed = 0;
coresUsed = 0;
remainingDuration = 0;
2021-12-21 10:08:59 +00:00
log = _log;
}
2023-10-19 10:54:48 +00:00
/**
* Update the Process object with the current memory usage
*/
2021-12-21 10:08:59 +00:00
public void update() {
OSProcess osp = osProcess; // Shallow copy to try to not run into a race condition via being nulled
try {
if (osp != null && osp.updateAttributes()){ // We enter if updateAttributes() was successful
long mem = osp.getResidentSetSize() / 1024; // Avoid multiple ram usage calls, because again, they might differ
if (mem != 0){
memoryUsed.set(mem);
if (peakMemoryUsed < mem) {
peakMemoryUsed = mem;
}
}
}
} catch (NullPointerException ex) { // We are racing the system itself, we can't avoid catching NPE's
log.debug("RenderProcess::Handled process becoming unavailable mid-update");
osProcess = null;
memoryUsed.set(0);
}
}
/**
2023-10-19 10:54:48 +00:00
* Get how long the render process has been running for
* @return duration in seconds
*/
public int getDuration() {
if (startTime != -1 && endTime != -1) {
return (int) ((endTime - startTime) / 1000);
}
else if (startTime != -1) {
return (int) ((new Date().getTime() - startTime) / 1000);
}
return 0;
}
2023-10-19 10:54:48 +00:00
/**
* Kills the render process
*/
public void kill() {
if (process != null){
try {
OS.getOS().kill(process);
log.debug("RenderProcess::Process killed");
} catch (NullPointerException ex) { // We are racing the system itself, we can't avoid catching NPE's
log.debug("RenderProcess::Handled process becoming unavailable before getting killed");
}
}
}
2023-10-19 10:54:48 +00:00
/**
* Ends the render process by killing it and recording the time it ended
*/
public void finish() {
kill();
endTime = new Date().getTime();
2021-12-21 10:08:59 +00:00
osProcess = null;
process = null;
}
2023-10-19 10:54:48 +00:00
/**
* Records when the render process started
*/
public void start() {
startTime = new Date().getTime();
}
2023-10-19 10:54:48 +00:00
/**
* Returns the exit value for the render process.
* @return the exit value of the process represented
* by this Process object. By convention,
* the value 0 indicates normal termination.
* For this method it might also mean that the
* process is still running.
* Returns -1 if process is null.
* @see Process#exitValue()
*/
public int exitValue() {
int value = 0;
if (process == null) {
return -1;
}
try {
value = process.exitValue();
}
catch (IllegalThreadStateException e) {
// the process is not finished yet
}
catch (Exception e) {
// actually is for java.io.IOException: GetExitCodeProcess error=6, The handle is invalid
// it was not declared throwable
// the process is not finished yet
}
return value;
}
}