oshi.software.os.OSProcess Java Examples

The following examples show how to use oshi.software.os.OSProcess. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProcessStatistics.java    From garmadon with Apache License 2.0 6 votes vote down vote up
ProcessStatistics() {
    super(PROCESS_HEADER);
    prevTimeStamp = System.currentTimeMillis();
    procStat = LinuxHelper.openFile(new File("/proc/self/stat"));
    procStatus = LinuxHelper.openFile(new File("/proc/self/status"));
    procIO = LinuxHelper.openFile(new File("/proc/self/io"));
    if (procStat == null) {
        this.os = new SystemInfo().getOperatingSystem();
        currentPid = os.getProcessId();
        OSProcess process = os.getProcess(currentPid);
        prevUser = process.getUserTime();
        prevSys = process.getKernelTime();
        pageSize = 0;
        hz = 0;
    } else {
        currentPid = 0;
        String[] split = LinuxHelper.getFileLineSplit(procStat);
        if (split.length >= 24) {
            prevUser = Long.parseLong(split[13]);
            prevSys = Long.parseLong(split[14]);
        }
        pageSize = LinuxHelper.getPageSize();
        hz = getClockTick();
    }
}
 
Example #2
Source File: StatsTask.java    From Lavalink with MIT License 6 votes vote down vote up
private double getProcessRecentCpuUsage() {
    double output;
    HardwareAbstractionLayer hal = si.getHardware();
    OperatingSystem os = si.getOperatingSystem();
    OSProcess p = os.getProcess(os.getProcessId());

    if (cpuTime != 0) {
        double uptimeDiff = p.getUpTime() - uptime;
        double cpuDiff = (p.getKernelTime() + p.getUserTime()) - cpuTime;
        output = cpuDiff / uptimeDiff;
    } else {
        output = ((double) (p.getKernelTime() + p.getUserTime())) / (double) p.getUserTime();
    }

    // Record for next invocation
    uptime = p.getUpTime();
    cpuTime = p.getKernelTime() + p.getUserTime();
    return output / hal.getProcessor().getLogicalProcessorCount();
}
 
Example #3
Source File: SystemCommand.java    From LagMonitor with MIT License 6 votes vote down vote up
private void displayProcessInfo(CommandSender sender) {
    sender.sendMessage(PRIMARY_COLOR + "Process:");

    Optional<OSProcess> optProcess = plugin.getNativeData().getProcess();
    if (optProcess.isPresent()) {
        OSProcess process = optProcess.get();

        sendMessage(sender, "    PID", String.valueOf(process.getProcessID()));
        sendMessage(sender, "    Name", process.getName());
        sendMessage(sender, "    Path", process.getPath());
        sendMessage(sender, "    Working directory", process.getCurrentWorkingDirectory());
        sendMessage(sender, "    User", process.getUser());
        sendMessage(sender, "    Group", process.getGroup());
    } else {
        sendError(sender, NATIVE_NOT_FOUND);
    }
}
 
Example #4
Source File: SystemCommand.java    From LagMonitor with MIT License 6 votes vote down vote up
private void displayProcessInfo(CommandSender sender) {
    sender.sendMessage(PRIMARY_COLOR + "Process:");

    Optional<OSProcess> optProcess = plugin.getNativeData().getProcess();
    if (optProcess.isPresent()) {
        OSProcess process = optProcess.get();

        sendMessage(sender, "    PID", String.valueOf(process.getProcessID()));
        sendMessage(sender, "    Name", process.getName());
        sendMessage(sender, "    Path", process.getPath());
        sendMessage(sender, "    Working directory", process.getCurrentWorkingDirectory());
        sendMessage(sender, "    User", process.getUser());
        sendMessage(sender, "    Group", process.getGroup());
    } else {
        sendError(sender, NATIVE_NOT_FOUND);
    }
}
 
Example #5
Source File: ProcessHelper.java    From buck with Apache License 2.0 6 votes vote down vote up
@Nullable
static ProcessResourceConsumption getProcessResourceConsumptionInternal(
    @Nullable OSProcess process) {
  if (process == null) {
    return null;
  }
  return ProcessResourceConsumption.of(
      process.getResidentSetSize(),
      process.getVirtualSize(),
      process.getUpTime(),
      process.getUserTime(),
      process.getKernelTime(),
      process.getUserTime() + process.getKernelTime(),
      process.getBytesRead(),
      process.getBytesWritten(),
      process.getBytesRead() + process.getBytesWritten());
}
 
Example #6
Source File: NativeManager.java    From LagMonitor with MIT License 5 votes vote down vote up
public Optional<OSProcess> getProcess() {
    if (info == null) {
        return Optional.empty();
    }

    return Optional.of(info.getOperatingSystem().getProcess(pid));
}
 
Example #7
Source File: NativeManager.java    From LagMonitor with MIT License 5 votes vote down vote up
public Optional<OSProcess> getProcess() {
    if (info == null) {
        return Optional.empty();
    }

    return Optional.of(info.getOperatingSystem().getProcess(pid));
}
 
Example #8
Source File: ProcessHelper.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Gets resource consumption of the process with the given pid. */
@Nullable
public ProcessResourceConsumption getProcessResourceConsumption(long pid) {
  try {
    OperatingSystem os = OSHI.getOperatingSystem();
    OSProcess process = os.getProcess((int) pid);
    return getProcessResourceConsumptionInternal(process);
  } catch (Exception ex) {
    // do nothing
    return null;
  }
}
 
Example #9
Source File: ProcessHelper.java    From buck with Apache License 2.0 5 votes vote down vote up
public Node add(OSProcess info) {
  Node node = getOrCreate(info.getProcessID());
  if (node.info == null) {
    node.info = info;
  }
  Node parent = getOrCreate(info.getParentProcessID());
  parent.addChild(node);
  return node;
}
 
Example #10
Source File: NativeSaveTask.java    From LagMonitor with MIT License 4 votes vote down vote up
@Override
public void run() {
    Instant currentTime = Instant.now();
    int timeDiff = (int) Duration.between(lastCheck, currentTime).getSeconds();

    int mcReadDiff = 0;
    int mcWriteDiff = 0;

    TrafficReader trafficReader = plugin.getTrafficReader();
    if (trafficReader != null) {
        int mcRead = LagUtils.byteToMega(trafficReader.getIncomingBytes().longValue());
        mcReadDiff = getDifference(mcRead, lastMcRead, timeDiff);
        lastMcRead = mcRead;

        int mcWrite = LagUtils.byteToMega(trafficReader.getOutgoingBytes().longValue());
        mcWriteDiff = getDifference(mcWrite, lastMcWrite, timeDiff);
        lastMcWrite = mcWrite;
    }

    int totalSpace = LagUtils.byteToMega(plugin.getNativeData().getTotalSpace());
    int freeSpace = LagUtils.byteToMega(plugin.getNativeData().getFreeSpace());

    //4 decimal places -> Example: 0.2456
    float freeSpacePct = round((freeSpace * 100 / (float) totalSpace), 4);

    int diskReadDiff = 0;
    int diskWriteDiff = 0;
    int netReadDiff = 0;
    int netWriteDiff = 0;

    Optional<SystemInfo> systemInfo = plugin.getNativeData().getSystemInfo();
    if (systemInfo.isPresent()) {
        NetworkIF[] networkIfs = systemInfo.get().getHardware().getNetworkIFs();
        if (networkIfs.length > 0) {
            NetworkIF networkInterface = networkIfs[0];

            int netRead = LagUtils.byteToMega(networkInterface.getBytesRecv());
            netReadDiff = getDifference(netRead, lastNetRead, timeDiff);
            lastNetRead = netRead;

            int netWrite = LagUtils.byteToMega(networkInterface.getBytesSent());
            netWriteDiff = getDifference(netWrite, lastNetWrite, timeDiff);
            lastNetWrite = netWrite;
        }

        Path root = Paths.get(".").getRoot();
        Optional<OSProcess> optProcess = plugin.getNativeData().getProcess();
        if (root != null && optProcess.isPresent()) {
            OSProcess process = optProcess.get();
            String rootFileSystem = root.toAbsolutePath().toString();

            int diskRead = LagUtils.byteToMega(process.getBytesRead());
            diskReadDiff = getDifference(diskRead, lastDiskRead, timeDiff);
            lastDiskRead = diskRead;

            int diskWrite = LagUtils.byteToMega(process.getBytesWritten());
            diskWriteDiff = getDifference(diskWrite, lastDiskWrite, timeDiff);
            lastDiskWrite = diskWrite;
        }
    }

    lastCheck = currentTime;
    storage.saveNative(mcReadDiff, mcWriteDiff, freeSpace, freeSpacePct, diskReadDiff, diskWriteDiff
            , netReadDiff, netWriteDiff);
}
 
Example #11
Source File: NativeSaveTask.java    From LagMonitor with MIT License 4 votes vote down vote up
@Override
public void run() {
    Instant currentTime = Instant.now();
    int timeDiff = (int) Duration.between(lastCheck, currentTime).getSeconds();

    int mcReadDiff = 0;
    int mcWriteDiff = 0;

    TrafficReader trafficReader = plugin.getTrafficReader();
    if (trafficReader != null) {
        int mcRead = LagUtils.byteToMega(trafficReader.getIncomingBytes().longValue());
        mcReadDiff = getDifference(mcRead, lastMcRead, timeDiff);
        lastMcRead = mcRead;

        int mcWrite = LagUtils.byteToMega(trafficReader.getOutgoingBytes().longValue());
        mcWriteDiff = getDifference(mcWrite, lastMcWrite, timeDiff);
        lastMcWrite = mcWrite;
    }

    int totalSpace = LagUtils.byteToMega(plugin.getNativeData().getTotalSpace());
    int freeSpace = LagUtils.byteToMega(plugin.getNativeData().getFreeSpace());

    //4 decimal places -> Example: 0.2456
    float freeSpacePct = round((freeSpace * 100 / (float) totalSpace), 4);

    int diskReadDiff = 0;
    int diskWriteDiff = 0;
    int netReadDiff = 0;
    int netWriteDiff = 0;

    Optional<SystemInfo> systemInfo = plugin.getNativeData().getSystemInfo();
    if (systemInfo.isPresent()) {
        NetworkIF[] networkIfs = systemInfo.get().getHardware().getNetworkIFs();
        if (networkIfs.length > 0) {
            NetworkIF networkInterface = networkIfs[0];

            int netRead = LagUtils.byteToMega(networkInterface.getBytesRecv());
            netReadDiff = getDifference(netRead, lastNetRead, timeDiff);
            lastNetRead = netRead;

            int netWrite = LagUtils.byteToMega(networkInterface.getBytesSent());
            netWriteDiff = getDifference(netWrite, lastNetWrite, timeDiff);
            lastNetWrite = netWrite;
        }

        Path root = Paths.get(".").getRoot();
        Optional<OSProcess> optProcess = plugin.getNativeData().getProcess();
        if (root != null && optProcess.isPresent()) {
            OSProcess process = optProcess.get();
            String rootFileSystem = root.toAbsolutePath().toString();

            int diskRead = LagUtils.byteToMega(process.getBytesRead());
            diskReadDiff = getDifference(diskRead, lastDiskRead, timeDiff);
            lastDiskRead = diskRead;

            int diskWrite = LagUtils.byteToMega(process.getBytesWritten());
            diskWriteDiff = getDifference(diskWrite, lastDiskWrite, timeDiff);
            lastDiskWrite = diskWrite;
        }
    }

    lastCheck = currentTime;
    storage.saveNative(mcReadDiff, mcWriteDiff, freeSpace, freeSpacePct, diskReadDiff, diskWriteDiff
            , netReadDiff, netWriteDiff);
}