src/Pure/ML/ml_profiling.scala
author wenzelm
Mon, 20 Mar 2023 10:59:27 +0100
changeset 77692 3e746e684f4b
parent 75393 87ebf5a50283
permissions -rw-r--r--
clarified operations for ML object sizes;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/ML/ml_profiling.scala
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     3
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73839
diff changeset
     4
ML profiling (via Poly/ML run-time system).
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     5
*/
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     6
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     7
package isabelle
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     8
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
     9
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    10
import java.util.Locale
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    11
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    12
import scala.collection.immutable.SortedMap
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    13
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    14
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73843
diff changeset
    15
object ML_Profiling {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73843
diff changeset
    16
  sealed case class Entry(name: String, count: Long) {
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    17
    def clean_name: Entry = copy(name = """-?\(\d+\).*$""".r.replaceAllIn(name, ""))
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    18
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    19
    def print: String =
73843
014b944f4972 tuned messages;
wenzelm
parents: 73840
diff changeset
    20
      String.format(Locale.ROOT, "%12d %s",
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    21
        count.asInstanceOf[AnyRef], name.asInstanceOf[AnyRef])
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    22
  }
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    23
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73843
diff changeset
    24
  sealed case class Report(kind: String, entries: List[Entry]) {
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    25
    def clean_name: Report = copy(entries = entries.map(_.clean_name))
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    26
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    27
    def total: Entry = Entry("TOTAL", entries.iterator.map(_.count).sum)
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    28
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    29
    def print: String =
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73839
diff changeset
    30
      ("profile_" + kind + ":\n") + cat_lines((entries ::: List(total)).map(_.print))
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    31
  }
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    32
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73843
diff changeset
    33
  def account(reports: List[Report]): List[Report] = {
73836
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    34
    val empty = SortedMap.empty[String, Long].withDefaultValue(0L)
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    35
    var results = SortedMap.empty[String, SortedMap[String, Long]].withDefaultValue(empty)
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    36
    for (report <- reports) {
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    37
      val kind = report.kind
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    38
      val map = report.entries.foldLeft(results(kind))(
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    39
        (m, e) => m + (e.name -> (e.count + m(e.name))))
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    40
      results = results + (kind -> map)
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    41
    }
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    42
    for ((kind, map) <- results.toList)
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    43
      yield Report(kind, for ((name, count) <- map.toList.sortBy(_._2)) yield Entry(name, count))
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    44
  }
690fdc14f7fb add missing file;
wenzelm
parents:
diff changeset
    45
}