src/Pure/System/java_statistics.scala
author wenzelm
Mon, 22 Feb 2021 12:30:05 +0100
changeset 73273 17c28251fff0
parent 73164 e2132e1553a9
child 75393 87ebf5a50283
permissions -rw-r--r--
clarified signature: process_result timing from Isabelle/Scala;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72250
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/java_statistics.scala
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     3
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     4
Java runtime statistics.
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     6
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     8
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
     9
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    10
object Java_Statistics
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    11
{
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    12
  /* memory status */
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    13
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    14
  sealed case class Memory_Status(heap_size: Long, heap_free: Long)
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    15
  {
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    16
    def heap_used: Long = (heap_size - heap_free) max 0
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    17
    def heap_used_fraction: Double =
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    18
      if (heap_size == 0) 0.0 else heap_used.toDouble / heap_size
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    19
  }
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    20
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    21
  def memory_status(): Memory_Status =
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    22
  {
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    23
    val heap_size = Runtime.getRuntime.totalMemory()
73164
e2132e1553a9 proper heap_free;
wenzelm
parents: 72250
diff changeset
    24
    val heap_free = Runtime.getRuntime.freeMemory()
e2132e1553a9 proper heap_free;
wenzelm
parents: 72250
diff changeset
    25
    Memory_Status(heap_size, heap_free)
72250
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    26
  }
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    27
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    28
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    29
  /* JVM statistics */
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    30
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    31
  def jvm_statistics(): Properties.T =
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    32
  {
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    33
    val status = memory_status()
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    34
    val threads = Thread.activeCount()
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    35
    val workers = Isabelle_Thread.pool.getPoolSize
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    36
    val workers_active = Isabelle_Thread.pool.getActiveCount
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    37
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    38
    List(
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    39
      "java_heap_size" -> status.heap_size.toString,
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    40
      "java_heap_used" -> status.heap_used.toString,
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    41
      "java_threads_total" -> threads.toString,
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    42
      "java_workers_total" -> workers.toString,
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    43
      "java_workers_active" -> workers_active.toString)
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    44
  }
13976f92a2d0 clarified modules;
wenzelm
parents:
diff changeset
    45
}