소스 검색

test mytop

master
10195501441 3 년 전
부모
커밋
582d763289
3개의 변경된 파일83개의 추가작업 그리고 321개의 파일을 삭제
  1. +3
    -1
      .vscode/settings.json
  2. +65
    -305
      yeeshell.c
  3. +15
    -15
      yeeshell.h

+ 3
- 1
.vscode/settings.json 파일 보기

@ -2,6 +2,8 @@
"files.associations": {
"yeeshell.h": "c",
"types.h": "c",
"signal.h": "c"
"signal.h": "c",
"termios.h": "c",
"stat.h": "c"
}
}

+ 65
- 305
yeeshell.c 파일 보기

@ -6,23 +6,19 @@
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <termcap.h>
#include <termios.h>
#include <curses.h>
#include <limits.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
//#include <sys/ioc_tty.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <curses.h>
//#include <timers.h>
#include <limits.h>
#include <termcap.h>
#include <termios.h>
#include <time.h>
#include <assert.h>
#include <minix/com.h>
#include <minix/config.h>
#include <minix/type.h>
@ -38,19 +34,10 @@
char *history[CMDLINE_HISTORY_MAX_QUANTITY];
int cmdline_amount = 0;
/* mytop CPU part */
struct proc *proc = NULL, *prev_proc = NULL;
int nr_total;
unsigned int nr_procs, nr_tasks;
/* name of cpu cycle types, in the order they appear in /psinfo. */
const char *cputimenames[] = {"user", "ipc", "kernelcall"};
#define CPUTIMENAMES (sizeof(cputimenames) / sizeof(cputimenames[0]))
int blockedverbose = 0;
int order = ORDER_CPU;
int main()
{
char *cmdline = NULL, *pwd = NULL;
@ -459,49 +446,19 @@ int mytop_memory()
int mytop_CPU()
{
pid_t pid;
if ((pid = fork()) == 0)
int cputimemode = 1;
getkinfo();
print_memory();
get_procs();
if (prev_proc == NULL)
{
printf("fork error\n");
return;
}
if (pid == 0)
{
int cputimemode = 1;
getkinfo();
print_memory();
get_procs();
if (prev_proc == NULL)
get_procs();
print_procs(prev_proc, proc, cputimemode);
exit(0);
}
return 1;
print_procs(prev_proc, proc, cputimemode);
return 0;
}
void getkinfo(void)
{
FILE *fp;
if ((fp = fopen("kinfo", "r")) == NULL)
{
fprintf(stderr, "opening " _PATH_PROC "kinfo failed\n");
exit(1);
}
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2)
{
fprintf(stderr, "reading from " _PATH_PROC "kinfo failed\n");
exit(1);
}
fclose(fp);
nr_total = (int)(nr_procs + nr_tasks);
}
void get_procs(void)
void get_procs()
{
struct proc *p;
int i;
@ -527,14 +484,14 @@ void get_procs(void)
parse_dir();
}
void parse_dir(void)
void parse_dir()
{
DIR *p_dir;
struct dirent *p_ent;
pid_t pid;
char *end;
if ((p_dir = opendir(".")) == NULL)
if ((p_dir = opendir("/proc")) == NULL)
{
perror("opendir on " _PATH_PROC);
exit(1);
@ -545,7 +502,9 @@ void parse_dir(void)
pid = strtol(p_ent->d_name, &end, 10);
if (!end[0] && pid != 0)
{
parse_file(pid);
}
}
closedir(p_dir);
@ -558,13 +517,14 @@ void parse_file(pid_t pid)
unsigned long cycles_hi, cycles_lo;
FILE *fp;
struct proc *p;
int slot;
int i;
sprintf(path, "%d/psinfo", pid);
sprintf(path, "/proc/%d/psinfo", pid);
if ((fp = fopen(path, "r")) == NULL)
{
return;
}
if (fscanf(fp, "%d", &version) != 1)
{
@ -584,7 +544,7 @@ void parse_file(pid_t pid)
return;
}
slot = SLOT_NR(endpt);
slot++;
if (slot < 0 || slot >= nr_total)
{
@ -596,18 +556,19 @@ void parse_file(pid_t pid)
p = &proc[slot];
if (type == TYPE_TASK)
{
p->p_flags |= IS_TASK;
}
else if (type == TYPE_SYSTEM)
{
p->p_flags |= IS_SYSTEM;
}
p->p_endpoint = endpt;
p->p_pid = pid;
if (fscanf(fp, " %255s %c %d %d %lu %*u %lu %lu",
name, &state, &p->p_blocked, &p->p_priority,
&p->p_user_time, &cycles_hi, &cycles_lo) != 7)
if (fscanf(fp, " %255s %c %d %d %lu %*u %lu %lu", name, &state, &p->p_blocked, &p->p_priority, &p->p_user_time, &cycles_hi, &cycles_lo) != 7)
{
fclose(fp);
return;
}
@ -616,17 +577,17 @@ void parse_file(pid_t pid)
p->p_name[sizeof(p->p_name) - 1] = 0;
if (state != STATE_RUN)
{
p->p_flags |= BLOCKED;
}
p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
p->p_memory = 0L;
if (!(p->p_flags & IS_TASK))
{
int j;
if ((j = fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u",
&p->p_memory, &effuid, &p->p_nice)) != 3)
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;
}
@ -634,12 +595,13 @@ void parse_file(pid_t pid)
p->p_effuid = effuid;
}
else
{
p->p_effuid = 0;
}
for (i = 1; i < CPUTIMENAMES; i++)
{
if (fscanf(fp, " %lu %lu",
&cycles_hi, &cycles_lo) == 2)
if (fscanf(fp, " %lu %lu", &cycles_hi, &cycles_lo) == 2)
{
p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
}
@ -662,17 +624,35 @@ void parse_file(pid_t pid)
fclose(fp);
}
void print_procs(int maxlines, struct proc *proc1, struct proc *proc2, int cputimemode)
void getkinfo()
{
FILE *fp;
if ((fp = fopen("/proc/kinfo", "r")) == NULL)
{
fprintf(stderr, "opening " _PATH_PROC "kinfo failed\n");
exit(1);
}
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2)
{
fprintf(stderr, "reading from " _PATH_PROC "kinfo failed\n");
exit(1);
}
fclose(fp);
nr_total = (int)(nr_procs + nr_tasks);
}
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]));
@ -688,253 +668,33 @@ void print_procs(int maxlines, struct proc *proc1, struct proc *proc2, int cputi
{
u64_t uticks;
if (!(proc2[p].p_flags & USED))
{
continue;
}
tick_procs[nprocs].p = proc2 + p;
tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
uticks = cputicks(&proc1[p], &proc2[p], 1);
total_ticks = total_ticks + uticks;
if (p - NR_TASKS == IDLE)
{
idleticks = uticks;
continue;
}
if (p - NR_TASKS == KERNEL)
{
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;
qsort(tick_procs, nprocs, sizeof(tick_procs[0]), cmp_procs);
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.0 * idleticks / total_ticks);
#define NEWLINE \
do \
{ \
printf("\n"); \
if (--maxlines <= 0) \
{ \
return; \
} \
} while (0)
NEWLINE;
printf("CPU time displayed ('%c' to cycle): %s; ",
TIMECYCLEKEY, cputimemodename(cputimemode));
printf(" sort order ('%c' to cycle): %s", ORDERKEY, ordername(order));
NEWLINE;
NEWLINE;
printf(" PID USERNAME PRI NICE SIZE STATE TIME CPU COMMAND");
NEWLINE;
for (p = 0; p < nprocs; p++)
{
struct proc *pr;
int level = 0;
pr = tick_procs[p].p;
if ((pr->p_flags & IS_TASK) && pr->p_pid != KERNEL)
{
/* skip old kernel tasks as they don't run anymore */
continue;
}
/* If we're in blocked verbose mode, indicate start of
* blocked processes.
*/
if (blockedverbose && (pr->p_flags & BLOCKED) && !blockedseen)
{
NEWLINE;
printf("Blocked processes:");
NEWLINE;
blockedseen = 1;
}
print_proc(&tick_procs[p], total_ticks);
NEWLINE;
if (!blockedverbose)
continue;
/* Traverse dependency chain if blocked. */
while (pr->p_flags & BLOCKED)
{
endpoint_t dep = NONE;
struct tp *tpdep;
level += 5;
if ((dep = pr->p_blocked) == NONE)
{
printf("not blocked on a process");
NEWLINE;
break;
}
if (dep == ANY)
break;
tpdep = lookup(dep, tick_procs, nprocs);
pr = tpdep->p;
printf("%*s> ", level, "");
print_proc(tpdep, total_ticks);
NEWLINE;
}
}
}
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode)
{
int i;
u64_t t = 0;
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;
}
char *cputimemodename(int cputimemode)
{
static char name[100];
int i;
name[0] = '\0';
for (i = 0; i < CPUTIMENAMES; i++)
{
if (CPUTIME(cputimemode, i))
{
assert(strlen(name) +
strlen(cputimenames[i]) <
sizeof(name));
strcat(name, cputimenames[i]);
strcat(name, " ");
}
}
return name;
}
void print_proc(struct tp *tp, u64_t total_ticks)
{
int euid = 0;
static struct passwd *who = NULL;
static int last_who = -1;
char *name = "";
int ticks;
struct proc *pr = tp->p;
printf("%5d ", pr->p_pid);
euid = pr->p_effuid;
name = pr->p_name;
if (last_who != euid || !who)
{
who = getpwuid(euid);
last_who = euid;
}
if (who && who->pw_name)
printf("%-8s ", who->pw_name);
else if (!(pr->p_flags & IS_TASK))
printf("%8d ", pr->p_effuid);
else
printf(" ");
printf(" %2d ", pr->p_priority);
if (!(pr->p_flags & IS_TASK))
{
printf(" %3d ", pr->p_nice);
}
else
printf(" ");
printf("%6ldK", (pr->p_memory + 512) / 1024);
printf("%6s", (pr->p_flags & BLOCKED) ? "" : "RUN");
ticks = pr->p_user_time;
printf(" %3u:%02u ", (ticks / system_hz / 60), (ticks / system_hz) % 60);
printf("%6.2f%% %s", 100.0 * tp->ticks / total_ticks, name);
}
struct tp *lookup(endpoint_t who, struct tp *tptab, int np)
{
int t;
for (t = 0; t < np; t++)
if (who == tptab[t].p->p_endpoint)
return &tptab[t];
fprintf(stderr, "lookup: tp %d (0x%x) not found.\n", who, who);
abort();
return NULL;
}
int cmp_procs(const void *v1, const void *v2)
{
struct tp *p1 = (struct tp *)v1, *p2 = (struct tp *)v2;
int p1blocked, p2blocked;
if (order == ORDER_MEMORY)
{
if (p1->p->p_memory < p2->p->p_memory)
return 1;
if (p1->p->p_memory > p2->p->p_memory)
return -1;
return 0;
}
p1blocked = !!(p1->p->p_flags & BLOCKED);
p2blocked = !!(p2->p->p_flags & BLOCKED);
/* Primarily order by used number of cpu cycles.
*
* Exception: if in blockedverbose mode, a blocked
* process is always printed after an unblocked
* process, and used cpu cycles don't matter.
*
* In both cases, process slot number is a tie breaker.
*/
if (blockedverbose && (p1blocked || p2blocked))
{
if (!p1blocked && p2blocked)
return -1;
if (!p2blocked && p1blocked)
return 1;
}
else if (p1->ticks != p2->ticks)
{
return (p2->ticks - p1->ticks);
return;
}
/* Process slot number is a tie breaker. */
return (int)(p1->p - p2->p);
printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks);
printf("%6.2f%% system", 100.0 * systemticks / total_ticks);
printf("%6.2f%% in total\n", 100.0 * (systemticks + userticks) / total_ticks);
}

+ 15
- 15
yeeshell.h 파일 보기

@ -17,8 +17,15 @@
#define _PATH_PROC "/proc/"
#define SLOT_NR(e) (_ENDPOINT_P(e) + nr_tasks)
const char *cputimenames[] = {"user", "ipc", "kernelcall"};
#define CPUTIMENAMES (sizeof(cputimenames) / sizeof(cputimenames[0]))
#define CPUTIME(m, i) (m & (1L << (i)))
#define PSINFO_VERSION 0
#define TYPE_TASK 'T'
#define TYPE_SYSTEM 'S'
#define TYPE_USER 'U'
#define STATE_RUN 'R'
#define TIMECYCLEKEY 't'
#define ORDERKEY 'o'
@ -27,13 +34,13 @@
#define IS_SYSTEM 0x4
#define BLOCKED 0x8
#define CPUTIME(m, i) (m & (1L << (i)))
#define ORDER_CPU 0
#define ORDER_MEMORY 1
#define ORDER_HIGHEST ORDER_MEMORY
/* process info */
#define TC_BUFFER 1024
#define TC_STRINGS 200
struct proc
{
int p_flags;
@ -84,16 +91,9 @@ int built_in(char **args);
int builtin_cd(char **args);
int builtin_history(char **args);
int builtin_mytop();
int mytop_memory();
/*********** mytop_CPU part ***********/
void getkinfo(void);
void get_procs(void);
void parse_dir(void);
/* mytop routine */
void get_procs();
void parse_dir();
void parse_file(pid_t pid);
void print_procs(int maxlines, struct proc *proc1, struct proc *proc2, int cputimemode);
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode);
char *cputimemodename(int cputimemode);
void print_proc(struct tp *tp, u64_t total_ticks);
struct tp *lookup(endpoint_t who, struct tp *tptab, int np);
int cmp_procs(const void *v1, const void *v2);
void getkinfo();

불러오는 중...
취소
저장