/
profile.cprof.ex
274 lines (218 loc) · 9.58 KB
/
profile.cprof.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
defmodule Mix.Tasks.Profile.Cprof do
use Mix.Task
@shortdoc "Profiles the given file or expression with cprof"
@moduledoc """
Profiles the given file or expression using Erlang's `cprof` tool.
`cprof` can be useful when you want to discover the bottlenecks related
to function calls.
Before running the code, it invokes the `app.start` task which compiles
and loads your project. After that, the target expression is profiled together
with all matching function calls, by setting breakpoints containing
counters. These can only be set on BEAM code so BIFs cannot be call
count traced.
To profile the code, you can use syntax similar to the `mix run` task:
mix profile.cprof -e Hello.world
mix profile.cprof -e "[1, 2, 3] |> Enum.reverse |> Enum.map(&Integer.to_string/1)"
mix profile.cprof my_script.exs arg1 arg2 arg3
This task is automatically reenabled, so you can profile multiple times
in the same Mix invocation.
## Command line options
* `--matching` - only profile calls matching the given `Module.function/arity` pattern
* `--limit` - filters out any results with a call count less than the limit
* `--module` - filters out any results not pertaining to the given module
* `--eval`, `-e` - evaluate the given code
* `--require`, `-r` - requires pattern before running the command
* `--parallel`, `-p` - makes all requires parallel
* `--no-compile` - does not compile even if files require compilation
* `--no-deps-check` - does not check dependencies
* `--no-archives-check` - does not check archives
* `--no-halt` - does not halt the system after running the command
* `--no-start` - does not start applications after compilation
* `--no-elixir-version-check` - does not check the Elixir version from mix.exs
## Profile output
Example output:
CNT
Total 15
Enum 6 <--
Enum."-map/2-lists^map/1-0-"/2 4
Enum.reverse/1 1
Enum.map/2 1
:elixir_compiler 4 <--
anonymous fn/1 in :elixir_compiler.__FILE__/1 3
anonymous fn/0 in :elixir_compiler.__FILE__/1 1
String.Chars.Integer 3 <--
String.Chars.Integer.to_string/1 3
:erlang 2 <--
:erlang.trace_pattern/3 2
Profile done over 20229 matching functions
The default output contains data gathered from all matching functions. The left
column structures each module and its total call count trace is presented on the right.
Each module has its count discriminated by function below. The `<--` symbol is meant to
help visualize where a new module call count begins.
The first row (Total) is the sum of all function calls. In the last row the number of
matching functions that were considered for profiling is presented.
When `--matching` option is specified, call count tracing will be started only for
the functions matching the given pattern:
String.Chars.Integer 3 <--
String.Chars.Integer.to_string/1 3
Profile done over 1 matching functions
The pattern can be a module name, such as `String` to count all calls to that module,
a call without arity, such as `String.split`, to count all calls to that function
regardless of arity, or a call with arity, such as `String.split/2`, to count all
calls to that exact module, function and arity.
## Caveats
You should be aware the profiler is stopped as soon as the code has finished running. This
may need special attention, when: running asynchronous code as function calls which were
called before the profiler stopped will not be counted; running synchronous code as long
running computations and a profiler without a proper MFA trace pattern or filter may
lead to a result set which is difficult to comprehend.
Other caveats are the impossibility to call count trace BIFs, since breakpoints can
only be set on BEAM code; functions calls performed by `:cprof` are not traced; the
maximum size of a call counter is equal to the host machine's word size
(for example, 2147483647 in a 32-bit host).
"""
@switches [
parallel: :boolean,
require: :keep,
eval: :keep,
config: :keep,
matching: :string,
halt: :boolean,
compile: :boolean,
deps_check: :boolean,
limit: :integer,
module: :string,
start: :boolean,
archives_check: :boolean,
warmup: :boolean,
elixir_version_check: :boolean,
parallel_require: :keep
]
@aliases [r: :require, p: :parallel, e: :eval, c: :config]
@impl true
def run(args) do
{opts, head} = OptionParser.parse_head!(args, aliases: @aliases, strict: @switches)
Mix.Task.reenable("profile.cprof")
Mix.Tasks.Run.run(
["--no-mix-exs" | args],
opts,
head,
&profile_code(&1, opts),
&profile_code(File.read!(&1), opts)
)
end
defp profile_code(code_string, opts) do
opts = Enum.map(opts, &parse_opt/1)
content =
quote do
unquote(__MODULE__).profile(
fn ->
unquote(Code.string_to_quoted!(code_string))
end,
unquote(Macro.escape(opts))
)
end
# Use compile_quoted since it leaves less noise than eval_quoted
Code.compile_quoted(content)
end
defp parse_opt({:matching, matching}) do
case Mix.Utils.parse_mfa(matching) do
{:ok, [m, f, a]} -> {:matching, {m, f, a}}
{:ok, [m, f]} -> {:matching, {m, f, :_}}
{:ok, [m]} -> {:matching, {m, :_, :_}}
:error -> Mix.raise("Invalid matching pattern: #{matching}")
end
end
defp parse_opt({:module, module}), do: {:module, string_to_existing_module(module)}
defp parse_opt(other), do: other
@doc """
Allows to programmatically run the `cprof` profiler on expression in `fun`.
## Options
* `:matching` - only profile calls matching the given pattern in form of
`{module, function, arity}`, where each element may be replaced by `:_`
to allow any value
* `:limit` - filters out any results with a call count less than the limit
* `:module` - filters out any results not pertaining to the given module
"""
def profile(fun, opts \\ []) when is_function(fun, 0) do
fun
|> profile_and_analyse(opts)
|> print_output
:cprof.stop()
end
defp profile_and_analyse(fun, opts) do
if Keyword.get(opts, :warmup, true) do
IO.puts("Warmup...")
fun.()
end
num_matched_functions =
case Keyword.fetch(opts, :matching) do
{:ok, matching} -> :cprof.start(matching)
:error -> :cprof.start()
end
apply(fun, [])
:cprof.pause()
limit = Keyword.get(opts, :limit)
module = Keyword.get(opts, :module)
analysis_result =
case {limit, module} do
{nil, nil} ->
:cprof.analyse()
{limit, nil} ->
:cprof.analyse(limit)
{limit, module} ->
if limit do
:cprof.analyse(module, limit)
else
:cprof.analyse(module)
end
end
{num_matched_functions, analysis_result}
end
defp string_to_existing_module(":" <> module), do: String.to_existing_atom(module)
defp string_to_existing_module(module), do: Module.concat([module])
defp print_output({num_matched_functions, {all_call_count, mod_analysis_list}}) do
print_total_row(all_call_count)
Enum.each(mod_analysis_list, &print_analysis_result/1)
print_number_of_matched_functions(num_matched_functions)
end
defp print_output({num_matched_functions, {_mod, _call_count, _mod_fun_list} = mod_analysis}) do
print_analysis_result(mod_analysis)
print_number_of_matched_functions(num_matched_functions)
end
defp print_number_of_matched_functions(num_matched_functions) do
IO.puts("Profile done over #{num_matched_functions} matching functions")
end
defp print_total_row(all_call_count) do
IO.puts("")
print_row(["s", "s", "s"], ["", "CNT", ""])
print_row(["s", "B", "s"], ["Total", all_call_count, ""])
end
defp print_analysis_result({module, total_module_count, module_fun_list}) do
module
|> Atom.to_string()
|> module_name_for_printing()
|> print_module(total_module_count, "", "<--")
Enum.each(module_fun_list, &print_function(&1, " "))
end
defp print_module(module, count, prefix, suffix) do
print_row(["s", "B", "s"], ["#{prefix}#{module}", count, suffix])
end
defp module_name_for_printing("Elixir." <> rest = _module_name), do: rest
defp module_name_for_printing(module_name), do: ":" <> module_name
defp print_function({fun, count}, prefix, suffix \\ "") do
print_row(["s", "B", "s"], ["#{prefix}#{function_text(fun)}", count, suffix])
end
defp function_text({module, function, arity}) do
Exception.format_mfa(module, function, arity)
end
defp function_text(other), do: inspect(other)
@columns [-60, 12, 5]
defp print_row(formats, data) do
Stream.zip(@columns, formats)
|> Stream.map(fn {width, format} -> "~#{width}#{format}" end)
|> Enum.join()
|> :io.format(data)
IO.puts("")
end
end