Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): add some mem related toolings (internal) #318

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions c_src/quicer_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,18 @@ cleanup_owner_signals(QuicerStreamCTX *s_ctx)
OwnerSignalQueueDestroy(s_ctx->sig_queue);
s_ctx->sig_queue = NULL;
}

ERL_NIF_TERM
copy_stream_handle(ErlNifEnv *env,
__unused_parm__ int argc,
const ERL_NIF_TERM argv[])
{
QuicerStreamCTX *ctx = NULL;
if (!enif_get_resource(env, argv[0], ctx_stream_t, (void **)&ctx))
{
return enif_make_badarg(env);
}
assert(ctx != NULL);
enif_make_copy(ctx->env, ctx->eHandle);
return ATOM_OK;
}
3 changes: 3 additions & 0 deletions c_src/quicer_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,7 @@ void cache_stream_id(QuicerStreamCTX *s_ctx);

void cleanup_owner_signals(QuicerStreamCTX *s_ctx);

ERL_NIF_TERM
copy_stream_handle(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

#endif // __QUICER_CTX_H_
31 changes: 31 additions & 0 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ limitations under the License.
#include "quicer_listener.h"
#include "quicer_vsn.h"

#if defined(__linux__)
#include <malloc.h>
#endif

#include <openssl/err.h>
#include <openssl/ssl.h>

Expand Down Expand Up @@ -1692,6 +1696,28 @@ make_event(ErlNifEnv *env,
prop); // 4th element, event props :: any()) //
}

#if defined(__linux__)
ERL_NIF_TERM
do_malloc_trim(__unused_parm__ ErlNifEnv *env,
__unused_parm__ int argc,
__unused_parm__ const ERL_NIF_TERM argv[])
{
CXPLAT_FRE_ASSERT(argc == 0);
malloc_trim(0);
return ATOM_OK;
}

ERL_NIF_TERM
do_malloc_stats(__unused_parm__ ErlNifEnv *env,
__unused_parm__ int argc,
__unused_parm__ const ERL_NIF_TERM argv[])
{
CXPLAT_FRE_ASSERT(argc == 0);
malloc_stats();
return ATOM_OK;
}
#endif

static ErlNifFunc nif_funcs[] = {
/* | name | arity| funptr | flags|
*
Expand Down Expand Up @@ -1741,8 +1767,13 @@ static ErlNifFunc nif_funcs[] = {
{ "get_conn_owner", 1, get_conn_owner1, 0},
{ "get_stream_owner", 1, get_stream_owner1, 0},
{ "get_listener_owner", 1, get_listener_owner1, 0},
{ "copy_stream_handle", 1, copy_stream_handle, 0},
/* for testing */
{ "mock_buffer_sig", 3, mock_buffer_sig, 0},
#if defined(__linux__)
{ "malloc_trim", 0, do_malloc_trim, 0},
{ "malloc_stats", 0, do_malloc_stats, 0},
#endif
#ifdef QUICER_USE_SNK
{ "set_snab_kc_pid", 1, set_snab_kc_pid, 0},
{ "get_snab_kc_pid", 0, get_snab_kc_pid, 0},
Expand Down
19 changes: 19 additions & 0 deletions src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
get_stream_rid/1
]).

%% tools
-export([
malloc_trim/0,
malloc_stats/0
]).

%% For tests only
-export([
open_connection/0,
Expand All @@ -65,6 +71,7 @@
get_conn_owner/1,
get_stream_owner/1,
get_listener_owner/1,
copy_stream_handle/1,
mock_buffer_sig/3,
set_snab_kc_pid/1,
get_snab_kc_pid/0
Expand Down Expand Up @@ -346,6 +353,14 @@ get_conn_rid(_Handle) ->
get_stream_rid(_Handle) ->
erlang:nif_error(nif_library_not_loaded).

-spec malloc_trim() -> ok.
malloc_trim() ->
erlang:nif_error(nif_library_not_loaded).

-spec malloc_stats() -> ok.
malloc_stats() ->
erlang:nif_error(nif_library_not_loaded).

-spec controlling_process(connection_handle() | stream_handle(), pid()) ->
ok
| {error, closed | badarg | owner_dead | not_owner}.
Expand Down Expand Up @@ -385,6 +400,10 @@ get_connections() ->
get_connections(_RegHandle) ->
erlang:nif_error(nif_library_not_loaded).

-spec copy_stream_handle(stream_handle()) -> {ok, stream_handle()} | {error, badarg}.
copy_stream_handle(_H) ->
erlang:nif_error(nif_library_not_loaded).

%% @doc enable signal buffering, used in stream handoff.
%% * not exposed API.
-spec enable_sig_buffer(stream_handle()) -> ok.
Expand Down
22 changes: 20 additions & 2 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
tc_peercert_server/1,
tc_peercert_server_nocert/1,

%% Toolings
tc_malloc_trim/1,
tc_malloc_stats/1,

%% Versions test
tc_abi_version/1
%% testcase to verify env works
Expand Down Expand Up @@ -226,6 +230,14 @@ end_per_group(_Groupname, _Config) ->
%%%===================================================================
%%% Testcase specific setup/teardown
%%%===================================================================
init_per_testcase(TC, Config) when
TC =:= tc_malloc_trim orelse
TC =:= tc_malloc_stats
->
case os:type() of
{unix, darwin} -> {skip, "Not runnable on MacOS"};
_ -> Config
end;
init_per_testcase(_TestCase, Config) ->
quicer_test_lib:cleanup_msquic(),
[{timetrap, 5000} | Config].
Expand Down Expand Up @@ -838,10 +850,10 @@ tc_stream_controlling_process_demon(Config) ->
end
),
receive
{'DOWN', MonRef, process, NewOwner, {Res, Stm}} ->
{'DOWN', MonRef, process, OldOwner, {Res, Stm}} ->
ct:pal("Set controlling_process res: ~p", [Res])
end,
?assertEqual({error, owner_dead}, quicer:controlling_process(Stm, NewOwner)),
?assertEqual({error, owner_dead}, quicer:controlling_process(Stm, OldOwner)),
ok = quicer:setopt(Stm, active, true),
{ok, _Len} = quicer:send(Stm, <<"owner_changed">>),
receive
Expand Down Expand Up @@ -3193,6 +3205,12 @@ tc_setopt_congestion_control_algorithm(Config) ->
SPid ! done,
ok.

tc_malloc_trim(_) ->
quicer_nif:malloc_trim().

tc_malloc_stats(_) ->
quicer_nif:malloc_stats().

%%% ====================
%%% Internal helpers
%%% ====================
Expand Down
Loading