写在前面:
记录一下日常,之前有做过相关工作,但是没有记录下来,因此写一写文章。
本文主要用于在程序中获取CPU和内存的相关信息,比如:温度、CPU频率、内存使用、空闲等等。
cat /sys/devices/system/cpu/present
频率需要分不同的核,当然每个不同的处理器不一样。可以结合自己的。这里举例cpu0。
下表所列为freq对应的参数,可以直接通过cat查看相关信息。
具体的内容含义可以参考:可以手动设置CPU的策略
链接: cpu频率相关详解
//修改cpu频率测量
echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo conservative > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
performance // 性能
conservative //自动调整测策略--平滑
ondemand //自动调整--瞬时
//关闭cpu3,注意不能直接关闭cpu0,,,可以从最高的关闭3 2 1
echo 0 > /sys/devices/system/cpu/cpu3/online
内存相关的信息如下:
具体内容如下:
参考链接: meminfo详解
device_info.c
#include "device_info.h"
#include "return_data.h"
void mem_info_fun (mem_info *m)
{
FILE *fd;
int n;
char buff[256];
fd = fopen ("/proc/meminfo", "r");
//从fd文件中读取一行数据到buff
fgets (buff, sizeof(buff), fd);
//把buffer中的数据按照后面的格式存储到对应的地址中
sscanf (buff, "%s %u", m->MemTotal, &m->mem_total);
// printf("%s %s %u\n", buff,m->MemTotal,m->mem_total);
fgets (buff, sizeof(buff), fd);
sscanf (buff, "%s %u", m->MemFree, &m->mem_free);
fgets (buff, sizeof(buff), fd);
sscanf (buff, "%s %u", m->MemAvailable, &m->mem_available);
// printf("%s %s %u \n", buff,m->MemAvailable, m->mem_available);
m->mem_use_ratio = ((m->mem_total*1.0f - m->mem_available*1.0f)/m->mem_total*1.0f) * 100;
// printf("mem_ratio:%d \n",m->mem_ratio);
fclose(fd); //关闭文件fd
}
int cpu_info_fun(cpu_info *data)
{
int ret = SUCCESS;
FILE *fd;
static uint8_t get_cpu_num = 0;
char buff[100];
char path_buff[100];
fd = fopen ("/sys/devices/system/cpu/present", "r");
if(NULL == fd)
{
perror("cpu_num fopen");
return ERR_NULL;
}
//获取一行字符
fgets (buff, sizeof(buff), fd);
fclose(fd); //关闭文件fd
//把buffer中的数据按照后面的格式存储到对应的地址中
sscanf (buff, "0-%hhu", &data->cpu_num);
if(data->cpu_num >= CPU_MAX_NUM)
{
printf("cpu_info_fun error \n");
}
// printf("cpu_num:%u %s\n",data->cpu_num,path_buff);
sprintf(path_buff, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq",get_cpu_num);
// printf("path_buff:%s\n",path_buff);
fd = fopen(path_buff, "r");
if(NULL == fd)
{
perror("cpu_info_frep fopen");
return ERR_NULL;
}
fgets (buff, sizeof(buff), fd);
sscanf(buff, "%u", &data->cpu_cur_freq[get_cpu_num]);
// printf("cpu_cur%d_freq:%u\n",get_cpu_num,data->cpu_cur_freq[get_cpu_num]);
fclose(fd); //关闭文件fd
if(get_cpu_num++ >= data->cpu_num)
{
get_cpu_num = 0;
}
uint32_t temp = 0;
fd = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if(NULL == fd)
{
perror("cpu_info_temp fopen");
return ERR_NULL;
}
fgets (buff, sizeof(buff), fd);
sscanf(buff, "%u", &temp);
fclose(fd); //关闭文件fd
data->cpu_cur_temp = temp / 100;
// printf("temp:%u\n",data->cpu_cur_temp);
return ret;
}
int get_mem_info(mem_info *mem_data)
{
if(NULL == mem_data)
{
printf("get_device_info null\n");
return ERR_NULL;
}
//获取内存
mem_info_fun(mem_data);
return SUCCESS;
}
int get_cpu_info(cpu_info *mem_data)
{
if(NULL == mem_data)
{
printf("get_cpu_info null\n");
return ERR_NULL;
}
//获取内存
cpu_info_fun(mem_data);
return SUCCESS;
}
int get_device_info_ioctrol(uint8_t num, void *data)
{
int ret = SUCCESS;
if(NULL == data)
{
printf("get_device_info_ioctrol null \r\n");
return ERR_NULL;
}
switch (num)
{
case DEVICE_CPU_INFO:
{
ret |= get_cpu_info((cpu_info *)data);
break;
}
break;
case DEVICE_MEM_INFO:
{
ret |= get_mem_info((mem_info *)data);
break;
}
default:
break;
}
return ret;
}
device_info.h
#ifndef DEVICE_INFO_H
#define DEVICE_INFO_H
#include
#include "stdint.h"
#define CPU_MAX_NUM 32
enum DEVICE_INFO
{
DEVICE_CPU_INFO = 0,
DEVICE_MEM_INFO,
DEVICE_NUM_MAX
} device_info;// 最大参数
//定义一个mem_info的结构体
typedef struct _mem_info {
char MemTotal[20]; //定义一个char类型的数组名name有20个元素
uint32_t mem_total;
char MemFree[20];
uint32_t mem_free;
char MemAvailable[20];
uint32_t mem_available;
uint8_t mem_use_ratio;
}mem_info;
//定义一个cpu_info的结构体
typedef struct _cpu_info {
uint8_t cpu_num;
int16_t cpu_cur_temp;
uint32_t cpu_cur_freq[CPU_MAX_NUM];
}cpu_info;
int get_device_info_ioctrol(uint8_t num, void *data);
#endif