recon_to_file.exs
· 927 B · Elixir
Raw
spawn(fn ->
log_file = "vm_top_processes.log"
loop = fn loop ->
timestamp = DateTime.utc_now() |> DateTime.to_string()
top_cpu = :recon.proc_count(:redu, 5)
top_mailbox = :recon.proc_count(:message_queue_len, 5)
format_proc = fn {pid, value, info} ->
name = case info[:registered_name] do
[] -> inspect(pid)
nil -> inspect(pid)
n -> "#{n} (#{inspect(pid)})"
end
" #{name} | #{info[:current_function] |> inspect()} | value=#{value}\n"
end
cpu_lines = Enum.map_join(top_cpu, "", format_proc)
mailbox_lines = Enum.map_join(top_mailbox, "", format_proc)
entry = """
========== #{timestamp} ==========
TOP 5 CPU TIME (reductions):
#{cpu_lines}
TOP 5 MAILBOX LENGTH:
#{mailbox_lines}
"""
File.write!(log_file, entry, [:append])
Process.sleep(15_000)
loop.(loop)
end
loop.(loop)
end)
| 1 | spawn(fn -> |
| 2 | log_file = "vm_top_processes.log" |
| 3 | |
| 4 | loop = fn loop -> |
| 5 | timestamp = DateTime.utc_now() |> DateTime.to_string() |
| 6 | |
| 7 | top_cpu = :recon.proc_count(:redu, 5) |
| 8 | top_mailbox = :recon.proc_count(:message_queue_len, 5) |
| 9 | |
| 10 | format_proc = fn {pid, value, info} -> |
| 11 | name = case info[:registered_name] do |
| 12 | [] -> inspect(pid) |
| 13 | nil -> inspect(pid) |
| 14 | n -> "#{n} (#{inspect(pid)})" |
| 15 | end |
| 16 | " #{name} | #{info[:current_function] |> inspect()} | value=#{value}\n" |
| 17 | end |
| 18 | |
| 19 | cpu_lines = Enum.map_join(top_cpu, "", format_proc) |
| 20 | mailbox_lines = Enum.map_join(top_mailbox, "", format_proc) |
| 21 | |
| 22 | entry = """ |
| 23 | ========== #{timestamp} ========== |
| 24 | TOP 5 CPU TIME (reductions): |
| 25 | #{cpu_lines} |
| 26 | TOP 5 MAILBOX LENGTH: |
| 27 | #{mailbox_lines} |
| 28 | """ |
| 29 | |
| 30 | File.write!(log_file, entry, [:append]) |
| 31 | Process.sleep(15_000) |
| 32 | loop.(loop) |
| 33 | end |
| 34 | |
| 35 | loop.(loop) |
| 36 | end) |
| 37 |