Commit 971ea090 authored by Mygod's avatar Mygod

Reduce battery usage - optimize performance critical block

parent fd01712b
...@@ -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 {
......
...@@ -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
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment