src/Pure/ML/ml_profiling.ML
author wenzelm
Thu, 15 Aug 2024 12:43:17 +0200
changeset 80712 05b16602a683
parent 73843 014b944f4972
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62508
d0b68218ea55 discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents: 62501
diff changeset
     1
(*  Title:      Pure/ML/ml_profiling.ML
61715
5dc95d957569 speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
5dc95d957569 speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff changeset
     3
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
     4
ML profiling (via Poly/ML run-time system).
61715
5dc95d957569 speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff changeset
     5
*)
5dc95d957569 speculative support for polyml-5.6, according to git commit 3527f4ba7b8b;
wenzelm
parents:
diff changeset
     6
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
     7
signature BASIC_ML_PROFILING =
62824
3498c66b5e55 proper signature;
wenzelm
parents: 62508
diff changeset
     8
sig
73834
364bac6691de clarified modules;
wenzelm
parents: 62945
diff changeset
     9
  val profile_time: ('a -> 'b) -> 'a -> 'b
364bac6691de clarified modules;
wenzelm
parents: 62945
diff changeset
    10
  val profile_time_thread: ('a -> 'b) -> 'a -> 'b
364bac6691de clarified modules;
wenzelm
parents: 62945
diff changeset
    11
  val profile_allocations: ('a -> 'b) -> 'a -> 'b
62824
3498c66b5e55 proper signature;
wenzelm
parents: 62508
diff changeset
    12
end;
3498c66b5e55 proper signature;
wenzelm
parents: 62508
diff changeset
    13
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    14
signature ML_PROFILING =
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    15
sig
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    16
  val check_mode: string -> unit
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    17
  val profile: string -> ('a -> 'b) -> 'a -> 'b
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    18
  include BASIC_ML_PROFILING
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    19
end;
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    20
62824
3498c66b5e55 proper signature;
wenzelm
parents: 62508
diff changeset
    21
structure ML_Profiling: ML_PROFILING =
61885
acdfc76a6c33 more explicit ML profiling, with official Isabelle output;
wenzelm
parents: 61715
diff changeset
    22
struct
acdfc76a6c33 more explicit ML profiling, with official Isabelle output;
wenzelm
parents: 61715
diff changeset
    23
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    24
(* mode *)
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    25
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    26
val modes =
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    27
  Symtab.make
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    28
    [("time", PolyML.Profiling.ProfileTime),
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    29
     ("time_thread", PolyML.Profiling.ProfileTimeThisThread),
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    30
     ("allocations", PolyML.Profiling.ProfileAllocations)];
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    31
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    32
fun get_mode kind =
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    33
  (case Symtab.lookup modes kind of
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    34
    SOME mode => mode
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    35
  | NONE => error ("Bad profiling mode: " ^ quote kind));
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    36
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    37
fun check_mode "" = ()
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    38
  | check_mode kind = ignore (get_mode kind);
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    39
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    40
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    41
(* profile *)
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    42
73835
5dae03d50db1 more formal ML profiling messages;
wenzelm
parents: 73834
diff changeset
    43
fun print_entry count name =
5dae03d50db1 more formal ML profiling messages;
wenzelm
parents: 73834
diff changeset
    44
  let
5dae03d50db1 more formal ML profiling messages;
wenzelm
parents: 73834
diff changeset
    45
    val c = string_of_int count;
73843
014b944f4972 tuned messages;
wenzelm
parents: 73840
diff changeset
    46
    val prefix = Symbol.spaces (Int.max (0, 12 - size c));
73835
5dae03d50db1 more formal ML profiling messages;
wenzelm
parents: 73834
diff changeset
    47
  in prefix ^ c ^ " " ^ name end;
5dae03d50db1 more formal ML profiling messages;
wenzelm
parents: 73834
diff changeset
    48
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    49
fun profile "" f x = f x
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    50
  | profile kind f x =
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    51
      let
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    52
        val mode = get_mode kind;
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    53
        fun output entries =
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    54
          (case fold (curry (op +) o fst) entries 0 of
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    55
            0 => ()
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    56
          | total =>
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    57
              let
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    58
                val body = entries
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    59
                  |> sort (int_ord o apply2 fst)
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    60
                  |> map (fn (count, name) =>
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    61
                      let val markup = Markup.ML_profiling_entry {name = name, count = count}
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    62
                      in XML.Elem (markup, [XML.Text (print_entry count name ^ "\n")]) end);
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    63
                val head = XML.Text ("profile_" ^ kind ^ ":\n");
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    64
                val foot = XML.Text (print_entry total "TOTAL");
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    65
                val msg = XML.Elem (Markup.ML_profiling kind, head :: body @ [foot]);
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    66
              in tracing (YXML.string_of msg) end);
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    67
      in PolyML.Profiling.profileStream output mode f x end;
61885
acdfc76a6c33 more explicit ML profiling, with official Isabelle output;
wenzelm
parents: 61715
diff changeset
    68
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    69
fun profile_time f = profile "time" f;
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    70
fun profile_time_thread f = profile "time_thread" f;
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    71
fun profile_allocations f = profile "allocations" f;
61885
acdfc76a6c33 more explicit ML profiling, with official Isabelle output;
wenzelm
parents: 61715
diff changeset
    72
acdfc76a6c33 more explicit ML profiling, with official Isabelle output;
wenzelm
parents: 61715
diff changeset
    73
end;
73834
364bac6691de clarified modules;
wenzelm
parents: 62945
diff changeset
    74
73840
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    75
structure Basic_ML_Profiling: BASIC_ML_PROFILING = ML_Profiling;
a5200fa7cb4c more systematic treatment of profiling mode;
wenzelm
parents: 73838
diff changeset
    76
open Basic_ML_Profiling;