• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #272

21 Jun 2024 08:17PM UTC coverage: 77.548% (+0.04%) from 77.506%
#272

push

github

web-flow
Resource based tuner (#2110)

275 of 338 new or added lines in 11 files covered. (81.36%)

12 existing lines in 5 files now uncovered.

19522 of 25174 relevant lines covered (77.55%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/temporal-sdk/src/main/java/io/temporal/worker/tuning/JVMSystemResourceInfo.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.worker.tuning;
22

23
import com.sun.management.OperatingSystemMXBean;
24
import io.temporal.common.Experimental;
25
import java.lang.management.ManagementFactory;
26
import java.time.Instant;
27
import java.util.concurrent.locks.Lock;
28
import java.util.concurrent.locks.ReentrantLock;
29

30
/** {@link SystemResourceInfo} implementation that uses JVM-specific APIs to get resource usage. */
31
@Experimental
32
public class JVMSystemResourceInfo implements SystemResourceInfo {
1✔
33
  // As of relatively recent Java versions (including backports), this class will properly deal with
34
  // containerized environments as well as running on bare metal.
35
  // See https://bugs.openjdk.org/browse/JDK-8226575 for more details on which versions the fixes
36
  // have been backported to.
37
  OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
1✔
38

39
  private final Lock refreshLock = new ReentrantLock();
1✔
40
  private SystemInfo lastSystemInfo;
41

42
  @Override
43
  public double getCPUUsagePercent() {
44
    return refresh().cpuUsagePercent;
1✔
45
  }
46

47
  @Override
48
  public double getMemoryUsagePercent() {
49
    return refresh().memoryUsagePercent;
1✔
50
  }
51

52
  @SuppressWarnings("deprecation") // deprecated APIs needed since replacements are for Java 14+
53
  private SystemInfo refresh() {
54

55
    refreshLock.lock();
1✔
56
    try {
57
      if (lastSystemInfo == null
1✔
58
          || Instant.now().isAfter(lastSystemInfo.refreshed.plusMillis(100))) {
1✔
59
        // This can return NaN seemingly when usage is very low
60
        double lastCpuUsage = osBean.getSystemCpuLoad();
1✔
61
        if (lastCpuUsage < 0 || Double.isNaN(lastCpuUsage)) {
1✔
NEW
62
          lastCpuUsage = 0;
×
63
        }
64

65
        Runtime runtime = Runtime.getRuntime();
1✔
66
        long jvmUsedMemory = runtime.totalMemory() - runtime.freeMemory();
1✔
67
        long jvmMaxMemory = runtime.maxMemory();
1✔
68

69
        double lastMemUsage = ((double) jvmUsedMemory / jvmMaxMemory);
1✔
70
        Instant lastRefresh = Instant.now();
1✔
71
        lastSystemInfo = new SystemInfo(lastRefresh, lastCpuUsage, lastMemUsage);
1✔
72
      }
73
    } finally {
74
      refreshLock.unlock();
1✔
75
    }
76

77
    return lastSystemInfo;
1✔
78
  }
79

80
  private static class SystemInfo {
81
    private final Instant refreshed;
82
    private final double cpuUsagePercent;
83
    private final double memoryUsagePercent;
84

85
    private SystemInfo(Instant refreshed, double cpuUsagePercent, double memoryUsagePercent) {
1✔
86
      this.refreshed = refreshed;
1✔
87
      this.cpuUsagePercent = cpuUsagePercent;
1✔
88
      this.memoryUsagePercent = memoryUsagePercent;
1✔
89
    }
1✔
90
  }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc