diff --git a/yeeshell.c b/yeeshell.c index 146c494..002544d 100644 --- a/yeeshell.c +++ b/yeeshell.c @@ -9,14 +9,14 @@ #include #include -#include +//#include #include #include #include #include #include -#include +//#include #include #include #include @@ -45,9 +45,12 @@ 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; @@ -892,4 +895,46 @@ struct tp *lookup(endpoint_t who, struct tp *tptab, int np) abort(); return NULL; -} \ No newline at end of file +} + +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); + } + + /* Process slot number is a tie breaker. */ + return (int)(p1->p - p2->p); +} diff --git a/yeeshell.h b/yeeshell.h index 57ed3d2..ade2264 100644 --- a/yeeshell.h +++ b/yeeshell.h @@ -27,10 +27,12 @@ #define IS_SYSTEM 0x4 #define BLOCKED 0x8 -#define CPUTIMENAMES (sizeof(cputimenames) / sizeof(cputimenames[0])) - #define CPUTIME(m, i) (m & (1L << (i))) +#define ORDER_CPU 0 +#define ORDER_MEMORY 1 +#define ORDER_HIGHEST ORDER_MEMORY + /* process info */ struct proc { @@ -93,4 +95,5 @@ void print_procs(int maxlines, struct proc *proc1, struct proc *proc2, int cputi 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); \ No newline at end of file +struct tp *lookup(endpoint_t who, struct tp *tptab, int np); +int cmp_procs(const void *v1, const void *v2); \ No newline at end of file