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 {
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 {
......
......@@ -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
}
}
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