MetricsSnapshot.java

/**
* Copyright 2010,2021 Nikolas S Boyd.
Permission is granted to copy this work provided this copyright statement is retained in all copies.

*/
package Hoot.Metrics;

import Hoot.Runtime.Functions.*;
import Hoot.Runtime.Faces.*;
import Hoot.Runtime.Values.*;
import Hoot.Runtime.Blocks.*;
import Smalltalk.Core.*;
import Smalltalk.Blocks.*;
import Smalltalk.Magnitudes.*;
import Hoot.Behaviors.*;
import Hoot.Behaviors.Nil;
import Hoot.Behaviors.Object;
import Hoot.Behaviors.True;
import Hoot.Behaviors.False;
import Hoot.Behaviors.Boolean;
import Hoot.Magnitudes.*;
import Hoot.Magnitudes.Integer;
import Hoot.Magnitudes.Character;
import Hoot.Magnitudes.Float;
import Hoot.Magnitudes.Double;
import Hoot.Collections.*;
import Hoot.Collections.String;
import java.lang.Runtime;
import java.lang.management.*;
import javax.management.MBeanServer;
import Hoot.Streams.Transcript;
import static Hoot.Magnitudes.SmallInteger.*;
import Hoot.Collections.OrderedCollection;

public class MetricsSnapshot extends Object
{

  protected static final Runtime HostRuntime = Runtime.getRuntime();
    protected LongInteger currentTimeMsecs = LongInteger.from(System.currentTimeMillis());
    public LongInteger currentTimeMsecs() { return this.currentTimeMsecs; }
    public Object getCurrentTimeMsecs() { return (Object)this.currentTimeMsecs; }

    public MetricsSnapshot currentTimeMsecs(LongInteger value) { this.currentTimeMsecs = value; return this; }
    public void setCurrentTimeMsecs(Object value) { this.currentTimeMsecs = (LongInteger)value; }

  ;
    protected LongInteger currentProcessors = LongInteger.from(HostRuntime.availableProcessors());
    public LongInteger currentProcessors() { return this.currentProcessors; }
    public Object getCurrentProcessors() { return (Object)this.currentProcessors; }

    public MetricsSnapshot currentProcessors(LongInteger value) { this.currentProcessors = value; return this; }
    public void setCurrentProcessors(Object value) { this.currentProcessors = (LongInteger)value; }

  ;
    protected LongInteger currentTotalMemory = LongInteger.from(HostRuntime.totalMemory());
    public LongInteger currentTotalMemory() { return this.currentTotalMemory; }
    public Object getCurrentTotalMemory() { return (Object)this.currentTotalMemory; }

    public MetricsSnapshot currentTotalMemory(LongInteger value) { this.currentTotalMemory = value; return this; }
    public void setCurrentTotalMemory(Object value) { this.currentTotalMemory = (LongInteger)value; }

  ;
    protected LongInteger currentFreeMemory = LongInteger.from(HostRuntime.freeMemory());
    public LongInteger currentFreeMemory() { return this.currentFreeMemory; }
    public Object getCurrentFreeMemory() { return (Object)this.currentFreeMemory; }

    public MetricsSnapshot currentFreeMemory(LongInteger value) { this.currentFreeMemory = value; return this; }
    public void setCurrentFreeMemory(Object value) { this.currentFreeMemory = (LongInteger)value; }

  ;
    protected LongInteger currentMaxMemory = LongInteger.from(HostRuntime.maxMemory());
    public LongInteger currentMaxMemory() { return this.currentMaxMemory; }
    public Object getCurrentMaxMemory() { return (Object)this.currentMaxMemory; }

    public MetricsSnapshot currentMaxMemory(LongInteger value) { this.currentMaxMemory = value; return this; }
    public void setCurrentMaxMemory(Object value) { this.currentMaxMemory = (LongInteger)value; }

  ;
  protected static final MBeanServer Manager = ManagementFactory.getPlatformMBeanServer();
  protected static final String MemoryUsageReport = String.from("%s: %s - %s = %s bytes used with %s cores");

  /**
   * @return 
   */
  public   Integer coreCount()
  {
    java.lang.String exitID = "MetricsSnapshot>>coreCount";
    Frame f0 = new Frame(exitID);
    long coreCount = currentProcessors.longValue();
    if ((coreCount > 2)) {
       coreCount = (coreCount / 2);
    };
    return (Integer)LongInteger.from(coreCount);
  }

  /**
   * @return 
   */
  public   Transcript log()
  {
    java.lang.String exitID = "MetricsSnapshot>>log";
    Frame f0 = new Frame(exitID);
    return (Transcript)Transcript.log();
  }

  /**
   * @return 
   */
  public   MetricsSnapshot reportUsage(final String snapshotName)
  {
    java.lang.String exitID = "MetricsSnapshot>>reportUsage";
    Frame f0 = new Frame(exitID);
    LongInteger usedMemory = (currentTotalMemory.minus(currentFreeMemory));
    this.log().printLine(MemoryUsageReport.formatWith(snapshotName, currentTotalMemory.printString(), currentFreeMemory.printString(), usedMemory.printString(), this.coreCount().printString()));
    return (MetricsSnapshot)this;
  }

  /**
   * @return 
   */
  public   void reportHeapUsage()
  {
    java.lang.String exitID = "MetricsSnapshot>>reportHeapUsage";
    Frame f0 = new Frame(exitID);
    MemoryUsage nonh = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();

    MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    this.log().printLine(("nonH usage: " + nonh));
    this.log().printLine(("heap usage: " + heap));
  }
}