All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.reflect.MetaSystem Maven / Gradle / Ivy

There is a newer version: 4.13.21
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nstream.reflect;

import com.sun.management.OperatingSystemMXBean;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import nstream.reflect.model.ProcessStats;
import nstream.reflect.model.SystemStats;
import swim.concurrent.AbstractTimer;
import swim.concurrent.TimerRef;

public class MetaSystem {
  final MetaEdge meta;
  final OperatingSystemMXBean operatingSystemMXBean;
  final RuntimeMXBean runtimeMXBean;
  volatile ProcessStats processStats;
  volatile SystemStats systemStats;
  TimerRef systemStatsTimer;

  public MetaSystem(MetaEdge meta) {
    this.meta = meta;
    this.operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    this.runtimeMXBean = ManagementFactory.getRuntimeMXBean();
  }

  public ProcessStats processStats() {
    return this.processStats;
  }

  public SystemStats systemStats() {
    return this.systemStats;
  }

  protected void updateSystemStats() {
    final SystemStats system = new SystemStats();
    final ProcessStats process = new ProcessStats();

    system.cpuTotal = 100 * this.operatingSystemMXBean.getAvailableProcessors();
    system.cpuUsage = (int) Math.round(this.operatingSystemMXBean.getProcessCpuLoad() * (double) system.cpuTotal);

    system.memTotal = this.operatingSystemMXBean.getTotalPhysicalMemorySize();
    system.memUsage = system.memTotal - this.operatingSystemMXBean.getFreePhysicalMemorySize();

    long diskTotal = 0L;
    long diskFree = 0L;
    try {
      for (Path root : FileSystems.getDefault().getRootDirectories()) {
        final FileStore store = Files.getFileStore(root);
        diskTotal += store.getTotalSpace();
        diskFree += store.getUsableSpace();
      }
    } catch (IOException swallow) {
      // nop
    }
    system.diskUsage = diskTotal - diskFree;
    system.diskTotal = diskTotal;
    system.startTime = this.runtimeMXBean.getStartTime();

    process.cpuTotal = system.cpuTotal;
    process.cpuUsage = system.cpuUsage;

    final Runtime runtime = Runtime.getRuntime();
    process.memTotal = runtime.totalMemory();
    process.memUsage = process.memTotal - runtime.freeMemory();

    this.processStats = process;
    this.systemStats = system;
  }

  public void open() {
    this.systemStatsTimer = this.meta.schedule().setTimer(POLL_INTERVAL, new MetaSystemStatsTimer(this));
  }

  public void close() {
    final TimerRef systemStatsTimer = this.systemStatsTimer;
    if (systemStatsTimer != null) {
      systemStatsTimer.cancel();
      this.systemStatsTimer = null;
    }
  }

  static final long POLL_INTERVAL = 1000L;
}

final class MetaSystemStatsTimer extends AbstractTimer {
  final MetaSystem system;

  MetaSystemStatsTimer(MetaSystem system) {
    this.system = system;
  }

  @Override
  public void runTimer() {
    this.system.updateSystemStats();
    this.system.meta.cueSystemStats();
    reschedule(MetaSystem.POLL_INTERVAL);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy