Skip to content

Commit

Permalink
Cache thread counters when updating OS Process with suspended state (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Apr 10, 2024
1 parent 276f4c4 commit 25071c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 6.5.1 (in progress)

* Your contribution here!
##### New Features
* [#2603](https://github.com/oshi/oshi/pull/2603): Add part number to Global Memory - [@BartekDziurowicz](https://github.com/BartekDziurowicz).

##### Bug fixes / Improvements
* [#2605](https://github.com/oshi/oshi/pull/2605): Reduce CpuStat.getSystemCpuLoadticks memory allocation pressure - [@chrisribble](https://github.com/chrisribble).
* [#2612](https://github.com/oshi/oshi/pull/2612): Use 1k buffer in FileUtils.readLines to reduce heap allocation pressure - [@chrisribble](https://github.com/chrisribble).
* [#2621](https://github.com/oshi/oshi/pull/2621): Cache thread counters when updating OS Process with suspended state - [@dbwiddis](https://github.com/dbwiddis).

# 6.5.0 (2024-03-10)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 The OSHI Project Contributors
* Copyright 2020-2024 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.software.os.windows;
Expand Down Expand Up @@ -85,6 +85,7 @@ public class WindowsOSProcess extends AbstractOSProcess {
private Supplier<List<String>> args = memoize(this::queryArguments);
private Supplier<Triplet<String, String, Map<String, String>>> cwdCmdEnv = memoize(
this::queryCwdCommandlineEnvironment);
private Map<Integer, ThreadPerformanceData.PerfCounterBlock> tcb;

private String name;
private String path;
Expand Down Expand Up @@ -112,7 +113,9 @@ public WindowsOSProcess(int pid, WindowsOperatingSystem os,
this.os = os;
// Initially set to match OS bitness. If 64 will check later for 32-bit process
this.bitness = os.getBitness();
updateAttributes(processMap.get(pid), processWtsMap.get(pid), threadMap);
// Initialize thread counters
this.tcb = threadMap;
updateAttributes(processMap.get(pid), processWtsMap.get(pid));
}

@Override
Expand Down Expand Up @@ -268,8 +271,10 @@ public long getMinorFaults() {

@Override
public List<OSThread> getThreadDetails() {
Map<Integer, ThreadPerformanceData.PerfCounterBlock> threads = ThreadPerformanceData
.buildThreadMapFromPerfCounters(Collections.singleton(this.getProcessID()), this.getName(), -1);
Map<Integer, ThreadPerformanceData.PerfCounterBlock> threads = tcb == null
? ThreadPerformanceData.buildThreadMapFromPerfCounters(Collections.singleton(this.getProcessID()),
this.getName(), -1)
: tcb;
return threads.entrySet().stream().parallel()
.map(entry -> new WindowsOSThread(getProcessID(), entry.getKey(), this.name, entry.getValue()))
.collect(Collectors.toList());
Expand All @@ -285,20 +290,18 @@ public boolean updateAttributes() {
if (pcb == null) {
pcb = ProcessPerformanceData.buildProcessMapFromPerfCounters(pids);
}
Map<Integer, ThreadPerformanceData.PerfCounterBlock> tcb = null;
if (USE_PROCSTATE_SUSPENDED) {
tcb = ThreadPerformanceData.buildThreadMapFromRegistry(null);
this.tcb = ThreadPerformanceData.buildThreadMapFromRegistry(null);
// otherwise performance counters with WMI backup
if (tcb == null) {
tcb = ThreadPerformanceData.buildThreadMapFromPerfCounters(null);
if (this.tcb == null) {
this.tcb = ThreadPerformanceData.buildThreadMapFromPerfCounters(null);
}
}
Map<Integer, WtsInfo> wts = ProcessWtsData.queryProcessWtsMap(pids);
return updateAttributes(pcb.get(this.getProcessID()), wts.get(this.getProcessID()), tcb);
return updateAttributes(pcb.get(this.getProcessID()), wts.get(this.getProcessID()));
}

private boolean updateAttributes(ProcessPerformanceData.PerfCounterBlock pcb, WtsInfo wts,
Map<Integer, ThreadPerformanceData.PerfCounterBlock> threadMap) {
private boolean updateAttributes(ProcessPerformanceData.PerfCounterBlock pcb, WtsInfo wts) {
this.name = pcb.getName();
this.path = wts.getPath(); // Empty string for Win7+
this.parentProcessID = pcb.getParentProcessID();
Expand All @@ -319,13 +322,13 @@ private boolean updateAttributes(ProcessPerformanceData.PerfCounterBlock pcb, Wt
// UNKNOWN. Processes are considered running unless all of their threads are
// SUSPENDED.
this.state = RUNNING;
if (threadMap != null) {
if (this.tcb != null) {
// If user hasn't enabled this in properties, we ignore
int pid = this.getProcessID();
// If any thread is NOT suspended, set running
for (ThreadPerformanceData.PerfCounterBlock tcb : threadMap.values()) {
if (tcb.getOwningProcessID() == pid) {
if (tcb.getThreadWaitReason() == 5) {
for (ThreadPerformanceData.PerfCounterBlock tpd : this.tcb.values()) {
if (tpd.getOwningProcessID() == pid) {
if (tpd.getThreadWaitReason() == 5) {
this.state = SUSPENDED;
} else {
this.state = RUNNING;
Expand Down

0 comments on commit 25071c7

Please sign in to comment.