_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; }
int CheckProc(pid_t pid) { glibtop_proclist bof; unsigned int *n; if ((n = glibtop_get_proclist (&bof, GLIBTOP_KERN_PROC_PID , (int64_t)pid)) == NULL) { g_free(n); return -1; } g_free(n); return 0; }
void alltray_get_processes_in_pgrp(int pgrp, int **procs, int *procs_len) { pid_t *retval = NULL; gint64 wanted_pgid = (gint64)pgrp; glibtop_proclist *proclist_info = (glibtop_proclist *)g_malloc0(sizeof(glibtop_proclist)); retval = glibtop_get_proclist(proclist_info, GLIBTOP_KERN_PROC_PGRP, wanted_pgid); *procs_len = proclist_info->number; free(proclist_info); *procs = retval; }
int CheckProcChange(void) { glibtop_proclist buf; unsigned int *n; unsigned int nbPr; int diffNbProc; if ((n = glibtop_get_proclist (&buf, GLIBTOP_KERN_PROC_UID, (int64_t)getuid())) == NULL) return -1; nbPr = (int)buf.number; if ((nbPr == gNbProcTotal) && (n[nbPr - 1] == lastProcPid)) return 0; /* nothing changed */ if (procBaseName && (n[procBasePos] != procBasePid)) /* some proc killed before the baseproc (=oldest proc) */ { if (CheckProc(procBasePid)) { /* baseproc doesn't exist anymore */ GarbageCollector(pList); pList = GetProcList(); /* so we create a whole new list */ posProc = pList; return 1; } else while (n[--procBasePos] != procBasePid); /* here we find what's the new pos. of baseproc */ } diffNbProc = (nbPr - procBasePos) - gNbProc; /* nb of changes after baseproc */ if (diffNbProc == 0 && (n[nbPr - 1] == lastProcPid)){ /* only changes before baseproc */ gNbProcTotal = nbPr; g_free(n); return 0; } if (diffNbProc > 0 && n[nbPr - diffNbProc - 1] == lastProcPid) /* only proc to add */ CheckProcToAdd(nbPr - diffNbProc, n, nbPr); else { /* to remove [and to add] */ int nb; nb = CheckProcToRemove(n, nbPr); if (nb != -diffNbProc) CheckProcToAdd(nbPr - diffNbProc - 1, n, nbPr); } g_free(n); return 1; }
static PyObject* gtop_proclist(PyObject *self, PyObject *args) { glibtop_proclist buf; PyObject *list; pid_t *pids; guint64 which = GLIBTOP_KERN_PROC_ALL; guint64 arg = 0; if(!PyArg_ParseTuple(args, "|KK", &which, &arg)) return NULL; pids = glibtop_get_proclist(&buf, which, arg); INIT_LIST_WITH(list, PyI_L, pids, buf.number); g_free(pids); return list; }
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); }
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; }