Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use os:timestamp/0 instead of erlang:now/0

It's faster and consumes less CPU. Learned from WhatsApp
scaling presentation.

Change-Id: I6d44da4cec0c0129a77cb267125e2e6bf7b27539
Reviewed-on: http://review.couchbase.org/14602
Reviewed-by: Damien Katz <damien@couchbase.com>
Tested-by: Damien Katz <damien@couchbase.com>
  • Loading branch information...
commit 051c82074857673725ef03fbe53e76b58383d528 1 parent ce625bf
@fdmanana fdmanana authored Damienkatz committed
View
6 src/couch_set_view/src/couch_set_view_compactor.erl
@@ -54,7 +54,7 @@ compact_group(Group, EmptyGroup, SetName, FileName, CompactFileName) ->
fd = GroupFd,
sig = GroupSig
} = Group,
- StartTime = now(),
+ StartTime = os:timestamp(),
#set_view_group{
id_btree = EmptyIdBtree,
@@ -116,7 +116,7 @@ compact_group(Group, EmptyGroup, SetName, FileName, CompactFileName) ->
ok = couch_file:flush(NewGroup#set_view_group.fd),
CompactResult = #set_view_compactor_result{
group = NewGroup,
- compact_time = timer:now_diff(now(), StartTime) / 1000000,
+ compact_time = timer:now_diff(os:timestamp(), StartTime) / 1000000,
cleanup_kv_count = CleanupKVCount
},
maybe_retry_compact(CompactResult, SetName, StartTime, GroupFd, CompactFileName).
@@ -128,7 +128,7 @@ maybe_retry_compact(CompactResult0, SetName, StartTime, GroupFd, CompactFileName
type = Type
} = NewGroup,
CompactResult = CompactResult0#set_view_compactor_result{
- compact_time = timer:now_diff(now(), StartTime) / 1000000
+ compact_time = timer:now_diff(os:timestamp(), StartTime) / 1000000
},
{ok, Pid} = get_group_pid(SetName, DDocId, Type),
case gen_server:call(Pid, {compact_done, CompactResult}, infinity) of
View
4 src/couch_set_view/src/couch_set_view_group.erl
@@ -1583,7 +1583,7 @@ cleaner(#state{group = Group} = State) ->
} = Group,
FileName = index_file_name(?root_dir(State), ?set_name(State), ?type(State), Sig),
ok = couch_set_view_util:open_raw_read_fd(Fd, FileName),
- StartTime = now(),
+ StartTime = os:timestamp(),
PurgeFun = couch_set_view_util:make_btree_purge_fun(Group),
{ok, NewIdBtree, {Go, IdPurgedCount}} =
couch_btree:guided_purge(IdBtree, PurgeFun, {go, 0}),
@@ -1608,7 +1608,7 @@ cleaner(#state{group = Group} = State) ->
views = NewViews,
index_header = Header#set_view_index_header{cbitmask = NewCbitmask}
},
- Duration = timer:now_diff(now(), StartTime) / 1000000,
+ Duration = timer:now_diff(os:timestamp(), StartTime) / 1000000,
commit_header(NewGroup, true),
exit({clean_group, NewGroup, TotalPurgedCount, Duration}).
View
14 src/couch_set_view/src/couch_set_view_updater.erl
@@ -68,7 +68,7 @@ update(Owner, Group, FileName) ->
process_flag(trap_exit, true),
- BeforeEnterTs = now(),
+ BeforeEnterTs = os:timestamp(),
Parent = self(),
Pid = spawn_link(fun() ->
case Type of
@@ -77,7 +77,7 @@ update(Owner, Group, FileName) ->
replica ->
ok = couch_index_barrier:enter(couch_replica_index_barrier, Parent)
end,
- exit({done, (timer:now_diff(now(), BeforeEnterTs) / 1000000)})
+ exit({done, (timer:now_diff(os:timestamp(), BeforeEnterTs) / 1000000)})
end),
BlockedTime = receive
@@ -89,7 +89,7 @@ update(Owner, Group, FileName) ->
EmptyResult = #set_view_updater_result{
group = Group,
indexing_time = 0.0,
- blocked_time = timer:now_diff(now(), BeforeEnterTs) / 1000000,
+ blocked_time = timer:now_diff(os:timestamp(), BeforeEnterTs) / 1000000,
state = updating_active,
cleanup_kv_count = 0,
cleanup_time = 0,
@@ -154,7 +154,7 @@ update(Owner, Group, FileName, ActiveParts, PassiveParts, MaxSeqs, BlockedTime)
sig = GroupSig
} = Group,
- StartTime = now(),
+ StartTime = os:timestamp(),
NumChanges = lists:foldl(
fun({{PartId, NewSeq}, {PartId, OldSeq}}, Acc) ->
Acc + (NewSeq - OldSeq)
@@ -274,7 +274,7 @@ wait_result_loop(StartTime, DocLoader, Mapper, Writer, BlockedTime) ->
{writer_finished, WriterAcc} ->
Result = #set_view_updater_result{
group = WriterAcc#writer_acc.group,
- indexing_time = timer:now_diff(now(), StartTime) / 1000000,
+ indexing_time = timer:now_diff(os:timestamp(), StartTime) / 1000000,
blocked_time = BlockedTime,
state = WriterAcc#writer_acc.state,
cleanup_kv_count = WriterAcc#writer_acc.cleanup_kv_count,
@@ -636,7 +636,7 @@ write_changes(WriterAcc, ViewKeyValuesToAdd, DocIdViewIdKeys, PartIdSeqs) ->
{ok, LookupResults, IdBtree2} =
couch_btree:query_modify(IdBtree, LookupDocIds, AddDocIdViewIdKeys, RemoveDocIds);
_ ->
- CleanupStart = now(),
+ CleanupStart = os:timestamp(),
{ok, LookupResults, {Go, IdBtreePurgedKeyCount}, IdBtree2} =
couch_btree:query_modify(
IdBtree, LookupDocIds, AddDocIdViewIdKeys, RemoveDocIds, CleanupFun, {go, 0}),
@@ -702,7 +702,7 @@ write_changes(WriterAcc, ViewKeyValuesToAdd, DocIdViewIdKeys, PartIdSeqs) ->
end,
IdBitmap, Views3),
NewCbitmask = ?set_cbitmask(Group) band CombinedBitmap,
- CleanupTime = timer:now_diff(now(), CleanupStart) / 1000000,
+ CleanupTime = timer:now_diff(os:timestamp(), CleanupStart) / 1000000,
?LOG_INFO("Updater for set view `~s`, ~s group `~s`, performed cleanup "
"of ~p key/value pairs in ~.3f seconds",
[SetName, GroupType, GroupName, CleanupKvCount, CleanupTime])
View
10 src/couchdb/couch_replicator_worker.erl
@@ -72,7 +72,7 @@
start_link(Cp, #db{} = Source, Target, ChangesManager, _MaxConns) ->
Pid = spawn_link(fun() ->
- erlang:put(last_stats_report, now()),
+ erlang:put(last_stats_report, os:timestamp()),
queue_fetch_loop(Source, Target, Cp, Cp, ChangesManager)
end),
{ok, Pid};
@@ -88,7 +88,7 @@ init({Cp, Source, Target, ChangesManager, MaxConns}) ->
LoopPid = spawn_link(fun() ->
queue_fetch_loop(Source, Target, Parent, Cp, ChangesManager)
end),
- erlang:put(last_stats_report, now()),
+ erlang:put(last_stats_report, os:timestamp()),
State = #state{
cp = Cp,
max_parallel_conns = MaxConns,
@@ -235,7 +235,7 @@ queue_fetch_loop(Source, Target, Parent, Cp, ChangesManager) ->
end,
close_db(Target2),
ok = gen_server:call(Cp, {report_seq_done, ReportSeq, Stats}, infinity),
- erlang:put(last_stats_report, now()),
+ erlang:put(last_stats_report, os:timestamp()),
?LOG_DEBUG("Worker reported completion of seq ~p", [ReportSeq]),
queue_fetch_loop(Source, Target, Parent, Cp, ChangesManager)
end.
@@ -325,7 +325,7 @@ spawn_writer(Target, #batch{docs = DocList, size = Size}) ->
after_full_flush(#state{stats = Stats, flush_waiter = Waiter} = State) ->
gen_server:reply(Waiter, {ok, Stats}),
- erlang:put(last_stats_report, now()),
+ erlang:put(last_stats_report, os:timestamp()),
State#state{
stats = #rep_stats{},
flush_waiter = nil,
@@ -406,7 +406,7 @@ find_missing(DocInfos, Target) ->
maybe_report_stats(Cp, Stats) ->
- Now = now(),
+ Now = os:timestamp(),
case timer:now_diff(erlang:get(last_stats_report), Now) >= ?STATS_DELAY of
true ->
ok = gen_server:call(Cp, {add_stats, Stats}, infinity),
View
4 src/couchdb/couch_task_status.erl
@@ -82,7 +82,7 @@ get(Prop) ->
maybe_persist(TaskProps0) ->
{LastUpdateTime, Frequency} = erlang:get(task_status_update),
- case timer:now_diff(Now = now(), LastUpdateTime) >= Frequency of
+ case timer:now_diff(Now = os:timestamp(), LastUpdateTime) >= Frequency of
true ->
put(task_status_update, {Now, Frequency}),
TaskProps = ?set(TaskProps0, updated_on, timestamp(Now)),
@@ -146,7 +146,7 @@ code_change(_OldVsn, State, _Extra) ->
timestamp() ->
- timestamp(now()).
+ timestamp(os:timestamp()).
timestamp({Mega, Secs, _}) ->
Mega * 1000000 + Secs.
View
4 src/mochiweb/mochiweb_acceptor.erl
@@ -14,10 +14,10 @@ start_link(Server, Listen, Loop) ->
proc_lib:spawn_link(?MODULE, init, [Server, Listen, Loop]).
init(Server, Listen, Loop) ->
- T1 = now(),
+ T1 = os:timestamp(),
case catch mochiweb_socket:accept(Listen) of
{ok, Socket} ->
- gen_server:cast(Server, {accepted, self(), timer:now_diff(now(), T1)}),
+ gen_server:cast(Server, {accepted, self(), timer:now_diff(os:timestamp(), T1)}),
call_loop(Loop, Socket);
{error, closed} ->
exit(normal);
Please sign in to comment.
Something went wrong with that request. Please try again.