Skip to content

Commit

Permalink
feat(tool): mem tool quicer_nif:malloc_trim
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Oct 30, 2024
1 parent 32ef743 commit 01eac96
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 12 additions & 0 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include "quicer_listener.h"
#include "quicer_vsn.h"

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

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

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;
}

static ErlNifFunc nif_funcs[] = {
/* | name | arity| funptr | flags|
*
Expand Down Expand Up @@ -1744,6 +1755,7 @@ static ErlNifFunc nif_funcs[] = {
{ "copy_stream_handle", 1, copy_stream_handle, 0},
/* for testing */
{ "mock_buffer_sig", 3, mock_buffer_sig, 0},
{ "malloc_trim", 0, do_malloc_trim, 0},
#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
7 changes: 7 additions & 0 deletions src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
get_stream_rid/1
]).

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

%% For tests only
-export([
open_connection/0,
Expand Down Expand Up @@ -347,6 +350,10 @@ 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 controlling_process(connection_handle() | stream_handle(), pid()) ->
ok
| {error, closed | badarg | owner_dead | not_owner}.
Expand Down
10 changes: 8 additions & 2 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@
tc_peercert_server/1,
tc_peercert_server_nocert/1,

%% Toolings
tc_malloc_trim/1,

%% Versions test
tc_abi_version/1
%% testcase to verify env works
Expand Down Expand Up @@ -838,10 +841,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 +3196,9 @@ tc_setopt_congestion_control_algorithm(Config) ->
SPid ! done,
ok.

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

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

0 comments on commit 01eac96

Please sign in to comment.