Skip to content
Permalink
Browse files

rename cpu-usage to scheduler-usage, promote to farmbot_core

  • Loading branch information...
ConnorRigby committed Oct 2, 2019
1 parent de64961 commit e3382355a6737ab849477d2b493f7ad93a688be8
@@ -119,8 +119,8 @@ defmodule FarmbotCore.BotState do
GenServer.call(bot_state_server, {:report_memory_usage, megabytes})
end

def report_cpu_usage(bot_state_server \\ __MODULE__, percent) do
GenServer.call(bot_state_server, {:report_cpu_usage, percent})
def report_scheduler_usage(bot_state_server \\ __MODULE__, percent) do
GenServer.call(bot_state_server, {:report_scheduler_usage, percent})
end

def report_soc_temp(bot_state_server \\ __MODULE__, temp_celcius) do
@@ -377,8 +377,8 @@ defmodule FarmbotCore.BotState do
{:reply, reply, state}
end

def handle_call({:report_cpu_usage, percent}, _form, state) do
change = %{informational_settings: %{cpu_usage: percent}}
def handle_call({:report_scheduler_usage, average_percent}, _form, state) do
change = %{informational_settings: %{scheduler_usage: average_percent}}

{reply, state} =
BotStateNG.changeset(state.tree, change)
@@ -0,0 +1,44 @@
defmodule FarmbotCore.BotState.SchedulerUsageReporter do
alias FarmbotCore.BotState
use GenServer
@default_timeout_ms 5000

def start_link(args) do
GenServer.start_link(__MODULE__, args)
end

def init(_args) do
_ = :msacc.start()
{:ok, %{}, @default_timeout_ms}
end

def handle_info(:timeout, state) do
scheduler_info = for %{
type: type,
id: id,
counters: %{
aux: aux,
check_io: check_io,
emulator: emulator,
gc: gc,
other: other,
port: port,
sleep: sleep
}
} when type in [:scheduler, :dirty_cpu_scheduler] <- :msacc.stats() do
denominator = aux + check_io + emulator + gc + other + port + sleep
numerator = denominator - sleep
{"#{type}_#{id}", numerator / denominator}
end

average = calculate_average(scheduler_info, Enum.count(scheduler_info))
_ = BotState.report_scheduler_usage(average)
{:noreply, state, @default_timeout_ms}
end

defp calculate_average(usage, count, acc \\ 0)
defp calculate_average([{_, usage} | rest], count, acc),
do: calculate_average(rest, count, acc + usage)
defp calculate_average([], count, acc),
do: round((acc / count) * 100)
end
@@ -8,7 +8,8 @@ defmodule FarmbotCore.BotState.Supervisor do
def init([]) do
children = [
FarmbotCore.BotState,
FarmbotCore.BotState.FileSystem
FarmbotCore.BotState.FileSystem,
FarmbotCore.BotState.SchedulerUsageReporter
]
Supervisor.init(children, [strategy: :one_for_all])
end
@@ -24,7 +24,7 @@ defmodule FarmbotCore.BotStateNG.InformationalSettings do
field(:uptime, :integer)
field(:memory_usage, :integer)
field(:disk_usage, :integer)
field(:cpu_usage, :integer)
field(:scheduler_usage, :integer)
field(:sync_status, :string, default: "sync_now")
field(:locked, :boolean, default: false)
field(:last_status, :string)
@@ -57,7 +57,9 @@ defmodule FarmbotCore.BotStateNG.InformationalSettings do
uptime: informational_settings.uptime,
memory_usage: informational_settings.memory_usage,
disk_usage: informational_settings.disk_usage,
cpu_usage: informational_settings.cpu_usage,
scheduler_usage: informational_settings.scheduler_usage,
# this field is required for the frontend. Maybe remove in the future.
cpu_usage: informational_settings.scheduler_usage,
sync_status: informational_settings.sync_status,
locked: informational_settings.locked,
last_status: informational_settings.last_status,
@@ -86,7 +88,7 @@ defmodule FarmbotCore.BotStateNG.InformationalSettings do
:uptime,
:memory_usage,
:disk_usage,
:cpu_usage,
:scheduler_usage,
:sync_status,
:locked,
:last_status,
@@ -89,14 +89,14 @@ defmodule FarmbotCore.BotStateNGTest do
assert mut.informational_settings.memory_usage == 512
end

test "reports cpu_usage" do
test "reports scheduler usage" do
orig = BotStateNG.new()

mut =
BotStateNG.changeset(orig, %{informational_settings: %{cpu_usage: 10}})
BotStateNG.changeset(orig, %{informational_settings: %{scheduler_usage: 10}})
|> Ecto.Changeset.apply_changes()

assert mut.informational_settings.cpu_usage == 10
assert mut.informational_settings.scheduler_usage == 10
end

test "reports uptime" do

This file was deleted.

@@ -4,7 +4,6 @@ defmodule FarmbotOS.Platform.Target.InfoWorker.Supervisor do

alias FarmbotOS.Platform.Target.InfoWorker.{
DiskUsage,
CpuUsage,
MemoryUsage,
SocTemp,
Throttle,
@@ -19,7 +18,6 @@ defmodule FarmbotOS.Platform.Target.InfoWorker.Supervisor do
def init([]) do
children = [
DiskUsage,
CpuUsage,
MemoryUsage,
SocTemp,
Throttle,

0 comments on commit e338235

Please sign in to comment.
You can’t perform that action at this time.