Commit 48db1369 authored by Petr Štětka's avatar Petr Štětka
Browse files

Added first version of backend.

parent ed1b97fd
Petr Štětka <pstetka@redhat.com>
......@@ -28,10 +28,21 @@ endif()
find_package(PkgConfig)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} libgtop)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} atk)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} cairo)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} gdk-3.0)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} gdk-pixbuf-2.0)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} gio-2.0)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} glib-2.0)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} gobject-2.0)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} gtk+-3.0)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} pango)
set(MODULES_TO_CHECK ${MODULES_TO_CHECK} x11)
pkg_check_modules(DEPS REQUIRED ${MODULES_TO_CHECK})
add_subdirectory(po)
add_subdirectory(data)
add_subdirectory(src)
### CMakeLists automatically created with AutoVala
### Do not edit
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.gnome.Usage.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications/ )
### CMakeLists automatically created with AutoVala
### Do not edit
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/window.ui DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/gnome-usage/ )
......@@ -4,8 +4,11 @@ project_name: gnome-usage
*vala_version: 0.32
*vala_binary: src/gnome-usage
vala_binary: src/gnome-usage
*version: 0.0.1
*vala_vapi: vapis/glibtop.vapi
c_check_package: libgtop
*vala_package: posix
*vala_check_package: atk
*vala_check_package: cairo
*vala_check_package: gdk-3.0
......@@ -16,15 +19,29 @@ project_name: gnome-usage
*vala_check_package: gtk+-3.0
*vala_check_package: pango
*vala_check_package: x11
c_library: gtop-2.0
*vala_source: application.vala
*vala_source: data-view.vala
*vala_source: gnome-usage.vala
*vala_source: performance-view.vala
*vala_source: power-view.vala
*vala_source: storage-view.vala
*vala_source: system-monitor.vala
*vala_source: view.vala
*vala_source: window.vala
h_folder: /usr/include/libgtop-2.0
*po: po
*translate: vala src/gnome-usage.vala
*translate: vala src/application.vala
*translate: vala src/storage-view.vala
*translate: vala src/window.vala
*translate: vala src/performance-view.vala
*translate: vala src/power-view.vala
*translate: vala src/application.vala
*translate: vala src/data-view.vala
*translate: vala src/system-monitor.vala
*translate: vala src/gnome-usage.vala
*translate: vala src/view.vala
*data: data/local
......
src/application.vala
src/data-view.vala
src/gnome-usage.vala
src/performance-view.vala
src/power-view.vala
src/storage-view.vala
src/system-monitor.vala
src/view.vala
src/window.vala
......@@ -5,9 +5,11 @@ namespace Usage {
public class Application : Gtk.Application {
private Window window;
public SystemMonitor monitor;
public Application () {
application_id = "org.gnome.usage";
monitor = new SystemMonitor ();
}
public override void activate () {
......
namespace Usage {
public class DataView : View {
public DataView () {
name = "DATA";
title = _("Data");
var label1 = new Gtk.Label("Page 1 Content.");
add(label1);
}
}
}
......@@ -7,9 +7,6 @@ public static int main (string[] args) {
Intl.textdomain(Constants.GETTEXT_PACKAGE);
Intl.bind_textdomain_codeset(Constants.GETTEXT_PACKAGE, "utf-8");
stdout.printf ("Not Translatable string");
stdout.printf (_("Translatable string!"));
var application = new Usage.Application ();
return application.run (args);
}
namespace Usage {
public class PerformanceView : View {
public PerformanceView () {
name = "performance";
title = _("Performance");
var label1 = new Gtk.Label("Page 1 Content.");
var label2 = new Gtk.Label("Page 2 Content.");
string processList = "";
var paned = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
paned.add1(label1);
paned.add2(label2);
add(paned);
Timeout.add (1000, () => {
label1.set_text ("%d%%".printf ((int) (monitor.cpu_load * 100)));
processList = "";
foreach (unowned Process process in monitor.get_processes ()) {
var load = process.cpu_load * 100;
if(load > 0)
processList += "\n" + process.cmdline + "\t" + load.to_string();
label2.set_text(processList);
}
return true;
});
}
}
}
namespace Usage {
public class PowerView : View {
public PowerView () {
name = "POWER";
title = _("Power");
var label1 = new Gtk.Label("Page 1 Content.");
add(label1);
}
}
}
namespace Usage {
public class StorageView : View {
public StorageView () {
name = "STORAGE";
title = _("Storage");
var label1 = new Gtk.Label("Page 1 Content.");
add(label1);
}
}
}
using Posix;
namespace Usage {
[Compact]
public class Process {
internal uint pid;
internal string cmdline;
internal double cpu_load;
internal uint64 cpu_last_used;
internal double mem_usage;
internal bool alive;
}
public class SystemMonitor {
public double cpu_load { get; private set; }
public double mem_usage { get; private set; }
public double swap_usage { get; private set; }
uint64 cpu_last_used = 0;
uint64 cpu_last_total = 0;
const int UPDATE_INTERVAL = 100;
HashTable<uint, Process> process_table;
public List<unowned Process> get_processes () {
return process_table.get_values ();
}
public SystemMonitor () {
GTop.init ();
process_table = new HashTable<uint, Process> (direct_hash, direct_equal);
Timeout.add (UPDATE_INTERVAL, () => {
/* CPU */
GTop.Cpu cpu_data;
GTop.get_cpu (out cpu_data);
var used = cpu_data.user + cpu_data.nice + cpu_data.sys;
cpu_load = ((double) (used - cpu_last_used)) / (cpu_data.total - cpu_last_total);
/* Memory */
GTop.Mem mem;
GTop.get_mem (out mem);
mem_usage = ((double) (mem.used - mem.buffer - mem.cached)) / mem.total;
/* Swap */
GTop.Swap swap;
GTop.get_swap (out swap);
swap_usage = (double) swap.used / swap.total;
foreach (unowned Process process in process_table.get_values ()) {
process.alive = false;
}
var uid = Posix.getuid ();
GTop.Proclist proclist;
var pids = GTop.get_proclist (out proclist, GTop.KERN_PROC_UID, uid);
for (int i = 0; i < proclist.number; i++) {
GTop.ProcState proc_state;
GTop.ProcTime proc_time;
GTop.get_proc_state (out proc_state, pids[i]);
GTop.get_proc_time (out proc_time, pids[i]);
if (!(pids[i] in process_table)) {
var process = new Process ();
process.pid = pids[i];
process.alive = true;
process.cmdline = (string) proc_state.cmd;
process.cpu_load = 0;
process.cpu_last_used = proc_time.rtime;
process.mem_usage = 0;
process_table.insert (pids[i], (owned) process);
} else {
unowned Process process = process_table[pids[i]];
process.cpu_load = ((double) (proc_time.rtime - process.cpu_last_used)) / (cpu_data.total - cpu_last_total);
process.alive = true;
process.cpu_last_used = proc_time.rtime;
GTop.ProcMem proc_mem;
GTop.get_proc_mem (out proc_mem, process.pid);
process.mem_usage = (double) (proc_mem.resident - proc_mem.share) / mem.total;
}
}
foreach (unowned Process process in process_table.get_values ()) {
if (process.alive == false) {
process_table.remove (process.pid);
}
}
cpu_last_used = used;
cpu_last_total = cpu_data.total;
return true;
});
}
}
}
[CCode (cheader_filename = "glibtop.h", lower_case_cprefix = "glibtop_")]
namespace GTop {
public void init ();
[CCode (cname = "glibtop_cpu", cheader_filename = "glibtop/cpu.h")]
public struct Cpu {
uint64 flags;
uint64 total;
uint64 user;
uint64 nice;
uint64 sys;
uint64 idle;
uint64 iowait;
uint64 irq;
uint64 softirq;
uint64 frequency;
uint64 xcpu_total[32];
uint64 xcpu_user[32];
uint64 xcpu_nice[32];
uint64 xcpu_sys[32];
uint64 xcpu_idle[32];
uint64 xcpu_iowait[32];
uint64 xcpu_irq[32];
uint64 xcpu_softirq[32];
uint64 xcpu_flags;
}
public void get_cpu (out Cpu cpu);
[CCode (cname = "GLIBTOP_KERN_PROC_ALL")]
public const int KERN_PROC_ALL;
[CCode (cname = "GLIBTOP_KERN_PROC_UID")]
public const int KERN_PROC_UID;
[CCode (cname = "GLIBTOP_EXCLUDE_IDLE")]
public const int EXCLUDE_IDLE;
[CCode (cname = "glibtop_proclist", cheader_filename = "glibtop/proclist.h")]
public struct Proclist {
uint64 flags;
uint64 number;
uint64 total;
uint64 size;
}
[CCode (array_length = false, array_null_terminated = false)]
public uint[] get_proclist (out Proclist proclist, uint64 which, uint64 arg);
[CCode (cname = "glibtop_proc_state", cheader_filename = "glibtop/procstate.h")]
public struct ProcState {
uint64 flags;
char cmd[40];
uint state;
int uid;
int gid;
int ruid;
int rgid;
int has_cpu;
int processor;
int last_processor;
}
public void get_proc_state (out ProcState proc_state, uint pid);
[CCode (cname = "glibtop_proc_time", cheader_filename = "glibtop/proctime.h")]
public struct ProcTime {
uint64 flags;
uint64 start_time;
uint64 rtime;
uint64 utime;
uint64 stime;
uint64 cutime;
uint64 cstime;
uint64 timeout;
uint64 it_real_value;
uint64 frequency;
uint64 xcpu_utime[32];
uint64 xcpu_stime[32];
}
public void get_proc_time (out ProcTime proc_time, uint pid);
[CCode (cname = "glibtop_mem", cheader_filename = "glibtop/mem.h")]
public struct Mem {
uint64 flags;
uint64 total;
uint64 used;
uint64 free;
uint64 shared;
uint64 buffer;
uint64 cached;
uint64 user;
uint64 locked;
}
public void get_mem (out Mem mem);
[CCode (cname = "glibtop_swap", cheader_filename = "glibtop/swap.h")]
public struct Swap {
uint64 flags;
uint64 total;
uint64 used;
uint64 free;
uint64 pagein;
uint64 pageout;
}
public void get_swap (out Swap swap);
[CCode (cname = "glibtop_proc_mem", cheader_filename = "glibtop/procmem.h")]
public struct ProcMem {
uint64 flags;
uint64 size;
uint64 vsize;
uint64 resident;
uint64 share;
uint64 rss;
uint64 rss_rlim;
}
public void get_proc_mem (out ProcMem proc_mem, uint pid);
[CCode (cname = "glibtop_netlist", cheader_filename = "glibtop/netlist.h")]
public struct Netlist {
uint64 flags;
uint32 number;
}
[CCode (array_length = false, array_null_terminated = false)]
public string[] get_netlist (out Netlist netlist);
}
namespace Usage {
public abstract class View : Gtk.Bin {
protected SystemMonitor monitor;
public string title;
//public signal void mode_changed (string? title);
/*public virtual void go_back () {
}*/
public View () {
monitor = (GLib.Application.get_default () as Application).monitor;
visible = true;
}
}
}
namespace Usage
{
public enum UIView {
/*CPU,
MEMORY,
DISK,
NETWORK,*/
PERFORMANCE,
DATA,
STORAGE,
POWER,
N_VIEWS;
public static UIView[] all () {
return { PERFORMANCE, DATA, STORAGE, POWER }; /*CPU, MEMORY, DISK, NETWORK,*/
}
}
public class Window : Gtk.ApplicationWindow
{
public Window (Gtk.Application application)
......@@ -14,6 +30,32 @@ namespace Usage
stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT);
stack.set_transition_duration(700);
var views = new View[UIView.N_VIEWS];
/*views[UIView.CPU] = new CPUView ();
views[UIView.MEMORY] = new MemoryView ();
views[UIView.DISK] = new DiskView ();
views[UIView.NETWORK] = new NetworkView ();*/
views[UIView.PERFORMANCE] = new PerformanceView ();
views[UIView.DATA] = new DataView ();
views[UIView.STORAGE] = new StorageView ();
views[UIView.POWER] = new PowerView ();
foreach (var view in UIView.all ()) {
stack.add_titled (views[view], views[view].name, views[view].title);
/*views[view].mode_changed.connect ((title) => {
if (title == null) {
header_bar.custom_title = stack_switcher;
back_button.hide ();
} else {
header_bar.custom_title = null;
header_bar.title = title;
back_button.show ();
}
});*/
};
/*
var label1 = new Gtk.Label("Page 1 Content.");
var label5 = new Gtk.Label("Page 5 Content.");
var paned = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
......@@ -26,6 +68,7 @@ namespace Usage
stack.add_titled(label3, "page-3", "Storage");
var label4 = new Gtk.Label("Page 4 Content.");
stack.add_titled(label4, "page-4", "Power");
*/
var stack_switcher = new Gtk.StackSwitcher();
stack_switcher.halign = Gtk.Align.CENTER;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment