<commit_before>%%
%% %CopyrightBegin%
%%
%% Copyright <NAME> 2013. All Rights Reserved.
%%
%% %CopyrightEnd%
%%
-module(epl_tracer).
-behaviour(gen_server).

-include_lib("epl/include/epl.hrl").

%% API
-export([start_link/1,
         subscribe/2,
         unsubscribe/2,
         command/3,
         trace_pid/1,
         enable_ets_call_tracing/1,
         disable_ets_call_tracing/1]).

%% gen_server callbacks
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

-record(state, {subscribers = [],
                ref,
                remote_pid,
                timeout = 0}).

-define(POLL, 5000).

%% ===================================================================
%% API functions
%% ===================================================================

%% @doc Starts epl_tracer gen_server process.
-spec start_link(Node :: atom()) -> {ok, pid()} | ignore | {error, term()}.
start_link(Node) ->
    gen_server:start_link({local, Node}, ?MODULE, Node, []).


%% @doc Adds provided `Pid' to `Node' tracer's subscribers list.
-spec subscribe(Node :: atom(), Pid :: pid()) -> ok.
subscribe(Node, Pid) ->
    gen_server:call(Node, {subscribe, Pid}).

%% @doc Removes provided `Pid' from `Node' tracer's subscribers list.
-spec unsubscribe(Node :: atom(), Pid :: pid()) -> ok.
unsubscribe(Node, Pid) ->
    gen_server:call(Node, {unsubscribe, Pid}).

%% @doc Runs provided `Fun' with `Args' on `Node'.
-spec command(Node :: atom(), Fun :: fun(), Args :: list()) -> tuple().
command(Node, Fun, Args) ->
    gen_server:call(Node, {command, Fun, Args}).

%% @doc Traces provied `Pid'.
-spec trace_pid(Pid :: pid()) -> ok.
trace_pid(Pid) ->
    Node = erlang:node(Pid),
    gen_server:call(Node, {trace_pid, Pid}).

%% @doc Enables tracing for ets insert/lookup functions calls on the `Node'.
-spec enable_ets_call_tracing(Node :: atom()) -> ok.
enable_ets_call_tracing(Node) ->
    gen_server:call(Node, enable_ets_call_tracing).

%% @doc Disables tracing for ets insert/lookup functions calls on the `Node'.
-spec enable_ets_call_tracing(Node :: atom()) -> ok.
disable_ets_call_tracing(Node) ->
    gen_server:call(Node, disable_ets_call_tracing).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init(Node) ->
    RemoteFunStr =
        "fun (F, Ref, init) ->
                 %% create neccessary tables
                 EtsOptions = [named_table, ordered_set, private],
                 epl_receive   = ets:new(epl_receive,   EtsOptions),
                 epl_send      = ets:new(epl_send,      EtsOptions),
                 epl_send_self = ets:new(epl_send_self, EtsOptions),
                 epl_spawn     = ets:new(epl_spawn,     EtsOptions),
                 epl_exit      = ets:new(epl_exit,      EtsOptions),
                 epl_trace     = ets:new(epl_trace, [named_table,bag,private]),
                 epl_ets_func  = ets:new(epl_ets_func,  
                                         [named_table, duplicate_bag, private]),

                 %% turn on tracer for all processes<commit_msg>Fix spec bug<commit_after>%%
%% %CopyrightBegin%
%%
%% Copyright <NAME> 2013. All Rights Reserved.
%%
%% %CopyrightEnd%
%%
-module(epl_tracer).
-behaviour(gen_server).

-include_lib("epl/include/epl.hrl").

%% API
-export([start_link/1,
         subscribe/2,
         unsubscribe/2,
         command/3,
         trace_pid/1,
         enable_ets_call_tracing/1,
         disable_ets_call_tracing/1]).

%% gen_server callbacks
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

-record(state, {subscribers = [],
                ref,
                remote_pid,
                timeout = 0}).

-define(POLL, 5000).

%% ===================================================================
%% API functions
%% ===================================================================

%% @doc Starts epl_tracer gen_server process.
-spec start_link(Node :: atom()) -> {ok, pid()} | ignore | {error, term()}.
start_link(Node) ->
    gen_server:start_link({local, Node}, ?MODULE, Node, []).


%% @doc Adds provided `Pid' to `Node' tracer's subscribers list.
-spec subscribe(Node :: atom(), Pid :: pid()) -> ok.
subscribe(Node, Pid) ->
    gen_server:call(Node, {subscribe, Pid}).

%% @doc Removes provided `Pid' from `Node' tracer's subscribers list.
-spec unsubscribe(Node :: atom(), Pid :: pid()) -> ok.
unsubscribe(Node, Pid) ->
    gen_server:call(Node, {unsubscribe, Pid}).

%% @doc Runs provided `Fun' with `Args' on `Node'.
-spec command(Node :: atom(), Fun :: fun(), Args :: list()) -> tuple().
command(Node, Fun, Args) ->
    gen_server:call(Node, {command, Fun, Args}).

%% @doc Traces provied `Pid'.
-spec trace_pid(Pid :: pid()) -> ok.
trace_pid(Pid) ->
    Node = erlang:node(Pid),
    gen_server:call(Node, {trace_pid, Pid}).

%% @doc Enables tracing for ets insert/lookup functions calls on the `Node'.
-spec enable_ets_call_tracing(Node :: atom()) -> ok.
enable_ets_call_tracing(Node) ->
    gen_server:call(Node, enable_ets_call_tracing).

%% @doc Disables tracing for ets insert/lookup functions calls on the `Node'.
-spec disable_ets_call_tracing(Node :: atom()) -> ok.
disable_ets_call_tracing(Node) ->
    gen_server:call(Node, disable_ets_call_tracing).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
init(Node) ->
    RemoteFunStr =
        "fun (F, Ref, init) ->
                 %% create neccessary tables
                 EtsOptions = [named_table, ordered_set, private],
                 epl_receive   = ets:new(epl_receive,   EtsOptions),
                 epl_send      = ets:new(epl_send,      EtsOptions),
                 epl_send_self = ets:new(epl_send_self, EtsOptions),
                 epl_spawn     = ets:new(epl_spawn,     EtsOptions),
                 epl_exit      = ets:new(epl_exit,      EtsOptions),
                 epl_trace     = ets:new(epl_trace, [named_table,bag,private]),
                 epl_ets_func  = ets:new(epl_ets_func,  
                                         [named_table, duplicate_bag, private]),

                 %% turn on tracer for all processes
{'fraction_non_alphanumeric': 0.2197630922693267, 'fraction_numerical': 0.00685785536159601, 'mean_word_length': 2.9295774647887325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2237666', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1982, 'n_words': 427}