diff --git a/c_src/betree.c b/c_src/betree.c index c3b1563..08c385e 100644 --- a/c_src/betree.c +++ b/c_src/betree.c @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/src/erl_betree.erl b/src/erl_betree.erl index 4cda8d0..d0add44 100644 --- a/src/erl_betree.erl +++ b/src/erl_betree.erl @@ -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) -> @@ -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). @@ -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).