Skip to content

Commit

Permalink
feat(tool): new quicer_nif:malloc_stats/0
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Oct 30, 2024
1 parent ad9239f commit 815c8aa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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 @@ -1703,6 +1706,16 @@ do_malloc_trim(__unused_parm__ ErlNifEnv *env,
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;
}

static ErlNifFunc nif_funcs[] = {
/* | name | arity| funptr | flags|
*
Expand Down Expand Up @@ -1755,7 +1768,10 @@ static ErlNifFunc nif_funcs[] = {
{ "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
9 changes: 8 additions & 1 deletion src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
]).

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

%% For tests only
-export([
Expand Down Expand Up @@ -354,6 +357,10 @@ get_stream_rid(_Handle) ->
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
12 changes: 12 additions & 0 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@

%% Toolings
tc_malloc_trim/1,
tc_malloc_stats/1,

%% Versions test
tc_abi_version/1
Expand Down Expand Up @@ -229,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 @@ -3199,6 +3208,9 @@ tc_setopt_congestion_control_algorithm(Config) ->
tc_malloc_trim(_) ->
quicer_nif:malloc_trim().

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

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

0 comments on commit 815c8aa

Please sign in to comment.