Esempio n. 1
0
int CheckProcToAdd(int pos, unsigned int *procList, unsigned int procListSize)
{
   _desc *glump;
   int i, compteur = 0;
   glibtop_proc_state buf;

   for (i = pos; i < procListSize ; i++){

      compteur++;
      glump = (_desc *)malloc(sizeof(_desc));
      usleep(20000); /* libgtop seems to need a little bit of time */
      if (CheckProc(procList[i])) continue; /* checking if the process isn't already dead */

      glibtop_get_proc_state(&buf, glump -> pid = procList[i]);
      strcpy(glump -> name, buf.cmd);
      if (strlen(glump -> name) > MAX_CHAR)
	glump -> name[MAX_CHAR] = 0;

      pList -> previous = glump;
      glump -> next = pList;
      glump -> previous = NULL;
      if (posProc == pList) posProc = glump;
      pList = glump;
      gNbProc++;
      gNbProcTotal++;

      lastProcPid = glump -> pid;
   }

   return compteur;
}
Esempio n. 2
0
static PyObject* gtop_proc_state(PyObject *self, PyObject *args)
{
	glibtop_proc_state buf;
	PyObject *d;
	unsigned pid;

	if(!PyArg_ParseTuple(args, "I", &pid))
		return NULL;

	glibtop_get_proc_state(&buf, pid);

	d = PyDict_New();

	my_dict_add_and_decref(d, "cmd", PyS_S(buf.cmd));
	my_dict_add_and_decref(d, "state", PyL_UL(buf.state));
	my_dict_add_and_decref(d, "uid", PyI_L(buf.uid));
	my_dict_add_and_decref(d, "gid", PyI_L(buf.gid));
	my_dict_add_and_decref(d, "ruid", PyI_L(buf.ruid));
	my_dict_add_and_decref(d, "rgid", PyI_L(buf.rgid));
	my_dict_add_and_decref(d, "has_cpu", PyB_L(buf.has_cpu));
	my_dict_add_and_decref(d, "processor", PyI_L(buf.processor));
	my_dict_add_and_decref(d, "last_processor", PyI_L(buf.last_processor));

	return _struct_new(d);
}
Esempio n. 3
0
_desc *GetProcList(void) 		       /* create a double linked list */
{
   glibtop_proclist buf;
   unsigned int *n;
   unsigned int nbPr;
   int i;
   _desc *lastOne;
   _desc *glump;
   _desc *res;

   if ((n = glibtop_get_proclist (&buf, GLIBTOP_KERN_PROC_UID, (int64_t)getuid())) == NULL) {
      fprintf(stderr, "Problem using libgtop\n");
      exit(1);
   };

   nbPr = (int)buf.number;

   glump = (_desc *)malloc(sizeof(_desc));
   res = glump;
   lastOne = NULL;

   for (i = nbPr; i; i--){
      char *bof;
      glibtop_proc_state buf;
      glump -> previous = lastOne;
      glump -> next = (_desc *)malloc(sizeof(_desc));

      glibtop_get_proc_state(&buf, glump -> pid = n[i - 1]);
      strcpy(glump -> name, bof = buf.cmd);
      if (strlen(glump -> name) > MAX_CHAR)
	glump -> name[MAX_CHAR] = 0;

      lastOne = glump;
      glump = glump -> next;

      if (procBaseName && !strcmp(bof, procBaseName)) {
	 procBasePos = i - 1;
	 procBasePid = n[i - 1];
	 break;
      }
   }

   lastOne -> next = NULL;
   lastProcPid = n[nbPr - 1];
   g_free(n);

   if (procBaseName && i) gNbProc = nbPr - i + 1;   /* procBase has been found */
   else {			       /* procBaseName is null or hasn't been found */
      procBaseName = NULL;
      procBasePos = 0;
      procBasePid = n[0];
      gNbProc = nbPr;
   }

   gNbProcTotal = nbPr;

   return res;
}
Esempio n. 4
0
/*
 * Return a newly-allocated string that contains the name of the
 * process for the given PID.  This means that the caller owns the
 * variable.
 */
char *
alltray_get_process_name(int pid) {
  char *retval = NULL;
  glibtop_proc_state *glbuf = NULL;

  glbuf = (glibtop_proc_state *)g_malloc0(sizeof(glibtop_proc_state));
  glibtop_get_proc_state(glbuf, pid);

  retval = g_strndup(glbuf->cmd, 40);
  free(glbuf);

  return(retval);
}
Esempio n. 5
0
int
main (int argc, char *argv [])
{
	glibtop_union data;
	unsigned c;
	struct rusage total_start, total_end;
	struct rusage rusage_start, rusage_end;
	struct timeval elapsed_utime, elapsed_stime;
	pid_t pid;

	pid = getpid ();

	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR);
	textdomain (GETTEXT_PACKAGE);


	glibtop_init_r (&glibtop_global_server, 0, 0);

	display_self_times();

	printf ("%-12s (%-10s): %7s - %9s - %9s\n",
		"Feature", "Flags", "Count", "utime", "stime");
	printf ("-------------------------------------------"
		"---------------\n");

	getrusage (RUSAGE_SELF, &total_start);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_cpu (&data.cpu);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("CPU          (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.cpu.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++)
		glibtop_get_mem (&data.mem);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Memory       (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.mem.flags, PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++)
		glibtop_get_swap (&data.swap);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Swap         (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.swap.flags, PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_uptime (&data.uptime);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Uptime       (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.uptime.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_loadavg (&data.loadavg);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Loadavg      (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.loadavg.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) {
		pid_t* ptr = glibtop_get_proclist (&data.proclist, 0, 0);
		g_free (ptr);
	}

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proclist     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proclist.flags,
		PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) {
		char** ptr = glibtop_get_netlist (&data.netlist);
		g_strfreev (ptr);
	}

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Netlist      (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proclist.flags,
		PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_state (&data.proc_state, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_State   (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_state.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_uid (&data.proc_uid, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Uid     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_uid.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_mem (&data.proc_mem, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Mem     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_mem.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) {
		glibtop_map_entry* entries;
		entries = glibtop_get_proc_map (&data.proc_map, pid);
		g_free (entries);
	}

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Map     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_map.flags, PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
		(long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_segment (&data.proc_segment, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Segment (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_segment.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++) {
		char** argv;
		argv = glibtop_get_proc_argv (&data.proc_args, pid, 0);
		g_strfreev(argv);
	}

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Args    (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_args.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_time (&data.proc_time, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Time    (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_time.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_signal (&data.proc_signal, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Signal  (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_signal.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &rusage_start);

	for (c = 0; c < PROFILE_COUNT; c++)
		glibtop_get_proc_kernel (&data.proc_kernel, pid);

	getrusage (RUSAGE_SELF, &rusage_end);

	libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
			  &elapsed_stime);

	printf ("Proc_Kernel  (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
		(unsigned long) data.proc_kernel.flags, PROFILE_COUNT,
		(long double) ELAPSED_UTIME / PROFILE_COUNT,
		(long double) ELAPSED_STIME / PROFILE_COUNT);

	getrusage (RUSAGE_SELF, &total_end);

	libgtop_timersub (&total_end.ru_utime, &total_start.ru_utime,
			  &elapsed_utime);

	libgtop_timersub (&total_end.ru_stime, &total_start.ru_stime,
			  &elapsed_stime);

	printf ("-------------------------------------------"
		"---------------\n");

	printf ("%-36s %9lu - %9lu\n\n", "TOTAL",
		ELAPSED_UTIME, ELAPSED_STIME);

	printf ("All timings are in clock ticks "
		"(1000000 ticks per second).\n\n");

	display_self_times();

	glibtop_close ();

	exit (0);
}
Esempio n. 6
0
void
update_process_info (void)
{
  static guint64 old_total_jiffies = 0;
  
  if (!awn_proc_users)
  {
    g_debug ("%s: no users",__func__);
    return;
  }
  pid_t * p;
  gint y ;
  glibtop_proclist proclist;
  GList * old_awn_proc_info=awn_proc_info;  
  guint64  total_jiffies;
  glibtop_cpu cpu;
  gdouble percent;
  gdouble total_per = 0;

  glibtop_get_cpu (&cpu);
  total_jiffies = cpu.total;
  
  awn_proc_info = NULL;

/*  p = glibtop_get_proclist(&proclist, GLIBTOP_KERN_PROC_RUID, getuid());*/
  p = glibtop_get_proclist(&proclist, GLIBTOP_KERN_PROC_ALL, -1);

//  g_debug ("number of entries = %d",proclist.number);
  for (y = 0;y < proclist.number;y++)
  {
    AwnProcInfo * data = g_malloc (sizeof(AwnProcInfo));
    GList       * search;    

    data->pid = p[y];
    glibtop_get_proc_state(&data->proc_state, p[y]);
    glibtop_get_proc_time(&data->proc_time, p[y]);

    search = g_list_find_custom (old_awn_proc_info,GINT_TO_POINTER(p[y]),
                                 (GCompareFunc)_cmp_find_pid);
    
    if (search)
    {
      AwnProcInfo * search_data = search->data;
      long time_diff;
      double jiffies;      
      
      jiffies = total_jiffies - old_total_jiffies;
//      g_debug ("%d  jiffies = %lf",p[y],jiffies);
      time_diff = (data->proc_time.utime + data->proc_time.stime) - (search_data->proc_time.utime+search_data->proc_time.stime);
//      g_debug ("%d  time diff = %ld",p[y],time_diff);
      percent = time_diff / (jiffies / cpu.frequency) ;
//      g_debug ("percent for %d = %lf",p[y],percent);
    }
    else
    {
      percent = 0;
    }
    data->percent_cpu = percent;
    total_per = total_per + percent;
    awn_proc_info = g_list_prepend (awn_proc_info,data);
  }
  g_list_foreach (old_awn_proc_info,(GFunc)g_free,NULL);  
  g_list_free (old_awn_proc_info);  
  g_free (p);
  old_total_jiffies = total_jiffies;
}