Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
Created August 5, 2021 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlipscombe/15fd66afc80064f00e44994cb742aa91 to your computer and use it in GitHub Desktop.
Save rlipscombe/15fd66afc80064f00e44994cb742aa91 to your computer and use it in GitHub Desktop.
Processes to graphviz
def to_dot(pid, path) do
# Starting with the given process, find all of the processes that it's linked to/from, and that it's monitoring/monitored; put them in the dot file.
f = File.open!(path, [:write])
IO.write(f, "digraph G {\n")
IO.write(f, "concentrate=true")
seen = MapSet.new()
to_dot(pid, f, seen)
IO.write(f, "}\n")
File.close(f)
end
defp to_dot(pid, f, seen) do
if MapSet.member?(seen, pid) do
:ok
else
seen = MapSet.put(seen, pid)
IO.write(f, "\"#{inspect(pid)}\"\n")
info = Process.info(pid, [:links, :monitors, :monitored_by])
for link when is_pid(link) <- info[:links] do
IO.write(f, "\"#{inspect(pid)}\" -> \"#{inspect(link)}\"\n")
to_dot(link, f, seen)
end
for {:process, m} <- info[:monitors] do
IO.write(f, "\"#{inspect(pid)}\" -> \"#{inspect(m)}\" [style=dashed]\n")
to_dot(m, f, seen)
end
# for {:process, m} <- info[:monitored_by] do
# IO.write(f, "\"#{inspect(m)}\" -> \"#{inspect(pid)}\" [style=dashed]\n")
# to_dot(m, f, seen)
# end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment