Browse Source

ft: 代码修改

master
SisMaker 4 years ago
parent
commit
eb70d6c274
6 changed files with 36 additions and 36 deletions
  1. +2
    -2
      src/eTpf.erl
  2. +3
    -3
      src/tracer/tpTracerConsole.erl
  3. +14
    -14
      src/tracer/tpTracerFile.erl
  4. +1
    -1
      src/tracer/tpTracerNif.erl
  5. +1
    -1
      src/tracer/tpTracerSocket.erl
  6. +15
    -15
      test/lg_SUITE.erl

+ 2
- 2
src/eTpf.erl View File

@ -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);

src/tracer/tpRawConsoleTracer.erl → src/tracer/tpTracerConsole.erl View File

@ -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.

src/tracer/tpFileTracer.erl → src/tracer/tpTracerFile.erl View File

@ -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),

src/tracer/tpTracer.erl → src/tracer/tpTracerNif.erl View File

@ -1,4 +1,4 @@
-module(tpTracer).
-module(tpTracerNif).
%-behavior(erl_tracer).
-export([enabled/3]).

src/tracer/tpSocketTracer.erl → src/tracer/tpTracerSocket.erl View File

@ -1,4 +1,4 @@
-module(tpSocketTracer).
-module(tpTracerSocket).
-export([start_link/2]).
-export([init/2]).

+ 15
- 15
test/lg_SUITE.erl View File

@ -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(),

Loading…
Cancel
Save