Update CPU Info overlay
Change-Id: Icfc7971b1362be45d95c763e008a024252893a46 Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -57,6 +57,7 @@ public class CPUInfoService extends Service {
|
||||
private String[] mCpu = null;
|
||||
private String[] mCurrFreq = null;
|
||||
private String[] mCurrGov = null;
|
||||
private String[] mCurrLoad = null;
|
||||
|
||||
private int CPU_TEMP_DIVIDER = 1;
|
||||
private String CPU_TEMP_SENSOR = "";
|
||||
@@ -72,7 +73,11 @@ public class CPUInfoService extends Service {
|
||||
private static final String CPU_ROOT = "/sys/devices/system/cpu/cpu";
|
||||
private static final String CPU_CUR_TAIL = "/cpufreq/scaling_cur_freq";
|
||||
private static final String CPU_GOV_TAIL = "/cpufreq/scaling_governor";
|
||||
private static final String GPU_FREQ_PATH = "/sys/class/kgsl/kgsl-3d0/gpuclk";
|
||||
private static final String GPU_FREQ_PATH = "/sys/class/kgsl/kgsl-3d0/clock_mhz";
|
||||
private static final String GPU_LOAD_PATH = "/sys/class/kgsl/kgsl-3d0/gpu_busy_percentage";
|
||||
private static final String THERMAL_DIR = "/sys/class/thermal/";
|
||||
private static final String[] BATTERY_TYPES = {"battery"};
|
||||
private static final String[] GPU_TYPES = {"gpu-0"};
|
||||
|
||||
private class CPUView extends View {
|
||||
private Paint mOnlinePaint;
|
||||
@@ -87,6 +92,7 @@ public class CPUInfoService extends Service {
|
||||
private String mGpuTemp;
|
||||
private String mBatTemp;
|
||||
private String mGpuFreq;
|
||||
private String mGpuLoad;
|
||||
|
||||
private boolean mDataAvail;
|
||||
|
||||
@@ -103,17 +109,20 @@ public class CPUInfoService extends Service {
|
||||
mGpuTemp = parts[1];
|
||||
mBatTemp = parts[2];
|
||||
mGpuFreq = parts[3];
|
||||
mGpuLoad = parts[4];
|
||||
|
||||
String[] cpuParts=parts[4].split("\\|");
|
||||
String[] cpuParts=parts[5].split("\\|");
|
||||
for(int i=0; i<cpuParts.length; i++){
|
||||
String cpuInfo=cpuParts[i];
|
||||
String cpuInfoParts[]=cpuInfo.split(":");
|
||||
if(cpuInfoParts.length==3){
|
||||
if(cpuInfoParts.length==4){
|
||||
mCurrFreq[i]=cpuInfoParts[1];
|
||||
mCurrGov[i]=cpuInfoParts[2];
|
||||
mCurrLoad[i]=cpuInfoParts[3];
|
||||
} else {
|
||||
mCurrFreq[i]="0";
|
||||
mCurrGov[i]="";
|
||||
mCurrLoad[i]="0%";
|
||||
}
|
||||
}
|
||||
mDataAvail = true;
|
||||
@@ -180,12 +189,16 @@ public class CPUInfoService extends Service {
|
||||
String cpu=mCpu[i];
|
||||
String freq=mCurrFreq[i];
|
||||
String gov=mCurrGov[i];
|
||||
return "cpu" + cpu + ": " + gov + " " + String.format("%8s", toMHz(freq));
|
||||
String load=mCurrLoad[i];
|
||||
return "cpu" + cpu + ": " + String.format("%3s", load) + " " + gov + " " + String.format("%8s", toMHz(freq));
|
||||
}
|
||||
|
||||
private String getTemp(String rawTemp) {
|
||||
try {
|
||||
float temp = Float.parseFloat(rawTemp);
|
||||
if (temp > 10000) {
|
||||
return String.format("%.2f", temp / 1000.0f);
|
||||
}
|
||||
if (CPU_TEMP_DIVIDER > 1) {
|
||||
return String.format("%.2f", temp / CPU_TEMP_DIVIDER);
|
||||
} else {
|
||||
@@ -203,33 +216,23 @@ public class CPUInfoService extends Service {
|
||||
return;
|
||||
}
|
||||
|
||||
final int W = mNeededWidth;
|
||||
final int RIGHT = getWidth()-1;
|
||||
|
||||
int x = RIGHT - mPaddingRight;
|
||||
int top = mPaddingTop + 2;
|
||||
int bottom = mPaddingTop + mFH - 2;
|
||||
|
||||
int y = mPaddingTop - (int)mAscent;
|
||||
|
||||
if(!mCpuTemp.equals("0")) {
|
||||
canvas.drawText("CPU: " + getTemp(mCpuTemp) + "°C",
|
||||
canvas.drawText("CPU: " + getTemp(mCpuTemp) + " °C",
|
||||
RIGHT-mPaddingRight-mMaxWidth, y-1, mOnlinePaint);
|
||||
y += mFH;
|
||||
}
|
||||
|
||||
if(!mGpuTemp.equals("0")) {
|
||||
String gpuText = "GPU: " + getTemp(mGpuTemp) + "°C";
|
||||
if(!mGpuFreq.equals("0")) {
|
||||
gpuText += " " + toMHzGPU(mGpuFreq);
|
||||
}
|
||||
canvas.drawText(gpuText,
|
||||
canvas.drawText("GPU: " + getTemp(mGpuTemp) + " °C",
|
||||
RIGHT-mPaddingRight-mMaxWidth, y-1, mOnlinePaint);
|
||||
y += mFH;
|
||||
}
|
||||
|
||||
if(!mBatTemp.equals("0")) {
|
||||
canvas.drawText("BAT: " + getTemp(mBatTemp) + "°C",
|
||||
canvas.drawText("BAT: " + getTemp(mBatTemp) + " °C",
|
||||
RIGHT-mPaddingRight-mMaxWidth, y-1, mOnlinePaint);
|
||||
y += mFH;
|
||||
}
|
||||
@@ -241,11 +244,17 @@ public class CPUInfoService extends Service {
|
||||
canvas.drawText(s, RIGHT-mPaddingRight-mMaxWidth,
|
||||
y-1, mOnlinePaint);
|
||||
} else {
|
||||
canvas.drawText("cpu" + mCpu[i] + ": offline", RIGHT-mPaddingRight-mMaxWidth,
|
||||
canvas.drawText("Core " + mCpu[i] + ": offline", RIGHT-mPaddingRight-mMaxWidth,
|
||||
y-1, mOfflinePaint);
|
||||
}
|
||||
y += mFH;
|
||||
}
|
||||
|
||||
if(!mGpuFreq.equals("0") || !mGpuLoad.equals("0%")) {
|
||||
String gpuStats = "GPU: " + mGpuLoad + " " + mGpuFreq + " MHz";
|
||||
canvas.drawText(gpuStats, RIGHT-mPaddingRight-mMaxWidth, y-1, mOnlinePaint);
|
||||
y += mFH;
|
||||
}
|
||||
}
|
||||
|
||||
void updateDisplay() {
|
||||
@@ -254,9 +263,24 @@ public class CPUInfoService extends Service {
|
||||
}
|
||||
final int NW = mNumCpus;
|
||||
|
||||
int maxW = 0;
|
||||
if(!mCpuTemp.equals("0")) maxW = Math.max(maxW, (int)mOnlinePaint.measureText("CPU: " + getTemp(mCpuTemp) + "°C"));
|
||||
if(!mGpuTemp.equals("0")) maxW = Math.max(maxW, (int)mOnlinePaint.measureText("GPU: " + getTemp(mGpuTemp) + "°C"));
|
||||
if(!mBatTemp.equals("0")) maxW = Math.max(maxW, (int)mOnlinePaint.measureText("BAT: " + getTemp(mBatTemp) + "°C"));
|
||||
|
||||
for(int i=0; i<mCurrFreq.length; i++){
|
||||
String s = getCPUInfoString(i);
|
||||
maxW = Math.max(maxW, (int)mOnlinePaint.measureText(s));
|
||||
}
|
||||
|
||||
String gpuStats = "gpu: " + mGpuLoad + " " + mGpuFreq + " MHz";
|
||||
maxW = Math.max(maxW, (int)mOnlinePaint.measureText(gpuStats));
|
||||
|
||||
mMaxWidth = maxW;
|
||||
|
||||
int neededWidth = mPaddingLeft + mPaddingRight + mMaxWidth;
|
||||
int numExtraLines = (mCpuTempAvail?1:0) + (mGpuTempAvail?1:0) + (mBatTempAvail?1:0);
|
||||
int neededHeight = mPaddingTop + mPaddingBottom + (mFH*(numExtraLines+NW));
|
||||
int numTempLines = (mCpuTempAvail?1:0) + (mGpuTempAvail?1:0) + (mBatTempAvail?1:0);
|
||||
int neededHeight = mPaddingTop + mPaddingBottom + (mFH * (numTempLines + NW + 1));
|
||||
if (neededWidth != mNeededWidth || neededHeight != mNeededHeight) {
|
||||
mNeededWidth = neededWidth;
|
||||
mNeededHeight = neededHeight;
|
||||
@@ -274,18 +298,6 @@ public class CPUInfoService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
private String toMHzGPU(String hzString) {
|
||||
try {
|
||||
long val = Long.parseLong(hzString);
|
||||
if (val > 10000000) {
|
||||
return (val / 1000000) + " MHz";
|
||||
}
|
||||
return (val / 1000) + " MHz";
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public Handler getHandler(){
|
||||
return mCurCPUHandler;
|
||||
}
|
||||
@@ -294,10 +306,14 @@ public class CPUInfoService extends Service {
|
||||
protected class CurCPUThread extends Thread {
|
||||
private boolean mInterrupt = false;
|
||||
private Handler mHandler;
|
||||
private long[] mLastTotal = null;
|
||||
private long[] mLastIdle = null;
|
||||
|
||||
public CurCPUThread(Handler handler, int numCpus){
|
||||
mHandler=handler;
|
||||
mNumCpus = numCpus;
|
||||
mLastTotal = new long[numCpus];
|
||||
mLastIdle = new long[numCpus];
|
||||
}
|
||||
|
||||
public void interrupt() {
|
||||
@@ -323,19 +339,30 @@ public class CPUInfoService extends Service {
|
||||
String gpuFreq = CPUInfoService.readOneLine(GPU_FREQ_PATH);
|
||||
sb.append(gpuFreq == null ? "0" : gpuFreq).append(";");
|
||||
|
||||
String gpuLoadStr = "0%";
|
||||
String gpuBusy = CPUInfoService.readOneLine(GPU_LOAD_PATH);
|
||||
if (gpuBusy != null) {
|
||||
gpuLoadStr = gpuBusy.replaceAll("[^0-9]", "") + "%";
|
||||
}
|
||||
sb.append(gpuLoadStr).append(";");
|
||||
|
||||
String[] cpuLoads = getCpuLoad();
|
||||
|
||||
for(int i=0; i<mNumCpus; i++) {
|
||||
final String currCpu = mCpu[i];
|
||||
final String freqFile=CPU_ROOT + mCpu[i] + CPU_CUR_TAIL;
|
||||
String currFreq = CPUInfoService.readOneLine(freqFile);
|
||||
final String govFile=CPU_ROOT + mCpu[i] + CPU_GOV_TAIL;
|
||||
String currGov = CPUInfoService.readOneLine(govFile);
|
||||
String currLoad = (i < cpuLoads.length) ? cpuLoads[i] : "0%";
|
||||
|
||||
if(currFreq==null){
|
||||
currFreq="0";
|
||||
currGov="";
|
||||
currLoad="0%";
|
||||
}
|
||||
|
||||
sb.append(currCpu+":"+currFreq+":"+currGov+"|");
|
||||
sb.append(currCpu+":"+currFreq+":"+currGov+":"+currLoad+"|");
|
||||
}
|
||||
sb.deleteCharAt(sb.length()-1);
|
||||
mHandler.sendMessage(mHandler.obtainMessage(1, sb.toString()));
|
||||
@@ -344,6 +371,45 @@ public class CPUInfoService extends Service {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private String[] getCpuLoad() {
|
||||
String[] results = new String[mNumCpus];
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader("/proc/stat"));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (!line.startsWith("cpu")) continue;
|
||||
if (line.startsWith("cpu ")) continue;
|
||||
|
||||
String[] tokens = line.split("\\s+");
|
||||
String cpuName = tokens[0];
|
||||
int cpuId = Integer.parseInt(cpuName.substring(3));
|
||||
|
||||
if (cpuId >= mNumCpus) continue;
|
||||
|
||||
long idle = Long.parseLong(tokens[4]);
|
||||
long total = 0;
|
||||
for (int k=1; k<tokens.length; k++) {
|
||||
total += Long.parseLong(tokens[k]);
|
||||
}
|
||||
|
||||
long diffIdle = idle - mLastIdle[cpuId];
|
||||
long diffTotal = total - mLastTotal[cpuId];
|
||||
int load = 0;
|
||||
if (diffTotal > 0) {
|
||||
load = (int)((diffTotal - diffIdle) * 100 / diffTotal);
|
||||
}
|
||||
|
||||
mLastIdle[cpuId] = idle;
|
||||
mLastTotal[cpuId] = total;
|
||||
results[cpuId] = load + "%";
|
||||
}
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
for(int j=0; j<mNumCpus; j++) results[j] = "0%";
|
||||
}
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
@@ -351,14 +417,30 @@ public class CPUInfoService extends Service {
|
||||
super.onCreate();
|
||||
|
||||
CPU_TEMP_DIVIDER = getResources().getInteger(R.integer.config_cpuTempDivider);
|
||||
CPU_TEMP_SENSOR = getResources().getString(R.string.config_cpuTempSensor);
|
||||
GPU_TEMP_SENSOR = getResources().getString(R.string.config_gpuTempSensor);
|
||||
BATTERY_TEMP_SENSOR = getResources().getString(R.string.config_batteryTempSensor);
|
||||
DISPLAY_CPUS = getResources().getString(R.string.config_displayCpus);
|
||||
|
||||
String autoBat = findThermalZoneByType(BATTERY_TYPES);
|
||||
if (autoBat != null) {
|
||||
BATTERY_TEMP_SENSOR = autoBat;
|
||||
Log.d(TAG, "Auto-detected Battery zone: " + autoBat);
|
||||
} else {
|
||||
BATTERY_TEMP_SENSOR = getResources().getString(R.string.config_batteryTempSensor);
|
||||
}
|
||||
|
||||
String autoGpu = findThermalZoneByType(GPU_TYPES);
|
||||
if (autoGpu != null) {
|
||||
GPU_TEMP_SENSOR = autoGpu;
|
||||
Log.d(TAG, "Auto-detected GPU zone: " + autoGpu);
|
||||
} else {
|
||||
GPU_TEMP_SENSOR = getResources().getString(R.string.config_gpuTempSensor);
|
||||
}
|
||||
|
||||
CPU_TEMP_SENSOR = getResources().getString(R.string.config_cpuTempSensor);
|
||||
|
||||
mNumCpus = getCpus(DISPLAY_CPUS);
|
||||
mCurrFreq = new String[mNumCpus];
|
||||
mCurrGov = new String[mNumCpus];
|
||||
mCurrLoad = new String[mNumCpus];
|
||||
|
||||
mCpuTempAvail = readOneLine(CPU_TEMP_SENSOR) != null;
|
||||
mGpuTempAvail = readOneLine(GPU_TEMP_SENSOR) != null;
|
||||
@@ -399,6 +481,21 @@ public class CPUInfoService extends Service {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String findThermalZoneByType(String[] typeNames) {
|
||||
for (String target : typeNames) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String zonePath = THERMAL_DIR + "thermal_zone" + i;
|
||||
String typePath = zonePath + "/type";
|
||||
String typeValue = readOneLine(typePath);
|
||||
|
||||
if (typeValue != null && typeValue.trim().equalsIgnoreCase(target)) {
|
||||
return zonePath + "/temp";
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String readOneLine(String fname) {
|
||||
BufferedReader br;
|
||||
String line = null;
|
||||
|
||||
Reference in New Issue
Block a user