#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;
}