Skip to content

Commit d9ffe76

Browse files
committed
Add support for MIX_PROFILE_FLAGS for more configuration around task profiling
1 parent 16a1472 commit d9ffe76

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

lib/mix/lib/mix.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ defmodule Mix do
360360
* `MIX_PATH` - appends extra code paths
361361
362362
* `MIX_PROFILE` - a list of comma-separated Mix tasks to profile the time spent on
363-
functions by the process running the task
363+
functions by the process running the task, such as `MIX_PROFILE=compile,test`.
364+
You can also set `MIX_PROFILE_FLAGS` to control the flags given to profiling,
365+
see `mix profile.tprof` for all options. By default, it uses `--no-warmup`.
364366
365367
* `MIX_QUIET` - does not print information messages to the terminal
366368

lib/mix/lib/mix/cli.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ defmodule Mix.CLI do
2222
if env_variable_activated?("MIX_QUIET"), do: Mix.shell(Mix.Shell.Quiet)
2323

2424
if profile = System.get_env("MIX_PROFILE") do
25-
Mix.State.put(:profile, String.split(profile, ","))
25+
flags = System.get_env("MIX_PROFILE_FLAGS", "")
26+
{opts, args} = Mix.Tasks.Profile.Tprof.parse!(OptionParser.split(flags))
27+
28+
if args != [] do
29+
Mix.raise("Invalid arguments given to MIX_PROFILE_FLAGS: #{inspect(args)}")
30+
end
31+
32+
opts = Keyword.put_new(opts, :warmup, false)
33+
Mix.State.put(:profile, {opts, String.split(profile, ",")})
2634
end
2735

2836
case check_for_shortcuts(args) do

lib/mix/lib/mix/task.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,24 @@ defmodule Mix.Task do
520520
shell.info(["<- Ran mix ", task, " in ", Integer.to_string(div(time, 1000)), "ms"])
521521
res
522522

523-
task in Mix.State.get(:profile, []) ->
523+
opts = profile_opts_for(task) ->
524524
shell = Mix.shell()
525525
shell.info(["-> Profiling mix ", task_to_string(task, args), project_to_string(proj)])
526-
Mix.Tasks.Profile.Tprof.profile(fun, warmup: false, set_on_spawn: false)
526+
Mix.Tasks.Profile.Tprof.profile(fun, opts)
527527

528528
true ->
529529
fun.()
530530
end
531531
end
532532

533+
defp profile_opts_for(task) do
534+
{opts, tasks} = Mix.State.get(:profile, {[], []})
535+
536+
if task in tasks do
537+
opts
538+
end
539+
end
540+
533541
defp project_to_string(nil), do: ""
534542
defp project_to_string(proj), do: " (inside #{inspect(proj)})"
535543

lib/mix/lib/mix/tasks/profile.tprof.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ defmodule Mix.Tasks.Profile.Tprof do
5656
* `--no-deps-check` - does not check dependencies
5757
* `--no-archives-check` - does not check archives
5858
* `--no-halt` - does not halt the system after running the command
59+
* `--no-set-on-spawn` - does not profile spawned processes
5960
* `--no-start` - does not start applications after compilation
6061
* `--no-elixir-version-check` - does not check the Elixir version from mix.exs
6162
@@ -159,7 +160,8 @@ defmodule Mix.Tasks.Profile.Tprof do
159160
archives_check: :boolean,
160161
warmup: :boolean,
161162
elixir_version_check: :boolean,
162-
parallel_require: :keep
163+
parallel_require: :keep,
164+
set_on_spawn: :boolean
163165
]
164166

165167
@aliases [
@@ -171,7 +173,7 @@ defmodule Mix.Tasks.Profile.Tprof do
171173

172174
@impl true
173175
def run(args) do
174-
{opts, head} = OptionParser.parse_head!(args, aliases: @aliases, strict: @switches)
176+
{opts, head} = parse!(args)
175177
Mix.Task.reenable("profile.tprof")
176178

177179
Mix.Tasks.Run.run(
@@ -183,9 +185,13 @@ defmodule Mix.Tasks.Profile.Tprof do
183185
)
184186
end
185187

186-
defp profile_code(code_string, opts) do
187-
opts = Enum.map(opts, &parse_opt/1)
188+
@doc false
189+
def parse!(args) do
190+
{opts, args} = OptionParser.parse!(args, aliases: @aliases, strict: @switches)
191+
{Enum.map(opts, &parse_opt/1), args}
192+
end
188193

194+
defp profile_code(code_string, opts) do
189195
content =
190196
quote do
191197
unquote(__MODULE__).profile(

0 commit comments

Comments
 (0)