Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
971ea090
Commit
971ea090
authored
Dec 26, 2015
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce battery usage - optimize performance critical block
parent
fd01712b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
50 deletions
+30
-50
src/main/scala/com/github/shadowsocks/BaseService.scala
src/main/scala/com/github/shadowsocks/BaseService.scala
+16
-16
src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala
...n/scala/com/github/shadowsocks/utils/TrafficMonitor.scala
+14
-34
No files found.
src/main/scala/com/github/shadowsocks/BaseService.scala
View file @
971ea090
...
@@ -87,8 +87,7 @@ trait BaseService extends Service {
...
@@ -87,8 +87,7 @@ trait BaseService extends Service {
val
task
=
new
TimerTask
{
val
task
=
new
TimerTask
{
def
run
{
def
run
{
TrafficMonitor
.
updateRate
()
TrafficMonitor
.
updateRate
()
updateTrafficRate
(
TrafficMonitor
.
getTxRate
,
TrafficMonitor
.
getRxRate
,
updateTrafficRate
()
TrafficMonitor
.
getTxTotal
,
TrafficMonitor
.
getRxTotal
)
}
}
}
}
timer
=
new
Timer
(
true
)
timer
=
new
Timer
(
true
)
...
@@ -122,7 +121,7 @@ trait BaseService extends Service {
...
@@ -122,7 +121,7 @@ trait BaseService extends Service {
def
stopRunner
()
{
def
stopRunner
()
{
// Make sure update total traffic when stopping the runner
// Make sure update total traffic when stopping the runner
updateTrafficTotal
(
TrafficMonitor
.
getDeltaTx
,
TrafficMonitor
.
getDeltaRx
)
updateTrafficTotal
(
TrafficMonitor
.
txTotal
,
TrafficMonitor
.
rxTotal
)
TrafficMonitor
.
reset
()
TrafficMonitor
.
reset
()
if
(
trafficMonitorThread
!=
null
)
{
if
(
trafficMonitorThread
!=
null
)
{
...
@@ -132,19 +131,16 @@ trait BaseService extends Service {
...
@@ -132,19 +131,16 @@ trait BaseService extends Service {
}
}
def
updateTrafficTotal
(
tx
:
Long
,
rx
:
Long
)
{
def
updateTrafficTotal
(
tx
:
Long
,
rx
:
Long
)
{
val
handler
=
new
Handler
(
getContext
.
getMainLooper
)
val
config
=
this
.
config
// avoid race conditions without locking
handler
.
post
(()
=>
{
if
(
config
!=
null
)
{
val
config
=
this
.
config
// avoid race conditions without locking
ShadowsocksApplication
.
profileManager
.
getProfile
(
config
.
profileId
)
match
{
if
(
config
!=
null
)
{
case
Some
(
profile
)
=>
ShadowsocksApplication
.
profileManager
.
getProfile
(
config
.
profileId
)
match
{
profile
.
tx
+=
tx
case
Some
(
profile
)
=>
profile
.
rx
+=
rx
profile
.
tx
+=
tx
ShadowsocksApplication
.
profileManager
.
updateProfile
(
profile
)
profile
.
rx
+=
rx
case
None
=>
// Ignore
ShadowsocksApplication
.
profileManager
.
updateProfile
(
profile
)
case
None
=>
// Ignore
}
}
}
}
)
}
}
}
def
stopBackgroundService
()
def
stopBackgroundService
()
...
@@ -162,10 +158,14 @@ trait BaseService extends Service {
...
@@ -162,10 +158,14 @@ trait BaseService extends Service {
changeState
(
s
,
null
)
changeState
(
s
,
null
)
}
}
def
updateTrafficRate
(
txRate
:
String
,
rxRate
:
String
,
txTotal
:
String
,
rxTotal
:
String
)
{
def
updateTrafficRate
()
{
val
handler
=
new
Handler
(
getContext
.
getMainLooper
)
val
handler
=
new
Handler
(
getContext
.
getMainLooper
)
handler
.
post
(()
=>
{
handler
.
post
(()
=>
{
if
(
callbackCount
>
0
)
{
if
(
callbackCount
>
0
)
{
val
txRate
=
TrafficMonitor
.
getTxRate
val
rxRate
=
TrafficMonitor
.
getRxRate
val
txTotal
=
TrafficMonitor
.
getTxTotal
val
rxTotal
=
TrafficMonitor
.
getRxTotal
val
n
=
callbacks
.
beginBroadcast
()
val
n
=
callbacks
.
beginBroadcast
()
for
(
i
<-
0
until
n
)
{
for
(
i
<-
0
until
n
)
{
try
{
try
{
...
...
src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala
View file @
971ea090
...
@@ -5,26 +5,19 @@ import java.text.DecimalFormat
...
@@ -5,26 +5,19 @@ import java.text.DecimalFormat
import
com.github.shadowsocks.
{
R
,
ShadowsocksApplication
}
import
com.github.shadowsocks.
{
R
,
ShadowsocksApplication
}
case
class
Traffic
(
tx
:
Long
,
rx
:
Long
,
timestamp
:
Long
)
object
TrafficMonitor
{
object
TrafficMonitor
{
var
last
:
Traffic
=
getTraffic
(
0
,
0
)
// Bytes per second
// Bytes per second
var
txRate
:
Long
=
0
var
txRate
:
Long
=
_
var
rxRate
:
Long
=
0
var
rxRate
:
Long
=
_
// Bytes for the current session
// Bytes for the current session
var
txTotal
:
Long
=
0
var
txTotal
:
Long
=
_
var
rxTotal
:
Long
=
0
var
rxTotal
:
Long
=
_
// Bytes for the last query
// Bytes for the last query
var
txLast
:
Long
=
0
var
txLast
:
Long
=
_
var
rxLast
:
Long
=
0
var
rxLast
:
Long
=
_
var
timestampLast
:
Long
=
_
def
getTraffic
(
tx
:
Long
,
rx
:
Long
)
:
Traffic
=
{
new
Traffic
(
tx
,
rx
,
System
.
currentTimeMillis
())
}
private
val
units
=
Array
(
"KB"
,
"MB"
,
"GB"
,
"TB"
,
"PB"
,
"EB"
,
"ZB"
,
"YB"
,
"BB"
,
"NB"
,
"DB"
,
"CB"
)
private
val
units
=
Array
(
"KB"
,
"MB"
,
"GB"
,
"TB"
,
"PB"
,
"EB"
,
"ZB"
,
"YB"
,
"BB"
,
"NB"
,
"DB"
,
"CB"
)
private
val
numberFormat
=
new
DecimalFormat
(
"@@@"
)
private
val
numberFormat
=
new
DecimalFormat
(
"@@@"
)
...
@@ -40,15 +33,15 @@ object TrafficMonitor {
...
@@ -40,15 +33,15 @@ object TrafficMonitor {
}
}
def
updateRate
()
{
def
updateRate
()
{
val
now
=
getTraffic
(
txTotal
,
rxTotal
)
val
now
=
System
.
currentTimeMillis
()
val
delta
=
now
.
timestamp
-
last
.
timestamp
val
delta
=
now
-
timestampLast
val
deltaTx
=
now
.
tx
-
last
.
tx
val
deltaRx
=
now
.
rx
-
last
.
rx
if
(
delta
!=
0
)
{
if
(
delta
!=
0
)
{
txRate
=
deltaTx
*
1000
/
delta
txRate
=
(
txTotal
-
txLast
)
*
1000
/
delta
rxRate
=
deltaRx
*
1000
/
delta
rxRate
=
(
rxTotal
-
rxLast
)
*
1000
/
delta
txLast
=
txTotal
rxLast
=
rxTotal
timestampLast
=
now
}
}
last
=
now
}
}
def
update
(
tx
:
Long
,
rx
:
Long
)
{
def
update
(
tx
:
Long
,
rx
:
Long
)
{
...
@@ -63,7 +56,6 @@ object TrafficMonitor {
...
@@ -63,7 +56,6 @@ object TrafficMonitor {
rxTotal
=
0
rxTotal
=
0
txLast
=
0
txLast
=
0
rxLast
=
0
rxLast
=
0
last
=
getTraffic
(
0
,
0
)
}
}
def
getTxTotal
()
:
String
=
{
def
getTxTotal
()
:
String
=
{
...
@@ -89,17 +81,5 @@ object TrafficMonitor {
...
@@ -89,17 +81,5 @@ object TrafficMonitor {
def
getRate
()
:
String
=
{
def
getRate
()
:
String
=
{
formatTraffic
(
txRate
+
rxRate
)
formatTraffic
(
txRate
+
rxRate
)
}
}
def
getDeltaTx
()
:
Long
=
{
val
last
=
txLast
txLast
=
txTotal
txTotal
-
last
}
def
getDeltaRx
()
:
Long
=
{
val
last
=
rxLast
rxLast
=
rxTotal
rxTotal
-
last
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment