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
Show 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 {
val
task
=
new
TimerTask
{
def
run
{
TrafficMonitor
.
updateRate
()
updateTrafficRate
(
TrafficMonitor
.
getTxRate
,
TrafficMonitor
.
getRxRate
,
TrafficMonitor
.
getTxTotal
,
TrafficMonitor
.
getRxTotal
)
updateTrafficRate
()
}
}
timer
=
new
Timer
(
true
)
...
...
@@ -122,7 +121,7 @@ trait BaseService extends Service {
def
stopRunner
()
{
// Make sure update total traffic when stopping the runner
updateTrafficTotal
(
TrafficMonitor
.
getDeltaTx
,
TrafficMonitor
.
getDeltaRx
)
updateTrafficTotal
(
TrafficMonitor
.
txTotal
,
TrafficMonitor
.
rxTotal
)
TrafficMonitor
.
reset
()
if
(
trafficMonitorThread
!=
null
)
{
...
...
@@ -132,8 +131,6 @@ trait BaseService extends Service {
}
def
updateTrafficTotal
(
tx
:
Long
,
rx
:
Long
)
{
val
handler
=
new
Handler
(
getContext
.
getMainLooper
)
handler
.
post
(()
=>
{
val
config
=
this
.
config
// avoid race conditions without locking
if
(
config
!=
null
)
{
ShadowsocksApplication
.
profileManager
.
getProfile
(
config
.
profileId
)
match
{
...
...
@@ -144,7 +141,6 @@ trait BaseService extends Service {
case
None
=>
// Ignore
}
}
})
}
def
stopBackgroundService
()
...
...
@@ -162,10 +158,14 @@ trait BaseService extends Service {
changeState
(
s
,
null
)
}
def
updateTrafficRate
(
txRate
:
String
,
rxRate
:
String
,
txTotal
:
String
,
rxTotal
:
String
)
{
def
updateTrafficRate
()
{
val
handler
=
new
Handler
(
getContext
.
getMainLooper
)
handler
.
post
(()
=>
{
if
(
callbackCount
>
0
)
{
val
txRate
=
TrafficMonitor
.
getTxRate
val
rxRate
=
TrafficMonitor
.
getRxRate
val
txTotal
=
TrafficMonitor
.
getTxTotal
val
rxTotal
=
TrafficMonitor
.
getRxTotal
val
n
=
callbacks
.
beginBroadcast
()
for
(
i
<-
0
until
n
)
{
try
{
...
...
src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala
View file @
971ea090
...
...
@@ -5,26 +5,19 @@ import java.text.DecimalFormat
import
com.github.shadowsocks.
{
R
,
ShadowsocksApplication
}
case
class
Traffic
(
tx
:
Long
,
rx
:
Long
,
timestamp
:
Long
)
object
TrafficMonitor
{
var
last
:
Traffic
=
getTraffic
(
0
,
0
)
// Bytes per second
var
txRate
:
Long
=
0
var
rxRate
:
Long
=
0
var
txRate
:
Long
=
_
var
rxRate
:
Long
=
_
// Bytes for the current session
var
txTotal
:
Long
=
0
var
rxTotal
:
Long
=
0
var
txTotal
:
Long
=
_
var
rxTotal
:
Long
=
_
// Bytes for the last query
var
txLast
:
Long
=
0
var
rxLast
:
Long
=
0
def
getTraffic
(
tx
:
Long
,
rx
:
Long
)
:
Traffic
=
{
new
Traffic
(
tx
,
rx
,
System
.
currentTimeMillis
())
}
var
txLast
:
Long
=
_
var
rxLast
:
Long
=
_
var
timestampLast
:
Long
=
_
private
val
units
=
Array
(
"KB"
,
"MB"
,
"GB"
,
"TB"
,
"PB"
,
"EB"
,
"ZB"
,
"YB"
,
"BB"
,
"NB"
,
"DB"
,
"CB"
)
private
val
numberFormat
=
new
DecimalFormat
(
"@@@"
)
...
...
@@ -40,15 +33,15 @@ object TrafficMonitor {
}
def
updateRate
()
{
val
now
=
getTraffic
(
txTotal
,
rxTotal
)
val
delta
=
now
.
timestamp
-
last
.
timestamp
val
deltaTx
=
now
.
tx
-
last
.
tx
val
deltaRx
=
now
.
rx
-
last
.
rx
val
now
=
System
.
currentTimeMillis
()
val
delta
=
now
-
timestampLast
if
(
delta
!=
0
)
{
txRate
=
deltaTx
*
1000
/
delta
rxRate
=
deltaRx
*
1000
/
delta
txRate
=
(
txTotal
-
txLast
)
*
1000
/
delta
rxRate
=
(
rxTotal
-
rxLast
)
*
1000
/
delta
txLast
=
txTotal
rxLast
=
rxTotal
timestampLast
=
now
}
last
=
now
}
def
update
(
tx
:
Long
,
rx
:
Long
)
{
...
...
@@ -63,7 +56,6 @@ object TrafficMonitor {
rxTotal
=
0
txLast
=
0
rxLast
=
0
last
=
getTraffic
(
0
,
0
)
}
def
getTxTotal
()
:
String
=
{
...
...
@@ -89,17 +81,5 @@ object TrafficMonitor {
def
getRate
()
:
String
=
{
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