From 0674823957862aa3db7363de6bf0979340282ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Tue, 11 Jun 2024 17:42:08 -0300 Subject: [PATCH 01/14] Adds support for OTP 27 process labels in Kino.Process diagrams --- lib/kino/process.ex | 71 ++++++++++++++++++++++++---- lib/kino/process/label_tracer.ex | 29 ++++++++++++ test/kino/process_test.exs | 81 ++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 9 deletions(-) create mode 100644 lib/kino/process/label_tracer.ex diff --git a/lib/kino/process.ex b/lib/kino/process.ex index 5e97106b..7ecb5635 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -6,12 +6,13 @@ defmodule Kino.Process do alias Kino.Mermaid alias Kino.Process.Tracer + alias Kino.Process.LabelTracer @mermaid_classdefs """ - classDef root fill:#c4b5fd, stroke:#374151, stroke-width:4px; - classDef supervisor fill:#c4b5fd, stroke:#374151, stroke-width:1px; - classDef worker fill:#66c2a5, stroke:#374151, stroke-width:1px; - classDef notstarted color:#777, fill:#d9d9d9, stroke:#777, stroke-width:1px; + classDef root fill:#c4b5fd, stroke:#374151, stroke-width:4px, line-height:1.5em; + classDef supervisor fill:#c4b5fd, stroke:#374151, stroke-width:1px, line-height:1.5em; + classDef worker fill:#66c2a5, stroke:#374151, stroke-width:1px, line-height:1.5em; + classDef notstarted color:#777, fill:#d9d9d9, stroke:#777, stroke-width:1px, line-height:1.5em; classDef ets fill:#a5f3fc, stroke:#374151, stroke-width:1px; """ @@ -315,6 +316,9 @@ defmodule Kino.Process do def seq_trace(trace_pids, trace_function, opts) when is_list(trace_pids) or trace_pids == :all do + # Set up the tracer responsible for storing calls to Process.set_label/1 + {:ok, process_label_tracer_pid} = LabelTracer.start_link() + # Set up the process message tracer and the Erlang seq_trace_module calling_pid = self() {:ok, tracer_pid} = Tracer.start_link() @@ -374,13 +378,17 @@ defmodule Kino.Process do |> maybe_add_participant(to) end) + process_labels = LabelTracer.get_process_labels(process_label_tracer_pid) + GenServer.stop(process_label_tracer_pid) + # Generate the Mermaid formatted list of participants participants = Enum.map_join(participants_lookup, "\n", fn {pid, idx} -> if pid == calling_pid do "participant #{idx} AS self();" else - generate_participant_entry(pid, idx) + process_label = Map.get(process_labels, pid, :undefined) + generate_participant_entry(pid, idx, process_label) end end) @@ -413,6 +421,7 @@ defmodule Kino.Process do sequence_diagram = Mermaid.new(""" + %%{init: {'themeCSS': '.actor:last-of-type:not(:only-of-type) {dominant-baseline: hanging;}'} }%% sequenceDiagram #{participants} #{messages} @@ -421,15 +430,37 @@ defmodule Kino.Process do {func_result, sequence_diagram} end - defp generate_participant_entry(pid, idx) do + defp get_label(pid) do + # :proc_lib.get_label/1 was added in OTP 27 + case function_exported?(:proc_lib, :get_label, 1) do + true -> :proc_lib.get_label(pid) + false -> :undefined + end + end + + defp generate_participant_entry(pid, idx, process_label) do try do {:registered_name, name} = process_info(pid, :registered_name) "participant #{idx} AS #{module_or_atom_to_string(name)};" rescue - _ -> "participant #{idx} AS #35;PID#{:erlang.pid_to_list(pid)};" + _ -> + case process_label do + :undefined -> + "participant #{idx} AS #35;PID#{:erlang.pid_to_list(pid)};" + + process_label -> + "participant #{idx} AS #{format_for_mermaid_participant_alias(pid, process_label)};" + end end end + defp format_for_mermaid_participant_alias(pid, process_label) do + %{"pid" => pid_text} = Regex.named_captures(~r/#PID(?.*)/, inspect(pid)) + + participant_alias = "#{inspect(process_label)}
#{pid_text}" + String.replace(participant_alias, "\"", "") + end + defp maybe_add_participant({participants, idx}, pid) when is_pid(pid) do if Map.has_key?(participants, pid) do {participants, idx} @@ -740,13 +771,35 @@ defmodule Kino.Process do display = case process_info(pid, :registered_name) do - {:registered_name, []} -> inspect(pid) - {:registered_name, name} -> module_or_atom_to_string(name) + {:registered_name, []} -> + inspect(pid) + + case get_label(pid) do + :undefined -> inspect(pid) + process_label -> format_for_mermaid_graph_node(pid, process_label) + end + + {:registered_name, name} -> + module_or_atom_to_string(name) end "#{idx}(#{display}):::#{type}" end + defp format_for_mermaid_graph_node(pid, process_label) do + %{"pid" => pid_text} = Regex.named_captures(~r/#PID(?.*)/, inspect(pid)) + + "#{inspect(process_label)}
#{pid_text}" + |> String.replace("\"", "") + |> format_as_mermaid_unicode_text() + end + + # this is needed to use unicode inside node's text + # (https://mermaid.js.org/syntax/flowchart.html#unicode-text) + defp format_as_mermaid_unicode_text(node_text) do + "\"#{node_text}\"" + end + defp module_or_atom_to_string(atom) do case Atom.to_string(atom) do "Elixir." <> rest -> rest diff --git a/lib/kino/process/label_tracer.ex b/lib/kino/process/label_tracer.ex new file mode 100644 index 00000000..e95b685f --- /dev/null +++ b/lib/kino/process/label_tracer.ex @@ -0,0 +1,29 @@ +defmodule Kino.Process.LabelTracer do + use GenServer + + def start_link() do + GenServer.start_link(__MODULE__, %{}) + end + + def get_process_labels(pid) do + GenServer.call(pid, :get_process_labels) + end + + @impl true + def init(_) do + :erlang.trace(:all, true, [:call, {:tracer, self()}]) + :erlang.trace_pattern({Process, :set_label, 1}, true, [:local]) + {:ok, %{}} + end + + @impl true + def handle_info({:trace, pid, :call, {Process, :set_label, [process_label]}}, state) do + state = Map.put(state, pid, process_label) + {:noreply, state} + end + + @impl true + def handle_call(:get_process_labels, _from, state) do + {:reply, state, state} + end +end diff --git a/test/kino/process_test.exs b/test/kino/process_test.exs index 2e660659..1f18ad96 100644 --- a/test/kino/process_test.exs +++ b/test/kino/process_test.exs @@ -114,6 +114,39 @@ defmodule Kino.ProcessTest do assert content =~ "0(supervisor_parent):::root ---> 2(#{inspect(agent)}):::worker" end + # Process.set_label/1 was addeed in Elixir 1.17.0 + if function_exported?(Process, :set_label, 1) do + test "uses process label in the diagram to identify a process" do + process_label = "my task" + + supervisor = + start_supervised!(%{ + id: Supervisor, + start: + {Supervisor, :start_link, + [ + [ + {Task, + fn -> + Process.set_label(process_label) + Process.sleep(:infinity) + end} + ], + [name: :supervisor_parent, strategy: :one_for_one] + ]} + }) + + [{_, task, _, _}] = Supervisor.which_children(supervisor) + + diagram = Kino.Process.sup_tree(supervisor) |> mermaid() + + %{"pid" => pid_text} = Regex.named_captures(~r/#PID(?.*)/, inspect(task)) + + assert diagram =~ + "0(supervisor_parent):::root ---> 1(\"#{process_label}
#{pid_text}\"):::worker" + end + end + test "raises if supervisor does not exist" do assert_raise ArgumentError, ~r/the provided identifier :not_a_valid_supervisor does not reference a running process/, @@ -121,6 +154,54 @@ defmodule Kino.ProcessTest do end end + describe "seq_trace/2" do + # Process.set_label/1 was addeed in Elixir 1.17.0 + if function_exported?(Process, :set_label, 1) do + test "uses process label to identify a process" do + parent = self() + process_label = "ponger" + + trace_function = fn -> + child = + spawn(fn -> + Process.set_label(process_label) + + receive do + :ping -> + send(parent, :pong) + end + end) + + send(child, :ping) + + receive do + :pong -> :ponged! + end + end + + {_func_result, diagram} = Kino.Process.seq_trace(trace_function) + diagram = mermaid(diagram) + + assert diagram =~ ~r/participant 1 AS #{process_label}<0.\d+.0>;/ + end + end + end + + defmodule Ponger do + def start_link(process_label) do + Task.start_link(fn -> ponger(process_label) end) + end + + defp ponger(process_label) do + Process.set_label(process_label) + + receive do + {:ping, from} -> + send(from, :pong) + end + end + end + defp mermaid(%Kino.JS{ref: ref}) do send(Kino.JS.DataStore, {:connect, self(), %{origin: "client:#{inspect(self())}", ref: ref}}) assert_receive {:connect_reply, data, %{ref: ^ref}} From 07f90b1ff2b13d1d1f65b488300ef525fb37da8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Wed, 12 Jun 2024 10:42:18 -0300 Subject: [PATCH 02/14] Remove warning when trying to call :proc_lib.get_labe/1 --- lib/kino/process.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/kino/process.ex b/lib/kino/process.ex index 7ecb5635..36ec27d9 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -430,12 +430,11 @@ defmodule Kino.Process do {func_result, sequence_diagram} end - defp get_label(pid) do - # :proc_lib.get_label/1 was added in OTP 27 - case function_exported?(:proc_lib, :get_label, 1) do - true -> :proc_lib.get_label(pid) - false -> :undefined - end + # :proc_lib.get_label/1 was added in OTP 27 + if Code.ensure_loaded?(:proc_lib) and function_exported?(:proc_lib, :get_label, 1) do + defp get_label(pid), do: :proc_lib.get_label(pid) + else + defp get_label(_pid), do: :undefined end defp generate_participant_entry(pid, idx, process_label) do From f9622ab10482c34dd58a77c9745f7d56c6b5ef7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Wed, 12 Jun 2024 10:49:49 -0300 Subject: [PATCH 03/14] Refactor transforming pid to string --- lib/kino/process.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kino/process.ex b/lib/kino/process.ex index 36ec27d9..91b3dfe1 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -454,7 +454,7 @@ defmodule Kino.Process do end defp format_for_mermaid_participant_alias(pid, process_label) do - %{"pid" => pid_text} = Regex.named_captures(~r/#PID(?.*)/, inspect(pid)) + pid_text = :erlang.pid_to_list(pid) |> List.to_string() participant_alias = "#{inspect(process_label)}
#{pid_text}" String.replace(participant_alias, "\"", "") @@ -786,7 +786,7 @@ defmodule Kino.Process do end defp format_for_mermaid_graph_node(pid, process_label) do - %{"pid" => pid_text} = Regex.named_captures(~r/#PID(?.*)/, inspect(pid)) + pid_text = :erlang.pid_to_list(pid) |> List.to_string() "#{inspect(process_label)}
#{pid_text}" |> String.replace("\"", "") From 83c663edfacb840291d53fa1df9062c5d47c4560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Wed, 12 Jun 2024 10:50:43 -0300 Subject: [PATCH 04/14] Delete unused module --- test/kino/process_test.exs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/kino/process_test.exs b/test/kino/process_test.exs index 1f18ad96..8266ba18 100644 --- a/test/kino/process_test.exs +++ b/test/kino/process_test.exs @@ -187,21 +187,6 @@ defmodule Kino.ProcessTest do end end - defmodule Ponger do - def start_link(process_label) do - Task.start_link(fn -> ponger(process_label) end) - end - - defp ponger(process_label) do - Process.set_label(process_label) - - receive do - {:ping, from} -> - send(from, :pong) - end - end - end - defp mermaid(%Kino.JS{ref: ref}) do send(Kino.JS.DataStore, {:connect, self(), %{origin: "client:#{inspect(self())}", ref: ref}}) assert_receive {:connect_reply, data, %{ref: ^ref}} From 0bdbf766311de0a1d7565fe54ec6ca9f1491eb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Wed, 12 Jun 2024 11:00:20 -0300 Subject: [PATCH 05/14] Forgot to delete inspect --- lib/kino/process.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/kino/process.ex b/lib/kino/process.ex index 91b3dfe1..b9f86037 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -771,8 +771,6 @@ defmodule Kino.Process do display = case process_info(pid, :registered_name) do {:registered_name, []} -> - inspect(pid) - case get_label(pid) do :undefined -> inspect(pid) process_label -> format_for_mermaid_graph_node(pid, process_label) From e3942ee8d9ac60c60092e933c9126177048a8edb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 10:10:08 -0300 Subject: [PATCH 06/14] Get process labels during using the already existent seqtrace --- lib/kino/process.ex | 11 ++------ lib/kino/process/label_tracer.ex | 29 -------------------- lib/kino/process/tracer.ex | 34 +++++++++++++++++++----- test/kino/process_test.exs | 45 +++++++++++++++++++++----------- 4 files changed, 59 insertions(+), 60 deletions(-) delete mode 100644 lib/kino/process/label_tracer.ex diff --git a/lib/kino/process.ex b/lib/kino/process.ex index b9f86037..0cfb4230 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -6,7 +6,6 @@ defmodule Kino.Process do alias Kino.Mermaid alias Kino.Process.Tracer - alias Kino.Process.LabelTracer @mermaid_classdefs """ classDef root fill:#c4b5fd, stroke:#374151, stroke-width:4px, line-height:1.5em; @@ -316,9 +315,6 @@ defmodule Kino.Process do def seq_trace(trace_pids, trace_function, opts) when is_list(trace_pids) or trace_pids == :all do - # Set up the tracer responsible for storing calls to Process.set_label/1 - {:ok, process_label_tracer_pid} = LabelTracer.start_link() - # Set up the process message tracer and the Erlang seq_trace_module calling_pid = self() {:ok, tracer_pid} = Tracer.start_link() @@ -328,7 +324,7 @@ defmodule Kino.Process do previous_tracer = :seq_trace.set_system_tracer(tracer_pid) # Run the user supplied function and capture the events if no errors were encountered - {raw_trace_events, func_result} = + {%{raw_trace_events: raw_trace_events, process_labels: process_labels}, func_result} = try do func_result = try do @@ -340,7 +336,7 @@ defmodule Kino.Process do :seq_trace.reset_trace() end - {Tracer.get_trace_events(tracer_pid), func_result} + {Tracer.get_trace_info(tracer_pid), func_result} after # The Tracer GenServer is no longer needed, shut it down GenServer.stop(tracer_pid) @@ -378,9 +374,6 @@ defmodule Kino.Process do |> maybe_add_participant(to) end) - process_labels = LabelTracer.get_process_labels(process_label_tracer_pid) - GenServer.stop(process_label_tracer_pid) - # Generate the Mermaid formatted list of participants participants = Enum.map_join(participants_lookup, "\n", fn {pid, idx} -> diff --git a/lib/kino/process/label_tracer.ex b/lib/kino/process/label_tracer.ex deleted file mode 100644 index e95b685f..00000000 --- a/lib/kino/process/label_tracer.ex +++ /dev/null @@ -1,29 +0,0 @@ -defmodule Kino.Process.LabelTracer do - use GenServer - - def start_link() do - GenServer.start_link(__MODULE__, %{}) - end - - def get_process_labels(pid) do - GenServer.call(pid, :get_process_labels) - end - - @impl true - def init(_) do - :erlang.trace(:all, true, [:call, {:tracer, self()}]) - :erlang.trace_pattern({Process, :set_label, 1}, true, [:local]) - {:ok, %{}} - end - - @impl true - def handle_info({:trace, pid, :call, {Process, :set_label, [process_label]}}, state) do - state = Map.put(state, pid, process_label) - {:noreply, state} - end - - @impl true - def handle_call(:get_process_labels, _from, state) do - {:reply, state, state} - end -end diff --git a/lib/kino/process/tracer.ex b/lib/kino/process/tracer.ex index 77448229..0d4d0079 100644 --- a/lib/kino/process/tracer.ex +++ b/lib/kino/process/tracer.ex @@ -7,22 +7,22 @@ defmodule Kino.Process.Tracer do GenServer.start_link(__MODULE__, nil) end - def get_trace_events(tracer) do - GenServer.call(tracer, :get_trace_events) + def get_trace_info(tracer) do + GenServer.call(tracer, :get_trace_info) end @impl true def init(_) do - {:ok, []} + {:ok, %{raw_trace_events: [], process_labels: %{}}} end @impl true - def handle_call(:get_trace_events, _from, trace_events) do - {:reply, trace_events, trace_events} + def handle_call(:get_trace_info, _from, trace_info) do + {:reply, trace_info, trace_info} end @impl true - def handle_info({:seq_trace, _, {:send, _, from, to, message}, timestamp}, trace_events) do + def handle_info({:seq_trace, _, {:send, _, from, to, message}, timestamp}, trace_info) do new_event = %{ type: :send, timestamp: timestamp, @@ -31,10 +31,30 @@ defmodule Kino.Process.Tracer do message: message } - {:noreply, [new_event | trace_events]} + trace_events = [new_event | trace_info.raw_trace_events] + process_labels = update_process_labels(trace_info.process_labels, from, to) + + {:noreply, %{trace_info | raw_trace_events: trace_events, process_labels: process_labels}} end def handle_info(_ignored_event, trace_events) do {:noreply, trace_events} end + + defp update_process_labels(process_labels, pid1, pid2) do + process_labels + |> put_new_label(pid1) + |> put_new_label(pid2) + end + + defp put_new_label(process_labels, pid) do + Map.put_new(process_labels, pid, get_label(pid)) + end + + # :proc_lib.get_label/1 was added in OTP 27 + if Code.ensure_loaded?(:proc_lib) and function_exported?(:proc_lib, :get_label, 1) do + defp get_label(pid), do: :proc_lib.get_label(pid) + else + defp get_label(_pid), do: :undefined + end end diff --git a/test/kino/process_test.exs b/test/kino/process_test.exs index 8266ba18..016810cf 100644 --- a/test/kino/process_test.exs +++ b/test/kino/process_test.exs @@ -158,35 +158,50 @@ defmodule Kino.ProcessTest do # Process.set_label/1 was addeed in Elixir 1.17.0 if function_exported?(Process, :set_label, 1) do test "uses process label to identify a process" do - parent = self() process_label = "ponger" + ponger = start_supervised!({Kino.ProcessTest.Ponger, [label: process_label]}) - trace_function = fn -> - child = - spawn(fn -> - Process.set_label(process_label) - - receive do - :ping -> - send(parent, :pong) - end - end) - - send(child, :ping) + traced_function = fn -> + send(ponger, {:ping, self()}) receive do :pong -> :ponged! end end - {_func_result, diagram} = Kino.Process.seq_trace(trace_function) + {_func_result, diagram} = Kino.Process.seq_trace(traced_function) diagram = mermaid(diagram) - assert diagram =~ ~r/participant 1 AS #{process_label}<0.\d+.0>;/ + ponger_pid = :erlang.pid_to_list(ponger) |> List.to_string() + assert diagram =~ ~r/participant 1 AS #{process_label}#{ponger_pid};/ end end end + defmodule Ponger do + use GenServer + + def start_link(opts) do + GenServer.start_link(__MODULE__, opts) + end + + # Process.set_label/1 was addeed in Elixir 1.17.0 + @compile {:no_warn_undefined, {Process, :set_label, 1}} + @impl true + def init(opts) do + Process.set_label(opts[:label]) + + {:ok, nil} + end + + @impl true + def handle_info({:ping, from}, state) do + send(from, :pong) + + {:noreply, state} + end + end + defp mermaid(%Kino.JS{ref: ref}) do send(Kino.JS.DataStore, {:connect, self(), %{origin: "client:#{inspect(self())}", ref: ref}}) assert_receive {:connect_reply, data, %{ref: ^ref}} From 40011c50ae387cf8393e278c8d79967166631b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 10:38:03 -0300 Subject: [PATCH 07/14] Update lib/kino/process.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- lib/kino/process.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kino/process.ex b/lib/kino/process.ex index 0cfb4230..c2f318a6 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -423,7 +423,7 @@ defmodule Kino.Process do {func_result, sequence_diagram} end - # :proc_lib.get_label/1 was added in OTP 27 + # TODO: use :proc_lib.get_label/1 once we require OTP 27 if Code.ensure_loaded?(:proc_lib) and function_exported?(:proc_lib, :get_label, 1) do defp get_label(pid), do: :proc_lib.get_label(pid) else From 34537bbdfb371dccfbf6f4acdc8c883e90c6f289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 10:38:18 -0300 Subject: [PATCH 08/14] Update test/kino/process_test.exs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- test/kino/process_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/kino/process_test.exs b/test/kino/process_test.exs index 016810cf..e6b32e68 100644 --- a/test/kino/process_test.exs +++ b/test/kino/process_test.exs @@ -114,7 +114,7 @@ defmodule Kino.ProcessTest do assert content =~ "0(supervisor_parent):::root ---> 2(#{inspect(agent)}):::worker" end - # Process.set_label/1 was addeed in Elixir 1.17.0 + # TODO: remove once we require Elixir v1.17.0 if function_exported?(Process, :set_label, 1) do test "uses process label in the diagram to identify a process" do process_label = "my task" From cfaf5fbaebea18a7a4925cc1cc89ab38d57f4286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 10:39:18 -0300 Subject: [PATCH 09/14] Update lib/kino/process/tracer.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- lib/kino/process/tracer.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/kino/process/tracer.ex b/lib/kino/process/tracer.ex index 0d4d0079..0499e921 100644 --- a/lib/kino/process/tracer.ex +++ b/lib/kino/process/tracer.ex @@ -32,7 +32,11 @@ defmodule Kino.Process.Tracer do } trace_events = [new_event | trace_info.raw_trace_events] - process_labels = update_process_labels(trace_info.process_labels, from, to) + + process_labels = + trace_info.process_labels + |> put_new_label(from) + |> put_new_label(to) {:noreply, %{trace_info | raw_trace_events: trace_events, process_labels: process_labels}} end From 4381395eac6a4a01efc41ab3a4b03953e82331bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 10:39:24 -0300 Subject: [PATCH 10/14] Update lib/kino/process/tracer.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- lib/kino/process/tracer.ex | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/kino/process/tracer.ex b/lib/kino/process/tracer.ex index 0499e921..9d47e3fd 100644 --- a/lib/kino/process/tracer.ex +++ b/lib/kino/process/tracer.ex @@ -45,12 +45,6 @@ defmodule Kino.Process.Tracer do {:noreply, trace_events} end - defp update_process_labels(process_labels, pid1, pid2) do - process_labels - |> put_new_label(pid1) - |> put_new_label(pid2) - end - defp put_new_label(process_labels, pid) do Map.put_new(process_labels, pid, get_label(pid)) end From 8ce6b306b544485e95ee98f86b1255aa17ca7ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 10:54:34 -0300 Subject: [PATCH 11/14] Format --- lib/kino/process/tracer.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kino/process/tracer.ex b/lib/kino/process/tracer.ex index 9d47e3fd..0378f3ab 100644 --- a/lib/kino/process/tracer.ex +++ b/lib/kino/process/tracer.ex @@ -33,7 +33,7 @@ defmodule Kino.Process.Tracer do trace_events = [new_event | trace_info.raw_trace_events] - process_labels = + process_labels = trace_info.process_labels |> put_new_label(from) |> put_new_label(to) From 4a8b4f957b6b6cc7ba0cc5762568bf6078e8708c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 11:02:34 -0300 Subject: [PATCH 12/14] Update lib/kino/process.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- lib/kino/process.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kino/process.ex b/lib/kino/process.ex index c2f318a6..3c367cfc 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -449,8 +449,8 @@ defmodule Kino.Process do defp format_for_mermaid_participant_alias(pid, process_label) do pid_text = :erlang.pid_to_list(pid) |> List.to_string() - participant_alias = "#{inspect(process_label)}
#{pid_text}" - String.replace(participant_alias, "\"", "") + label = process_label |> inspect() |> String.replace(~s{"}, "") + "#{label}
#{pid_text}" end defp maybe_add_participant({participants, idx}, pid) when is_pid(pid) do From 9bc1cea27d17744572612c307b4efc4ec8dc22db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 11:07:03 -0300 Subject: [PATCH 13/14] Improve code clarity --- lib/kino/process.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/kino/process.ex b/lib/kino/process.ex index 3c367cfc..e058f02f 100644 --- a/lib/kino/process.ex +++ b/lib/kino/process.ex @@ -779,9 +779,9 @@ defmodule Kino.Process do defp format_for_mermaid_graph_node(pid, process_label) do pid_text = :erlang.pid_to_list(pid) |> List.to_string() - "#{inspect(process_label)}
#{pid_text}" - |> String.replace("\"", "") - |> format_as_mermaid_unicode_text() + label = process_label |> inspect() |> String.replace(~s{"}, "") + + format_as_mermaid_unicode_text("#{label}
#{pid_text}") end # this is needed to use unicode inside node's text From 3686d545c9c717428294ef0689a86a5ed98ca30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Thu, 13 Jun 2024 13:02:44 -0300 Subject: [PATCH 14/14] Update lib/kino/process/tracer.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/kino/process/tracer.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kino/process/tracer.ex b/lib/kino/process/tracer.ex index 0378f3ab..eb277e69 100644 --- a/lib/kino/process/tracer.ex +++ b/lib/kino/process/tracer.ex @@ -46,7 +46,7 @@ defmodule Kino.Process.Tracer do end defp put_new_label(process_labels, pid) do - Map.put_new(process_labels, pid, get_label(pid)) + Map.put_new_lazy(process_labels, pid, fn -> get_label(pid) end) end # :proc_lib.get_label/1 was added in OTP 27