Top/ps process monitor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2223 lines
68 KiB

5 years ago
#include "system.h"
#include "process.h"
#include "disks.h"
5 years ago
#include "ui.h"
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#if defined(__APPLE__) && defined(__MACH__)
# define __MacOS__
#endif
static Eina_Lock _lock;
void
ui_shutdown(Ui *ui)
{
evas_object_hide(ui->win);
ui->shutting_down = EINA_TRUE;
if (ui->thread_system)
ecore_thread_cancel(ui->thread_system);
if (ui->thread_process)
ecore_thread_cancel(ui->thread_process);
if (ui->thread_system)
ecore_thread_wait(ui->thread_system, 1.0);
if (ui->thread_process)
ecore_thread_wait(ui->thread_process, 1.0);
eina_lock_free(&_lock);
ecore_main_loop_quit();
}
5 years ago
static void
5 years ago
_system_stats_thread(void *data, Ecore_Thread *thread)
5 years ago
{
Ui *ui;
int i;
ui = data;
while (1)
{
results_t *results = malloc(sizeof(results_t));
system_stats_all_get(results);
ecore_thread_feedback(thread, results);
for (i = 0; i < ui->poll_delay * 2; i++)
{
if (ecore_thread_check(thread))
return;
if (ui->skip_wait)
{
ui->skip_wait = EINA_FALSE;
break;
}
5 years ago
usleep(500000);
}
}
}
static unsigned long
_mem_adjust(Data_Unit unit, unsigned long value)
5 years ago
{
if (unit == DATA_UNIT_KB)
{
//FIXME: KB is memory base default.
}
else if (unit == DATA_UNIT_MB)
{
value >>= 10;
}
else if (unit == DATA_UNIT_GB)
{
value >>= 20;
}
5 years ago
return value;
5 years ago
}
4 years ago
static void
_progressbar_value_force_set(Evas_Object *progressbar, double val)
{
elm_progressbar_value_set(progressbar, 0.00001);
elm_progressbar_value_set(progressbar, val);
}
static char *
_network_transfer_format(double rate)
5 years ago
{
char buf[1024];
double outgoing;
const char *unit = "B/s";
5 years ago
if (rate > 1048576)
{
rate /= 1048576;
unit = "MB/s";
}
else if (rate > 1024 && rate < 1048576)
{
rate /= 1024;
unit = "KB/s";
}
5 years ago
snprintf(buf, sizeof(buf), "%.2f %s", rate, unit);
5 years ago
return strdup(buf);
5 years ago
}
static void
_misc_view_update(Ui *ui, results_t *results)
5 years ago
{
Evas_Object *box, *frame, *progress;
char *tmp;
int i;
5 years ago
if (!ui->misc_visible)
return;
5 years ago
elm_box_clear(ui->misc_activity);
5 years ago
box = elm_box_add(ui->content);
evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_show(box);
5 years ago
if (results->power.battery_count)
{
frame = elm_frame_add(box);
evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, 0);
evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
if (results->power.have_ac)
elm_object_text_set(frame, "Battery (plugged in)");
else
elm_object_text_set(frame, "Battery");
5 years ago
evas_object_show(frame);
5 years ago
progress = elm_progressbar_add(frame);
evas_object_size_hint_align_set(progress, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(progress, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_progressbar_span_size_set(progress, 1.0);
elm_progressbar_unit_format_set(progress, "%1.2f%%");
4 years ago
_progressbar_value_force_set(progress, (double)results->power.percent / 100);
evas_object_show(progress);
elm_object_content_set(frame, progress);
elm_box_pack_end(box, frame);
}
5 years ago
frame = elm_frame_add(box);
evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, 0);
evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_object_text_set(frame, "Network Incoming");
evas_object_show(frame);
5 years ago
progress = elm_progressbar_add(frame);
evas_object_size_hint_align_set(progress, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(progress, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_progressbar_span_size_set(progress, 1.0);
tmp = _network_transfer_format(results->incoming);
elm_progressbar_unit_format_set(progress, tmp);
free(tmp);
5 years ago
if (results->incoming == 0)
4 years ago
_progressbar_value_force_set(progress, 0.0);
else
4 years ago
_progressbar_value_force_set(progress, 1.0);
5 years ago
evas_object_show(progress);
elm_object_content_set(frame, progress);
elm_box_pack_end(box, frame);
frame = elm_frame_add(box);
evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, 0);
evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_object_text_set(frame, "Network Outgoing");
evas_object_show(frame);
progress = elm_progressbar_add(frame);
evas_object_size_hint_align_set(progress, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(progress, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_progressbar_span_size_set(progress, 1.0);
tmp = _network_transfer_format(results->outgoing);
elm_progressbar_unit_format_set(progress, tmp);
free(tmp);
if (results->outgoing == 0)
4 years ago
_progressbar_value_force_set(progress, 0.0);
else
4 years ago
_progressbar_value_force_set(progress, 1.0);
evas_object_show(progress);
5 years ago
elm_object_content_set(frame, progress);
elm_box_pack_end(box, frame);
elm_box_pack_end(ui->misc_activity, box);
}
static unsigned long
_disk_adjust(Data_Unit unit, unsigned long value)
{
if (unit == DATA_UNIT_KB)
{
value >>= 10;
}
else if (unit == DATA_UNIT_MB)
{
value >>= 20;
}
else if (unit == DATA_UNIT_GB)
{
value >>= 30;
}
return value;
5 years ago
}
static void
_ui_disk_add(Ui *ui, const char *path, const char *mount, unsigned long total, unsigned long used)
5 years ago
{
Evas_Object *frame, *progress;
double ratio, value;
frame = elm_frame_add(ui->disk_activity);
evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, 0);
evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, 0);
elm_object_text_set(frame, eina_slstr_printf("%s on %s", path, mount));
evas_object_show(frame);
progress = elm_progressbar_add(frame);
evas_object_size_hint_align_set(progress, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(progress, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_progressbar_span_size_set(progress, 1.0);
elm_progressbar_unit_format_set(progress,
eina_slstr_printf(
"%lu%c of %lu%c",
4 years ago
_disk_adjust(ui->data_unit, used), ui->data_unit,
_disk_adjust(ui->data_unit, total), ui->data_unit));
elm_object_content_set(frame, progress);
ratio = total / 100.0;
value = used / ratio;
if (used == 0 && total == 0)
4 years ago
_progressbar_value_force_set(progress, 1.0);
else
4 years ago
_progressbar_value_force_set(progress, value / 100.0);
evas_object_show(progress);
elm_box_pack_end(ui->disk_activity, frame);
5 years ago
}
static void
_disk_view_update(Ui *ui)
5 years ago
{
Eina_List *disks;
char *path;
unsigned long total, used;
if (!ui->disk_visible)
return;
elm_box_clear(ui->disk_activity);
disks = disks_get();
EINA_LIST_FREE(disks, path)
5 years ago
{
char *mount = disk_mount_point_get(path);
if (mount)
{
if (disk_usage_get(mount, &total, &used))
{
_ui_disk_add(ui, path, mount, total, used);
}
free(mount);
}
free(path);
5 years ago
}
if (disks)
free(disks);
5 years ago
}
static void
_memory_view_update(Ui *ui, results_t *results)
5 years ago
{
Evas_Object *box, *frame, *progress;
double ratio, value;
5 years ago
if (!ui->mem_visible)
return;
5 years ago
progress = ui->progress_mem_used;
elm_progressbar_unit_format_set(progress,
eina_slstr_printf(
"%lu %c / %lu %c",
4 years ago
_mem_adjust(ui->data_unit, results->memory.used), ui->data_unit,
_mem_adjust(ui->data_unit, results->memory.total), ui->data_unit));
ratio = results->memory.total / 100.0;
value = results->memory.used / ratio;
4 years ago
_progressbar_value_force_set(progress, value / 100);
5 years ago
progress = ui->progress_mem_cached;
elm_progressbar_unit_format_set(progress, eina_slstr_printf(
"%lu %c / %lu %c",
4 years ago
_mem_adjust(ui->data_unit, results->memory.cached), ui->data_unit,
_mem_adjust(ui->data_unit, results->memory.total), ui->data_unit));
ratio = results->memory.total / 100.0;
value = results->memory.cached / ratio;
4 years ago
_progressbar_value_force_set(progress, value / 100);
5 years ago
progress = ui->progress_mem_buffered;
elm_progressbar_unit_format_set(progress,
eina_slstr_printf(
"%lu %c / %lu %c",
4 years ago
_mem_adjust(ui->data_unit, results->memory.buffered), ui->data_unit,
_mem_adjust(ui->data_unit, results->memory.total), ui->data_unit));
ratio = results->memory.total / 100.0;
value = results->memory.buffered / ratio;
4 years ago
_progressbar_value_force_set(progress, value / 100);
5 years ago
progress = ui->progress_mem_shared;
elm_progressbar_unit_format_set(progress,
eina_slstr_printf(
"%lu %c / %lu %c",
4 years ago
_mem_adjust(ui->data_unit, results->memory.shared), ui->data_unit,
_mem_adjust(ui->data_unit, results->memory.total), ui->data_unit));
ratio = results->memory.total / 100.0;
value = results->memory.shared / ratio;
4 years ago
_progressbar_value_force_set(progress, value / 100);
progress = ui->progress_mem_swap;
elm_progressbar_unit_format_set(progress,
eina_slstr_printf(
"%lu %c / %lu %c",
_mem_adjust(ui->data_unit, results->memory.swap_total - results->memory.swap_used), ui->data_unit,
_mem_adjust(ui->data_unit, results->memory.swap_total), ui->data_unit));
ratio = results->memory.swap_total / 100.0;
value = (results->memory.swap_total - results->memory.swap_used) / ratio;
_progressbar_value_force_set(progress, value / 100);
5 years ago
}
static void
_cpu_view_update(Ui *ui, results_t *results)
5 years ago
{
Evas_Object *box, *frame, *progress;
int i;
5 years ago
if (!ui->cpu_visible)
return;
5 years ago
elm_box_clear(ui->cpu_activity);
5 years ago
box = elm_box_add(ui->content);
evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_show(box);
5 years ago
for (i = 0; i < results->cpu_count; i++)
5 years ago
{
frame = elm_frame_add(box);
evas_object_size_hint_align_set(frame, EVAS_HINT_FILL, 0);
evas_object_size_hint_weight_set(frame, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
if (i == 0 && results->temperature != INVALID_TEMP)
elm_object_text_set(frame, eina_slstr_printf("CPU %d (%d °C)", i, results->temperature));
else
elm_object_text_set(frame, eina_slstr_printf("CPU %d", i));
5 years ago
evas_object_show(frame);
5 years ago
progress = elm_progressbar_add(frame);
evas_object_size_hint_align_set(progress, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_size_hint_weight_set(progress, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_progressbar_span_size_set(progress, 1.0);
elm_progressbar_unit_format_set(progress, "%1.2f%%");
5 years ago
4 years ago
_progressbar_value_force_set(progress, results->cores[i]->percent / 100);
evas_object_show(progress);
elm_object_content_set(frame, progress);
elm_box_pack_end(box, frame);
5 years ago
}
elm_box_pack_end(ui->cpu_activity, box);
5 years ago
}
static void
_system_stats_thread_feedback_cb(void *data, Ecore_Thread *thread, void *msg)
5 years ago
{
Ui *ui;
results_t *results;
double cpu_usage = 0.0;
5 years ago
int i;
ui = data;
results = msg;
5 years ago
if (ecore_thread_check(thread))
goto out;
_cpu_view_update(ui, results);
_memory_view_update(ui, results);
_disk_view_update(ui);
_misc_view_update(ui, results);
for (i = 0; i < results->cpu_count; i++)
{
cpu_usage += results->cores[i]->percent;
free(results->cores[i]);
5 years ago
}
cpu_usage = cpu_usage / results->cpu_count;
5 years ago
4 years ago
_progressbar_value_force_set(ui->progress_cpu, (double)cpu_usage / 100);
elm_progressbar_unit_format_set(ui->progress_mem, eina_slstr_printf("%ld %c out of %ld %c",
4 years ago
_mem_adjust(ui->data_unit, results->memory.used), ui->data_unit,
_mem_adjust(ui->data_unit, results->memory.total), ui->data_unit));
4 years ago
_progressbar_value_force_set(ui->progress_mem, (double)((results->memory.total / 100.0) * results->memory.used) / 1000000000000);
out:
free(results->cores);
free(results);
}
static int
_sort_by_pid(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return inf1->pid - inf2->pid;
5 years ago
}
static int
_sort_by_uid(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return inf1->uid - inf2->uid;
5 years ago
}
static int
_sort_by_nice(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return inf1->nice - inf2->nice;
}
5 years ago
static int
_sort_by_pri(const void *p1, const void *p2)
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return inf1->priority - inf2->priority;
5 years ago
}
static int
_sort_by_cpu(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return inf1->cpu_id - inf2->cpu_id;
}
5 years ago
static int
_sort_by_threads(const void *p1, const void *p2)
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return inf1->numthreads - inf2->numthreads;
5 years ago
}
static int
_sort_by_size(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
int64_t size1, size2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
size1 = inf1->mem_size;
size2 = inf2->mem_size;
5 years ago
if (size1 < size2)
return -1;
if (size2 > size1)
return 1;
5 years ago
return 0;
5 years ago
}
static int
_sort_by_rss(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
int64_t size1, size2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
size1 = inf1->mem_rss;
size2 = inf2->mem_rss;
5 years ago
if (size1 < size2)
return -1;
if (size2 > size1)
return 1;
5 years ago
return 0;
5 years ago
}
static int
_sort_by_cpu_usage(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
double one, two;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
one = inf1->cpu_usage;
two = inf2->cpu_usage;
5 years ago
if (one < two)
return -1;
if (two > one)
return 1;
5 years ago
return 0;
5 years ago
}
static int
_sort_by_cmd(const void *p1, const void *p2)
5 years ago
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return strcasecmp(inf1->command, inf2->command);
}
5 years ago
static int
_sort_by_state(const void *p1, const void *p2)
{
const Proc_Stats *inf1, *inf2;
5 years ago
inf1 = p1; inf2 = p2;
5 years ago
return strcmp(inf1->state, inf2->state);
5 years ago
}
static void
_fields_append(Ui *ui, Proc_Stats *proc)
5 years ago
{
int64_t mem_size, mem_rss;
5 years ago
if (ui->program_pid == proc->pid)
return;
5 years ago
mem_size = proc->mem_size;
mem_rss = proc->mem_rss;
5 years ago
if (ui->data_unit == DATA_UNIT_KB)
{
mem_size >>= 10;
mem_rss >>= 10;
}
else if (ui->data_unit == DATA_UNIT_MB)
{
mem_size >>= 20;
mem_rss >>= 20;
}
else
{
mem_size >>= 30;
mem_rss >>= 30;
}
5 years ago
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_PID], eina_slstr_printf("<link>%d</link> <br>", proc->pid), TEXT_FIELD_MAX);
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_UID], eina_slstr_printf("%d <br>", proc->uid), TEXT_FIELD_MAX);
4 years ago
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_SIZE], eina_slstr_printf("%lld %c<br>", mem_size, ui->data_unit), TEXT_FIELD_MAX);
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_RSS], eina_slstr_printf("%lld %c<br>", mem_rss, ui->data_unit), TEXT_FIELD_MAX);
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_COMMAND], eina_slstr_printf("%s<br>", proc->command), TEXT_FIELD_MAX);
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_STATE], eina_slstr_printf("%s <br>", proc->state), TEXT_FIELD_MAX);
eina_strlcat(ui->fields[PROCESS_INFO_FIELD_CPU_USAGE], eina_slstr_printf("%.1f%% <br>", proc->cpu_usage), TEXT_FIELD_MAX);
5 years ago
}
static void
_fields_show(Ui *ui)
5 years ago
{
elm_object_text_set(ui->entry_pid, ui->fields[PROCESS_INFO_FIELD_PID]);
elm_object_text_set(ui->entry_uid, ui->fields[PROCESS_INFO_FIELD_UID]);
elm_object_text_set(ui->entry_size, ui->fields[PROCESS_INFO_FIELD_SIZE]);
elm_object_text_set(ui->entry_rss, ui->fields[PROCESS_INFO_FIELD_RSS]);
elm_object_text_set(ui->entry_cmd, ui->fields[PROCESS_INFO_FIELD_COMMAND]);
elm_object_text_set(ui->entry_state, ui->fields[PROCESS_INFO_FIELD_STATE]);
elm_object_text_set(ui->entry_cpu_usage, ui->fields[PROCESS_INFO_FIELD_CPU_USAGE]);
5 years ago
}
static void
_fields_clear(Ui *ui)
5 years ago
{
for (int i = 0; i < PROCESS_INFO_FIELDS; i++)
{
ui->fields[i][0] = '\0';
}
5 years ago
}
static void
_fields_free(Ui *ui)
5 years ago
{
for (int i = 0; i < PROCESS_INFO_FIELDS; i++)
{
free(ui->fields[i]);
}
}
5 years ago
static Eina_List *
_list_sort(Ui *ui, Eina_List *list)
{
switch (ui->sort_type)
{
case SORT_BY_NONE:
case SORT_BY_PID:
list = eina_list_sort(list, eina_list_count(list), _sort_by_pid);
break;
5 years ago
case SORT_BY_UID:
list = eina_list_sort(list, eina_list_count(list), _sort_by_uid);
break;
case SORT_BY_NICE:
list = eina_list_sort(list, eina_list_count(list), _sort_by_nice);
break;
case SORT_BY_PRI:
list = eina_list_sort(list, eina_list_count(list), _sort_by_pri);
break;
case SORT_BY_CPU:
list = eina_list_sort(list, eina_list_count(list), _sort_by_cpu);
break;
5 years ago
case SORT_BY_THREADS:
list = eina_list_sort(list, eina_list_count(list), _sort_by_threads);
break;
5 years ago
case SORT_BY_SIZE:
list = eina_list_sort(list, eina_list_count(list), _sort_by_size);
break;
5 years ago
case SORT_BY_RSS:
list = eina_list_sort(list, eina_list_count(list), _sort_by_rss);
break;
5 years ago
case SORT_BY_CMD:
list = eina_list_sort(list, eina_list_count(list), _sort_by_cmd);
break;
5 years ago
case SORT_BY_STATE:
list = eina_list_sort(list, eina_list_count(list), _sort_by_state);
break;
case SORT_BY_CPU_USAGE:
list = eina_list_sort(list, eina_list_count(list), _sort_by_cpu_usage);
break;
5 years ago
}
if (ui->sort_reverse)
list = eina_list_reverse(list);
5 years ago
return list;
5 years ago
}
static void
_system_process_list_feedback_cb(void *data, Ecore_Thread *thread EINA_UNUSED, void *msg EINA_UNUSED)
5 years ago
{
Ui *ui;
Eina_List *list, *l;
5 years ago
Proc_Stats *proc;
eina_lock_take(&_lock);
5 years ago
ui = data;
5 years ago
list = proc_info_all_get();
5 years ago
EINA_LIST_FOREACH(list, l, proc)
5 years ago
{
int64_t time_prev = ui->cpu_times[proc->pid];
proc->cpu_usage = 0;
if (!ui->first_run && proc->cpu_time > time_prev)
{
proc->cpu_usage = (double)(proc->cpu_time - time_prev) / ui->poll_delay;
}
5 years ago
}
list = _list_sort(ui, list);
5 years ago
EINA_LIST_FREE(list, proc)
5 years ago
{
_fields_append(ui, proc);
ui->first_run = EINA_FALSE;
ui->cpu_times[proc->pid] = proc->cpu_time;
free(proc);
5 years ago
}
if (list)
eina_list_free(list);
5 years ago
_fields_show(ui);
_fields_clear(ui);
eina_lock_release(&_lock);