this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add log and hash functions

garrison bfb8dcc9 345393a0

+48 -31
+1 -29
lib/trinity/scheduler/simulation_supervisor.ex
··· 123 123 end 124 124 125 125 defp end_simulation(%State{} = state, reason) do 126 - %{sim: sim, parent_pid: parent_pid, ref: ref, result: result} = state 127 - 128 - %Simulation{log: log, log_atomic: log_atomic} = sim 129 - hash = :atomics.get(log_atomic, 1) 130 - 131 - require Logger 132 - Logger.info """ 133 - Simulation complete 134 - Hash: #{print_hash(hash)} 135 - Log: 136 - 137 - #{print_log(log)} 138 - """ 139 - 126 + %{parent_pid: parent_pid, ref: ref, result: result} = state 140 127 send parent_pid, {ref, reason, result} 141 - end 142 - 143 - defp print_hash(hash) do 144 - :crypto.hash(:sha256, <<hash::integer-32>>) 145 - |> Base.encode32() 146 - |> String.slice(0, 8) 147 - end 148 - 149 - defp print_log(log) do 150 - :ets.tab2list(log) 151 - |> Enum.take(-10) 152 - |> Enum.reduce("", fn {i, msg}, acc -> 153 - i_pad = String.pad_leading(Integer.to_string(i), 2) 154 - acc <> i_pad <> ": " <> msg <> "\n" 155 - end) 156 128 end 157 129 158 130 defp spawn_sim_child(%Simulation{} = sim, fun) do
+33
lib/trinity/sim_logger.ex
··· 4 4 @spec get_sim :: Simulation.t | nil 5 5 defp get_sim, do: Process.get(simulation_key()) 6 6 7 + @spec get_hash :: non_neg_integer 8 + def get_hash do 9 + %{log_atomic: log_atomic} = get_sim() 10 + :atomics.get(log_atomic, 1) 11 + end 12 + 13 + @spec log_head(non_neg_integer) :: [String.t] 14 + def log_head(count) do 15 + scan_log(1, count) 16 + end 17 + 18 + @spec log_tail(non_neg_integer) :: [String.t] 19 + def log_tail(count) do 20 + %{log_atomic: log_atomic} = get_sim() 21 + last_i = :atomics.get(log_atomic, 2) 22 + scan_log(last_i - count, count) 23 + end 24 + 25 + @spec scan_log(non_neg_integer, non_neg_integer) :: [String.t] 26 + def scan_log(i, count) do 27 + %{log: log} = get_sim() 28 + do_scan_log(log, i + count, count, []) 29 + end 30 + 31 + defp do_scan_log(_log, _i, 0, acc) do 32 + acc 33 + end 34 + 35 + defp do_scan_log(log, i, count, acc) do 36 + [{_i, message}] = :ets.lookup(log, i) 37 + do_scan_log(log, i - 1, count - 1, [message | acc]) 38 + end 39 + 7 40 defmacro debug(message) do 8 41 key = simulation_key() 9 42
+14 -2
test/trinity_test.exs
··· 53 53 end 54 54 55 55 test "scheduler" do 56 - result = Sim.run_simulation(fn -> 56 + message = Sim.run_simulation(fn -> 57 57 nodes = [:n1, :n2, :n3] 58 58 pids = 59 59 Enum.map(nodes, fn node -> ··· 99 99 Scheduler.yield(1000) 100 100 #dbg Scheduler.dump(), limit: :infinity 101 101 :success 102 + 103 + hash = SimLogger.get_hash() 104 + tail = SimLogger.log_tail(10) 105 + """ 106 + Simulation complete. 107 + 108 + Hash: #{hash} 109 + 110 + Log tail: 111 + #{Enum.join(tail, "\n")} 112 + """ 102 113 end, seed: 101) 103 114 104 - assert result == :success 115 + require Logger 116 + Logger.info(message) 105 117 end 106 118 end