1+ function getCpuUrge ( callback ) {
2+ chrome . system . cpu . getInfo ( function ( info ) {
3+ var total = 0 ;
4+ var user = 0 ;
5+ var kernel = 0 ;
6+ for ( var i = 0 ; i < info . processors . length ; i ++ ) {
7+ total += info . processors [ i ] . usage . total - cpu_history . last_total [ i ] ;
8+ cpu_history . last_total [ i ] = info . processors [ i ] . usage . total ;
9+ user += info . processors [ i ] . usage . user - cpu_history . last_user [ i ] ;
10+ cpu_history . last_user [ i ] = info . processors [ i ] . usage . user ;
11+ kernel += info . processors [ i ] . usage . kernel - cpu_history . last_kernel [ i ] ;
12+ cpu_history . last_kernel [ i ] = info . processors [ i ] . usage . kernel ;
13+ }
14+ user = Math . round ( user / total * 100 ) ;
15+ kernel = Math . round ( kernel / total * 100 ) ;
16+ callback ( { user :user , kernel :kernel , total :user + kernel } ) ;
17+ } ) ;
18+ }
19+
20+ function getMemUrge ( callback ) {
21+ chrome . system . memory . getInfo ( function ( info ) {
22+ callback ( info ) ;
23+ } ) ;
24+ }
25+
26+ function updateCpuHistory ( ) {
27+ getCpuUrge ( function ( urge ) {
28+ cpu_history . user . shift ( ) ;
29+ cpu_history . user . push ( urge . user ) ;
30+ cpu_history . kernel . shift ( ) ;
31+ cpu_history . kernel . push ( urge . kernel ) ;
32+ cpu_history . total . shift ( ) ;
33+ cpu_history . total . push ( urge . total ) ;
34+ showCpu ( ) ;
35+ } ) ;
36+ }
37+
38+ function updateMemHistory ( ) {
39+ getMemUrge ( function ( urge ) {
40+ mem_history . used . shift ( ) ;
41+ mem_history . used . push ( Math . round ( ( urge . capacity - urge . availableCapacity ) / urge . capacity * 100 ) ) ;
42+ showMem ( ) ;
43+ } ) ;
44+ }
45+
46+ function updateData ( ) {
47+ updateCpuHistory ( ) ;
48+ updateMemHistory ( ) ;
49+ }
50+
51+ function showCpu ( ) {
52+ var history = {
53+ labels : ( function ( ) { for ( var i = 0 , labels = [ ] ; i < ponits_num ; labels . push ( '' ) , i ++ ) ; return labels ; } ) ( ) ,
54+ datasets : [
55+ {
56+ fillColor : "rgba(220,220,220,0.5)" ,
57+ data : cpu_history . total
58+ } ,
59+ {
60+ fillColor : "rgba(90,140,255,0.5)" ,
61+ data : cpu_history . kernel
62+ } ,
63+ {
64+ fillColor : "rgba(255,90,90,0.5)" ,
65+ data : cpu_history . user
66+ }
67+ ]
68+ } ;
69+
70+ var now = [
71+ {
72+ value : cpu_history . total [ ponits_num - 1 ] ,
73+ color :"rgba(220,220,220,0.7)"
74+ } ,
75+ {
76+ value : 100 - cpu_history . total [ ponits_num - 1 ] ,
77+ color : "rgba(220,220,220,0.3)"
78+ }
79+ ] ;
80+ var his_ctx = document . getElementById ( 'cpu_history' ) . getContext ( "2d" ) ;
81+ var now_ctx = document . getElementById ( "cpu_total" ) . getContext ( "2d" ) ;
82+ new Chart ( his_ctx ) . Line ( history , { scaleFontSize :4 , pointDot :false , animation :false } ) ;
83+ new Chart ( now_ctx ) . Pie ( now , { segmentShowStroke :false , animation :false } ) ;
84+ }
85+
86+ function showMem ( ) {
87+ var history = {
88+ labels : ( function ( ) { for ( var i = 0 , labels = [ ] ; i < ponits_num ; labels . push ( '' ) , i ++ ) ; return labels ; } ) ( ) ,
89+ datasets : [
90+ {
91+ fillColor : "rgba(220,220,220,0.5)" ,
92+ data : mem_history . used
93+ }
94+ ]
95+ } ;
96+
97+ var now = [
98+ {
99+ value : mem_history . used [ ponits_num - 1 ] ,
100+ color :"rgba(220,220,220,0.7)"
101+ } ,
102+ {
103+ value : 100 - mem_history . used [ ponits_num - 1 ] ,
104+ color : "rgba(220,220,220,0.3)"
105+ }
106+ ] ;
107+ var his_ctx = document . getElementById ( 'mem_history' ) . getContext ( "2d" ) ;
108+ var now_ctx = document . getElementById ( "mem_total" ) . getContext ( "2d" ) ;
109+ new Chart ( his_ctx ) . Line ( history , { scaleFontSize :4 , pointDot :false , animation :false } ) ;
110+ new Chart ( now_ctx ) . Pie ( now , { segmentShowStroke :false , animation :false } ) ;
111+ }
112+
113+ function init ( ) {
114+ cpu_history = {
115+ user : [ ] ,
116+ kernel : [ ] ,
117+ total : [ ] ,
118+ last_user : [ ] ,
119+ last_kernel : [ ] ,
120+ last_total : [ ]
121+ } ;
122+ mem_history = {
123+ used : [ ]
124+ } ;
125+ init_cpu_history ( ) ;
126+ }
127+
128+ function init_cpu_history ( ) {
129+ for ( var i = 0 ; i < ponits_num ; i ++ ) {
130+ cpu_history . user . push ( 0 ) ;
131+ cpu_history . kernel . push ( 0 ) ;
132+ cpu_history . total . push ( 0 ) ;
133+ }
134+ chrome . system . cpu . getInfo ( function ( info ) {
135+ for ( var i = 0 ; i < info . processors . length ; i ++ ) {
136+ cpu_history . last_total . push ( info . processors [ i ] . usage . total ) ;
137+ cpu_history . last_user . push ( info . processors [ i ] . usage . user ) ;
138+ cpu_history . last_kernel . push ( info . processors [ i ] . usage . kernel ) ;
139+ }
140+ init_mem_history ( ) ;
141+ } ) ;
142+ }
143+
144+ function init_mem_history ( ) {
145+ for ( var i = 0 ; i < ponits_num ; i ++ ) {
146+ mem_history . used . push ( 0 ) ;
147+ }
148+ updateData ( ) ;
149+ setInterval ( updateData , 1000 ) ;
150+ }
151+
152+ var cpu_history , mem_history , ponits_num = 20 ;
153+
154+ init ( ) ;
0 commit comments