Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 9 Feb 1998 20:04:23 +0100 (CET)
From:      haury@sagem.fr
To:        hackers@FreeBSD.ORG
Subject:   TOP: proposal to add sort feature
Message-ID:   <199802091904.UAA27173@sagem.fr>

next in thread | raw e-mail | index | archive | help

--ELM887051063-26654-0_
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

I have modified the top utility this weekend in order to have the sort option.

I have put the patch file for -stable (CTM src-2.2 611) in attachment.
This patch works on -current (CTM cvs-cur 4053) but I haven't compile it.

The job done was to modify the current machine.c according to the top
original m-sunos5.c and adding a compile option in the Makefile.

I have used exactly the same sunos5 macros even if the ORDERKEY_PCTCPU
is not really a nice one.

It wasn't a great effort to do that since the needed code was completly written
on FreeBSD machine.c ;-).

-- 
Christian Haury (Christian.Haury@sagem.fr)	SAGEM Eragny - France
phone : +33 1 34 30 53 93 			fax : +33 1 34 30 50 28

<legal> Opinions hereabove are my own and not those of my employer </legal>

--ELM887051063-26654-0_
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: attachment; filename=DIFF
Content-Description: /usr/src/usr.bin/top patch file
Content-Transfer-Encoding: 8bit

*** ./usr.bin/top/Makefile.ctm	Tue Apr 29 09:44:15 1997
--- ./usr.bin/top/Makefile	Mon Feb  9 18:56:48 1998
***************
*** 3,9 ****
  TOPDIR=	${.CURDIR}/../../contrib/top
  .PATH:	${TOPDIR}
  
! CFLAGS+= -DHAVE_GETOPT -I${.CURDIR} -I${TOPDIR}
  
  #
  # The table size should be a prime number approximately twice as
--- 3,9 ----
  TOPDIR=	${.CURDIR}/../../contrib/top
  .PATH:	${TOPDIR}
  
! CFLAGS+= -DHAVE_GETOPT -DORDER -I${.CURDIR} -I${TOPDIR}
  
  #
  # The table size should be a prime number approximately twice as
*** ./usr.bin/top/machine.c.ctm	Mon Sep 29 10:12:55 1997
--- ./usr.bin/top/machine.c	Mon Feb  9 19:45:02 1998
***************
*** 212,217 ****
--- 212,220 ----
      NULL
  };
  
+ /* these are names given to allowed sorting orders -- first is default */
+ char *ordernames[] =
+ {"cpu", "size", "res", "time", NULL};
  
  /* these are for keeping track of the proc array */
  
***************
*** 300,305 ****
--- 303,309 ----
      statics->cpustate_names = cpustatenames;
      statics->memory_names = memorynames;
      statics->swap_names = swapnames;
+     statics->order_names = ordernames;
  
      /* all done! */
      return(0);
***************
*** 686,701 ****
  /* comparison routine for qsort */
  
  /*
!  *  proc_compare - comparison function for "qsort"
!  *	Compares the resource consumption of two processes using five
!  *  	distinct keys.  The keys (in descending order of importance) are:
!  *  	percent cpu, cpu ticks, state, resident set size, total virtual
!  *  	memory usage.  The process states are ordered as follows (from least
!  *  	to most important):  WAIT, zombie, sleep, stop, start, run.  The
!  *  	array declaration below maps a process state index into a number
!  *  	that reflects this ordering.
   */
  
  static unsigned char sorted_state[] =
  {
      0,	/* not used		*/
--- 690,711 ----
  /* comparison routine for qsort */
  
  /*
!  *  proc_compares - a set of comparison function for "qsort"
!  *
   */
  
+ int compare_cpu();  
+ int compare_size(); 
+ int compare_res();
+ int compare_time();
+ 
+ int (*proc_compares[])() = {
+     compare_cpu, 
+     compare_size,
+     compare_res,
+     compare_time,
+     NULL };
+ 
  static unsigned char sorted_state[] =
  {
      0,	/* not used		*/
***************
*** 707,714 ****
      4	/* stop			*/
  };
   
  int
! proc_compare(pp1, pp2)
  
  struct proc **pp1;
  struct proc **pp2;
--- 717,748 ----
      4	/* stop			*/
  };
   
+ #define ORDERKEY_PCTCPU /* compare percent cpu (pctcpu) */ \
+ 	if (lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu),\
+ 	     (result = lresult > 0.0 ? 1 : lresult < 0.0 ? -1 : 0) == 0)
+ 
+ #define ORDERKEY_CPTICKS /* compare cpu ticks (cpticks) */ \
+ 	if ((result = PP(p2, p_cpticks) - PP(p1, p_cpticks)) == 0)
+ 
+ #define ORDERKEY_STATE /* compare process state */ \
+ 	if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
+ 		sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
+ 
+ #define ORDERKEY_PRIO /* compare priority */ \
+ 	if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
+ 
+ #define ORDERKEY_RSSIZE /* compare resident set size (rssize) */ \
+ 	if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
+ 
+ #define ORDERKEY_MEM /* compare total virtual memory used */ \
+ 	if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
+ 
+ 
+ /* compare_cpu : the comparison function for sorting by cpu percentage */
+ /* (original proc_compare rewritten with macros) */
+ 
  int
! compare_cpu(pp1, pp2)
  
  struct proc **pp1;
  struct proc **pp2;
***************
*** 723,755 ****
      p1 = *(struct kinfo_proc **) pp1;
      p2 = *(struct kinfo_proc **) pp2;
  
!     /* compare percent cpu (pctcpu) */
!     if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0)
!     {
! 	/* use cpticks to break the tie */
! 	if ((result = PP(p2, p_cpticks) - PP(p1, p_cpticks)) == 0)
! 	{
! 	    /* use process state to break the tie */
! 	    if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] -
! 			  sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
! 	    {
! 		/* use priority to break the tie */
! 		if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
! 		{
! 		    /* use resident set size (rssize) to break the tie */
! 		    if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
! 		    {
! 			/* use total memory to break the tie */
! 			result = PROCSIZE(p2) - PROCSIZE(p1);
! 		    }
! 		}
! 	    }
! 	}
!     }
!     else
!     {
! 	result = lresult < 0 ? -1 : 1;
!     }
  
      return(result);
  }
--- 757,856 ----
      p1 = *(struct kinfo_proc **) pp1;
      p2 = *(struct kinfo_proc **) pp2;
  
!     ORDERKEY_PCTCPU
!     ORDERKEY_CPTICKS
!     ORDERKEY_STATE
!     ORDERKEY_PRIO
!     ORDERKEY_RSSIZE
!     ORDERKEY_MEM
!     ;
! 
!     return(result);
! }
! 
! /* compare_size - the comparison function for sorting by total memory usage */
! 
! int
! compare_size(pp1, pp2)
! 
! struct proc **pp1;
! struct proc **pp2;
! 
! {
!     register struct kinfo_proc *p1;
!     register struct kinfo_proc *p2;
!     register int result;
!     register pctcpu lresult;
! 
!     /* remove one level of indirection */
!     p1 = *(struct kinfo_proc **) pp1;
!     p2 = *(struct kinfo_proc **) pp2;
! 
!     ORDERKEY_MEM
!     ORDERKEY_RSSIZE
!     ORDERKEY_PCTCPU
!     ORDERKEY_CPTICKS
!     ORDERKEY_STATE
!     ORDERKEY_PRIO
!     ;
! 
!     return(result);
! }
! 
! /* compare_res - the comparison function for sorting by resident set size */
! 
! int
! compare_res(pp1, pp2)
! 
! struct proc **pp1;
! struct proc **pp2;
! 
! {
!     register struct kinfo_proc *p1;
!     register struct kinfo_proc *p2;
!     register int result;
!     register pctcpu lresult;
! 
!     /* remove one level of indirection */
!     p1 = *(struct kinfo_proc **) pp1;
!     p2 = *(struct kinfo_proc **) pp2;
! 
!     ORDERKEY_RSSIZE
!     ORDERKEY_MEM
!     ORDERKEY_PCTCPU
!     ORDERKEY_CPTICKS
!     ORDERKEY_STATE
!     ORDERKEY_PRIO
!     ;
! 
!     return(result);
! }
! 
! /* compare_time - the comparison function for sorting by total cpu time */
! 
! int
! compare_time(pp1, pp2)
! 
! struct proc **pp1;
! struct proc **pp2;
! 
! {
!     register struct kinfo_proc *p1;
!     register struct kinfo_proc *p2;
!     register int result;
!     register pctcpu lresult;
! 
!     /* remove one level of indirection */
!     p1 = *(struct kinfo_proc **) pp1;
!     p2 = *(struct kinfo_proc **) pp2;
! 
!     ORDERKEY_CPTICKS
!     ORDERKEY_PCTCPU
!     ORDERKEY_STATE
!     ORDERKEY_PRIO
!     ORDERKEY_MEM
!     ORDERKEY_RSSIZE
!     ;
  
      return(result);
  }

--ELM887051063-26654-0_--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi? <199802091904.UAA27173>