在Solaris 10和C中,我想定期查询
之类的数字我已经知道如何通过打开/ proc / pid / psinfo并从中读取(pr_rssize)来执行后者,但有没有办法实现前者?
过去我已经分发了一个prstat副本,解析了它的输出并将其读回到我的C代码中。由于我正在寻找越来越多的这类信息,这变得越来越乏味,而且感觉完全错了。当然有一种方法可以通过一些简单的C代码来实现。
感谢您的帮助。 NickB
答案 0 :(得分:2)
在Solaris上,可以通过阅读ps
pid /proc/
lwpid {{获取lwp特定的/lwp/
信息。 1}}。该文件包含/lwpsinfo
结构,其中包括:
lwpsinfo
有关其他详细信息,请参阅proc(4)。
答案 1 :(得分:1)
当您要求C解决方案时,也许您可以查看perl模块Solaris::Procfs以了解它如何提取信息?
答案 2 :(得分:1)
mem用法..你可能会找到/usr/include/sys/procfs.h psinfo_t结构
C代码,获取流程&每个线程的cpu使用情况。
#include <stdio.h>
#include <dirent.h>
#include <procfs.h>
int psinfo_thread_info(pid_t pid)
{
int i, nent, nread = 0;
size_t entsz;
FILE *fp = NULL;
char file_name[128] = {0,};
char *ptr, *buf;
prheader_t header;
lwpsinfo_t *lwpsinfo;
snprintf(file_name, sizeof(file_name), "/proc/%ld/lpsinfo", pid);
if ((fp = fopen(file_name, "r")) == NULL) {
return -1;
}
nread = fread(&header, sizeof(prheader_t), 1, fp);
if (nread < 0) {
fclose(fp);
return -1;
}
nent = header.pr_nent;
printf(" Thread_num: %d\n", nent);
entsz = header.pr_entsize * nent;
ptr = buf = malloc(entsz);
if (pread(fileno(fp), buf, entsz, sizeof (struct prheader)) != entsz) {
free(ptr);
free(buf);
fclose(fp);
return -1;
}
fclose(fp);
for (i = 0; i < nent; i++, ptr += header.pr_entsize)
{
lwpsinfo = (lwpsinfo_t *)ptr;
if (lwpsinfo == NULL) {
continue;
}
printf("[%2d thread] cpu_usage = %.2lf%%\n",
lwpsinfo->pr_lwpid,
((double)(lwpsinfo->pr_pctcpu * 100.0) / 0x8000));
}
free(ptr);
free(buf);
return 0;
}
int main(void)
{
FILE *fp = NULL;
DIR *proc_dir = NULL;
struct dirent *dir_entry = NULL;
int nread = 0;
char file_name[128] = {0,};
psinfo_t pinfo;
if ((proc_dir = opendir("/proc")) == NULL) {
printf("opendir failed\n");
return -1;
}
while ((dir_entry = readdir(proc_dir)) != NULL)
{
if (atoi(dir_entry->d_name) == 0) {
continue;
}
snprintf(file_name, sizeof(file_name), "/proc/%s/psinfo",
dir_entry->d_name);
if ((fp = fopen(file_name, "r")) == NULL) {
continue;
}
nread = fread(&pinfo, sizeof(pinfo), 1, fp);
if (nread < 0) {
fclose(fp);
continue;
}
fclose(fp);
printf("---------------------------\n");
printf("\nPROC:%s PID:%ld, CPU_USAGE:%.2lf%% ",
pinfo.pr_fname, pinfo.pr_pid,
((double)(pinfo.pr_pctcpu * 100.0) / 0x8000));
psinfo_thread_info(pinfo.pr_pid);
}
closedir(proc_dir);
return 0;
}
#include <stdio.h>
#include <dirent.h>
#include <procfs.h>
int psinfo_thread_info(pid_t pid)
{
int i, nent, nread = 0;
size_t entsz;
FILE *fp = NULL;
char file_name[128] = {0,};
char *ptr, *buf;
prheader_t header;
lwpsinfo_t *lwpsinfo;
snprintf(file_name, sizeof(file_name), "/proc/%ld/lpsinfo", pid);
if ((fp = fopen(file_name, "r")) == NULL) {
return -1;
}
nread = fread(&header, sizeof(prheader_t), 1, fp);
if (nread < 0) {
fclose(fp);
return -1;
}
nent = header.pr_nent;
printf(" Thread_num: %d\n", nent);
entsz = header.pr_entsize * nent;
ptr = buf = malloc(entsz);
if (pread(fileno(fp), buf, entsz, sizeof (struct prheader)) != entsz) {
free(ptr);
free(buf);
fclose(fp);
return -1;
}
fclose(fp);
for (i = 0; i < nent; i++, ptr += header.pr_entsize)
{
lwpsinfo = (lwpsinfo_t *)ptr;
if (lwpsinfo == NULL) {
continue;
}
printf("[%2d thread] cpu_usage = %.2lf%%\n",
lwpsinfo->pr_lwpid,
((double)(lwpsinfo->pr_pctcpu * 100.0) / 0x8000));
}
free(ptr);
free(buf);
return 0;
}
int main(void)
{
FILE *fp = NULL;
DIR *proc_dir = NULL;
struct dirent *dir_entry = NULL;
int nread = 0;
char file_name[128] = {0,};
psinfo_t pinfo;
if ((proc_dir = opendir("/proc")) == NULL) {
printf("opendir failed\n");
return -1;
}
while ((dir_entry = readdir(proc_dir)) != NULL)
{
if (atoi(dir_entry->d_name) == 0) {
continue;
}
snprintf(file_name, sizeof(file_name), "/proc/%s/psinfo",
dir_entry->d_name);
if ((fp = fopen(file_name, "r")) == NULL) {
continue;
}
nread = fread(&pinfo, sizeof(pinfo), 1, fp);
if (nread < 0) {
fclose(fp);
continue;
}
fclose(fp);
printf("---------------------------\n");
printf("\nPROC:%s PID:%ld, CPU_USAGE:%.2lf%% ",
pinfo.pr_fname, pinfo.pr_pid,
((double)(pinfo.pr_pctcpu * 100.0) / 0x8000));
psinfo_thread_info(pinfo.pr_pid);
}
closedir(proc_dir);
return 0;
}