void ztask_mon_proc_update (ztask_mon_proc_t *self, glibtop_cpu *cpu, glibtop_mem *memory, ztask_mon_proc_t *sys) { assert (self); assert (cpu); assert (memory); float total, used, load; if (!self->pid) { // updating system monitoring info if (!ztask_mon_proc_cpu_total_last (self)) { used = 0; total = 0; } else { total = cpu->total - ztask_mon_proc_cpu_total_last (self); used = cpu->user + cpu->nice + cpu->sys - ztask_mon_proc_cpu_used_last (self); } // load = used/total; load = 100 * used / MAX (total, 1.0f); load = MIN (load, 100.0f); self->cpu = load; self->mem_rss = (memory->used - memory->buffer - memory->cached); } else { // updating process monitoring info glibtop_proc_time proctime; glibtop_get_proc_time (&proctime, self->pid); if (!ztask_mon_proc_cpu_total_last (sys)) { used = 0; total = 0; } else { // updating system monitoring info total = cpu->total - ztask_mon_proc_cpu_total_last (sys); used = proctime.rtime - ztask_mon_proc_cpu_used_last (self); } // load = used/total; load = 100 * used / MAX (total, 1.0f); load = MIN (load, 100.0f); ztask_mon_proc_set_cpu_total_last (self, cpu->total); ztask_mon_proc_set_cpu_used_last (self, proctime.rtime); glibtop_proc_mem mem; glibtop_get_proc_mem (&mem, self->pid); self->cpu = (float) load; self->mem_rss = (float) (mem.rss); } }
void SmoothRefresh::reset() { glibtop_cpu cpu; glibtop_proc_time proctime; glibtop_get_cpu(&cpu); glibtop_get_proc_time(&proctime, getpid()); this->interval = ProcData::get_instance()->config.update_interval; this->last_pcpu = PCPU_LO; this->last_total_time = cpu.total; this->last_cpu_time = proctime.rtime; }
static double get_relative_time(void) { static unsigned long start_time; GTimeVal tv; if (G_UNLIKELY(!start_time)) { glibtop_proc_time buf; glibtop_get_proc_time(&buf, getpid()); start_time = buf.start_time; } g_get_current_time(&tv); return (tv.tv_sec - start_time) + 1e-6 * tv.tv_usec; }
char* proccess_get_time (pid_t pid) { glibtop_proc_time proc_time; glibtop_get_proc_time (&proc_time, pid); return g_strdup_printf ("CPU usage: start_time: %" G_GUINT64_FORMAT " rtime: %" G_GUINT64_FORMAT " utime: %" G_GUINT64_FORMAT " stime: %" G_GUINT64_FORMAT " cutime:%" G_GUINT64_FORMAT " cstime: %" G_GUINT64_FORMAT " timeout: %" G_GUINT64_FORMAT " it_real_value: %" G_GUINT64_FORMAT " frequency: %" G_GUINT64_FORMAT, proc_time.start_time, proc_time.rtime, proc_time.utime, proc_time.stime, proc_time.cutime, proc_time.cstime, proc_time.timeout, proc_time.it_real_value, proc_time.frequency); }
unsigned SmoothRefresh::get_own_cpu_usage() { glibtop_cpu cpu; glibtop_proc_time proctime; guint64 elapsed; unsigned usage = PCPU_LO; glibtop_get_cpu (&cpu); elapsed = cpu.total - this->last_total_time; if (elapsed) { // avoid division by 0 glibtop_get_proc_time(&proctime, getpid()); usage = (proctime.rtime - this->last_cpu_time) * 100 / elapsed; } usage = CLAMP(usage, 0, 100); this->last_total_time = cpu.total; this->last_cpu_time = proctime.rtime; return usage; }
static PyObject* gtop_proc_time(PyObject *self, PyObject *args) { glibtop_proc_time buf; PyObject *d, *smp; unsigned pid; unsigned i; if(!PyArg_ParseTuple(args, "I", &pid)) return NULL; glibtop_get_proc_time(&buf, pid); d = PyDict_New(); my_dict_add_and_decref(d, "start_time", PyL_ULL(buf.start_time)); my_dict_add_and_decref(d, "stime", PyL_ULL(buf.stime)); my_dict_add_and_decref(d, "rtime", PyL_ULL(buf.rtime)); my_dict_add_and_decref(d, "utime", PyL_ULL(buf.utime)); my_dict_add_and_decref(d, "cstime", PyL_ULL(buf.cstime)); my_dict_add_and_decref(d, "cutime", PyL_ULL(buf.cutime)); my_dict_add_and_decref(d, "timeout", PyL_ULL(buf.timeout)); my_dict_add_and_decref(d, "it_real_value", PyL_ULL(buf.it_real_value)); my_dict_add_and_decref(d, "frequency", PyL_ULL(buf.frequency)); smp = PyTuple_New(1 + glibtop_global_server->ncpu); for(i = 0; i <= glibtop_global_server->ncpu; ++i) { PyObject *a; a = get_smp_time(&buf, i); PyTuple_SET_ITEM(smp, i, a); } my_dict_add_and_decref(d, "cpus", smp); Py_INCREF(smp); my_dict_add_and_decref(d, "xcpu", smp); return _struct_new(d); }
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; }