├── LICENSE ├── README.md ├── c_src └── tiw_port_timer.c ├── docker └── Dockerfile ├── priv └── tiw.in ├── rebar ├── rebar.config └── src ├── tiw.app.src ├── tiw.erl ├── tiw.hrl └── tiw_example.erl /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | tiw 2 | === 3 | 4 | This tool allows you to dump all the timers inside the Erlang VM via gdb. 5 | Please note that this might not work properly on your OTP version and 6 | it currently *does not* work on OTP >= 18. 7 | 8 | Why? 9 | ---- 10 | There are two main reasons why you might need this: 11 | * you assumed that you can distribute load uniformly using timers, but 12 | some glitch caused timers to synchronize (all timers are triggered in a 13 | short period of time) 14 | * you like to look under the hood 15 | 16 | How? 17 | ---- 18 | Since one example is worth a thousand words... 19 | 20 | (you can rerun the commands on a Docker container built from 21 | ``docker/Dockerfile``, you need to start two terminals in order to attach 22 | `gdb` to `beam`). 23 | 24 | Terminal 1: 25 | ``` 26 | root@e9858d91550c:/tiw# erl -smp enable -pa ebin 27 | Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:1:1] [async-threads:10] 28 | [kernel-poll:false] 29 | 30 | Eshell V6.4 (abort with ^G) 31 | 1> os:getpid(). 32 | "19" 33 | 2> tiw_example:process_timers(100, 10000), 34 | 2> timer:sleep(2000), 35 | 2> tiw_example:bif_timers(100, 20000), 36 | 2> timer:sleep(2000), 37 | 2> tiw_example:port_timers(100, 30000). 38 | [<0.236.0>,<0.237.0>,<0.238.0>,<0.239.0>,<0.240.0>, 39 | <0.241.0>,<0.242.0>,<0.243.0>,<0.244.0>,<0.245.0>,<0.246.0>, 40 | <0.247.0>,<0.248.0>,<0.249.0>,<0.250.0>,<0.251.0>,<0.252.0>, 41 | <0.253.0>,<0.254.0>,<0.255.0>,<0.256.0>,<0.257.0>,<0.258.0>, 42 | <0.259.0>,<0.260.0>,<0.261.0>,<0.262.0>,<0.263.0>,<0.264.0>|...] 43 | ``` 44 | 45 | Terminal 2: 46 | ``` 47 | root@10a857bd690c:/# gdb --pid=19 48 | (gdb) source 49 | ~/.kerl/builds/17.5/otp_src_17.5/erts/etc/unix/etp-commands.in 50 | (gdb) source tiw/priv/tiw.in 51 | (gdb) set height 0 52 | (gdb) set logging file timers_smp 53 | (gdb) set logging on 54 | (gdb) tiw 55 | === tiw start 56 | 35696 57 | 36083 58 | 3 59 | bif_timer_timeout 60 | <0.145.0> ! {msg,10}. 61 | 62 | 36120 63 | 16 64 | ptimer_timeout 65 | #NotPid<0x2a47> | #Port<0.676> 66 | 67 | 36401 68 | 8 69 | ptimer_timeout 70 | <0.88.0> | #NotPort<0x583> 71 | ... 72 | 35329 73 | 9 74 | ptimer_timeout 75 | <0.101.0> | #NotPort<0x653> 76 | === tiw end 77 | ``` 78 | 79 | This should've generated a `/timers_smp` file. 80 | 81 | Later you can use some 82 | helpers to parse the file (of course it can be done somewhere else): 83 | ``` 84 | root@10a857bd690c:/tiw# erl -pa ebin 85 | Erlang/OTP 17 [erts-6.4] [source] [64-bit] [async-threads:10] 86 | [kernel-poll:false] 87 | 88 | Eshell V6.4 (abort with ^G) 89 | 1> rr("src/tiw.hrl"). 90 | [timer] 91 | 2> Timers = tiw:load("../timers_smp"). 92 | [#timer{expires_in = 1000,callback = aux_work_timeout, 93 | info = <<"NULL">>}, 94 | #timer{expires_in = 4993,callback = ptimer_timeout, 95 | info = <<"<0.36.0> | #NotPort<0x243>">>}, 96 | #timer{expires_in = 14993,callback = ptimer_timeout, 97 | info = <<"<0.37.0> | #NotPort<0x253>">>}, 98 | #timer{expires_in = 16995,callback = bif_timer_timeout, 99 | info = <<"<0.136.0> ! {msg,1}.">>}, 100 | #timer{expires_in = 24993,callback = ptimer_timeout, 101 | info = <<"<0.38.0> | #NotPort<0x263>">>}, 102 | #timer{expires_in = 29000,callback = ptimer_timeout, 103 | info = <<"#NotPid<0x2827> | #Port<0.642>">>}, 104 | #timer{expires_in = 34993,callback = ptimer_timeout, 105 | info = <<"<0.39.0> | #NotPort<0x273>">>}, 106 | #timer{expires_in = 36995,callback = bif_timer_timeout, 107 | info = <<"<0.137.0> ! {msg,2}.">>}, 108 | #timer{expires_in = 44993,callback = ptimer_timeout, 109 | info = <<"<0.40.0> | #NotPort<0x283>">>}, 110 | #timer{expires_in = 54993,callback = ptimer_timeout, 111 | info = <<"<0.41.0> | #NotPort<0x293>">>}, 112 | #timer{expires_in = 56995,callback = bif_timer_timeout, 113 | info = <<"<0.138.0> ! {msg,3}.">>}, 114 | #timer{expires_in = 59000,callback = ptimer_timeout, 115 | info = <<"#NotPid<0x2837> | #Port<0.643>">>}, 116 | #timer{...}, 117 | {...}|...] 118 | 3> tiw:buckets(Timers, 1000). 119 | [{{1000,1999},1}, 120 | {{4000,4999},1}, 121 | {{14000,14999},1}, 122 | {{16000,16999},1}, 123 | {{24000,24999},1}, 124 | {{29000,29999},1}, 125 | {{34000,34999},1}, 126 | {{36000,36999},1}, 127 | {{44000,44999},1}, 128 | {{54000,54999},1}, 129 | {{56000,56999},1}, 130 | {{59000,59999},1}, 131 | {{64000,...},1}, 132 | {{...},...}, 133 | {...}|...] 134 | ``` 135 | 136 | The second command returns number of timers in appropriate seconds 137 | (interval is the second argument, in milliseconds). 138 | -------------------------------------------------------------------------------- /c_src/tiw_port_timer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define get_int32(s) ((((unsigned char*) (s))[0] << 24) | \ 5 | (((unsigned char*) (s))[1] << 16) | \ 6 | (((unsigned char*) (s))[2] << 8) | \ 7 | (((unsigned char*) (s))[3])) 8 | 9 | static ErlDrvData timer_start(ErlDrvPort, char*); 10 | static void timer_stop(ErlDrvData); 11 | static void timer_read(ErlDrvData, char*, ErlDrvSizeT); 12 | static void timer(ErlDrvData); 13 | 14 | static ErlDrvEntry timer_driver_entry = 15 | { 16 | NULL, 17 | timer_start, 18 | timer_stop, 19 | timer_read, 20 | NULL, 21 | NULL, 22 | "tiw_port_timer", 23 | NULL, 24 | NULL, 25 | NULL, 26 | timer, 27 | NULL, 28 | NULL, 29 | NULL, 30 | NULL, 31 | NULL, 32 | ERL_DRV_EXTENDED_MARKER, 33 | ERL_DRV_EXTENDED_MAJOR_VERSION, 34 | ERL_DRV_EXTENDED_MINOR_VERSION, 35 | 0, 36 | NULL, 37 | NULL, 38 | NULL 39 | }; 40 | 41 | DRIVER_INIT(tiw_port_timer) 42 | { 43 | return &timer_driver_entry; 44 | } 45 | 46 | static ErlDrvData timer_start(ErlDrvPort port, char *buf) 47 | { 48 | return (ErlDrvData)port; 49 | } 50 | 51 | static void timer_read(ErlDrvData p, char *buf, ErlDrvSizeT len) 52 | { 53 | ErlDrvPort port = (ErlDrvPort) p; 54 | driver_set_timer(port, get_int32(buf)); 55 | } 56 | 57 | static void timer_stop(ErlDrvData port) 58 | { 59 | } 60 | 61 | static void timer(ErlDrvData port) 62 | { 63 | char* reply = "timeout"; 64 | driver_output((ErlDrvPort)port, reply, 7); 65 | } 66 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM studzien/erlang:17.5 2 | 3 | RUN apt-get install -y gdb git && \ 4 | git clone https://github.com/studzien/tiw && \ 5 | cd tiw && \ 6 | ./rebar compile 7 | -------------------------------------------------------------------------------- /priv/tiw.in: -------------------------------------------------------------------------------- 1 | define tiw-slot 2 | set $p = tiw[$arg0] 3 | while ($p != 0 && $arg0 == $p->slot) 4 | printf "%d\n%d\n", $p->slot, $p->count 5 | if (etp_smp_compiled) 6 | tiw-timeout-symbol-smp 7 | else 8 | tiw-timeout-symbol-nosmp 9 | end 10 | printf "\n" 11 | set $p = $p->next 12 | end 13 | end 14 | 15 | define tiw-timeout-symbol-nosmp 16 | if $p->timeout == &timeout_proc 17 | tiw-timeout-proc 18 | else 19 | if $p->timeout == &schedule_port_timeout 20 | tiw-timeout-port 21 | else 22 | if $p->timeout == &bif_timer_timeout 23 | tiw-timeout-bif_timer 24 | else 25 | if $p->timeout == &aux_work_timeout 26 | tiw-timeout-aux_work 27 | end 28 | end 29 | end 30 | end 31 | end 32 | 33 | define tiw-timeout-symbol-smp 34 | if $p->timeout == &ptimer_timeout 35 | tiw-timeout-ptimer 36 | else 37 | if $p->timeout == &bif_timer_timeout 38 | tiw-timeout-bif_timer 39 | else 40 | if $p->timeout == &aux_work_timeout 41 | tiw-timeout-aux_work 42 | end 43 | end 44 | end 45 | end 46 | 47 | ## auxiliary work 48 | define tiw-timeout-aux_work 49 | printf "aux_work_timeout\nNULL\n" 50 | end 51 | 52 | ## timers initiated by erlang:send_after/3 and erlang:start_timer/3 bifs 53 | define tiw-timeout-bif_timer 54 | printf "bif_timer_timeout\n" 55 | etp-pid-1 ((ErtsBifTimer*)($p->arg))->receiver.proc->ess->common.id 56 | printf " ! " 57 | etp ((ErtsBifTimer*)($p->arg))->message 58 | end 59 | 60 | ## process timeout ('receive ... after' block, so also timer:sleep/1) for smp 61 | define tiw-timeout-ptimer 62 | printf "ptimer_timeout\n" 63 | etp-pid-1 ((ErtsSmpPTimer*)($p->arg))->timer->id 64 | printf " | " 65 | etp-port-1 ((ErtsSmpPTimer*)($p->arg))->timer->id 66 | printf "\n" 67 | end 68 | 69 | ## process timeout but without smp 70 | define tiw-timeout-proc 71 | printf "timeout_proc\n" 72 | etp-pid-1 ((Process*)($p->arg))->common.id 73 | printf "\n" 74 | end 75 | 76 | ## port timeout 77 | define tiw-timeout-port 78 | printf "schedule_port_timeout\n" 79 | etp-port-1 ((Port*)($p->arg))->common.id 80 | printf "\n" 81 | end 82 | 83 | define tiw 84 | printf "=== tiw start\n" 85 | printf "%d\n", tiw_pos 86 | 87 | set $TIW_SIZE = 65536 88 | set $i = tiw_pos 89 | tiw-slot $i 90 | set $i = ($i+1) % $TIW_SIZE 91 | while ($i != tiw_pos) 92 | tiw-slot $i 93 | set $i = ($i+1) % $TIW_SIZE 94 | end 95 | printf "=== tiw end\n" 96 | end 97 | 98 | # vi: ft=gdb 99 | -------------------------------------------------------------------------------- /rebar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studzien/tiw/093c4676302a9d57a62363a18231f8d31c8144c7/rebar -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | {port_specs, 2 | [{".*", "priv/tiw_port_timer.so", ["c_src/tiw_port_timer.c"], []}] 3 | }. 4 | -------------------------------------------------------------------------------- /src/tiw.app.src: -------------------------------------------------------------------------------- 1 | {application, tiw, 2 | [ 3 | {description, "timer wheel helper"}, 4 | {vsn, "1"}, 5 | {modules, [ 6 | tiw 7 | ]}, 8 | {registered, []}, 9 | {applications, [ 10 | kernel, 11 | stdlib 12 | ]}, 13 | {env, []} 14 | ]}. 15 | -------------------------------------------------------------------------------- /src/tiw.erl: -------------------------------------------------------------------------------- 1 | -module(tiw). 2 | 3 | -compile(export_all). 4 | 5 | -include("tiw.hrl"). 6 | 7 | -define(TIW_SIZE, 65536). 8 | 9 | load(TiwFile) -> 10 | [Pos|Lines] = read_lines(TiwFile), 11 | parse_timers(Lines, i(Pos), []). 12 | 13 | buckets(Timers, Interval) -> 14 | Ranges = ranges(Timers, Interval), 15 | [ {Range, length(Results)} || {Range, Results} <- Ranges, 16 | Results =/= [] ]. 17 | 18 | ranges(Timers, Interval) -> 19 | Last = lists:last(Timers), 20 | ranges(Timers, Interval, Last#timer.expires_in+Interval, Interval, []). 21 | 22 | ranges(_, Current, Max, _, Acc) when Current > Max -> 23 | lists:reverse(Acc); 24 | ranges(Timers, Current, Max, Interval, Acc0) -> 25 | Pred = fun(#timer{expires_in = Exp}) when Exp < Current -> 26 | true; 27 | (#timer{}) -> 28 | false 29 | end, 30 | {Cool, Uncool} = lists:splitwith(Pred, Timers), 31 | Acc1 = [{{Current-Interval, Current-1}, Cool} | Acc0], 32 | ranges(Uncool, Current+Interval, Max, Interval, Acc1). 33 | 34 | read_lines(TiwFile) -> 35 | {ok, File} = file:open(TiwFile, [read, binary]), 36 | Result = do_read_lines(line(File), File), 37 | ok = file:close(File), 38 | Result. 39 | 40 | parse_timers([], _, Acc) -> 41 | lists:keysort(#timer.expires_in, Acc); 42 | parse_timers([_, _, _, <<>> | Rest], Pos, Acc) -> 43 | %% ignore incompatible format 44 | parse_timers(Rest, Pos, Acc); 45 | parse_timers([Slot, Count, Callback, Info, <<>> | Rest], Pos, Acc) -> 46 | Timer = #timer{expires_in = expiration(Pos, i(Slot), i(Count)), 47 | callback = binary_to_atom(Callback, utf8), 48 | info = Info}, 49 | parse_timers(Rest, Pos, [Timer | Acc]). 50 | 51 | expiration(Pos, Slot, Count) when Slot > Pos -> 52 | Slot - Pos + (Count * ?TIW_SIZE); 53 | expiration(Pos, Slot, Count) -> 54 | ((?TIW_SIZE - Pos) + Slot) + (Count * ?TIW_SIZE). 55 | 56 | %% discard prefix 57 | do_read_lines(eof, _File) -> 58 | erlang:error(tiw_block_not_found); 59 | do_read_lines({ok, <<"=== tiw start\n">>}, File) -> 60 | do_read_lines(line(File), File, []); 61 | do_read_lines(_, File) -> 62 | do_read_lines(line(File), File). 63 | 64 | %% actual data 65 | do_read_lines(eof, _, Acc) -> 66 | lists:reverse(Acc); 67 | do_read_lines({ok, <<"=== tiw end\n">>}, _, Acc) -> 68 | lists:reverse(Acc); 69 | do_read_lines({ok, Line}, File, Acc) -> 70 | do_read_lines(line(File), File, [strip(Line)|Acc]). 71 | 72 | i(Binary) -> 73 | binary_to_integer(Binary). 74 | 75 | line(File) -> 76 | file:read_line(File). 77 | 78 | strip(Binary) -> 79 | << <> || <> <= Binary, C =/= $\r, C =/= $\n >>. 80 | -------------------------------------------------------------------------------- /src/tiw.hrl: -------------------------------------------------------------------------------- 1 | -record(timer, {expires_in = 0, callback, info}). 2 | -------------------------------------------------------------------------------- /src/tiw_example.erl: -------------------------------------------------------------------------------- 1 | -module(tiw_example). 2 | 3 | -compile(export_all). 4 | 5 | process_timers(N, Ms) -> 6 | [ spawn_link(fun() -> 7 | receive 8 | after I*Ms -> 9 | ok 10 | end 11 | end) || I <- lists:seq(1, N) ]. 12 | 13 | bif_timers(N, Ms) -> 14 | [ begin 15 | Pid = spawn_link(fun() -> 16 | receive 17 | _ -> ok 18 | end 19 | end), 20 | erlang:send_after(I*Ms, Pid, {msg, I}) 21 | end || I <- lists:seq(1, N) ]. 22 | 23 | port_timers(N, Ms) -> 24 | ok = erl_ddll:load_driver("priv", "tiw_port_timer"), 25 | [ spawn_link(port_timer_fun(I*Ms)) || I <- lists:seq(1, N) ]. 26 | 27 | port_timer_fun(Timeout) -> 28 | fun() -> 29 | Port = erlang:open_port({spawn, tiw_port_timer}, [binary]), 30 | true = erlang:port_command(Port, <>), 31 | receive 32 | {Port, {data, <<"timeout">>}} -> 33 | ok 34 | end 35 | end. 36 | --------------------------------------------------------------------------------