Compare commits

...

2 Commits

Author SHA1 Message Date
8cb2c76614 Merge pull request 'Configurable progress reporting' (#13) from uw-progress-reporting into master
Reviewed-on: #13
Reviewed-by: Jarvis Carroll <jarviscarrol@qpq.swiss>
Reviewed-by: Craig Everett <craigeverett@qpq.swiss>
2025-09-24 14:44:09 +09:00
Ulf Wiger
07b658d509 Configurable progress reporting 2025-09-23 16:44:46 +02:00
15 changed files with 131 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{application,gmhive_client,
[{description,"Gajumaru Hive Client"},
{vsn,"0.5.1"},
{vsn,"0.6.0"},
{registered,[]},
{applications,[kernel,stdlib,sasl,gproc,inets,ssl,enoise,
gmconfig,gmhive_protocol,gmhive_worker]},

View File

@ -1,6 +1,6 @@
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
-module(gmhc_app).
-vsn("0.4.8").
-vsn("0.6.0").
-behaviour(application).
@ -49,4 +49,13 @@ set_things_up() ->
gmhc_config:load_config(),
logger:set_module_level([gmhw_pow_cuckoo], notice),
?LOG_DEBUG("Config: ~p", [gmconfig:user_config()]),
case gmhc_config:get_config([<<"report">>]) of
<<"debug">> ->
?LOG_NOTICE("Starting debug reporter", []),
gmhc_events:debug();
<<"progress">> ->
?LOG_NOTICE("Starting progress reporter", []),
gmhc_events:progress();
_ -> ok
end,
ok.

View File

@ -1,5 +1,5 @@
-module(gmhc_config).
-vsn("0.4.8").
-vsn("0.6.0").
-export([ load_config/0
, get_config/1

View File

@ -1,5 +1,5 @@
-module(gmhc_config_schema).
-vsn("0.4.8").
-vsn("0.6.0").
-export([ schema/0
, to_json/0 ]).
@ -39,6 +39,9 @@ schema() ->
, pool => pool()
, pool_admin => pool_admin()
, workers => workers()
, report => str(#{ enum => [<<"debug">>, <<"progress">>, <<"silent">>]
, default => <<"silent">>
, description => <<"Progress reporting">> })
}).
pool() ->

View File

@ -1,5 +1,5 @@
-module(gmhc_connector).
-vsn("0.4.8").
-vsn("0.6.0").
-behaviour(gen_server).

View File

@ -1,5 +1,5 @@
-module(gmhc_connectors_sup).
-vsn("0.4.8").
-vsn("0.6.0").
-behavior(supervisor).
-export([ start_link/0

View File

@ -1,5 +1,5 @@
-module(gmhc_counters).
-vsn("0.4.8").
-vsn("0.6.0").
-export([ initialize/0 ]).

View File

@ -1,5 +1,5 @@
-module(gmhc_eureka).
-vsn("0.4.8").
-vsn("0.6.0").
-export([get_pool_address/0]).

View File

@ -1,5 +1,5 @@
-module(gmhc_events).
-vsn("0.4.8").
-vsn("0.6.0").
-export([subscribe/1,
ensure_subscribed/1,
@ -7,7 +7,17 @@
ensure_unsubscribed/1,
publish/2]).
-export([debug/0]).
-export([debug/0,
progress/0,
stop/0]).
-export([rpt_debug/2,
rpt_progress/2]).
%% internal
-export([init_reporter/2]).
-include_lib("kernel/include/logger.hrl").
-export_type([event/0]).
@ -60,23 +70,100 @@ ensure_unsubscribed(Event) ->
debug() ->
ok = application:ensure_started(gproc),
spawn(fun() ->
spawn_reporter(fun() ->
sub(),
gmhive_worker:subscribe_returns(),
loop(fun rpt_debug/2, false)
end).
progress() ->
ok = application:ensure_started(gproc),
spawn_reporter(fun() ->
sub(),
loop(fun rpt_progress/2, true)
end).
spawn_reporter(F) ->
Parent = self(),
proc_lib:start_link(?MODULE, init_reporter, [F, Parent]).
init_reporter(F, Parent) ->
try_register_reporter(),
proc_lib:init_ack(Parent, self()),
F().
stop() ->
case whereis(gmhc_reporter) of
undefined ->
not_running;
Pid ->
exit(Pid, kill)
end.
sub() ->
subscribe(pool_notification),
subscribe({pool_notification, new_generation}),
subscribe(connected),
subscribe(puzzle),
subscribe(result),
subscribe(error),
subscribe(disconnected),
gmhive_worker:subscribe_returns(),
loop()
end).
subscribe(disconnected).
loop() ->
loop(F, Ts) ->
receive
stop -> ok;
{gproc_ps_event, E, Data} ->
io:fwrite("EVENT ~p: ~p~n", [E, Data]),
loop()
maybe_print(F(E, Data), Ts),
loop(F, Ts)
end.
try_register_reporter() ->
try register(gmhc_reporter, self())
catch
error:_ ->
?LOG_ERROR("Reporter already running. Try gmhc_events:stop().", []),
error(already_running)
end.
maybe_print([], _) ->
ok;
maybe_print(String, Ts) when is_boolean(Ts) ->
TSstr = [[ts(), " "] || Ts],
io:put_chars([TSstr, String, "\n"]).
ts() ->
calendar:system_time_to_rfc3339(erlang:system_time(millisecond),
[{unit, millisecond}, {offset, "Z"}]).
rpt_debug(E, Data) ->
io_lib:fwrite("EVENT ~p: ~p", [E, Data]).
rpt_progress(puzzle, #{info := {_Data, _Target, Nonce, _Config}}) ->
w("Trying nonce: ~p", [Nonce]);
rpt_progress(result, #{info := Info}) ->
case Info of
{error, no_solution} ->
[];
{ok, Cycles} ->
w("Found! Reporting ~w cycles to leader.", [length(Cycles)]);
Other ->
w("Unexpected 'result': ~tp", [Other])
end;
rpt_progress(pool_notification, #{info := #{msg := Msg}}) ->
case Msg of
#{solution_accepted := #{seq := Seq}} ->
w("The hive has produced a solution! Sequence: ~w", [Seq]);
#{new_generation := _} -> [];
#{candidate := _} -> [];
Other ->
w("Unexpected 'pool_notification': ~tp", [Other])
end;
rpt_progress(connected, _) ->
w("Connected!", []);
rpt_progress(disconnected, _) ->
w("Disconnected!", []);
rpt_progress(_, _) ->
[].
w(Fmt, Args) ->
io_lib:fwrite(Fmt, Args).

View File

@ -1,5 +1,5 @@
-module(gmhc_handler).
-vsn("0.4.8").
-vsn("0.6.0").
-behavior(gen_server).
-export([ start_link/0

View File

@ -1,5 +1,5 @@
-module(gmhc_server).
-vsn("0.4.8").
-vsn("0.6.0").
-behaviour(gen_server).

View File

@ -1,6 +1,6 @@
%% -*- mode: erlang; erlang-indent-level: 4; indent-tabs-mode: nil -*-
-module(gmhc_sup).
-vsn("0.4.8").
-vsn("0.6.0").
-behaviour(supervisor).

View File

@ -8,7 +8,7 @@
%%%-------------------------------------------------------------------
-module(gmhc_workers).
-vsn("0.4.8").
-vsn("0.6.0").
-export([
get_worker_configs/0

View File

@ -1,5 +1,5 @@
-module(gmhive_client).
-vsn("0.4.8").
-vsn("0.6.0").
-export([ connect/1
, disconnect/1

View File

@ -2,9 +2,9 @@
{type,app}.
{modules,[]}.
{prefix,"gmhc"}.
{desc,"Gajumaru Hive Client"}.
{author,"Ulf Wiger, QPQ AG"}.
{package_id,{"uwiger","gmhive_client",{0,5,1}}}.
{desc,"Gajumaru Hive Client"}.
{package_id,{"uwiger","gmhive_client",{0,6,0}}}.
{deps,[{"uwiger","gmhive_worker",{0,5,1}},
{"uwiger","gmcuckoo",{1,2,4}},
{"otpr","eblake2",{1,0,1}},