@@ -9,6 +9,7 @@ function lazyConstants() {
9
9
return _lazyConstants ;
10
10
}
11
11
12
+ exports . setup_cpuUsage = setup_cpuUsage ;
12
13
exports . setup_hrtime = setup_hrtime ;
13
14
exports . setupConfig = setupConfig ;
14
15
exports . setupKillAndExit = setupKillAndExit ;
@@ -22,6 +23,65 @@ const assert = process.assert = function(x, msg) {
22
23
} ;
23
24
24
25
26
+ function setup_cpuUsage ( ) {
27
+ const _cpuUsage = process . cpuUsage ;
28
+ const cpuValues = new Uint32Array ( 8 ) ;
29
+
30
+ process . cpuUsage = function cpuUsage ( prevValue ) {
31
+ const err = _cpuUsage ( cpuValues ) ;
32
+ if ( err !== 0 ) throw new Error ( 'unable to obtain cpu usage time' ) ;
33
+
34
+ // the second element of user/system comes back from libuv as
35
+ // microseconds, but we convert to nanoseconds here (* 1000) to
36
+ // be consistent with hrtime()
37
+ var currValue = {
38
+ user : [
39
+ cpuValues [ 0 ] * 0x100000000 + cpuValues [ 1 ] ,
40
+ ( cpuValues [ 2 ] * 0x100000000 + cpuValues [ 3 ] ) * 1000
41
+ ] ,
42
+ system : [
43
+ cpuValues [ 4 ] * 0x100000000 + cpuValues [ 5 ] ,
44
+ ( cpuValues [ 6 ] * 0x100000000 + cpuValues [ 7 ] ) * 1000
45
+ ]
46
+ } ;
47
+
48
+ if ( prevValue == null ) return currValue ;
49
+
50
+ if ( prevValue . user == null ) {
51
+ throw new Error ( 'previous value argument missing "user" property' ) ;
52
+ }
53
+ if ( prevValue . system == null ) {
54
+ throw new Error ( 'previous value argument missing "system" property' ) ;
55
+ }
56
+
57
+ currValue . user = diffSecUsec ( currValue . user , prevValue . user ) ;
58
+ currValue . system = diffSecUsec ( currValue . system , prevValue . system ) ;
59
+
60
+ return currValue ;
61
+ } ;
62
+ }
63
+
64
+
65
+ // diff the value from a current and previous [sec, usec] array
66
+ function diffSecUsec ( curr , prev ) {
67
+ if ( prev == null ) return curr ;
68
+
69
+ // perform the diff
70
+ var result = [ ] ;
71
+
72
+ result [ 0 ] = curr [ 0 ] - prev [ 0 ] ;
73
+ result [ 1 ] = curr [ 1 ] - prev [ 1 ] ;
74
+
75
+ // result[1] could be negative, on roll-overs, so ... be nice and normalize
76
+ if ( result [ 1 ] < 0 ) {
77
+ result [ 0 ] -= 1 ; // subtract 1 sec
78
+ result [ 1 ] += 1000 * 1000 * 1000 ; // add 1 sec's worth of usec's
79
+ }
80
+
81
+ return result ;
82
+ }
83
+
84
+
25
85
function setup_hrtime ( ) {
26
86
const _hrtime = process . hrtime ;
27
87
const hrValues = new Uint32Array ( 3 ) ;
0 commit comments