请教unix下通用获取cpu和内存信息的函数!!

jamesontan 2009-02-17 05:45:38
请教unix下通用获取cpu和内存信息的函数!! 注意,不是用户命令(top,prstat,vmstat,sar 等等。。)
请教C语言可调用的标准库函数!!
...全文
1163 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
superbtl 2009-07-27
  • 打赏
  • 举报
回复
系统调用 ps不行吗
jitashan 2009-07-27
  • 打赏
  • 举报
回复
#include <kstat.h>

LDFLAGS=-lkstat
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 Cocoky 的回复:]
你的makefile是怎么写的啊?
[/Quote]
没用使用makefile 直接用gcc
gcc -o getmeminfo getmeminfo.c
getmeminfo.c: In function `main':
getmeminfo.c:47: error: `data' undeclared (first use in this function)
getmeminfo.c:47: error: (Each undeclared identifier is reported only once
getmeminfo.c:47: error: for each function it appears in.)
Cocoky 2009-03-10
  • 打赏
  • 举报
回复
你的makefile是怎么写的啊?
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 Cocoky 的回复:]
获取内存情况:

ulong pagesize;
static ulong pageshift = 0, physpages = 0;
kstat_ctl_t *kc = NULL;
kstat_t *ksp;
kstat_named_t *knp;
unsigned long memtotal, memfree, memused;
unsigned long swaptotal, swapused;

struct anoninfo ai;

if(pageshift == 0)
{
for(pagesize = sysconf(_SC_PAGESIZE); pagesize > 1; pagesize >>= 1) …
[/Quote]

data->nFreeMem = memfree / 1024 / 1024;
data->nOccupiedMem = memused / 1024 / 1024;
data->nPhysicalMem = data->nFreeMem + data->nOccupiedMem
请问这段程序中的都data在那里定义啦?谢谢
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
getmeminfo.c: In function `main':
getmeminfo.c:19: error: storage size of 'ai' isn't known
getmeminfo.c:31: error: syntax error before '<' token
getmeminfo.c:36: warning: `return' with no value, in function returning non-void
getmeminfo.c:45: error: syntax error before '<' token
getmeminfo.c:47: error: `data' undeclared (first use in this function)
getmeminfo.c:47: error: (Each undeclared identifier is reported only once
getmeminfo.c:47: error: for each function it appears in.)
getmeminfo.c:53: warning: implicit declaration of function `swapctl'
getmeminfo.c:53: error: `SC_AINFO' undeclared (first use in this function)
getmeminfo.c:57: warning: `return' with no value, in function returning non-void
getmeminfo.c:60: error: syntax error before '<' token
getmeminfo.c:61: error: syntax error before '<' token
*** Error code 1
make: Fatal error: Command failed for target `getmeminfo'

老大,还是找不到结构体哦,网上搜索不到啦
Cocoky 2009-03-10
  • 打赏
  • 举报
回复
找不到结构体上网搜索一下就可以了
Cocoky 2009-03-10
  • 打赏
  • 举报
回复
/// structure of CPU idle
typedef struct _tag_CPUINFO
{
unsigned long nIdleCount;
}CPUINFO;

/// structure of memory information
typedef struct _tag_MEMINFO
{
unsigned long nPhysicalMem; // MB
unsigned long nVirtualMem;
unsigned long nOccupiedMem;
unsigned long nFreeMem;
}MEMINFO;

/// structure of disk information
typedef struct _tag_DISKINFO
{
char szPartionNum[16];
unsigned long nPhysicalSpace; // MB
unsigned long nOccupiedSpace;
unsigned long nFreeSpace;
} DISKINFO;

/// time structure
typedef struct _tag_LOCALTEMTIME { // st
unsigned short nYear;
unsigned short nMonth;
unsigned short nDayOfWeek;
unsigned short nDay;
unsigned short nHour;
unsigned short nMinute;
unsigned short nSecond;
unsigned short nMilliseconds;
} LOCALTEMTIME;
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 Cocoky 的回复:]
获取内存情况:

ulong pagesize;
static ulong pageshift = 0, physpages = 0;
kstat_ctl_t *kc = NULL;
kstat_t *ksp;
kstat_named_t *knp;
unsigned long memtotal, memfree, memused;
unsigned long swaptotal, swapused;

struct anoninfo ai;

if(pageshift == 0)
{
for(pagesize = sysconf(_SC_PAGESIZE); pagesize > 1; pagesize >>= 1) …
[/Quote]

getcpu.c: In function `main':
getcpu.c:7: error: `kstat_ctl_t' undeclared (first use in this function)
getcpu.c:7: error: (Each undeclared identifier is reported only once
getcpu.c:7: error: for each function it appears in.)
getcpu.c:7: error: `kc' undeclared (first use in this function)
getcpu.c:13: error: storage size of 'ai' isn't known
getcpu.c:25: error: syntax error before '<' token
getcpu.c:27: warning: implicit declaration of function `kstat_open'
getcpu.c:30: warning: `return' with no value, in function returning non-void
getcpu.c:33: warning: implicit declaration of function `kstat_lookup'
getcpu.c:33: warning: assignment makes pointer from integer without a cast
getcpu.c:34: warning: implicit declaration of function `kstat_read'
getcpu.c:36: warning: implicit declaration of function `kstat_data_lookup'
getcpu.c:39: error: syntax error before '<' token
getcpu.c:41: error: `data' undeclared (first use in this function)
getcpu.c:47: warning: implicit declaration of function `swapctl'
getcpu.c:47: error: `SC_AINFO' undeclared (first use in this function)
getcpu.c:50: warning: implicit declaration of function `kstat_close'
getcpu.c:51: warning: `return' with no value, in function returning non-void
getcpu.c:54: error: syntax error before '<' token
getcpu.c:55: error: syntax error before '<' token
*** Error code 1
make: Fatal error: Command failed for target `getcpu'
Cocoky 2009-03-10
  • 打赏
  • 举报
回复
这类代码主要用在网管程序中,用于监控服务器的实时情况。
Cocoky 2009-03-10
  • 打赏
  • 举报
回复
获取cpu信息,可能有多个cpu:

kstat_ctl_t *kc = NULL;
SYSTEM_CPU_WAITIO cpu_waitio[MAX_CPU_COUNT];
kstat_t *ksp;
cpu_stat_t cs;
int i;
int j;
static int n_cpus;
int n_smp_cpus;
ulong composite_user, composite_nice, composite_sys, composite_idle;
ulong composite_waitio, composite_waitswap, composite_waitpio;
char cpuid[128];
int nIdleTime;
double dIdleTime = 0.0;

composite_user = composite_nice = composite_sys = composite_idle = 0;
composite_waitio = composite_waitswap = composite_waitpio = 0;

if (data == NULL)
{
return -1;
}

// open the kstat
if ((kc = kstat_open()) == NULL)
{
perror("kstat_open");
return -1;
}

memset(cpu_waitio, 0, sizeof(SYSTEM_CPU_WAITIO)*MAX_CPU_COUNT);


n_cpus = n_smp_cpus = 0;

if(kstat_chain_update(kc) == -1)
{
perror("kstat_chain_update");
return -1;
}

for ( ksp = kc->kc_chain, i = 0, j = 0; ksp && (j < MAX_CPU_COUNT); ksp = ksp->ks_next, i++)
{
if (strcmp(ksp->ks_module, "cpu_stat"))
continue;
if (kstat_read(kc, ksp, NULL) != -1)
{
sprintf(cpu_waitio[j++].name , "cpu%d", ksp->ks_instance);
n_cpus++;
}
else
break;
}

if (n_cpus > 1)
n_smp_cpus++;

n_smp_cpus = 1;

if (kstat_chain_update(kc) == -1)
{
perror("kstat_chain_update");
return -1;
}


for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next)
{
if (strcmp(ksp->ks_module, "cpu_stat"))
continue;
if (kstat_read(kc, ksp, &cs) == -1)
{
perror("kstat_read");
continue;
}
for (i = 0; i < n_cpus; i++)
{
memset(cpuid, 0, sizeof(cpuid));
sprintf(cpuid, "cpu%d", ksp->ks_instance);
if (strcmp(cpu_waitio[i].name, cpuid))
{
continue;
}
cpu_waitio[i].user = cs.cpu_sysinfo.cpu[CPU_USER];
cpu_waitio[i].nice = cs.cpu_sysinfo.cpu[CPU_WAIT];
cpu_waitio[i].sys = cs.cpu_sysinfo.cpu[CPU_KERNEL];
cpu_waitio[i].idle = cs.cpu_sysinfo.cpu[CPU_IDLE];
cpu_waitio[i].wait_io = cs.cpu_sysinfo.wait[W_IO];
cpu_waitio[i].wait_swap = cs.cpu_sysinfo.wait[W_SWAP];
cpu_waitio[i].wait_pio = cs.cpu_sysinfo.wait[W_PIO];

if (n_smp_cpus)
{
composite_user += cpu_waitio[i].user;
composite_nice += cpu_waitio[i].nice;
composite_sys += cpu_waitio[i].sys;
composite_idle += cpu_waitio[i].idle;
composite_waitio += cpu_waitio[i].wait_io;
composite_waitswap += cpu_waitio[i].wait_swap;
composite_waitpio += cpu_waitio[i].wait_pio;
}
i++;
}
}

printf("CPU:[%d][%d][%d][%d][%d][%d][%d][%d]\n",
composite_user, composite_nice, composite_sys, composite_idle,
composite_waitio, composite_waitswap, composite_waitpio);
dIdleTime = (double)(composite_user)/(composite_user+composite_sys+composite_idle+composite_waitio+composite_waitswap);
nIdleTime = 100*dIdleTime;
data->nIdleCount = 100-(unsigned long)nIdleTime;
Cocoky 2009-03-10
  • 打赏
  • 举报
回复
获取内存情况:

ulong pagesize;
static ulong pageshift = 0, physpages = 0;
kstat_ctl_t *kc = NULL;
kstat_t *ksp;
kstat_named_t *knp;
unsigned long memtotal, memfree, memused;
unsigned long swaptotal, swapused;

struct anoninfo ai;

if(pageshift == 0)
{
for(pagesize = sysconf(_SC_PAGESIZE); pagesize > 1; pagesize >>= 1)
pageshift++;
}
if(physpages == 0)
{
physpages = sysconf(_SC_PHYS_PAGES);
}

memtotal = physpages << pageshift;

if((kc = kstat_open()) == NULL)
{
printf("kstat_open fail!\n");
return;
}

ksp = kstat_lookup(kc, (char*)"unix", -1, (char*)"system_pages");
if(ksp && kstat_read(kc, ksp, NULL) >= 0)
{
knp = (kstat_named_t *)kstat_data_lookup(ksp, (char*)"pagesfree");
if(knp)
{
memfree = knp->value.ui32 << pageshift;
memused = memtotal - memfree;
data->nFreeMem = memfree / 1024 / 1024;
data->nOccupiedMem = memused / 1024 / 1024;
data->nPhysicalMem = data->nFreeMem + data->nOccupiedMem;
}
}

if(swapctl(SC_AINFO, &ai) == -1)
{
printf("swapctl fail!\n");
kstat_close(kc);
return;
}

swaptotal = ai.ani_max << pageshift;
swapused = ai.ani_resv << pageshift;
data->nVirtualMem = swaptotal / 1024 / 1024;
kstat_close(kc);
howema 2009-03-10
  • 打赏
  • 举报
回复
直接用system("cat /proc/cpuinfo");
howema 2009-03-10
  • 打赏
  • 举报
回复
cat /proc/cpuinfo
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yutaooo 的回复:]


C/C++ code
man getrusage

看看是不是符合你要求 。。。
[/Quote]
谢谢
但是getrusage()函数只能得到一个进程在系统中和用户模式下执行的时间。我想得到的是当前CPU可以被利用的百分比。就像NT下面“任务管理器”里面那样,得到一个数字;或者象linux下的top命令一样,得到每个进程占用了多少CPU资源.
yutaooo 2009-03-10
  • 打赏
  • 举报
回复

确定 getrusage() 不满足 LZ 需要了 ?
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 benjaminweber 的回复:]
solaris下可以用 psrinfo -v
C语言也可以执行系统命令

还可以直接解析cpuid
不过要用到汇编
[/Quote]

这个难度好像大了点,能提供点获取的代码吗?汇编我还是菜鸟哈。
jamesontan 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 mosaic 的回复:]
楼主说的cpu和内存信息,是指什么?

cpu的个数? 主频? 还是当前各个cpu使用率?
内存信息包含什么? 已用的?空闲的? 总的物理内存? 虚拟内存?
[/Quote]

当前各个cpu使用率,内存信息包含已用的和空闲的,谢谢。
xiaopoy 2009-02-23
  • 打赏
  • 举报
回复
内陷汇编
cpuid;
得到CPU信息

内存的大小读取块设备属性即可。或者查这些目录的属性
1 block RAM disk
0 = /dev/ram0 第1个 RAM disk (initrd只能使用ram0)
1 = /dev/ram1 第2个 RAM disk
...
200 = /dev/ram200 第200个 RAM disk
piginthetree 2009-02-22
  • 打赏
  • 举报
回复
貌似没有这样的库函数或系统调用, 没有必要做这样的库函数吧
加载更多回复(14)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧