linux c实现获取系统温度,cpu主频,运行时间,内存使用情况

代码如下:

/*
 * @Description: 
 * @Version: 
 * @Autor: lishi
 * @Date: 2023-02-17 09:09:27
 * @LastEditors: lishi
 * @LastEditTime: 2023-02-17 10:18:45
 */
#include 
#include 
#include 
#include 

static int getSysIntValue(char *key)
{
    FILE *fp = NULL;
    int value = 0;
    if(key == NULL)return 0;
    
    fp = fopen(key, "r");
    if (fp == NULL) {
        printf("Error: could not open %s file\n",key);
        return 1;
    }

    fscanf(fp, "%d", &value);
    fclose(fp);fp = NULL;
    return value;
}

static int getSysStringValue(char *key,char *value,int bufLen)
{
    FILE *fp = NULL;
    int i = 0,j=0;
    char *tmp = NULL;
    if(key == NULL || value == NULL || bufLen <= 0)return 0;
    tmp = (char *)malloc(bufLen);
    if(tmp == NULL)return 0;
    memset(value,0,bufLen);
    fp = fopen(key, "r");
    if (fp == NULL) {
	    free(tmp);
	    tmp = NULL;
        printf("Error: could not open %s file\n",key);
        return 1;
    }
    fgets(tmp, bufLen, fp);
    for(i = 0;i < strlen(tmp);i++){
        if(tmp[i] != '\n' && tmp[i] != '\r'){
            value[j++] = tmp[i];
        }
    }
    fclose(fp);fp=NULL;
    free(tmp);tmp = NULL;
    return 0;
}

int main() 
{
    struct sysinfo info;
    int i = 0;
    long run_secs_total = 0;
    long run_hours = 0,run_mins = 0,run_secs = 0;
    long mem_total = 0,mem_free = 0,mem_used = 0;
    double mem_total2 = 0,mem_free2 = 0,mem_used2 = 0;
    if (sysinfo(&info) != 0) {
        printf("Error: could not get system information\n");
        return 1;
    }
    run_secs_total = info.uptime;
    run_hours = run_secs_total / 3600;
    run_mins = (run_secs_total%3600) / 60;
    run_secs = run_secs_total % 60;
    printf("%-20s: %02ld:%02ld:%02ld \n\n","System run time", run_hours,run_mins,run_secs);

    mem_total = info.totalram>>20;
    mem_free = info.freeram>>20;
    mem_used = (info.totalram - info.freeram)>>20;
    mem_total2 = mem_total/1024.0;
    mem_free2 = mem_free/1024.0;
    mem_used2 = mem_used/1024.0;
    printf("%-20s: %.2f GB\n", "Total RAM",mem_total2);
    printf("%-20s: %.2f GB\n", "Used RAM",mem_used2);
    printf("%-20s: %.2f GB\n", "Free RAM",mem_free2);
    printf("%-20s: %ld%%\n\n","Memory usage", ((info.totalram - info.freeram) * 100) / info.totalram);
    
    printf("%-20s: %d%%\n\n", "GPU utilization",getSysIntValue("/sys/devices/platform/fb000000.gpu/utilisation"));
    for(i=0;i<7;i++){
        char key[128] = {0};
        char value[256] = {0};
        int temp = 0;
        double temp2 = 0;
        sprintf(key,"/sys/class/thermal/thermal_zone%d/type",i);
        getSysStringValue(key,value,sizeof(value));
        memset(key,0,sizeof(key));
        sprintf(key,"/sys/class/thermal/thermal_zone%d/temp",i);
        temp = getSysIntValue(key);
        temp2 = temp/1000.0;
        printf("%-20s: %.3f°\n",value,temp2);
    }
    printf("\n");
    for(i=0;i<8;i++){
        char key[128] = {0};
        char value[256] = {0};
        long temp = 0;
        double temp2 = 0;
        sprintf(key,"/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq",i);
        getSysStringValue(key,value,sizeof(value));
        temp = strtol(value,NULL,10);
        temp2 = temp/1000000.0;
        memset(key,0,sizeof(key));
        sprintf(key,"cpu%d_cur_freq",i);
        printf("%-20s: %.3f GHZ\n",key,temp2);
    }
    return 0;
}

运行截图如下:
linux c实现获取系统温度,cpu主频,运行时间,内存使用情况_第1张图片

你可能感兴趣的:(日常记录,linux,c语言,c++)