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

Make sure that clock_gettime will work correctly on different Linux f… #8

Merged
merged 1 commit into from
Mar 28, 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
19 changes: 18 additions & 1 deletion c_src/betree.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,18 @@ static ERL_NIF_TERM nif_betree_make(ErlNifEnv* env, int argc, const ERL_NIF_TERM
return retval;
}

// Make sure that clock_gettime will work correctly on different Linux flavors.
int reverse_get_clock_type(int ct) {
int res = CLOCK_MONOTONIC;
switch (ct) {
case 0 : res = CLOCK_REALTIME; break;
case 1 : res = CLOCK_MONOTONIC; break;
case 2 : res = CLOCK_PROCESS_CPUTIME_ID; break;
case 3 : res = CLOCK_THREAD_CPUTIME_ID; break;
}
return res;
}

static ERL_NIF_TERM nif_betree_make_event(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ERL_NIF_TERM retval;
Expand All @@ -783,6 +795,7 @@ static ERL_NIF_TERM nif_betree_make_event(ErlNifEnv* env, int argc, const ERL_NI
if (!enif_get_int(env, argv[2], &clock_type)) {
return enif_make_badarg(env);
}
clock_type = reverse_get_clock_type(clock_type);
struct timespec start, done;
clock_gettime(clock_type, &start);

Expand Down Expand Up @@ -1055,10 +1068,11 @@ static ERL_NIF_TERM nif_betree_search_t(ErlNifEnv* env, int argc, const ERL_NIF_
return enif_make_badarg(env);
}

int clock_type;
int clock_type = 0;
if (!enif_get_int(env, argv[2], &clock_type)) {
return enif_make_badarg(env);
}
clock_type = reverse_get_clock_type(clock_type);
struct timespec start, done;
clock_gettime(clock_type, &start);
ERL_NIF_TERM search_res = nif_betree_search(env, argc - 1, argv);
Expand All @@ -1085,6 +1099,7 @@ static ERL_NIF_TERM nif_betree_search_evt(ErlNifEnv* env, int argc, const ERL_NI
if (!enif_get_int(env, argv[2], &clock_type)) {
return enif_make_badarg(env);
}
clock_type = reverse_get_clock_type(clock_type);
struct timespec start, done;
clock_gettime(clock_type, &start);

Expand Down Expand Up @@ -1136,6 +1151,7 @@ static ERL_NIF_TERM nif_betree_search_evt_ids(ErlNifEnv* env, int argc, const ER
if (!enif_get_int(env, argv[3], &clock_type)) {
return enif_make_badarg(env);
}
clock_type = reverse_get_clock_type(clock_type);
struct timespec start, done;
clock_gettime(clock_type, &start);

Expand Down Expand Up @@ -1217,6 +1233,7 @@ static ERL_NIF_TERM nif_betree_search_ids(ErlNifEnv* env, int argc, const ERL_NI
if (!enif_get_int(env, argv[3], &clock_type)) {
return enif_make_badarg(env);
}
clock_type = reverse_get_clock_type(clock_type);
struct timespec start, done;
clock_gettime(clock_type, &start);

Expand Down
38 changes: 16 additions & 22 deletions src/erl_betree.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
search_iterator_release/1
]).

-inline([check_clock_type/1]).

-define(CLOCK_REALTIME, 0).
-define(CLOCK_MONOTONIC, 6).
-define(CLOCK_PROCESS_CPUTIME_ID, 12).
-define(CLOCK_THREAD_CPUTIME_ID, 16).
% -define(CLOCK_REALTIME, 0).
-define(CLOCK_MONOTONIC, 1).
% -define(CLOCK_PROCESS_CPUTIME_ID, 2).
% -define(CLOCK_THREAD_CPUTIME_ID, 3).

betree_make(Domains) ->
erl_betree_nif:betree_make(Domains).
betree_make_event(Betree, Event) ->
betree_make_event(Betree, Event, ?CLOCK_MONOTONIC).

betree_make_event({_, Betree}, Event, CLockType) ->
erl_betree_nif:betree_make_event(Betree, Event, check_clock_type(CLockType));
betree_make_event(Betree, Event, CLockType) ->
erl_betree_nif:betree_make_event(Betree, Event, check_clock_type(CLockType)).
betree_make_event({_, Betree}, Event, ClockType) when is_integer(ClockType) ->
erl_betree_nif:betree_make_event(Betree, Event, ClockType);
betree_make_event(Betree, Event, ClockType) when is_integer(ClockType) ->
erl_betree_nif:betree_make_event(Betree, Event, ClockType).
betree_make_sub(Betree, SubId, Constants, Expr) ->
erl_betree_nif:betree_make_sub(Betree, SubId, Constants, Expr).
betree_insert_sub(Betree, Sub) ->
Expand All @@ -45,10 +44,10 @@ betree_search(Betree, Event) ->

% @doc Calculates time spend in NIF.
% Time value is in microseconds - the erlang:timestamp resolution.
betree_search(Betree, Event, CLockType) when is_list(Event) ->
erl_betree_nif:betree_search(Betree, Event, check_clock_type(CLockType));
betree_search(Betree, Event, CLockType) when is_reference(Event) ->
erl_betree_nif:betree_search_evt(Betree, Event, check_clock_type(CLockType)).
betree_search(Betree, Event, ClockType) when is_list(Event), is_integer(ClockType) ->
erl_betree_nif:betree_search(Betree, Event, ClockType);
betree_search(Betree, Event, ClockType) when is_reference(Event), is_integer(ClockType) ->
erl_betree_nif:betree_search_evt(Betree, Event, ClockType).

betree_write_dot(Betree, FileName) when is_list(FileName) ->
erl_betree_nif:betree_write_dot(Betree, FileName).
Expand All @@ -61,15 +60,10 @@ betree_search_ids(Betree, Event, Ids) ->
% Time value is in microseconds - the erlang:timestamp resolution.
betree_search_ids(_Betree, _Event, [], _CLockType) ->
{0, []};
betree_search_ids(Betree, Event, Ids, CLockType) when is_list(Event) ->
erl_betree_nif:betree_search_ids(Betree, Event, Ids, check_clock_type(CLockType));
betree_search_ids(Betree, Event, Ids, CLockType) when is_reference(Event) ->
erl_betree_nif:betree_search_evt(Betree, Event, Ids, check_clock_type(CLockType)).

check_clock_type(?CLOCK_REALTIME) -> ?CLOCK_REALTIME;
check_clock_type(?CLOCK_PROCESS_CPUTIME_ID) -> ?CLOCK_PROCESS_CPUTIME_ID;
check_clock_type(?CLOCK_THREAD_CPUTIME_ID) -> ?CLOCK_THREAD_CPUTIME_ID;
check_clock_type(_) -> ?CLOCK_MONOTONIC.
betree_search_ids(Betree, Event, Ids, ClockType) when is_list(Event), is_integer(ClockType) ->
erl_betree_nif:betree_search_ids(Betree, Event, Ids, ClockType);
betree_search_ids(Betree, Event, Ids, ClockType) when is_reference(Event), is_integer(ClockType) ->
erl_betree_nif:betree_search_evt(Betree, Event, Ids, ClockType).

search_iterator(Betree, Event) ->
erl_betree_nif:search_iterator(Betree, Event).
Expand Down
Loading