Skip to content

Add :report option to aggregate results over multiple processes in tprof #13632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/mix/lib/mix/tasks/profile.tprof.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ defmodule Mix.Tasks.Profile.Tprof do
* `--time` - filters out any results that took lower than specified (in µs), the `type` needs to be `time`
* `--memory` - filters out any results that used less memory than specified (in words), the `type` needs to be `memory`
* `--sort` - sorts the results by `calls`, `per_call` or by the value of `type` (default: the value of `type`)
* `--report` - returns the per-process breakdown when `process`, or the total for all processes when `total` (default: `process`).
Always `total` when `type` is `calls`.
* `--eval`, `-e` - evaluates the given code
* `--require`, `-r` - requires pattern before running the command
* `--parallel`, `-p` - makes all requires parallel
Expand Down Expand Up @@ -152,6 +154,7 @@ defmodule Mix.Tasks.Profile.Tprof do
time: :integer,
memory: :integer,
sort: :string,
report: :string,
start: :boolean,
archives_check: :boolean,
warmup: :boolean,
Expand Down Expand Up @@ -211,6 +214,10 @@ defmodule Mix.Tasks.Profile.Tprof do
defp parse_opt({:type, "memory"}), do: {:type, :memory}
defp parse_opt({:type, other}), do: Mix.raise("Invalid type option: #{other}")

defp parse_opt({:report, "process"}), do: {:report, :process}
defp parse_opt({:report, "total"}), do: {:report, :total}
defp parse_opt({:report, other}), do: Mix.raise("Invalid report option: #{other}")

defp parse_opt({:sort, "time"}), do: {:sort, :time}
defp parse_opt({:sort, "calls"}), do: {:sort, :calls}
defp parse_opt({:sort, "memory"}), do: {:sort, :memory}
Expand Down Expand Up @@ -238,6 +245,8 @@ defmodule Mix.Tasks.Profile.Tprof do
`type` needs to be `:memory`
* `:sort` - sort the results by `:calls`, `:per_call` or by the value of `type`
(default: the value of `type`)
* `:report` - returns the per-process breakdown when `:process`, or the total for all
processes when `:total` (default: `:process`). Always `:total` when `type` is `:calls`.
* `:warmup` - if the code should be warmed up before profiling (default: `true`)
* `:set_on_spawn` - if newly spawned processes should be measured (default: `true`)

Expand All @@ -259,6 +268,7 @@ defmodule Mix.Tasks.Profile.Tprof do
matching = Keyword.get(opts, :matching, {:_, :_, :_})
set_on_spawn = Keyword.get(opts, :set_on_spawn, true)
type = Keyword.get(opts, :type, :time)
report = Keyword.get(opts, :report, :process)

sort_by =
case Keyword.get(opts, :sort) do
Expand Down Expand Up @@ -288,7 +298,7 @@ defmodule Mix.Tasks.Profile.Tprof do
report: :return
})

inspected = tprof_module().inspect({tprof_type, traces}, :process, sort_by)
inspected = tprof_module().inspect({tprof_type, traces}, report, sort_by)

results =
inspected
Expand Down
8 changes: 8 additions & 0 deletions lib/mix/test/mix/tasks/profile.tprof_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ defmodule Mix.Tasks.Profile.TprofTest do
end)
end

test "aggregates totals over all processes", context do
in_tmp(context.test, fn ->
assert capture_io(fn ->
Tprof.run(["--report", "total", "-e", @expr])
end) =~ "Profile results over all processes"
end)
end

test "Module matching", context do
in_tmp(context.test, fn ->
refute capture_io(fn ->
Expand Down