From eb70d6c27453a6adb1a018093d21043fcc8ed94b Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Sun, 23 May 2021 15:27:56 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eTpf.erl | 4 +-- ...wConsoleTracer.erl => tpTracerConsole.erl} | 6 ++-- .../{tpFileTracer.erl => tpTracerFile.erl} | 28 ++++++++--------- src/tracer/{tpTracer.erl => tpTracerNif.erl} | 2 +- ...{tpSocketTracer.erl => tpTracerSocket.erl} | 2 +- test/lg_SUITE.erl | 30 +++++++++---------- 6 files changed, 36 insertions(+), 36 deletions(-) rename src/tracer/{tpRawConsoleTracer.erl => tpTracerConsole.erl} (87%) rename src/tracer/{tpFileTracer.erl => tpTracerFile.erl} (80%) rename src/tracer/{tpTracer.erl => tpTracerNif.erl} (97%) rename src/tracer/{tpSocketTracer.erl => tpTracerSocket.erl} (99%) diff --git a/src/eTpf.erl b/src/eTpf.erl index afd6a5c..b01b9ac 100644 --- a/src/eTpf.erl +++ b/src/eTpf.erl @@ -23,7 +23,7 @@ -spec trace(user_input()) -> ok. trace(Input) -> - trace(Input, tpRawConsoleTracer). + trace(Input, tpTracerConsole). -spec trace(user_input(), module()) -> ok. trace(Input, TracerMod) -> @@ -102,7 +102,7 @@ trace_input([{scope, Scope} | Tail], TracerState, Opts) -> %% or receives a function does. ExtraFlags = [running || maps:get(running, Opts, false)] ++ [send || maps:get(send, Opts, false)], [ - erlang:trace(PidPortSpec, true, [call, procs, timestamp, arity, return_to, set_on_spawn, {tracer, tpTracer, TracerState} | ExtraFlags]) + erlang:trace(PidPortSpec, true, [call, procs, timestamp, arity, return_to, set_on_spawn, {tracer, tpTracerNif, TracerState} | ExtraFlags]) || PidPortSpec <- Scope ], trace_input(Tail, TracerState, Opts); diff --git a/src/tracer/tpRawConsoleTracer.erl b/src/tracer/tpTracerConsole.erl similarity index 87% rename from src/tracer/tpRawConsoleTracer.erl rename to src/tracer/tpTracerConsole.erl index 6f4c793..5a7df32 100644 --- a/src/tracer/tpRawConsoleTracer.erl +++ b/src/tracer/tpTracerConsole.erl @@ -1,4 +1,4 @@ --module(tpRawConsoleTracer). +-module(tpTracerConsole). -export([start_link/2]). -export([init/1]). @@ -21,9 +21,9 @@ loop(Parent) -> receive {system, From, Request} -> sys:handle_system_msg(Request, From, Parent, ?MODULE, [], Parent); - Msg0 -> + RMsg -> %% Convert the event's monotonic time to its system time. - Msg = setelement(3, Msg0, erlang:time_offset(microsecond) + element(3, Msg0)), + Msg = setelement(3, RMsg, erlang:time_offset(microsecond) + element(3, RMsg)), erlang:display(Msg), loop(Parent) end. diff --git a/src/tracer/tpFileTracer.erl b/src/tracer/tpTracerFile.erl similarity index 80% rename from src/tracer/tpFileTracer.erl rename to src/tracer/tpTracerFile.erl index 4b027ed..dd15d8f 100644 --- a/src/tracer/tpFileTracer.erl +++ b/src/tracer/tpTracerFile.erl @@ -1,11 +1,14 @@ --module(tpFileTracer). +-module(tpTracerFile). -export([start_link/2]). -export([init/3]). --export([system_continue/3]). --export([system_terminate/4]). --export([system_code_change/4]). +%% sys callbacks +-export([ + system_continue/3 + , system_terminate/4 + , system_code_change/4 +]). -record(state, { parent :: pid(), @@ -30,21 +33,17 @@ init(Parent, Nth, Opts) -> %% We need to trap exit signals in order to shutdown properly. process_flag(trap_exit, true), %% No need to close the file, it'll be closed when the process exits. - Filename = filename:flatten([ - maps:get(filename_prefix, Opts, "traces.lz4"), - ".", integer_to_list(Nth)]), + Filename = filename:flatten([maps:get(filename_prefix, Opts, "traces.lz4"), ".", integer_to_list(Nth)]), {ok, IoDevice} = file:open(Filename, [write, raw]), - loop(#state{parent = Parent, filename = Filename, io_device = IoDevice, - max_size = maps:get(max_size, Opts, infinity), - events_per_frame = maps:get(events_per_frame, Opts, 100000)}). + State = #state{parent = Parent, filename = Filename, io_device = IoDevice, max_size = maps:get(max_size, Opts, infinity), events_per_frame = maps:get(events_per_frame, Opts, 100000)}, + loop(State). -loop(State = #state{parent = Parent, size = Size, io_device = IoDevice, - events_per_frame = MaxEvents, events_this_frame = NumEvents0, buffer = Buffer0}) -> +loop(State = #state{parent = Parent, size = Size, io_device = IoDevice, events_per_frame = MaxEvents, events_this_frame = NumEvents0, buffer = Buffer0}) -> receive - {'EXIT', Parent, Reason} -> - terminate(Reason, State); {system, From, Request} -> sys:handle_system_msg(Request, From, Parent, ?MODULE, [], State); + {'EXIT', Parent, Reason} -> + terminate(Reason, State); Msg0 -> Msg = tpTerm:truncate(Msg0), Bin = term_to_binary(Msg), @@ -62,6 +61,7 @@ loop(State = #state{parent = Parent, size = Size, io_device = IoDevice, end end. +%%IMY-todo 干掉这个函数 同时对于旋转的文件名 使用时间 年月日时分 maybe_rotate(State = #state{filename = Filename, size = Size, max_size = MaxSize, io_device = OldIoDevice}) when Size > MaxSize -> ok = file:close(OldIoDevice), diff --git a/src/tracer/tpTracer.erl b/src/tracer/tpTracerNif.erl similarity index 97% rename from src/tracer/tpTracer.erl rename to src/tracer/tpTracerNif.erl index 8bfb386..cbdde3d 100644 --- a/src/tracer/tpTracer.erl +++ b/src/tracer/tpTracerNif.erl @@ -1,4 +1,4 @@ --module(tpTracer). +-module(tpTracerNif). %-behavior(erl_tracer). -export([enabled/3]). diff --git a/src/tracer/tpSocketTracer.erl b/src/tracer/tpTracerSocket.erl similarity index 99% rename from src/tracer/tpSocketTracer.erl rename to src/tracer/tpTracerSocket.erl index a414459..0029bf5 100644 --- a/src/tracer/tpSocketTracer.erl +++ b/src/tracer/tpTracerSocket.erl @@ -1,4 +1,4 @@ --module(tpSocketTracer). +-module(tpTracerSocket). -export([start_link/2]). -export([init/2]). diff --git a/test/lg_SUITE.erl b/test/lg_SUITE.erl index 2b0c24f..16d0dbe 100644 --- a/test/lg_SUITE.erl +++ b/test/lg_SUITE.erl @@ -18,14 +18,14 @@ groups() -> app(Config) -> doc("Trace a specific application."), - eTpf:trace({app, stdlib}, tpFileTracer, config(priv_dir, Config) ++ "/app.lz4"), + eTpf:trace({app, stdlib}, tpTracerFile, config(priv_dir, Config) ++ "/app.lz4"), lists:seq(1, 10), eTpf:stop(), do_ensure_decompress(config(priv_dir, Config) ++ "/app.lz4"). callback(Config) -> doc("Trace using patterns from a callback function."), - eTpf:trace({callback, ?MODULE, do_callback}, tpFileTracer, + eTpf:trace({callback, ?MODULE, do_callback}, tpTracerFile, config(priv_dir, Config) ++ "/callback.lz4"), lists:seq(1, 10), eTpf:stop(), @@ -37,7 +37,7 @@ do_callback() -> callgrind_running(Config) -> doc("Save events to files on disk then build callgrind files."), PrivDir = config(priv_dir, Config), - eTpf:trace([{scope, [self()]}, ?MODULE, {app, stdlib}], tpFileTracer, + eTpf:trace([{scope, [self()]}, ?MODULE, {app, stdlib}], tpTracerFile, PrivDir ++ "/callgrind_running.lz4", #{mode => profile, running => true}), do_callgrind_running(), @@ -75,7 +75,7 @@ callgrind_running_cycle(Config) -> doc("Save events to files on disk then build callgrind files. " "Create a recursive cycle using two functions calling each other."), PrivDir = config(priv_dir, Config), - eTpf:trace([{scope, [self()]}, ?MODULE, {app, stdlib}], tpFileTracer, + eTpf:trace([{scope, [self()]}, ?MODULE, {app, stdlib}], tpTracerFile, PrivDir ++ "/callgrind_running_cycle.lz4", #{mode => profile, running => true}), do_callgrind_running_cycle(), @@ -126,7 +126,7 @@ do_callgrind_running_cycle2(Ref) -> file_tracer(Config) -> doc("Save events to files on disk."), - eTpf:trace(lists, tpFileTracer, config(priv_dir, Config) ++ "/file_tracer.lz4"), + eTpf:trace(lists, tpTracerFile, config(priv_dir, Config) ++ "/file_tracer.lz4"), lists:seq(1, 10), eTpf:stop(), do_ensure_decompress(config(priv_dir, Config) ++ "/file_tracer.lz4"). @@ -134,7 +134,7 @@ file_tracer(Config) -> file_tracer_rotation(Config) -> doc("Save events to files on disk; rotate the files if they get too big."), Prefix = config(priv_dir, Config) ++ "/file_tracer.lz4", - eTpf:trace(lists, tpFileTracer, #{ + eTpf:trace(lists, tpTracerFile, #{ filename_prefix => Prefix, max_size => 100, %% Intentionally low. events_per_frame => 10 %% Needed to trigger the rotation, default is too high. @@ -151,14 +151,14 @@ file_tracer_rotation(Config) -> mod(Config) -> doc("Trace a specific module."), - eTpf:trace(lists, tpFileTracer, config(priv_dir, Config) ++ "/mod.lz4"), + eTpf:trace(lists, tpTracerFile, config(priv_dir, Config) ++ "/mod.lz4"), lists:seq(1, 10), eTpf:stop(), do_ensure_decompress(config(priv_dir, Config) ++ "/mod.lz4"). profile_mode(Config) -> doc("Trace a specific module in profile mode."), - eTpf:trace(lists, tpFileTracer, config(priv_dir, Config) ++ "/profile_mode.lz4", + eTpf:trace(lists, tpTracerFile, config(priv_dir, Config) ++ "/profile_mode.lz4", #{mode => profile}), lists:seq(1, 10), eTpf:stop(), @@ -175,7 +175,7 @@ raw_console_tracer(_) -> running_true(Config) -> doc("Trace a specific module with running option enabled."), - eTpf:trace(lists, tpFileTracer, config(priv_dir, Config) ++ "/running_true.lz4", + eTpf:trace(lists, tpTracerFile, config(priv_dir, Config) ++ "/running_true.lz4", #{running => true}), lists:seq(1, 10), eTpf:stop(), @@ -183,7 +183,7 @@ running_true(Config) -> send_true(Config) -> doc("Trace a specific module with send option enabled."), - eTpf:trace(lists, tpFileTracer, config(priv_dir, Config) ++ "/send_true.lz4", + eTpf:trace(lists, tpTracerFile, config(priv_dir, Config) ++ "/send_true.lz4", #{send => true}), Self = self(), %% Send a message to and from an existing process. @@ -204,7 +204,7 @@ send_true(Config) -> socket_tracer(_) -> doc("Send events to a socket."), Port = 61234, - eTpf:trace(lists, tpSocketTracer, Port, #{pool_size => 1}), + eTpf:trace(lists, tpTracerSocket, Port, #{pool_size => 1}), {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {packet, 2}, {active, true}]), lists:seq(1, 10), @@ -214,7 +214,7 @@ socket_tracer(_) -> socket_tracer_client(Config) -> doc("Send events to a socket client."), Port = 61234, - eTpf:trace(lists, tpSocketTracer, Port, #{pool_size => 1}), + eTpf:trace(lists, tpTracerSocket, Port, #{pool_size => 1}), BaseFilename = config(priv_dir, Config) ++ "/socket_tracer_client.lz4", {ok, Pid} = tpSocketClient:start_link(Port, BaseFilename), timer:sleep(1000), @@ -229,7 +229,7 @@ socket_tracer_client(Config) -> socket_tracer_many(_) -> doc("Send events to many sockets."), Port = 61234, - eTpf:trace(lists, tpSocketTracer, Port, #{pool_size => 5}), + eTpf:trace(lists, tpTracerSocket, Port, #{pool_size => 5}), {ok, _} = gen_tcp:connect("localhost", Port, []), {ok, _} = gen_tcp:connect("localhost", Port + 1, []), {ok, _} = gen_tcp:connect("localhost", Port + 2, []), @@ -241,7 +241,7 @@ socket_tracer_many(_) -> socket_tracer_reconnect(_) -> doc("Confirm we can reconnect to the tracer."), Port = 61234, - eTpf:trace(lists, tpSocketTracer, Port, #{pool_size => 1}), + eTpf:trace(lists, tpTracerSocket, Port, #{pool_size => 1}), {ok, Socket0} = gen_tcp:connect("localhost", Port, [binary, {packet, 2}, {active, true}]), ok = gen_tcp:close(Socket0), @@ -267,7 +267,7 @@ stop_while_trace_is_running(Config) -> doc("Stop tracing while events are still coming in."), Self = self(), Pid = spawn_link(fun() -> Self ! {self(), continue}, lists:seq(1, 10000000) end), - eTpf:trace([{scope, [Pid]}, lists], tpFileTracer, + eTpf:trace([{scope, [Pid]}, lists], tpTracerFile, config(priv_dir, Config) ++ "/stop_while_trace_is_running.lz4"), receive {Pid, continue} -> ok after 100 -> error(timeout) end, eTpf:stop(),