chewbranca commented on code in PR #5491: URL: https://github.com/apache/couchdb/pull/5491#discussion_r2155940366
########## src/couch_stats/src/csrt_util.erl: ########## @@ -0,0 +1,485 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(csrt_util). + +-export([ + is_enabled/0, + is_enabled_init_p/0, + get_pid_ref/0, + get_pid_ref/1, + set_pid_ref/1, + should_track_init_p/2, + tnow/0, + tutc/0, + tutc/1 +]). + +%% JSON Conversion API +-export([ + convert_type/1, + convert_pidref/1, + convert_pid/1, + convert_ref/1, + to_json/1 +]). + +%% Delta API +-export([ + add_delta/2, + extract_delta/1, + get_delta/1, + get_delta_a/0, + get_delta_zero/0, + maybe_add_delta/1, + maybe_add_delta/2, + make_delta/1, + make_dt/2, + make_dt/3, + rctx_delta/2, + set_delta_a/1, + set_delta_zero/1 +]). + +%% Extra niceties and testing facilities +-export([ + set_fabric_init_p/2, + set_fabric_init_p/3, + map_to_rctx/1, + field/2 +]). + +-include_lib("couch_stats_resource_tracker.hrl"). + +-spec is_enabled() -> boolean(). +is_enabled() -> + config:get_boolean(?CSRT, "enabled", true). + +-spec is_enabled_init_p() -> boolean(). +is_enabled_init_p() -> + config:get_boolean(?CSRT_INIT_P, "enabled", true). + +-spec should_track_init_p(Mod :: atom(), Func :: atom()) -> boolean(). +should_track_init_p(fabric_rpc, Func) -> + is_enabled_init_p() andalso config:get_boolean(?CSRT_INIT_P, fabric_conf_key(Func), false); +should_track_init_p(_Mod, _Func) -> + false. + +%% Monotnonic time now in native format using time forward only event tracking +-spec tnow() -> integer(). +tnow() -> + erlang:monotonic_time(). + +%% Get current system time in UTC RFC 3339 format +-spec tutc() -> calendar:rfc3339_string(). +tutc() -> + tutc(tnow()). + +%% Convert a integer system time in milliseconds into UTC RFC 3339 format +-spec tutc(Time :: integer()) -> calendar:rfc3339_string(). +tutc(Time0) when is_integer(Time0) -> + Unit = millisecond, + Time1 = Time0 + erlang:time_offset(), + Time = erlang:convert_time_unit(Time1, native, Unit), + calendar:system_time_to_rfc3339(Time, [{unit, Unit}, {offset, "z"}]). + +%% Returns dt (delta time) in microseconds +%% @equiv make_dt(A, B, microsecond) +-spec make_dt(A, B) -> pos_integer() when + A :: integer(), + B :: integer(). +make_dt(A, B) -> + make_dt(A, B, microsecond). + +%% Returns monotonic dt (delta time) in specified time_unit() +-spec make_dt(A, B, Unit) -> pos_integer() when + A :: integer(), + B :: integer(), + Unit :: erlang:time_unit(). +make_dt(A, A, _Unit) when is_integer(A) -> + %% Handle edge case when monotonic_time()'s are equal + %% Always return a non zero value so we don't divide by zero + %% This always returns 1, independent of unit, as that's the smallest + %% possible positive integer value delta. + 1; +make_dt(A, B, Unit) when is_integer(A) andalso is_integer(B) andalso B > A -> + A1 = erlang:convert_time_unit(A, native, Unit), + B1 = erlang:convert_time_unit(B, native, Unit), Review Comment: Sure, have to add an `abs/2` call in there though, as native time unit can be a negative integer and we only want *Natural* numbers: ``` (node1@127.0.0.1)1> T0 = csrt_util:tnow(), timer:sleep(497), T1 = csrt_util:tnow(), erlang:convert_time_unit(abs(T1 - T0), native, millisecond). 497 ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org