@@ -18,21 +18,15 @@ Watchdog thread:
1818``` ts
1919const { captureStackTrace } = require (" cross-thread-stack-trace" );
2020
21- const stack = captureStackTrace ();
22- console .log (stack );
23- ```
24-
25- Build the module and run the test:
26-
27- ```
28- npm i && npm test
21+ const stacks = captureStackTrace ();
22+ console .log (stacks );
2923```
3024
3125Results in:
3226
3327``` js
3428{
35- main : [
29+ ' 0 ' : [
3630 {
3731 function: ' from' ,
3832 filename: ' node:buffer' ,
@@ -47,18 +41,18 @@ Results in:
4741 },
4842 {
4943 function: ' longWork' ,
50- filename: ' /Users/tim/Documents/Repositories/cross-thread-stack-trace/test /test.js' ,
44+ filename: ' /app /test.js' ,
5145 lineno: 20 ,
5246 colno: 29
5347 },
5448 {
5549 function: ' ?' ,
56- filename: ' /Users/tim/Documents/Repositories/cross-thread-stack-trace/test /test.js' ,
50+ filename: ' /app /test.js' ,
5751 lineno: 24 ,
5852 colno: 1
5953 }
6054 ],
61- ' worker- 2' : [
55+ ' 2' : [
6256 {
6357 function: ' from' ,
6458 filename: ' node:buffer' ,
@@ -73,16 +67,56 @@ Results in:
7367 },
7468 {
7569 function: ' longWork' ,
76- filename: ' /Users/tim/Documents/Repositories/cross-thread-stack-trace/test /worker.js' ,
70+ filename: ' /app /worker.js' ,
7771 lineno: 10 ,
7872 colno: 29
7973 },
8074 {
8175 function: ' ?' ,
82- filename: ' /Users/tim/Documents/Repositories/cross-thread-stack-trace/test /worker.js' ,
76+ filename: ' /app /worker.js' ,
8377 lineno: 14 ,
8478 colno: 1
8579 }
8680 ]
8781}
8882```
83+
84+ ## Detecting blocked event loops
85+
86+ In the main or worker threads if you call ` registerThread() ` regularly, times
87+ are recorded.
88+
89+ ``` ts
90+ const { registerThread } = require (" cross-thread-stack-trace" );
91+
92+ setInterval (() => {
93+ registerThread ();
94+ }, 200 );
95+ ```
96+
97+ In the watchdog thread you can call ` getThreadLastSeen() ` to get how long it's
98+ been in milliseconds since each thread registered.
99+
100+ If any thread has exceeded a threshold, you can call ` captureStackTrace() ` to
101+ get the stack traces for all threads.
102+
103+ ``` ts
104+ const { captureStackTrace, getThreadLastSeen } = require (
105+ " cross-thread-stack-trace" ,
106+ );
107+
108+ const THRESHOLD = 1000 ; // 1 second
109+
110+ setInterval (() => {
111+ for (const [thread, time] in Object .entries (getThreadLastSeen ())) {
112+ if (time > THRESHOLD ) {
113+ const stacks = captureStackTrace ();
114+ const blockedThread = stacks [thread ];
115+ console .log (
116+ ` Thread '${thread }' blocked more than ${THRESHOLD }ms ` ,
117+ blockedThread ,
118+ );
119+ }
120+ }
121+ }, 1000 );
122+ ```
0 commit comments