all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [patch] FreeBSD support for system processes
@ 2009-12-14 18:01 Eduard Wiebe
  2009-12-17 17:26 ` Dan Nicolaescu
  0 siblings, 1 reply; 4+ messages in thread
From: Eduard Wiebe @ 2009-12-14 18:01 UTC (permalink / raw
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 229 bytes --]


 Hello,

Following patch improves support for system processes on FreeBSD
machines.

2009-12-14  Eduard Wiebe  <usenet@pusto.de>

	* sysdep.c (list_system_processes, system_process_attributes): Add
	implementation for FreeBSD.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: sysdep.diff --]
[-- Type: text/x-patch, Size: 6806 bytes --]

diff --git a/src/sysdep.c b/src/sysdep.c
index b983694..b79e777 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -52,6 +52,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "blockinput.h"
 
+#ifdef __FreeBSD__
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <sys/resource.h> */
+#include <math.h>
+#endif
+
 #ifdef WINDOWSNT
 #define read sys_read
 #define write sys_write
@@ -3200,6 +3207,40 @@ list_system_processes ()
   return proclist;
 }
 
+#elif defined (__FreeBSD__)
+
+Lisp_Object
+list_system_processes ()
+{
+  int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PROC};
+  size_t len;
+  struct kinfo_proc *procs;
+  size_t i;
+
+  struct gcpro gcpro1;
+  Lisp_Object proclist = Qnil;
+
+  if (sysctl (mib, 3, NULL, &len, NULL, 0) != 0)
+    return proclist;
+  
+  procs = xmalloc (len);
+  if (sysctl (mib, 3, procs, &len, NULL, 0) != 0)
+    {
+      xfree (procs);
+      return proclist;
+    }
+  
+  GCPRO1 (proclist);
+  len /= sizeof (struct kinfo_proc);
+  for (i = 0; i < len; i++)
+    proclist = Fcons (make_fixnum_or_float (procs[i].ki_pid), proclist);
+  UNGCPRO;
+  
+  xfree (procs);
+
+  return  proclist;
+}
+
 /* The WINDOWSNT implementation is in w32.c.
    The MSDOS implementation is in dosfns.c.  */
 #elif !defined (WINDOWSNT) && !defined (MSDOS)
@@ -3743,6 +3784,176 @@ system_process_attributes (Lisp_Object pid)
   return attrs;
 }
 
+#elif defined(__FreeBSD__)
+
+Lisp_Object
+system_process_attributes (Lisp_Object pid)
+{
+  int proc_id;
+  int pagesize = getpagesize();
+  int npages;
+  int fscale;
+  struct passwd *pw;
+  struct group  *gr;
+  char *ttyname;
+  size_t len;
+  char args[MAXPATHLEN];
+  EMACS_TIME t, now;
+  
+  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID};
+  struct kinfo_proc proc;
+  size_t proclen = sizeof(proc);
+
+  struct gcpro gcpro1, gcpro2;
+  Lisp_Object attrs = Qnil;
+  Lisp_Object decoded_comm;
+  
+  CHECK_NUMBER_OR_FLOAT (pid);
+  proc_id = FLOATP (pid) ? XFLOAT_DATA (pid) : XINT (pid);
+  mib[3] = proc_id;
+  
+  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
+    return attrs;
+
+  GCPRO2 (attrs, decoded_comm);
+  
+  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float(proc.ki_uid)), attrs);
+  
+  BLOCK_INPUT;
+  pw = getpwuid (proc.ki_uid);
+  UNBLOCK_INPUT;
+  if (pw)
+    attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
+
+  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float(proc.ki_svgid)), attrs);
+
+  BLOCK_INPUT;
+  gr = getgrgid (proc.ki_svgid);
+  UNBLOCK_INPUT;
+  if (gr)
+    attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
+
+  decoded_comm = code_convert_string_norecord
+    (make_unibyte_string (proc.ki_comm, strlen (proc.ki_comm)),
+     Vlocale_coding_system, 0);
+  
+  attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
+  {
+    char state[2] = {'\0', '\0'};
+    switch (proc.ki_stat)
+      {
+      case SRUN:
+	state[0] = 'R';
+	break;
+
+      case SSLEEP:
+	state[0] = 'S';
+	break;
+	
+      case SLOCK:
+	state[0] = 'D';
+	break;
+
+      case SZOMB:
+	state[0] = 'Z';
+	break;
+	
+      case SSTOP:
+	state[0] = 'T';
+	break;
+      }
+    attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
+  }
+  
+  attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (proc.ki_ppid)), attrs);
+  attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (proc.ki_pgid)), attrs);
+  attrs = Fcons (Fcons (Qsess, make_fixnum_or_float (proc.ki_sid)),  attrs);
+
+  BLOCK_INPUT;
+  ttyname = proc.ki_tdev == NODEV ? NULL : devname (proc.ki_tdev, S_IFCHR);
+  UNBLOCK_INPUT;
+  if (ttyname)
+    attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
+  
+  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.ki_tpgid)), attrs);
+  attrs = Fcons (Fcons (Qminflt,  make_fixnum_or_float (proc.ki_rusage.ru_minflt)), attrs);
+  attrs = Fcons (Fcons (Qmajflt,  make_fixnum_or_float (proc.ki_rusage.ru_majflt)), attrs);
+  attrs = Fcons (Fcons (Qcminflt, make_number (proc.ki_rusage_ch.ru_minflt)), attrs);
+  attrs = Fcons (Fcons (Qcmajflt, make_number (proc.ki_rusage_ch.ru_majflt)), attrs);
+
+#define TIMELIST(ts)					\
+  list3 (make_number (EMACS_SECS (ts) >> 16 & 0xffff),	\
+	 make_number (EMACS_SECS (ts) & 0xffff),	\
+	 make_number (EMACS_USECS (ts)))
+
+  attrs = Fcons (Fcons (Qutime, TIMELIST(proc.ki_rusage.ru_utime)), attrs);
+  attrs = Fcons (Fcons (Qstime, TIMELIST(proc.ki_rusage.ru_stime)), attrs);
+  EMACS_ADD_TIME (t, proc.ki_rusage.ru_utime, proc.ki_rusage.ru_stime);
+  attrs = Fcons (Fcons (Qtime,  TIMELIST(t)), attrs);
+
+  attrs = Fcons (Fcons (Qcutime, TIMELIST(proc.ki_rusage_ch.ru_utime)), attrs);
+  attrs = Fcons (Fcons (Qcstime, TIMELIST(proc.ki_rusage_ch.ru_utime)), attrs);
+  EMACS_ADD_TIME (t, proc.ki_rusage_ch.ru_utime, proc.ki_rusage_ch.ru_stime);
+  attrs = Fcons (Fcons (Qctime, TIMELIST(t)), attrs);
+
+  attrs = Fcons (Fcons (Qthcount, make_fixnum_or_float (proc.ki_numthreads)), attrs);
+  attrs = Fcons (Fcons (Qpri,   make_number (proc.ki_pri.pri_native)), attrs);
+  attrs = Fcons (Fcons (Qnice,  make_number (proc.ki_nice)), attrs);
+  attrs = Fcons (Fcons (Qstart, TIMELIST(proc.ki_start)), attrs);
+  attrs = Fcons (Fcons (Qvsize, make_number (proc.ki_size >> 10)), attrs);
+  attrs = Fcons (Fcons (Qrss,   make_number (proc.ki_rssize * pagesize >> 10)), attrs);
+
+  EMACS_GET_TIME (now);
+  EMACS_SUB_TIME (t, now, proc.ki_start);
+  attrs = Fcons (Fcons (Qetime, TIMELIST(t)), attrs);
+
+#undef TIMELIST
+  
+  len = sizeof(fscale);
+  if (sysctlbyname ("kern.fscale", &fscale, &len, NULL, 0) == 0)
+    {
+      float   pcpu;
+      fixpt_t ccpu;
+      len = sizeof (ccpu);
+      if (sysctlbyname ("kern.ccpu", &ccpu, &len, NULL, 0) == 0)
+      	{
+      	  pcpu = 100.0 * ((double) proc.ki_pctcpu / fscale)
+  	    / (1.0 - exp(proc.ki_swtime * log((double) ccpu / fscale)));
+  	  attrs = Fcons (Fcons (Qpcpu, make_fixnum_or_float(pcpu)), attrs);
+      	}
+    }
+
+  len = sizeof(npages);
+  if (sysctlbyname ("hw.availpages", &npages, &len, NULL, 0) == 0)
+    {
+      float pmem = proc.ki_flag & P_INMEM
+  	? 100.0 * ((float) proc.ki_rssize / npages)
+  	: 0.0;
+      attrs = Fcons (Fcons (Qpmem, make_fixnum_or_float(pmem)), attrs);
+    }
+
+  mib[2] = KERN_PROC_ARGS;
+  len = MAXPATHLEN;
+  if (sysctl (mib, 4, args, &len, NULL, 0) == 0)
+    {
+      int i;
+      for (i = 0; i < len; i++)
+	{
+	  if (! args[i] && i < len - 1)
+	    args[i] = ' ';
+	}
+
+      decoded_comm = code_convert_string_norecord
+	(make_unibyte_string (args, strlen (args)),
+	 Vlocale_coding_system, 0);
+
+      attrs = Fcons (Fcons (Qargs, decoded_comm), attrs);
+    }
+  
+  UNGCPRO;
+  return attrs;
+}
+
 /* The WINDOWSNT implementation is in w32.c.
    The MSDOS implementation is in dosfns.c.  */
 #elif !defined (WINDOWSNT) && !defined (MSDOS)

[-- Attachment #3: Type: text/plain, Size: 18 bytes --]


-- 
Eduard Wiebe

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [patch] FreeBSD support for system processes
  2009-12-14 18:01 [patch] FreeBSD support for system processes Eduard Wiebe
@ 2009-12-17 17:26 ` Dan Nicolaescu
  2009-12-18 10:19   ` Eduard Wiebe
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Nicolaescu @ 2009-12-17 17:26 UTC (permalink / raw
  To: Eduard Wiebe; +Cc: emacs-devel

Eduard Wiebe <usenet@pusto.de> writes:

  >  Hello,
  > 
  > Following patch improves support for system processes on FreeBSD
  > machines.

Does this work on other BSDs?
We have a feature freeze now, so this can only get if approved by one of
the maintainers.
It would be a good idea to send this to bug-gnu-emacs@gnu.org so that it
does not get lost.


  > 2009-12-14  Eduard Wiebe  <usenet@pusto.de>
  > 
  > 	* sysdep.c (list_system_processes, system_process_attributes): Add
  > 	implementation for FreeBSD.
  > 
  > diff --git a/src/sysdep.c b/src/sysdep.c
  > index b983694..b79e777 100644
  > --- a/src/sysdep.c
  > +++ b/src/sysdep.c
  > @@ -52,6 +52,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
  >  
  >  #include "blockinput.h"
  >  
  > +#ifdef __FreeBSD__
  > +#include <sys/sysctl.h>
  > +#include <sys/user.h>
  > +#include <sys/resource.h> */
  > +#include <math.h>
  > +#endif
  > +
  >  #ifdef WINDOWSNT
  >  #define read sys_read
  >  #define write sys_write
  > @@ -3200,6 +3207,40 @@ list_system_processes ()
  >    return proclist;
  >  }
  >  
  > +#elif defined (__FreeBSD__)
  > +
  > +Lisp_Object
  > +list_system_processes ()
  > +{
  > +  int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PROC};
  > +  size_t len;
  > +  struct kinfo_proc *procs;
  > +  size_t i;
  > +
  > +  struct gcpro gcpro1;
  > +  Lisp_Object proclist = Qnil;
  > +
  > +  if (sysctl (mib, 3, NULL, &len, NULL, 0) != 0)
  > +    return proclist;
  > +  
  > +  procs = xmalloc (len);
  > +  if (sysctl (mib, 3, procs, &len, NULL, 0) != 0)
  > +    {
  > +      xfree (procs);
  > +      return proclist;
  > +    }
  > +  
  > +  GCPRO1 (proclist);
  > +  len /= sizeof (struct kinfo_proc);
  > +  for (i = 0; i < len; i++)
  > +    proclist = Fcons (make_fixnum_or_float (procs[i].ki_pid), proclist);
  > +  UNGCPRO;
  > +  
  > +  xfree (procs);
  > +
  > +  return  proclist;
  > +}
  > +
  >  /* The WINDOWSNT implementation is in w32.c.
  >     The MSDOS implementation is in dosfns.c.  */
  >  #elif !defined (WINDOWSNT) && !defined (MSDOS)
  > @@ -3743,6 +3784,176 @@ system_process_attributes (Lisp_Object pid)
  >    return attrs;
  >  }
  >  
  > +#elif defined(__FreeBSD__)
  > +
  > +Lisp_Object
  > +system_process_attributes (Lisp_Object pid)
  > +{
  > +  int proc_id;
  > +  int pagesize = getpagesize();
  > +  int npages;
  > +  int fscale;
  > +  struct passwd *pw;
  > +  struct group  *gr;
  > +  char *ttyname;
  > +  size_t len;
  > +  char args[MAXPATHLEN];
  > +  EMACS_TIME t, now;
  > +  
  > +  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID};
  > +  struct kinfo_proc proc;
  > +  size_t proclen = sizeof(proc);
  > +
  > +  struct gcpro gcpro1, gcpro2;
  > +  Lisp_Object attrs = Qnil;
  > +  Lisp_Object decoded_comm;
  > +  
  > +  CHECK_NUMBER_OR_FLOAT (pid);
  > +  proc_id = FLOATP (pid) ? XFLOAT_DATA (pid) : XINT (pid);
  > +  mib[3] = proc_id;
  > +  
  > +  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
  > +    return attrs;
  > +
  > +  GCPRO2 (attrs, decoded_comm);
  > +  
  > +  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float(proc.ki_uid)), attrs);
  > +  
  > +  BLOCK_INPUT;
  > +  pw = getpwuid (proc.ki_uid);
  > +  UNBLOCK_INPUT;
  > +  if (pw)
  > +    attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
  > +
  > +  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float(proc.ki_svgid)), attrs);
  > +
  > +  BLOCK_INPUT;
  > +  gr = getgrgid (proc.ki_svgid);
  > +  UNBLOCK_INPUT;
  > +  if (gr)
  > +    attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
  > +
  > +  decoded_comm = code_convert_string_norecord
  > +    (make_unibyte_string (proc.ki_comm, strlen (proc.ki_comm)),
  > +     Vlocale_coding_system, 0);
  > +  
  > +  attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
  > +  {
  > +    char state[2] = {'\0', '\0'};
  > +    switch (proc.ki_stat)
  > +      {
  > +      case SRUN:
  > +	state[0] = 'R';
  > +	break;
  > +
  > +      case SSLEEP:
  > +	state[0] = 'S';
  > +	break;
  > +	
  > +      case SLOCK:
  > +	state[0] = 'D';
  > +	break;
  > +
  > +      case SZOMB:
  > +	state[0] = 'Z';
  > +	break;
  > +	
  > +      case SSTOP:
  > +	state[0] = 'T';
  > +	break;
  > +      }
  > +    attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
  > +  }
  > +  
  > +  attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (proc.ki_ppid)), attrs);
  > +  attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (proc.ki_pgid)), attrs);
  > +  attrs = Fcons (Fcons (Qsess, make_fixnum_or_float (proc.ki_sid)),  attrs);
  > +
  > +  BLOCK_INPUT;
  > +  ttyname = proc.ki_tdev == NODEV ? NULL : devname (proc.ki_tdev, S_IFCHR);
  > +  UNBLOCK_INPUT;
  > +  if (ttyname)
  > +    attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
  > +  
  > +  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.ki_tpgid)), attrs);
  > +  attrs = Fcons (Fcons (Qminflt,  make_fixnum_or_float (proc.ki_rusage.ru_minflt)), attrs);
  > +  attrs = Fcons (Fcons (Qmajflt,  make_fixnum_or_float (proc.ki_rusage.ru_majflt)), attrs);
  > +  attrs = Fcons (Fcons (Qcminflt, make_number (proc.ki_rusage_ch.ru_minflt)), attrs);
  > +  attrs = Fcons (Fcons (Qcmajflt, make_number (proc.ki_rusage_ch.ru_majflt)), attrs);
  > +
  > +#define TIMELIST(ts)					\
  > +  list3 (make_number (EMACS_SECS (ts) >> 16 & 0xffff),	\
  > +	 make_number (EMACS_SECS (ts) & 0xffff),	\
  > +	 make_number (EMACS_USECS (ts)))
  > +
  > +  attrs = Fcons (Fcons (Qutime, TIMELIST(proc.ki_rusage.ru_utime)), attrs);
  > +  attrs = Fcons (Fcons (Qstime, TIMELIST(proc.ki_rusage.ru_stime)), attrs);
  > +  EMACS_ADD_TIME (t, proc.ki_rusage.ru_utime, proc.ki_rusage.ru_stime);
  > +  attrs = Fcons (Fcons (Qtime,  TIMELIST(t)), attrs);
  > +
  > +  attrs = Fcons (Fcons (Qcutime, TIMELIST(proc.ki_rusage_ch.ru_utime)), attrs);
  > +  attrs = Fcons (Fcons (Qcstime, TIMELIST(proc.ki_rusage_ch.ru_utime)), attrs);
  > +  EMACS_ADD_TIME (t, proc.ki_rusage_ch.ru_utime, proc.ki_rusage_ch.ru_stime);
  > +  attrs = Fcons (Fcons (Qctime, TIMELIST(t)), attrs);
  > +
  > +  attrs = Fcons (Fcons (Qthcount, make_fixnum_or_float (proc.ki_numthreads)), attrs);
  > +  attrs = Fcons (Fcons (Qpri,   make_number (proc.ki_pri.pri_native)), attrs);
  > +  attrs = Fcons (Fcons (Qnice,  make_number (proc.ki_nice)), attrs);
  > +  attrs = Fcons (Fcons (Qstart, TIMELIST(proc.ki_start)), attrs);
  > +  attrs = Fcons (Fcons (Qvsize, make_number (proc.ki_size >> 10)), attrs);
  > +  attrs = Fcons (Fcons (Qrss,   make_number (proc.ki_rssize * pagesize >> 10)), attrs);
  > +
  > +  EMACS_GET_TIME (now);
  > +  EMACS_SUB_TIME (t, now, proc.ki_start);
  > +  attrs = Fcons (Fcons (Qetime, TIMELIST(t)), attrs);
  > +
  > +#undef TIMELIST
  > +  
  > +  len = sizeof(fscale);
  > +  if (sysctlbyname ("kern.fscale", &fscale, &len, NULL, 0) == 0)
  > +    {
  > +      float   pcpu;
  > +      fixpt_t ccpu;
  > +      len = sizeof (ccpu);
  > +      if (sysctlbyname ("kern.ccpu", &ccpu, &len, NULL, 0) == 0)
  > +      	{
  > +      	  pcpu = 100.0 * ((double) proc.ki_pctcpu / fscale)
  > +  	    / (1.0 - exp(proc.ki_swtime * log((double) ccpu / fscale)));
  > +  	  attrs = Fcons (Fcons (Qpcpu, make_fixnum_or_float(pcpu)), attrs);
  > +      	}
  > +    }
  > +
  > +  len = sizeof(npages);
  > +  if (sysctlbyname ("hw.availpages", &npages, &len, NULL, 0) == 0)
  > +    {
  > +      float pmem = proc.ki_flag & P_INMEM
  > +  	? 100.0 * ((float) proc.ki_rssize / npages)
  > +  	: 0.0;
  > +      attrs = Fcons (Fcons (Qpmem, make_fixnum_or_float(pmem)), attrs);
  > +    }
  > +
  > +  mib[2] = KERN_PROC_ARGS;
  > +  len = MAXPATHLEN;
  > +  if (sysctl (mib, 4, args, &len, NULL, 0) == 0)
  > +    {
  > +      int i;
  > +      for (i = 0; i < len; i++)
  > +	{
  > +	  if (! args[i] && i < len - 1)
  > +	    args[i] = ' ';
  > +	}
  > +
  > +      decoded_comm = code_convert_string_norecord
  > +	(make_unibyte_string (args, strlen (args)),
  > +	 Vlocale_coding_system, 0);
  > +
  > +      attrs = Fcons (Fcons (Qargs, decoded_comm), attrs);
  > +    }
  > +  
  > +  UNGCPRO;
  > +  return attrs;
  > +}
  > +
  >  /* The WINDOWSNT implementation is in w32.c.
  >     The MSDOS implementation is in dosfns.c.  */
  >  #elif !defined (WINDOWSNT) && !defined (MSDOS)
  > 
  > -- 
  > Eduard Wiebe




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [patch] FreeBSD support for system processes
@ 2009-12-18  9:53 Eduard Wiebe
  0 siblings, 0 replies; 4+ messages in thread
From: Eduard Wiebe @ 2009-12-18  9:53 UTC (permalink / raw
  To: bug-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 315 bytes --]


 Hello,

following patch improves support for system processes on FreeBSD
machines.  Unfortantly i have no access to other BSDs, so i don't know
if this works on these.


2009-12-14  Eduard Wiebe  <usenet@pusto.de>

	* sysdep.c (list_system_processes, system_process_attributes): Add
	implementation for FreeBSD.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: sysdep.diff --]
[-- Type: text/x-patch, Size: 6806 bytes --]

diff --git a/src/sysdep.c b/src/sysdep.c
index b983694..b79e777 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -52,6 +52,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "blockinput.h"
 
+#ifdef __FreeBSD__
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <sys/resource.h> */
+#include <math.h>
+#endif
+
 #ifdef WINDOWSNT
 #define read sys_read
 #define write sys_write
@@ -3200,6 +3207,40 @@ list_system_processes ()
   return proclist;
 }
 
+#elif defined (__FreeBSD__)
+
+Lisp_Object
+list_system_processes ()
+{
+  int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PROC};
+  size_t len;
+  struct kinfo_proc *procs;
+  size_t i;
+
+  struct gcpro gcpro1;
+  Lisp_Object proclist = Qnil;
+
+  if (sysctl (mib, 3, NULL, &len, NULL, 0) != 0)
+    return proclist;
+  
+  procs = xmalloc (len);
+  if (sysctl (mib, 3, procs, &len, NULL, 0) != 0)
+    {
+      xfree (procs);
+      return proclist;
+    }
+  
+  GCPRO1 (proclist);
+  len /= sizeof (struct kinfo_proc);
+  for (i = 0; i < len; i++)
+    proclist = Fcons (make_fixnum_or_float (procs[i].ki_pid), proclist);
+  UNGCPRO;
+  
+  xfree (procs);
+
+  return  proclist;
+}
+
 /* The WINDOWSNT implementation is in w32.c.
    The MSDOS implementation is in dosfns.c.  */
 #elif !defined (WINDOWSNT) && !defined (MSDOS)
@@ -3743,6 +3784,176 @@ system_process_attributes (Lisp_Object pid)
   return attrs;
 }
 
+#elif defined(__FreeBSD__)
+
+Lisp_Object
+system_process_attributes (Lisp_Object pid)
+{
+  int proc_id;
+  int pagesize = getpagesize();
+  int npages;
+  int fscale;
+  struct passwd *pw;
+  struct group  *gr;
+  char *ttyname;
+  size_t len;
+  char args[MAXPATHLEN];
+  EMACS_TIME t, now;
+  
+  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID};
+  struct kinfo_proc proc;
+  size_t proclen = sizeof(proc);
+
+  struct gcpro gcpro1, gcpro2;
+  Lisp_Object attrs = Qnil;
+  Lisp_Object decoded_comm;
+  
+  CHECK_NUMBER_OR_FLOAT (pid);
+  proc_id = FLOATP (pid) ? XFLOAT_DATA (pid) : XINT (pid);
+  mib[3] = proc_id;
+  
+  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
+    return attrs;
+
+  GCPRO2 (attrs, decoded_comm);
+  
+  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float(proc.ki_uid)), attrs);
+  
+  BLOCK_INPUT;
+  pw = getpwuid (proc.ki_uid);
+  UNBLOCK_INPUT;
+  if (pw)
+    attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
+
+  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float(proc.ki_svgid)), attrs);
+
+  BLOCK_INPUT;
+  gr = getgrgid (proc.ki_svgid);
+  UNBLOCK_INPUT;
+  if (gr)
+    attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
+
+  decoded_comm = code_convert_string_norecord
+    (make_unibyte_string (proc.ki_comm, strlen (proc.ki_comm)),
+     Vlocale_coding_system, 0);
+  
+  attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
+  {
+    char state[2] = {'\0', '\0'};
+    switch (proc.ki_stat)
+      {
+      case SRUN:
+	state[0] = 'R';
+	break;
+
+      case SSLEEP:
+	state[0] = 'S';
+	break;
+	
+      case SLOCK:
+	state[0] = 'D';
+	break;
+
+      case SZOMB:
+	state[0] = 'Z';
+	break;
+	
+      case SSTOP:
+	state[0] = 'T';
+	break;
+      }
+    attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
+  }
+  
+  attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (proc.ki_ppid)), attrs);
+  attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (proc.ki_pgid)), attrs);
+  attrs = Fcons (Fcons (Qsess, make_fixnum_or_float (proc.ki_sid)),  attrs);
+
+  BLOCK_INPUT;
+  ttyname = proc.ki_tdev == NODEV ? NULL : devname (proc.ki_tdev, S_IFCHR);
+  UNBLOCK_INPUT;
+  if (ttyname)
+    attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
+  
+  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.ki_tpgid)), attrs);
+  attrs = Fcons (Fcons (Qminflt,  make_fixnum_or_float (proc.ki_rusage.ru_minflt)), attrs);
+  attrs = Fcons (Fcons (Qmajflt,  make_fixnum_or_float (proc.ki_rusage.ru_majflt)), attrs);
+  attrs = Fcons (Fcons (Qcminflt, make_number (proc.ki_rusage_ch.ru_minflt)), attrs);
+  attrs = Fcons (Fcons (Qcmajflt, make_number (proc.ki_rusage_ch.ru_majflt)), attrs);
+
+#define TIMELIST(ts)					\
+  list3 (make_number (EMACS_SECS (ts) >> 16 & 0xffff),	\
+	 make_number (EMACS_SECS (ts) & 0xffff),	\
+	 make_number (EMACS_USECS (ts)))
+
+  attrs = Fcons (Fcons (Qutime, TIMELIST(proc.ki_rusage.ru_utime)), attrs);
+  attrs = Fcons (Fcons (Qstime, TIMELIST(proc.ki_rusage.ru_stime)), attrs);
+  EMACS_ADD_TIME (t, proc.ki_rusage.ru_utime, proc.ki_rusage.ru_stime);
+  attrs = Fcons (Fcons (Qtime,  TIMELIST(t)), attrs);
+
+  attrs = Fcons (Fcons (Qcutime, TIMELIST(proc.ki_rusage_ch.ru_utime)), attrs);
+  attrs = Fcons (Fcons (Qcstime, TIMELIST(proc.ki_rusage_ch.ru_utime)), attrs);
+  EMACS_ADD_TIME (t, proc.ki_rusage_ch.ru_utime, proc.ki_rusage_ch.ru_stime);
+  attrs = Fcons (Fcons (Qctime, TIMELIST(t)), attrs);
+
+  attrs = Fcons (Fcons (Qthcount, make_fixnum_or_float (proc.ki_numthreads)), attrs);
+  attrs = Fcons (Fcons (Qpri,   make_number (proc.ki_pri.pri_native)), attrs);
+  attrs = Fcons (Fcons (Qnice,  make_number (proc.ki_nice)), attrs);
+  attrs = Fcons (Fcons (Qstart, TIMELIST(proc.ki_start)), attrs);
+  attrs = Fcons (Fcons (Qvsize, make_number (proc.ki_size >> 10)), attrs);
+  attrs = Fcons (Fcons (Qrss,   make_number (proc.ki_rssize * pagesize >> 10)), attrs);
+
+  EMACS_GET_TIME (now);
+  EMACS_SUB_TIME (t, now, proc.ki_start);
+  attrs = Fcons (Fcons (Qetime, TIMELIST(t)), attrs);
+
+#undef TIMELIST
+  
+  len = sizeof(fscale);
+  if (sysctlbyname ("kern.fscale", &fscale, &len, NULL, 0) == 0)
+    {
+      float   pcpu;
+      fixpt_t ccpu;
+      len = sizeof (ccpu);
+      if (sysctlbyname ("kern.ccpu", &ccpu, &len, NULL, 0) == 0)
+      	{
+      	  pcpu = 100.0 * ((double) proc.ki_pctcpu / fscale)
+  	    / (1.0 - exp(proc.ki_swtime * log((double) ccpu / fscale)));
+  	  attrs = Fcons (Fcons (Qpcpu, make_fixnum_or_float(pcpu)), attrs);
+      	}
+    }
+
+  len = sizeof(npages);
+  if (sysctlbyname ("hw.availpages", &npages, &len, NULL, 0) == 0)
+    {
+      float pmem = proc.ki_flag & P_INMEM
+  	? 100.0 * ((float) proc.ki_rssize / npages)
+  	: 0.0;
+      attrs = Fcons (Fcons (Qpmem, make_fixnum_or_float(pmem)), attrs);
+    }
+
+  mib[2] = KERN_PROC_ARGS;
+  len = MAXPATHLEN;
+  if (sysctl (mib, 4, args, &len, NULL, 0) == 0)
+    {
+      int i;
+      for (i = 0; i < len; i++)
+	{
+	  if (! args[i] && i < len - 1)
+	    args[i] = ' ';
+	}
+
+      decoded_comm = code_convert_string_norecord
+	(make_unibyte_string (args, strlen (args)),
+	 Vlocale_coding_system, 0);
+
+      attrs = Fcons (Fcons (Qargs, decoded_comm), attrs);
+    }
+  
+  UNGCPRO;
+  return attrs;
+}
+
 /* The WINDOWSNT implementation is in w32.c.
    The MSDOS implementation is in dosfns.c.  */
 #elif !defined (WINDOWSNT) && !defined (MSDOS)

[-- Attachment #3: Type: text/plain, Size: 18 bytes --]


-- 
Eduard Wiebe

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [patch] FreeBSD support for system processes
  2009-12-17 17:26 ` Dan Nicolaescu
@ 2009-12-18 10:19   ` Eduard Wiebe
  0 siblings, 0 replies; 4+ messages in thread
From: Eduard Wiebe @ 2009-12-18 10:19 UTC (permalink / raw
  To: emacs-devel

Dan Nicolaescu <dann@ics.uci.edu> writes:

> Eduard Wiebe <usenet@pusto.de> writes:
>
>   >  Hello,
>   > 
>   > Following patch improves support for system processes on FreeBSD
>   > machines.
>
> Does this work on other BSDs?

Unfortunatly i have no access to other BSD machines, so i'm not able to
answer your question now. Sorry.

> We have a feature freeze now, so this can only get if approved by one
> of the maintainers.  It would be a good idea to send this to
> bug-gnu-emacs@gnu.org so that it does not get lost.

Done.

-- 
Eduard Wiebe





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-12-18 10:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-14 18:01 [patch] FreeBSD support for system processes Eduard Wiebe
2009-12-17 17:26 ` Dan Nicolaescu
2009-12-18 10:19   ` Eduard Wiebe
  -- strict thread matches above, loose matches on Subject: below --
2009-12-18  9:53 Eduard Wiebe

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.