Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

342 lignes
9.1 KiB

/*
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <curses.h>
#include <stdlib.h>
#include <limits.h>
#include <termcap.h>
#include <termios.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <assert.h>
typedef int endpoint_t;
typedef uint64_t u64_t;
typedef long unsigned int vir_bytes;
#define USED 0x1
#define IS_TASK 0x2
#define IS_SYSTEM 0x4
#define BLOCKED 0x8
#define TYPE_TASK 'T'
#define TYPE_SYSTEM 'S'
#define STATE_RUN 'R'
#define MAX_NR_TASKS 1023
#define SELF ((endpoint_t) 0x8ace)
#define _MAX_MAGIC_PROC (SELF)
#define _ENDPOINT_GENERATION_SIZE (MAX_NR_TASKS+_MAX_MAGIC_PROC+1)
#define _ENDPOINT_P(e) \
((((e)+MAX_NR_TASKS) % _ENDPOINT_GENERATION_SIZE) - MAX_NR_TASKS)
#define SLOT_NR(e) (_ENDPOINT_P(e) + 5)
#define _PATH_PROC "/proc"
#define CPUTIME(m, i) (m & (1L << (i)))
const char *cputimenames[] = { "user", "ipc", "kernelcall" };
#define CPUTIMENAMES (sizeof(cputimenames)/sizeof(cputimenames[0]))
unsigned int nr_procs, nr_tasks;
int nr_total=0;
struct proc {
int p_flags;
endpoint_t p_endpoint;
pid_t p_pid;
u64_t p_cpucycles[CPUTIMENAMES];
int p_priority;
endpoint_t p_blocked;
time_t p_user_time;
vir_bytes p_memory;
uid_t p_effuid;
int p_nice;
char p_name[16+1];
};
struct proc *proc = NULL, *prev_proc = NULL;
//u64_t 64位 high和low32位 拼接成64位 high+low
static inline u64_t make64(unsigned long lo, unsigned long hi)
{
return ((u64_t)hi << 32) | (u64_t)lo;
}
//把每个pid/psinfo的信息读出来
//判断读取信息是否可用
void parse_file(pid_t pid)
{
char path[PATH_MAX], name[256], type, state;
int version, endpt, effuid;
unsigned long cycles_hi, cycles_lo;
FILE *fp;
struct proc *p;
int slot;
int i;
sprintf(path, "/proc/%d/psinfo", pid);
if ((fp = fopen(path, "r")) == NULL)
return;
if (fscanf(fp, "%d", &version) != 1) {
fclose(fp);
return;
}
if (version != 0) {
fputs("procfs version mismatch!\n", stderr);
exit(1);
}
if (fscanf(fp, " %c %d", &type, &endpt) != 2) {
fclose(fp);
return;
}
//统计总file数
//filenum+=1;
//原来的slot超出了nr_total
slot = SLOT_NR(endpt);
slot++;
//slot=slot_a;
//slot_a+=1;//赋值需保证在数组中不会重复
//判断endpoint的值是否合理 在0到nr_total的范围内
if(slot < 0 || slot >= nr_total) {
//fprintf(stderr, "top: unreasonable endpoint number %d\n", endpt);
fclose(fp);
return;
}
//slot为该进程结构体在数组中的位置
p = &proc[slot];//把slot地址赋值给p
if (type == TYPE_TASK)
//标示task进程
p->p_flags |= IS_TASK;
else if (type == TYPE_SYSTEM)
//标示system进程
p->p_flags |= IS_SYSTEM;
//将endpt和pid存入对应进程结构体
p->p_endpoint = endpt;
p->p_pid = pid;
//读入名字 状态 阻塞状态 动态优先级 进程时间 高周期 低周期
if (fscanf(fp, " %255s %c %d %d %ld %*u %lu %lu",
name, &state, &p->p_blocked, &p->p_priority,
&p->p_user_time, &cycles_hi, &cycles_lo) != 7) {
fclose(fp);
return;
}
//将指定长度的字符串复制到字符数组中
strncpy(p->p_name, name, sizeof(p->p_name)-1);
//数组置0
p->p_name[sizeof(p->p_name)-1] = 0;
if (state != STATE_RUN)//如果不是run的进程
p->p_flags |= BLOCKED;//标志阻塞
//拼接成64位,放在p_cpucycles[]数组中
p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
p->p_memory = 0L;
//判断是否为有效用户ID
if (!(p->p_flags & IS_TASK)) {
int j;
//读如内存 有效用户ID 和静态优先级
if ((j=fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u",
&p->p_memory, &effuid, &p->p_nice)) != 3) {
fclose(fp);
return;
}
p->p_effuid = effuid;
} else p->p_effuid = 0;
//连续读CPUTIMENAMES次cycles_hi,cycle_lo
for(i = 1; i < CPUTIMENAMES; i++) {
if(fscanf(fp, " %lu %lu",
&cycles_hi, &cycles_lo) == 2) {
//拼接成64位,放在p_cpucycles[]数组中
p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
} else {
p->p_cpucycles[i] = 0;
}
}
//读如内存 存入进程结构体
if ((p->p_flags & IS_TASK)) {
if(fscanf(fp, " %lu", &p->p_memory) != 1) {
p->p_memory = 0;
}
}
//按位或
p->p_flags |= USED;
fclose(fp);
}
void parse_dir(void)
{
DIR *p_dir;
struct dirent *p_ent;
pid_t pid;
char *end;
if ((p_dir = opendir("/proc/")) == NULL) {
perror("opendir on /proc");
exit(1);
}
p_ent=readdir(p_dir);
while(p_ent != NULL){
pid=strtol(p_ent->d_name,&end,10);
if(pid!=0 && !end[0]){
parse_file(pid);
}
p_ent=readdir(p_dir);
}
closedir(p_dir);
}
int print_memory(void)
{
FILE *fp;
unsigned int pagesize;
unsigned long total, free, largest, cached;
if ((fp = fopen("/proc/meminfo", "r")) == NULL)
return 0;
if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free,
&largest, &cached) != 5) {
fclose(fp);
return 0;
}
fclose(fp);
printf("main memory: %ldK total, %ldK free, %ldK contig free, "
"%ldK cached\n",
(pagesize * total)/1024, (pagesize * free)/1024,
(pagesize * largest)/1024, (pagesize * cached)/1024);
return 1;
}
struct tp {
struct proc *p;
u64_t ticks;
};
//计算cputicks 用到当前进程和其他进程的,还涉及CPUTIME
//滴答并不是简单的结构体中的滴答,因为在写文件的时候需要更新。需要通过当前进程来和该进程一起计算
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode)
{
int i;
u64_t t = 0;
//计算每个进程proc的滴答,通过proc和当前进程prev_proc做比较,如果endpoint相等,则在循环中分别计算
for(i = 0; i < CPUTIMENAMES; i++) {
if(!CPUTIME(timemode, i))
continue;
if(p1->p_endpoint == p2->p_endpoint) {
t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i];
} else {
t = t + p2->p_cpucycles[i];
}
}
return t;
}
void print_procs(
struct proc *proc1, struct proc *proc2, int cputimemode)
{
int p, nprocs;
u64_t idleticks = 0;
u64_t kernelticks = 0;
u64_t systemticks = 0;
u64_t userticks = 0;
u64_t total_ticks = 0;
int blockedseen = 0;
static struct tp *tick_procs = NULL;
if (tick_procs == NULL) {
tick_procs = malloc(nr_total * sizeof(tick_procs[0]));
if (tick_procs == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
}
for(p = nprocs = 0; p < nr_total; p++) {
u64_t uticks;
//如果当前进程标志不是used就continue 看下一个进程。
if(!(proc2[p].p_flags & USED))
continue;
tick_procs[nprocs].p = proc2 + p;
tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
//更新实时uticks
uticks = cputicks(&proc1[p], &proc2[p], 1);
//算出总的ticks
total_ticks = total_ticks + uticks;
//判断是否为idletick
//为0一直continue 不用计算
if(p-5 == 317) {
idleticks = uticks;
continue;
}
//判断是否为kerneltick
if(p-5 == ((endpoint_t) -1)) {
kernelticks = uticks;
}
if(!(proc2[p].p_flags & IS_TASK)) {
if(proc2[p].p_flags & IS_SYSTEM)
systemticks = systemticks + tick_procs[nprocs].ticks;
else
userticks = userticks + tick_procs[nprocs].ticks;
}
nprocs++;
}
if (total_ticks == 0)
return;
printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks);
printf("%6.2f%% system, ", 100.0 * systemticks / total_ticks);
printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks);
printf("%6.2f%% idle",100.00-(100.0 * (kernelticks+userticks+systemticks)/ total_ticks));
printf("\n");
}
void get_procs(void)
{
struct proc *p;
int i;
p = prev_proc;
prev_proc = proc;
proc = p;
if (proc == NULL) {
proc = malloc(nr_total * sizeof(proc[0]));
if (proc == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
}
for (i = 0; i < nr_total; i++)
proc[i].p_flags = 0;
parse_dir();
}
void getkinfo(void)
{
FILE *fp;
if ((fp = fopen("/proc/kinfo", "r")) == NULL) {
exit(1);
}
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) {
exit(1);
}
fclose(fp);
nr_total = (int) (nr_procs + nr_tasks);
}
void mytop()
{
if (chdir("/proc") != 0) {
perror("chdir to /proc" );
return;
}
print_memory();
getkinfo();
get_procs();
if(prev_proc==NULL)
get_procs();
print_procs(prev_proc,proc,1);
return;
}
*/
void mytop()
{
}