基本版:

CPU   

using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
using System;
using System.Text;

public class CpuTest : MonoBehaviour {

    void Start () {
        InvokeRepeating ("Refresh", 0, 1);
    }
    
    void Refresh(){
        CpuInfo.Update ();
    }

    void OnGUI(){
        GUILayout.Label ("CPU 使用率:" + CpuInfo.cpuUsageRate.ToString ("0.0") + " %");
    }

}



using System.IO;
using System.Threading;

public class CpuInfo {

    public static readonly string statPath = "/proc/stat";

    public static string [] GetStatInfoArray(){
        return File.ReadAllLines (statPath);
    }

    public static float cpuUsageRate = 0;

    public static void Update () {
        new Thread (Update2).Start ();
    }

    private static void Update2(){
        string line;
        string [] values;
        float cpu1, cpu2, idle1, idle2;

        //------------------------

        line = GetStatInfoArray ()[0];
        values = Split(line);
        cpu1 = int.Parse(values[1]) + int.Parse(values[2]) + int.Parse(values[3])+int.Parse(values[5]) + int.Parse(values[6]) + int.Parse(values[7]) + int.Parse(values[8]);
        idle1 = int.Parse(values[4]);

        //------------------------

        Thread.Sleep (10);

        line = GetStatInfoArray ()[0];
        values = Split(line);
        cpu2 = int.Parse(values[1]) + int.Parse(values[2]) + int.Parse(values[3])+int.Parse(values[5]) + int.Parse(values[6]) + int.Parse(values[7]) + int.Parse(values[8]);
        idle2 = int.Parse(values[4]);

        //------------------------

        cpuUsageRate = (cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1)) * 100;
//      cpuUsageRate = (cpu2 - cpu1 - (idle2 - idle1)) / (cpu2 - cpu1) * 100 ; 
//      http://blog.csdn.net/jk110333/article/details/8683478
    }

    static string [] Split(string data){
        data = data.Replace (" "," ");
        data = data.Replace (" "," ");
        data = data.Replace (" "," ");
        return data.Split(' ');
    }

}



強化版:

Screenshot_2015-04-29-18-46-25  

using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
using System;
using System.Text;

public class CpuTest : MonoBehaviour {

string cpuCoresCount = "No Data";
string cpuProcessorCount = "No Data";
string cpuName = "No Data";

void Start () {
InvokeRepeating ("Refresh", 0, 1);

cpuCoresCount = CpuInfo.GetCoresCount ();
cpuProcessorCount = CpuInfo.GetProcessorCount ();
cpuName = CpuInfo.GetModelName ();
}

void Refresh(){
CpuInfo.Update ();
}

void OnGUI(){
for(int i = 0; i < CpuInfo.cpuUsageRate.Length; i++){
string cpu = i == 0 ? "All" : i + "";
string usageRate = CpuInfo.cpuUsageRate[i].ToString("0.0");
string mhz = i == 0 ? "" : float.Parse(CpuInfo.cpuMHz[i-1]).ToString("0.0") + " MHz";
GUILayout.Label("CPU " + cpu + " 使用率:" + usageRate + " % \t " + mhz);
}

GUILayout.Space (10);

GUILayout.Label ("CPU 名稱:" + cpuName);
GUILayout.Label ("CPU 物理 核心 數量:" + cpuCoresCount);
GUILayout.Label ("CPU 處理器 數量 (執行緒) :" + cpuProcessorCount);


GUILayout.Space (10);

GUILayout.Label (CpuInfo.info);

}

}


using System.IO;
using System.Threading;
using System.Collections.Generic;

public class CpuInfo {

public static readonly string statPath = "/proc/stat";
public static readonly string cpuinfoPath = "/proc/cpuinfo";

public static float [] cpuUsageRate = null;
public static string [] cpuMHz = null;
public static string info = "No Data";

public static string GetCPUInfo(){
return File.ReadAllText (cpuinfoPath);
}

public static string GetStatInfo(){
return File.ReadAllText (statPath);
}

public static string [] GetCPUInfoArray(){
return File.ReadAllLines (cpuinfoPath);
}

public static string [] GetStatInfoArray(){
return File.ReadAllLines (statPath);
}

public static void Update () {
new Thread (Compute).Start ();
cpuMHz = GetMHz ();
info = CpuInfo.GetCPUInfo ();
}

private static void Compute(){

List<string> cpuLine = ReadLine ();

cpuUsageRate = new float[cpuLine.Count];

float [] cpu1 = new float[cpuUsageRate.Length];
float [] cpu2 = new float[cpuUsageRate.Length];
float [] idle1 = new float[cpuUsageRate.Length];
float [] idle2 = new float[cpuUsageRate.Length];

Compute2 (cpuLine, cpu1, idle1);

Thread.Sleep (5);

cpuLine = ReadLine ();
Compute2 (cpuLine, cpu2, idle2);

Compute3 (cpu1, cpu2, idle1, idle2);
}

private static void Compute2(List<string> cpuLine, float [] cpu, float [] idle){
for(int k = 0; k < cpuUsageRate.Length; k++){
for(int i = 1; i < 9 ; i++){
cpu[k] += int.Parse(Split(cpuLine[k])[i]);
}
idle[k] = int.Parse(Split(cpuLine[k])[4]);
}
}

private static void Compute3(float [] cpu1, float [] cpu2, float [] idle1, float [] idle2){
for(int k = 0; k < cpuUsageRate.Length; k++){
cpuUsageRate [k] = (cpu2[k] - cpu1[k] - (idle2[k] - idle1[k])) / (cpu2[k] - cpu1[k]) * 100;
}
}

private static List<string> ReadLine(){
List<string> cpuLine = new List<string> ();

foreach(string line2 in GetStatInfoArray ()){
string title = Split(line2)[0];
if(!title.ToLower().Contains("cpu")){
break;
}
cpuLine.Add(line2);
}
return cpuLine;
}

private static string [] Split(string data){
data = data.Replace ("\t\t\t"," ");
data = data.Replace ("\t\t"," ");
data = data.Replace ("\t\t"," ");
data = data.Replace (" "," ");
data = data.Replace (" "," ");
data = data.Replace (" "," ");
return data.Split(' ');
}

public static string GetCoresCount(){
foreach(string line in GetCPUInfoArray()){
if(line.Split(':')[0].ToLower().Contains("cpu cores")){
return line.Split(':')[1];
}
}
return "Unknown";
}

public static string GetProcessorCount(){
foreach(string line in GetCPUInfoArray()){
if(line.Split(':')[0].ToLower().Contains("siblings")){
return line.Split(':')[1];
}
}
return "Unknown";
}

public static string GetModelName(){
foreach(string line in GetCPUInfoArray()){
if(line.Split(':')[0].ToLower().Contains("model name")){
return line.Split(':')[1];
}
}
return "Unknown";
}

public static string [] GetMHz(){
List<string> MHz = new List<string> ();
foreach(string line in GetCPUInfoArray()){
if(line.Split(':')[0].ToLower().Contains("mhz")){
MHz.Add(line.Split(':')[1]);
}
}
return MHz.ToArray();
}

}








arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()