Skip to content

Commit 2199d0c

Browse files
author
José Valim
committed
Add IEx.Helpers.system_info/0
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent a794c6e commit 2199d0c

File tree

2 files changed

+103
-23
lines changed

2 files changed

+103
-23
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,28 @@ defmodule IEx.Helpers do
2222
2323
There are many other helpers available, here are some examples:
2424
25-
* `b/1` - prints callbacks info and docs for a given module
26-
* `c/1` - compiles a file into the current directory
27-
* `c/2` - compiles a file to the given path
28-
* `cd/1` - changes the current directory
29-
* `clear/0` - clears the screen
30-
* `e/1` - shows all exports (functions + macros) in a module
31-
* `flush/0` - flushes all messages sent to the shell
32-
* `h/0` - prints this help message
33-
* `h/1` - prints help for the given module, function or macro
34-
* `i/0` - prints information about the last value
35-
* `i/1` - prints information about the given term
36-
* `ls/0` - lists the contents of the current directory
37-
* `ls/1` - lists the contents of the specified directory
38-
* `pid/1` - creates a PID from a string
39-
* `pid/3` - creates a PID with the 3 integer arguments passed
40-
* `pwd/0` - prints the current working directory
41-
* `r/1` - recompiles the given module's source file
42-
* `recompile/0` - recompiles the current project
43-
* `respawn/0` - respawns the current shell
44-
* `v/0` - retrieves the last value from the history
45-
* `v/1` - retrieves the nth value from the history
25+
* `b/1` - prints callbacks info and docs for a given module
26+
* `c/1` - compiles a file into the current directory
27+
* `c/2` - compiles a file to the given path
28+
* `cd/1` - changes the current directory
29+
* `clear/0` - clears the screen
30+
* `e/1` - shows all exports (functions + macros) in a module
31+
* `flush/0` - flushes all messages sent to the shell
32+
* `h/0` - prints this help message
33+
* `h/1` - prints help for the given module, function or macro
34+
* `i/0` - prints information about the last value
35+
* `i/1` - prints information about the given term
36+
* `ls/0` - lists the contents of the current directory
37+
* `ls/1` - lists the contents of the specified directory
38+
* `pid/1` - creates a PID from a string
39+
* `pid/3` - creates a PID with the 3 integer arguments passed
40+
* `pwd/0` - prints the current working directory
41+
* `r/1` - recompiles the given module's source file
42+
* `recompile/0` - recompiles the current project
43+
* `respawn/0` - respawns the current shell
44+
* `system_info/0` - prints system info (versions, memory usage, stats)
45+
* `v/0` - retrieves the last value from the history
46+
* `v/1` - retrieves the nth value from the history
4647
4748
Help for all of those functions can be consulted directly from
4849
the command line using the `h/1` helper itself. Try:
@@ -497,6 +498,77 @@ defmodule IEx.Helpers do
497498
|> Enum.map_join(", ", &inspect/1)
498499
end
499500

501+
@doc """
502+
Prints system information such as versions, memory usage and statistics.
503+
"""
504+
def system_info do
505+
print_pane("System and architecture")
506+
507+
print_entry("Elixir version", System.version)
508+
print_entry("OTP version", :erlang.system_info(:otp_release))
509+
print_entry("ERTS version", :erlang.system_info(:version))
510+
print_entry("Compiled for", :erlang.system_info(:system_architecture))
511+
print_entry("Schedulers", :erlang.system_info(:schedulers))
512+
print_entry("Schedulers online", :erlang.system_info(:schedulers_online))
513+
514+
print_pane("Memory")
515+
print_memory("Total", :total, :MB)
516+
print_memory("Atoms", :atom)
517+
print_memory("Binaries", :binary)
518+
print_memory("Code", :code)
519+
print_memory("ETS", :ets)
520+
print_memory("Processes", :processes)
521+
522+
print_pane("Statistics / limits")
523+
print_uptime()
524+
print_entry("Run queue", :erlang.statistics(:run_queue))
525+
if :erlang.system_info(:otp_release) >= '20' do
526+
print_percentage("Atoms", :atom_count, :atom_limit)
527+
end
528+
print_percentage("ETS", :ets_count, :ets_limit)
529+
print_percentage("Ports", :port_count, :port_limit)
530+
print_percentage("Processes", :process_count, :process_limit)
531+
532+
IO.puts ""
533+
dont_display_result()
534+
end
535+
536+
defp print_pane(msg) do
537+
IO.puts IEx.color(:eval_result, ["\n## ", msg, " \n"])
538+
end
539+
540+
defp print_entry(_key, nil), do: :ok
541+
defp print_entry(key, value), do: IO.puts "#{pad_key(key)}#{value}"
542+
543+
defp print_uptime() do
544+
IO.write pad_key("Uptime")
545+
:c.uptime()
546+
end
547+
548+
defp print_percentage(key, min, max) do
549+
min = get_stat(min)
550+
max = get_stat(max)
551+
percentage = trunc((min / max) * 100)
552+
IO.puts "#{pad_key(key)}#{min} / #{max} (#{percentage}% used)"
553+
end
554+
555+
defp get_stat(:ets_count), do: length(:ets.all())
556+
defp get_stat(other), do: :erlang.system_info(other)
557+
558+
defp print_memory(key, memory, unit \\ :kB) do
559+
value =
560+
memory
561+
|> :erlang.memory()
562+
|> div(memory_unit(unit))
563+
|> round()
564+
IO.puts "#{pad_key(key)}#{value} #{unit}"
565+
end
566+
567+
defp memory_unit(:MB), do: 1024 * 1024
568+
defp memory_unit(:kB), do: 1024
569+
570+
defp pad_key(key), do: String.pad_trailing("#{key}:", 20, " ")
571+
500572
@doc """
501573
Flushes all messages sent to the shell and prints them out.
502574
"""
@@ -547,12 +619,15 @@ defmodule IEx.Helpers do
547619
Prints a list of all the functions and macros exported by the given module.
548620
"""
549621
def e(module \\ Kernel) do
550-
IEx.Autocomplete.exports(module) |> print_exports()
622+
print_exports(IEx.Autocomplete.exports(module))
551623
dont_display_result()
552624
end
553625

554626
defp print_exports(functions) do
555-
list = Enum.map(functions, fn({name, arity}) -> Atom.to_string(name) <> "/" <> Integer.to_string(arity) end)
627+
list =
628+
Enum.map(functions, fn {name, arity} ->
629+
Atom.to_string(name) <> "/" <> Integer.to_string(arity)
630+
end)
556631
print_table(list)
557632
end
558633

lib/iex/test/iex/helpers_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ defmodule IEx.HelpersTest do
1515
Application.delete_env(:elixir, :ansi_enabled)
1616
end
1717

18+
test "system_info() helper" do
19+
assert "## System and architecture" <> _ =
20+
capture_io(fn -> system_info() end)
21+
end
22+
1823
test "h helper" do
1924
assert "* IEx.Helpers\n\nWelcome to Interactive Elixir" <> _
2025
= capture_iex("h()")

0 commit comments

Comments
 (0)