erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 line
4.7 KiB

4 年之前
  1. %%%----------------------------------------------------------------------
  2. %%% File : game_timer.erl
  3. %%% Author : csj
  4. %%% Created : 2010-10-17
  5. %%% Description: 时间生成器
  6. %%%----------------------------------------------------------------------
  7. -module(game_timer).
  8. -behaviour(gen_server).
  9. %% --------------------------------------------------------------------
  10. %% Include files
  11. %% --------------------------------------------------------------------
  12. -include("common.hrl").
  13. %% --------------------------------------------------------------------
  14. %% External exports
  15. -export([now/0, now_seconds/0, cpu_time/0, start_link/0, start/1, info/0]).
  16. %% gen_server callbacks
  17. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  18. %% ====================================================================
  19. %% External functions
  20. %% ====================================================================
  21. now() ->
  22. [{timer, {Now, _}}] = ets:lookup(ets_timer, timer),
  23. Now.
  24. now_seconds() ->
  25. [{timer, {Now, _}}] = ets:lookup(ets_timer, timer),
  26. {MegaSecs, Secs, _MicroSecs} = Now,
  27. lists:concat([MegaSecs, Secs]).
  28. cpu_time() ->
  29. [{timer, {_, Wallclock_Time_Since_Last_Call}}] = ets:lookup(ets_timer, timer),
  30. Wallclock_Time_Since_Last_Call.
  31. info() ->
  32. [
  33. ets:info(ets_timer),
  34. ets:tab2list(ets_timer)
  35. ].
  36. -define(CLOCK, 100).
  37. start(Sup) ->
  38. supervisor:start_child(Sup,
  39. {game_timer,
  40. {game_timer, start_link, []},
  41. permanent, brutal_kill, worker, [game_timer]}).
  42. %% ====================================================================
  43. %% Server functions
  44. %% ====================================================================
  45. start_link() ->
  46. gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  47. %% --------------------------------------------------------------------
  48. %% Function: init/1
  49. %% Description: Initiates the server
  50. %% Returns: {ok, State} |
  51. %% {ok, State, Timeout} |
  52. %% ignore |
  53. %% {stop, Reason}
  54. %% --------------------------------------------------------------------
  55. init([]) ->
  56. ets:new(ets_timer, [set, protected, named_table]),
  57. ets:insert(ets_timer, {timer, {erlang:now(), 0}}),
  58. erlang:send_after(?CLOCK, self(), {event, clock}),
  59. misc:write_monitor_pid(self(), ?MODULE, {}),
  60. {ok, []}.
  61. %% --------------------------------------------------------------------
  62. %% Function: handle_call/3
  63. %% Description: Handling call messages
  64. %% Returns: {reply, Reply, State} |
  65. %% {reply, Reply, State, Timeout} |
  66. %% {noreply, State} |
  67. %% {noreply, State, Timeout} |
  68. %% {stop, Reason, Reply, State} | (terminate/2 is called)
  69. %% {stop, Reason, State} (terminate/2 is called)
  70. %% --------------------------------------------------------------------
  71. handle_call(_Request, _From, State) ->
  72. {reply, State, State}.
  73. %% --------------------------------------------------------------------
  74. %% Function: handle_cast/2
  75. %% Description: Handling cast messages
  76. %% Returns: {noreply, State} |
  77. %% {noreply, State, Timeout} |
  78. %% {stop, Reason, State} (terminate/2 is called)
  79. %% --------------------------------------------------------------------
  80. handle_cast(_Msg, State) ->
  81. {noreply, State}.
  82. %% --------------------------------------------------------------------
  83. %% Function: handle_info/2
  84. %% Description: Handling all non call/cast messages
  85. %% Returns: {noreply, State} |
  86. %% {noreply, State, Timeout} |
  87. %% {stop, Reason, State} (terminate/2 is called)
  88. %% --------------------------------------------------------------------
  89. handle_info({event, clock}, State) ->
  90. {_Total_Run_Time, Time_Since_Last_Call} = statistics(runtime),
  91. ets:insert(ets_timer, {timer, {erlang:now(), Time_Since_Last_Call}}),
  92. erlang:send_after(?CLOCK, self(), {event, clock}),
  93. {noreply, State};
  94. handle_info(_Info, State) ->
  95. {noreply, State}.
  96. %% --------------------------------------------------------------------
  97. %% Function: terminate/2
  98. %% Description: Shutdown the server
  99. %% Returns: any (ignored by gen_server)
  100. %% --------------------------------------------------------------------
  101. terminate(_Reason, _State) ->
  102. misc:delete_monitor_pid(self()),
  103. ok.
  104. %% --------------------------------------------------------------------
  105. %% Func: code_change/3
  106. %% Purpose: Convert process state when code is changed
  107. %% Returns: {ok, NewState}
  108. %% --------------------------------------------------------------------
  109. code_change(_OldVsn, State, _Extra) ->
  110. {ok, State}.
  111. %% --------------------------------------------------------------------
  112. %%% Internal functions
  113. %% --------------------------------------------------------------------