C语言获取MTK平台系统资源信息(CPU/GPU/fps/温度等),保存为表格形式输出

版权声明:转载请说明,谢谢。 https://blog.csdn.net/wuming_422103632/article/details/81604479
  • 对照的平台是几年前使用的比较差的X25平台,MTK应该很多信息,尤其是X系列,P系列都是类似的。
  • 首先需要知道MTK平台一些私有信息在哪里?比如fps,gpu info等。
#define SYSTEM_FPS_PATH "/proc/fps_tm/fps_count"
#define SYSTEM_GPU_FREQ_PATH "/sys/kernel/debug/ged/hal/current_freqency"
#define SYSTEM_GPU_LOADING_PATH "/sys/kernel/debug/ged/hal/gpu_utilization"
#define SYSTEM_GPU_AND_POWER_LIMIT_PATH "/proc/thermlmt"
  • 剩下的就是如何获取这些私有信息和共有信息(CPU loading and frequency)。

  • 可以根据参数设定抓取时间和抓取间隔。

  • 最重要的是以什么方式将抓取的信息展示出来,目前使用的是csv格式输出,可以使用Excel文本格式软件打开。实现代码如下
#define ERR_RETURN(STR) printf(STR)

int32 putString2Csv(char str[],char filename[],int mode)
{
    FILE *_fp;
    //try to open filename
    if ((_fp=fopen(filename,"a"))==NULL) {
        ERR_RETURN("fopen called error");
    }
    int _mode=mode;
    switch(_mode) {
        case ECWM_ONELINE:
        {
            fputs(str,_fp);
            fputs("\t",_fp);
        }break;
        case ECWM_OTHERLINE:
        {
            fputs("\n",_fp);
        }break;
        default:
            break;
    }

    if (fclose(_fp) !=0){
        ERR_RETURN("fclose called error");
    }
    return 1;
}

static const char *title_name[29]={"time","cpu_temp","bat_temp","pcb_temp","cpu0_freq",
                                  "cpu0_load","cpu1_freq","cpu1_load","cpu2_freq","cpu2_load",
                                  "cpu3_freq","cpu3_load","cpu4_freq","cpu4_load","cpu5_freq",
                                  "cpu5_load","cpu6_freq","cpu6_load","cpu7_freq","cpu7_load",
                                  "cpu8_freq","cpu8_load","cpu9_freq","cpu9_load","gpu_freq",
                                   "gpu_load","fps","cpu_limit","gpu_limit"};

//使用方式如下:
    for (i = 0;i < 29; ++i) {
        char strtemp[256];
        sprintf(strtemp,"%s",title_name[i]);
        putString2Csv(strtemp,save_log_path,ECWM_ONELINE);
    }
    putString2Csv("",save_log_path,ECWM_OTHERLINE);//must
  • 计算CPU loading的方式是根据/proc/stat的数据来update的,update的信息:
cat proc/stat                                                                                                                                                                                
cpu  542172 23540 4360257 184518989 2820 0 5294 0 0 0
cpu0 256160 6440 1696499 21305268 159 0 2244 0 0 0
cpu1 206729 7308 1348327 21881188 1340 0 2321 0 0 0
cpu2 45123 3048 322460 23245875 264 0 556 0 0 0
cpu3 22798 1533 99058 23666599 59 0 100 0 0 0
cpu4 7035 3428 674796 23099052 684 0 20 0 0 0
cpu5 3408 1680 191452 23641620 238 0 4 0 0 0
cpu6 518 64 22761 23828994 50 0 11 0 0 0
cpu7 398 36 4902 23850390 23 0 36 0 0 0
intr 187495158 0 12790737 69525078 0 0 1 27 54 146 13238 0 0 673242 0 5 3363 0 0 0 0 0 4114845 0 185934 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 144223 0 0 0 0 0 0 0 0 0 0 0 0 728094 0 0 1444 1436 0 5 3 0 0 0 0 3 0 1 34981716 0 0 0 0 2 36 0 0 0 0 0 0 0
ctxt 197330798
btime 1533886146
processes 205873
procs_running 2
procs_blocked 0
softirq 72663786 1 27931895 119496 8161 0 0 1045493 26464952 0 17093788
  • 对于上面/proc/stat上面一部分的内容详细code在kernel/sched/cputime.c,update cpu time类型如下:
    具体展示proc/stat信息的code在fs/proc/stat.c show_stat()函数里面,这里不讲解。
enum cpu_usage_stat {
    CPUTIME_USER,
    CPUTIME_NICE,
    CPUTIME_SYSTEM,
    CPUTIME_SOFTIRQ,
    CPUTIME_IRQ,
    CPUTIME_IDLE,
    CPUTIME_IOWAIT,
    CPUTIME_STEAL,
    CPUTIME_GUEST,
    CPUTIME_GUEST_NICE,
    NR_STATS,
};
  • 更新cpu loading计算方式如下:
static int get_cpu_loading()
{
    FILE *fp = fopen(SYSTEM_CPU_STAT, "r");
    char *delim = " ";
    char szTest[1000] = {0};
    char *p;
    char cpu[5];
    int i = 0;
    int cpu_id = 0;

    if(NULL == fp) {
        printf("failed to open %s file node\n",SYSTEM_CPU_STAT);
        return 1;
    }

    while(!feof(fp)) {
        memset(szTest, 0, sizeof(szTest));
        fgets(szTest, sizeof(szTest) - 1, fp);//include<\n>
        if(szTest[3] == ' ')
            continue;

        if(szTest[0] != 'c')
            break;

        cpu_id = szTest[3] - '0';

        sscanf(szTest,"%s %lu %lu %lu %lu %lu %lu %lu",
                            cpu,&new_cpu_time[cpu_id][0],&new_cpu_time[cpu_id][1],
                            &new_cpu_time[cpu_id][2],&new_cpu_time[cpu_id][3],
                            &new_cpu_time[cpu_id][4],&new_cpu_time[cpu_id][5],
                            &new_cpu_time[cpu_id][6]);
    }
    fclose(fp);
    return 0;
}
static void cacl_cpu_loading()
{
    int i = 0,j = 0;
    for(i = 0; i < SYSTEM_INFO; i++) {
        if(new_cpu_time[i][0] == old_cpu_time[i][0]) {
            cpu_loading[i] = 0;
            continue;
        }
        for( j = 0; j < CPU_TIME_NUM; j++ ) {
            total_cpu_time[i] += (new_cpu_time[i][j] - old_cpu_time[i][j]);
        }
        user_system_cpu_time[i] = (new_cpu_time[i][0] + new_cpu_time[i][2]) - (old_cpu_time[i][0] + old_cpu_time[i][2]);
        cpu_loading[i] = user_system_cpu_time[i] * 100 / total_cpu_time[i];
    }
}
//具体怎么计算看个人的写法了。
  • 抓取的信息带时间格式,实现如下:

static void create_save_log_file(void)
{
    struct tm *newtime;
    int len = 0;
    time_t t1;
    t1 = time(NULL);
    newtime = localtime(&t1);
    strftime(logfile,39,"thermal-log-%Y-%m-%d-%H:%M:%S.csv",newtime);
    logfile[38] = 0;
}
  • 最后由于之前做的时间比较急,很多没有完善,特别是针对于字符的处理不够精确,如果会Java更好了,可以做成一个app通用型的,以后有机会再做吧。如果有好的思路还请提供,thanks.
    全部代码可以参考如下:
    https://github.com/samarxie/fetch-sys-info

猜你喜欢

转载自blog.csdn.net/wuming_422103632/article/details/81604479