Skip to content

Commit

Permalink
refactor(ExTop, ExTop.View)
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshkukreti committed Oct 13, 2015
1 parent 9070416 commit 8d23433
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
8 changes: 4 additions & 4 deletions lib/ex_top.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExTop do
use GenServer

defstruct [:node, :data, :prev_schedulers,
defstruct [:node, :data, :schedulers_snapshot,
selected: 0, offset: 0, sort_by: 1, sort_order: :ascending]

def start_link(opts \\ []) do
Expand Down Expand Up @@ -46,9 +46,9 @@ defmodule ExTop do

def handle_info(:tick, state) do
GenServer.cast self, :render
prev_schedulers = state.data && state.data.schedulers
schedulers_snapshot = state.data && state.data.schedulers
data = :rpc.call state.node, ExTop.Collector, :collect, []
{:noreply, %{state | data: data, prev_schedulers: prev_schedulers}}
{:noreply, %{state | data: data, schedulers_snapshot: schedulers_snapshot}}
end

def handle_info({port, {:data, "\e[A" <> rest}}, state) do
Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule ExTop do
|> Enum.drop(state.offset)
|> Enum.take(20)
data = %{state.data | processes: processes}
|> Map.put(:prev_schedulers, state.prev_schedulers)
|> Map.put(:schedulers_snapshot, state.schedulers_snapshot)
IO.write [IO.ANSI.home,
ExTop.View.render(data, selected: state.selected)]
{:noreply, state}
Expand Down
10 changes: 6 additions & 4 deletions lib/ex_top/collector.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ExTop.Collector do
def collect do
memory = :erlang.memory

processes = for pid <- :erlang.processes do
info = :erlang.process_info(pid, [:current_function,
:initial_call,
Expand All @@ -19,23 +20,24 @@ defmodule ExTop.Collector do
end
end |> Enum.reject(&is_nil/1)

schedulers = :erlang.statistics(:scheduler_wall_time) |> Enum.sort

{{:input, io_input}, {:output, io_output}} = :erlang.statistics(:io)
run_queue = :erlang.statistics(:run_queue)
process_count = :erlang.system_info(:process_count)
process_limit = :erlang.system_info(:process_limit)
uptime = :erlang.statistics(:wall_clock) |> elem(0) |> div(1000)
schedulers = :erlang.statistics(:scheduler_wall_time) |> Enum.sort

%{memory: memory,
processes: processes,
system: %{
schedulers: schedulers,
statistics: %{
io_input: io_input,
io_output: io_output,
process_count: process_count,
process_limit: process_limit,
run_queue: run_queue,
uptime: uptime
},
schedulers: schedulers}
}}
end
end
28 changes: 14 additions & 14 deletions lib/ex_top/view.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
defmodule ExTop.View do
def render(data, opts \\ []) do
[concat3(schedulers(data[:prev_schedulers], data[:schedulers]),
memory(data[:memory]),
statistics(data[:system])),
[concat3(schedulers(data.schedulers_snapshot, data.schedulers),
memory(data.memory),
statistics(data.statistics)),
processes_separator,
processes_heading,
processes_separator,
processes_rows(data[:processes], opts),
processes_rows(data.processes, opts),
processes_separator] |> Enum.intersperse("\n\r")
end

defp statistics(system) do
defp statistics(statistics) do
["+-----------------+----------------+",
"| Statistics |",
"+-----------------+----------------+",
"| Uptime | #{just(inspect(system[:uptime]), 13, :right)}s |",
"| Process Count | #{just(inspect(system[:process_count]), 14, :right)} |",
"| Process Limit | #{just(inspect(system[:process_limit]), 14, :right)} |",
"| Run Queue | #{just(inspect(system[:run_queue]), 14, :right)} |",
"| IO Input | #{just(inspect(system[:io_input]), 14, :right)} |",
"| IO Output | #{just(inspect(system[:io_output]), 14, :right)} |"]
"| Uptime | #{just(inspect(statistics.uptime), 13, :right)}s |",
"| Process Count | #{just(inspect(statistics.process_count), 14, :right)} |",
"| Process Limit | #{just(inspect(statistics.process_limit), 14, :right)} |",
"| Run Queue | #{just(inspect(statistics.run_queue), 14, :right)} |",
"| IO Input | #{just(inspect(statistics.io_input), 14, :right)} |",
"| IO Output | #{just(inspect(statistics.io_output), 14, :right)} |"]
end

defp memory(memory) do
Expand All @@ -34,9 +34,9 @@ defmodule ExTop.View do
"| ETS | #{just(inspect(memory[:ets]), 13, :right)} "]
end

defp schedulers(prev, now) do
usages = if prev do
for {{n, a1, t1}, {n, a2, t2}} <- Enum.zip(prev, now) |> Enum.take(8) do
defp schedulers(old, new) do
usages = if old do
for {{n, a1, t1}, {n, a2, t2}} <- Enum.zip(old, new) |> Enum.take(8) do
{n, (a2 - a1) / (t2 - t1)}
end
else
Expand Down

0 comments on commit 8d23433

Please sign in to comment.