From c7077cfd71cc74222db699a910f8507c5c68b98c Mon Sep 17 00:00:00 2001 From: rbuj Date: Sun, 26 Jul 2020 16:21:46 +0200 Subject: multiload: Use the id instead of the name to identify the graph type --- multiload/global.h | 56 ++++++++++++++--- multiload/linux-proc.c | 121 ++++++++++++++++++----------------- multiload/linux-proc.h | 14 ++--- multiload/load-graph.c | 4 +- multiload/main.c | 168 +++++++++++++++++++++++++------------------------ multiload/properties.c | 8 +-- 6 files changed, 211 insertions(+), 160 deletions(-) diff --git a/multiload/global.h b/multiload/global.h index 635c9652..916c9946 100644 --- a/multiload/global.h +++ b/multiload/global.h @@ -10,8 +10,6 @@ G_BEGIN_DECLS -#define NCPUSTATES 5 -#define NGRAPHS 6 #define MIN_NET_THRESHOLD1 10 #define MIN_NET_THRESHOLD2 11 #define MIN_NET_THRESHOLD3 12 @@ -25,6 +23,47 @@ typedef void (*LoadGraphDataFunc) (int, int [], LoadGraph *); #include "netspeed.h" +typedef enum { + graph_cpuload = 0, + graph_memload, + graph_netload2, + graph_swapload, + graph_loadavg, + graph_diskload, + graph_n, +} E_graph; + +typedef enum { + memload_user = 0, + memload_shared, + memload_buffer, + memload_cached, + memload_free, + memload_n +} E_memload; + +typedef enum { + cpuload_usr = 0, + cpuload_sys, + cpuload_nice, + cpuload_iowait, + cpuload_free, + cpuload_n +} E_cpuload; + +typedef enum { + diskload_read = 0, + diskload_write, + diskload_free, + diskload_n +} E_diskload; + +typedef enum { + swapload_used = 0, + swapload_free, + swapload_n +} E_swapload; + struct _LoadGraph { MultiloadApplet *multiload; @@ -59,7 +98,7 @@ struct _MultiloadApplet GSettings *settings; - LoadGraph *graphs [NGRAPHS]; + LoadGraph *graphs [graph_n]; GtkWidget *box; @@ -71,20 +110,21 @@ struct _MultiloadApplet gboolean view_diskload; GtkWidget *about_dialog; - GtkWidget *check_boxes [NGRAPHS]; + GtkWidget *check_boxes [graph_n]; GtkWidget *prop_dialog; GtkWidget *notebook; int last_clicked; float cpu_used_ratio; - long cpu_time [NCPUSTATES]; - long cpu_last [NCPUSTATES]; + long cpu_time [cpuload_n]; + long cpu_last [cpuload_n]; int cpu_initialized; double loadavg1; - float memload_user_ratio; - float memload_cache_ratio; + guint64 memload_user; + guint64 memload_cache; + guint64 memload_total; float swapload_used_ratio; diff --git a/multiload/linux-proc.c b/multiload/linux-proc.c index e150346e..ea417421 100644 --- a/multiload/linux-proc.c +++ b/multiload/linux-proc.c @@ -46,14 +46,16 @@ static const unsigned needed_netload_flags = (1 << GLIBTOP_NETLOAD_IF_FLAGS) + (1 << GLIBTOP_NETLOAD_BYTES_TOTAL); - void -GetLoad (int Maximum, int data [5], LoadGraph *g) +GetLoad (int Maximum, + int data [cpuload_n], + LoadGraph *g) { - int usr, nice, sys, iowait, free; - int total; - glibtop_cpu cpu; MultiloadApplet *multiload; + glibtop_cpu cpu; + long cpu_aux [cpuload_n], used = 0, total = 0; + int current_scaled, used_scaled = 0; + int i; glibtop_get_cpu (&cpu); @@ -61,43 +63,39 @@ GetLoad (int Maximum, int data [5], LoadGraph *g) multiload = g->multiload; - multiload->cpu_time [0] = cpu.user; - multiload->cpu_time [1] = cpu.nice; - multiload->cpu_time [2] = cpu.sys; - multiload->cpu_time [3] = cpu.iowait + cpu.irq + cpu.softirq; - multiload->cpu_time [4] = cpu.idle; + multiload->cpu_time [cpuload_usr] = cpu.user; + multiload->cpu_time [cpuload_nice] = cpu.nice; + multiload->cpu_time [cpuload_sys] = cpu.sys; + multiload->cpu_time [cpuload_iowait] = cpu.iowait + cpu.irq + cpu.softirq; + multiload->cpu_time [cpuload_free] = cpu.idle; if (!multiload->cpu_initialized) { memcpy (multiload->cpu_last, multiload->cpu_time, sizeof (multiload->cpu_last)); multiload->cpu_initialized = 1; } - usr = multiload->cpu_time [0] - multiload->cpu_last [0]; - nice = multiload->cpu_time [1] - multiload->cpu_last [1]; - sys = multiload->cpu_time [2] - multiload->cpu_last [2]; - iowait = multiload->cpu_time [3] - multiload->cpu_last [3]; - free = multiload->cpu_time [4] - multiload->cpu_last [4]; - - total = usr + nice + sys + free + iowait; + for (i = 0; i < cpuload_n; i++) { + cpu_aux [i] = multiload->cpu_time [i] - multiload->cpu_last [i]; + total += cpu_aux [i]; + } - memcpy(multiload->cpu_last, multiload->cpu_time, sizeof multiload->cpu_last); - multiload->cpu_used_ratio = (float)(usr + nice + sys + iowait) / (float)total; + for (i = 0; i < cpuload_free; i++) { + used += cpu_aux [i]; + current_scaled = rint ((float)(cpu_aux [i] * Maximum) / (float)total); + used_scaled += current_scaled; + data [i] = current_scaled; + } + data [cpuload_free] = Maximum - used_scaled; - usr = rint (Maximum * (float)(usr) / total); - nice = rint (Maximum * (float)(nice) / total); - sys = rint (Maximum * (float)(sys) / total); - iowait = rint (Maximum * (float)(iowait) / total); - free = Maximum - usr - nice - sys - iowait; + multiload->cpu_used_ratio = (float)(used) / (float)total; - data [0] = usr; - data [1] = sys; - data [2] = nice; - data [3] = iowait; - data [4] = free; + memcpy (multiload->cpu_last, multiload->cpu_time, sizeof multiload->cpu_last); } void -GetDiskLoad (int Maximum, int data [3], LoadGraph *g) +GetDiskLoad (int Maximum, + int data [diskload_n], + LoadGraph *g) { static gboolean first_call = TRUE; static guint64 lastread = 0, lastwrite = 0; @@ -205,9 +203,9 @@ GetDiskLoad (int Maximum, int data [3], LoadGraph *g) multiload->diskload_used_ratio = (float)(readdiff + writediff) / (float)max; - data[0] = (float)Maximum * readdiff / (float)max; - data[1] = (float)Maximum * writediff / (float)max; - data[2] = (float)Maximum - (data [0] + data[1]); + data [diskload_read] = rint ((float)Maximum * (float)readdiff / (float)max); + data [diskload_write] = rint ((float)Maximum * (float)writediff / (float)max); + data [diskload_free] = Maximum - (data [0] + data[1]); } #if 0 @@ -259,38 +257,45 @@ GetPage (int Maximum, int data [3], LoadGraph *g) #endif /* 0 */ void -GetMemory (int Maximum, int data [5], LoadGraph *g) +GetMemory (int Maximum, + int data [memload_n], + LoadGraph *g) { - int user, shared, buffer, cached; - glibtop_mem mem; - float user_ratio, cache_ratio; MultiloadApplet *multiload; + glibtop_mem mem; + guint64 aux [memload_n], cache = 0; + int current_scaled, used_scaled = 0; + int i; glibtop_get_mem (&mem); g_return_if_fail ((mem.flags & needed_mem_flags) == needed_mem_flags); - user_ratio = (float)mem.user / (float)mem.total; - cache_ratio = (float)(mem.shared + mem.buffer + mem.cached) / (float)mem.total; - multiload = g->multiload; - multiload->memload_user_ratio = user_ratio; - multiload->memload_cache_ratio = cache_ratio; - - user = rint ((float)Maximum * user_ratio); - shared = rint ((float)Maximum * (float)mem.shared / (float)mem.total); - buffer = rint ((float)Maximum * (float)mem.buffer / (float)mem.total); - cached = rint ((float)Maximum * (float)mem.cached / (float)mem.total); - - data [0] = user; - data [1] = shared; - data [2] = buffer; - data [3] = cached; - data [4] = Maximum-user-shared-buffer-cached; + multiload->memload_user = mem.user; + multiload->memload_cache = mem.cached; + multiload->memload_total = mem.total; + + aux [memload_user] = mem.user; + aux [memload_shared] = mem.shared; + aux [memload_buffer] = mem.buffer; + aux [memload_cached] = mem.cached; + + for (i = 0; i < memload_free; i++) { + current_scaled = rint ((float)(aux [i] * Maximum) / (float)mem.total); + if (i != memload_user) { + cache += aux [i]; + } + used_scaled += current_scaled; + data [i] = current_scaled; + } + data [memload_free] = MAX (Maximum - used_scaled, 0); } void -GetSwap (int Maximum, int data [2], LoadGraph *g) +GetSwap (int Maximum, + int data [swapload_n], + LoadGraph *g) { int used; MultiloadApplet *multiload; @@ -318,7 +323,9 @@ GetSwap (int Maximum, int data [2], LoadGraph *g) } void -GetLoadAvg (int Maximum, int data [2], LoadGraph *g) +GetLoadAvg (int Maximum, + int data [2], + LoadGraph *g) { glibtop_loadavg loadavg; MultiloadApplet *multiload; @@ -376,7 +383,9 @@ is_net_device_virtual(char *device) } void -GetNet (int Maximum, int data [4], LoadGraph *g) +GetNet (int Maximum, + int data [4], + LoadGraph *g) { enum Types { IN_COUNT = 0, diff --git a/multiload/linux-proc.h b/multiload/linux-proc.h index fdcb1359..b64a7e8a 100644 --- a/multiload/linux-proc.h +++ b/multiload/linux-proc.h @@ -3,14 +3,14 @@ #include -G_GNUC_INTERNAL void GetLoad (int Maximum, int data [5], LoadGraph *g); -G_GNUC_INTERNAL void GetDiskLoad (int Maximum, int data [3], LoadGraph *g); +G_GNUC_INTERNAL void GetLoad (int Maximum, int data [cpuload_n], LoadGraph *g); +G_GNUC_INTERNAL void GetDiskLoad (int Maximum, int data [diskload_n], LoadGraph *g); #if 0 -G_GNUC_INTERNAL void GetPage (int Maximum, int data [3], LoadGraph *g); +G_GNUC_INTERNAL void GetPage (int Maximum, int data [3], LoadGraph *g); #endif /* 0 */ -G_GNUC_INTERNAL void GetMemory (int Maximum, int data [4], LoadGraph *g); -G_GNUC_INTERNAL void GetSwap (int Maximum, int data [2], LoadGraph *g); -G_GNUC_INTERNAL void GetLoadAvg (int Maximum, int data [2], LoadGraph *g); -G_GNUC_INTERNAL void GetNet (int Maximum, int data [4], LoadGraph *g); +G_GNUC_INTERNAL void GetMemory (int Maximum, int data [memload_n], LoadGraph *g); +G_GNUC_INTERNAL void GetSwap (int Maximum, int data [swapload_n], LoadGraph *g); +G_GNUC_INTERNAL void GetLoadAvg (int Maximum, int data [2], LoadGraph *g); +G_GNUC_INTERNAL void GetNet (int Maximum, int data [4], LoadGraph *g); #endif diff --git a/multiload/load-graph.c b/multiload/load-graph.c index bf004394..583803f9 100644 --- a/multiload/load-graph.c +++ b/multiload/load-graph.c @@ -70,7 +70,7 @@ load_graph_draw (LoadGraph *g) cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); /* all graphs except Load and Net go this path */ - if (g->id != 4 && g->id != 2) + if (g->id != graph_loadavg && g->id != graph_netload2) { for (i = 0; i < g->draw_width; i++) g->pos [i] = g->draw_height - 1; @@ -92,7 +92,7 @@ load_graph_draw (LoadGraph *g) } } /* This is for network graph */ - else if (g->id == 2) + else if (g->id == graph_netload2) { guint maxnet = 1; gint segments = 1; diff --git a/multiload/main.c b/multiload/main.c index 18f151ef..17ac1806 100644 --- a/multiload/main.c +++ b/multiload/main.c @@ -195,7 +195,7 @@ multiload_destroy_cb(GtkWidget *widget, gpointer data) gint i; MultiloadApplet *ma = data; - for (i = 0; i < NGRAPHS; i++) + for (i = 0; i < graph_n; i++) { load_graph_stop(ma->graphs[i]); if (ma->graphs[i]->colors) @@ -269,97 +269,99 @@ multiload_key_press_event_cb (GtkWidget *widget, GdkEventKey *event, MultiloadAp void multiload_applet_tooltip_update(LoadGraph *g) { - gchar *tooltip_text, *name; + gchar *tooltip_text; MultiloadApplet *multiload; + const char *tooltip_label [graph_n] = { + [graph_cpuload] = N_("Processor"), + [graph_memload] = N_("Memory"), + [graph_netload2] = N_("Network"), + [graph_swapload] = N_("Swap Space"), + [graph_loadavg] = N_("Load Average"), + [graph_diskload] = N_("Disk") + }; + const char *name; g_assert(g); - g_assert(g->name); multiload = g->multiload; /* label the tooltip intuitively */ - if (!strncmp(g->name, "cpuload", strlen("cpuload"))) - name = g_strdup(_("Processor")); - else if (!strncmp(g->name, "memload", strlen("memload"))) - name = g_strdup(_("Memory")); - else if (!strncmp(g->name, "netload2", strlen("netload2"))) - name = g_strdup(_("Network")); - else if (!strncmp(g->name, "swapload", strlen("swapload"))) - name = g_strdup(_("Swap Space")); - else if (!strncmp(g->name, "loadavg", strlen("loadavg"))) - name = g_strdup(_("Load Average")); - else if (!strncmp (g->name, "diskload", strlen("diskload"))) - name = g_strdup(_("Disk")); - else - g_assert_not_reached(); - - if (!strncmp(g->name, "memload", strlen("memload"))) { - float user_percent, cache_percent; - - user_percent = MIN (multiload->memload_user_ratio * 100.0f, 100.0f); - cache_percent = MIN (multiload->memload_cache_ratio * 100.0f, 100.0f); - tooltip_text = g_strdup_printf (_("%s:\n" - "%.01f%% in use by programs\n" - "%.01f%% in use as cache"), - name, - user_percent, - cache_percent); - - } else if (!strcmp(g->name, "loadavg")) { - - tooltip_text = g_strdup_printf (_("The system load average is %0.02f"), - multiload->loadavg1); - - } else if (!strcmp(g->name, "netload2")) { - char *tx_in, *tx_out; - tx_in = netspeed_get(multiload->netspeed_in); - tx_out = netspeed_get(multiload->netspeed_out); - /* xgettext: same as in graphic tab of g-s-m */ - tooltip_text = g_strdup_printf(_("%s:\n" - "Receiving %s\n" - "Sending %s"), - name, tx_in, tx_out); - g_free(tx_in); - g_free(tx_out); - } else { - float ratio = 1.0f; - float percent; - - if (!strcmp(g->name, "cpuload")) - ratio = multiload->cpu_used_ratio; - else if (!strcmp(g->name, "swapload")) - ratio = multiload->swapload_used_ratio; - else if (!strcmp (g->name, "diskload")) - ratio = multiload->diskload_used_ratio; - - percent = CLAMP (ratio * 100.0f, 0.0f, 100.0f); - tooltip_text = g_strdup_printf(_("%s:\n" - "%.01f%% in use"), - name, - percent); + name = gettext (tooltip_label [g->id]); + + switch (g->id) { + case graph_memload: { + float user_percent, cache_percent; + + user_percent = MIN ((float)(100 * multiload->memload_user) / (float)(multiload->memload_total), 100.0f); + cache_percent = MIN ((float)(100 * multiload->memload_cache) / (float)(multiload->memload_total), 100.0f); + tooltip_text = g_strdup_printf (_("%s:\n" + "%.01f%% in use by programs\n" + "%.01f%% in use as cache"), + name, + user_percent, + cache_percent); + break; + } + case graph_loadavg: { + tooltip_text = g_strdup_printf (_("The system load average is %0.02f"), + multiload->loadavg1); + break; + } + case graph_netload2: { + char *tx_in, *tx_out; + + tx_in = netspeed_get(multiload->netspeed_in); + tx_out = netspeed_get(multiload->netspeed_out); + /* xgettext: same as in graphic tab of g-s-m */ + tooltip_text = g_strdup_printf(_("%s:\n" + "Receiving %s\n" + "Sending %s"), + name, tx_in, tx_out); + g_free(tx_in); + g_free(tx_out); + break; + } + default: { + float ratio; + float percent; + + if (g->id == graph_cpuload) + ratio = multiload->cpu_used_ratio; + else if (g->id == graph_swapload) + ratio = multiload->swapload_used_ratio; + else if (g->id == graph_diskload) + ratio = multiload->diskload_used_ratio; + else + g_assert_not_reached (); + + percent = CLAMP (ratio * 100.0f, 0.0f, 100.0f); + tooltip_text = g_strdup_printf(_("%s:\n" + "%.01f%% in use"), + name, + percent); + } } gtk_widget_set_tooltip_text(g->disp, tooltip_text); g_free(tooltip_text); - g_free(name); } static void multiload_create_graphs(MultiloadApplet *ma) { struct { const char *label; - const char *name; - int num_colours; - LoadGraphDataFunc callback; - } graph_types[] = { - { _("CPU Load"), "cpuload", 5, GetLoad }, - { _("Memory Load"), "memload", 5, GetMemory }, - { _("Net Load"), "netload2", 6, GetNet }, - { _("Swap Load"), "swapload", 2, GetSwap }, - { _("Load Average"), "loadavg", 3, GetLoadAvg }, - { _("Disk Load"), "diskload", 3, GetDiskLoad } - }; + const char *name; + int num_colours; + LoadGraphDataFunc callback; + } graph_types [graph_n] = { + [graph_cpuload] = { _("CPU Load"), "cpuload", cpuload_n, GetLoad }, + [graph_memload] = { _("Memory Load"), "memload", memload_n, GetMemory }, + [graph_netload2] = { _("Net Load"), "netload2", 6, GetNet }, + [graph_swapload] = { _("Swap Load"), "swapload", swapload_n, GetSwap }, + [graph_loadavg] = { _("Load Average"), "loadavg", 3, GetLoadAvg }, + [graph_diskload] = { _("Disk Load"), "diskload", diskload_n, GetDiskLoad } + }; gint speed, size; guint net_threshold1; @@ -383,7 +385,7 @@ multiload_create_graphs(MultiloadApplet *ma) speed = MAX (speed, 50); size = CLAMP (size, 10, 400); - for (i = 0; i < G_N_ELEMENTS (graph_types); i++) + for (i = 0; i < graph_n; i++) { gboolean visible; char *key; @@ -391,7 +393,7 @@ multiload_create_graphs(MultiloadApplet *ma) /* This is a special case to handle migration from an * older version of netload to a newer one in the * 2.25.1 release. */ - if (g_strcmp0 ("netload2", graph_types[i].name) == 0) { + if (i == graph_netload2) { key = g_strdup ("view-netload"); } else { key = g_strdup_printf ("view-%s", graph_types[i].name); @@ -411,14 +413,14 @@ multiload_create_graphs(MultiloadApplet *ma) } /* for Network graph, colors[4] is grid line color, it should not be used in loop in load-graph.c */ /* for Network graph, colors[5] is indicator color, it should not be used in loop in load-graph.c */ - ma->graphs[2]->n = 4; + ma->graphs[graph_netload2]->n = 4; ma->net_threshold1 = net_threshold1; ma->net_threshold2 = net_threshold2; ma->net_threshold3 = net_threshold3; - ma->netspeed_in = netspeed_new(ma->graphs[2]); - ma->netspeed_out = netspeed_new(ma->graphs[2]); + ma->netspeed_in = netspeed_new (ma->graphs [graph_netload2]); + ma->netspeed_out = netspeed_new (ma->graphs [graph_netload2]); /* for Load graph, colors[2] is grid line color, it should not be used in loop in load-graph.c */ - ma->graphs[4]->n = 2; + ma->graphs[graph_loadavg]->n = 2; } /* remove the old graphs and rebuild them */ @@ -429,7 +431,7 @@ multiload_applet_refresh(MultiloadApplet *ma) MatePanelAppletOrient orientation; /* stop and free the old graphs */ - for (i = 0; i < NGRAPHS; i++) + for (i = 0; i < graph_n; i++) { if (!ma->graphs[i]) continue; @@ -455,12 +457,12 @@ multiload_applet_refresh(MultiloadApplet *ma) gtk_container_add(GTK_CONTAINER(ma->applet), ma->box); - /* create the NGRAPHS graphs, passing in their user-configurable properties with gsettings. */ + /* create the N graphs, passing in their user-configurable properties with gsettings. */ multiload_create_graphs (ma); /* only start and display the graphs the user has turned on */ - for (i = 0; i < NGRAPHS; i++) { + for (i = 0; i < graph_n; i++) { gtk_box_pack_start(GTK_BOX(ma->box), ma->graphs[i]->main_widget, TRUE, TRUE, 1); diff --git a/multiload/properties.c b/multiload/properties.c index d8299cde..e9ad76a9 100644 --- a/multiload/properties.c +++ b/multiload/properties.c @@ -64,7 +64,7 @@ properties_set_insensitive(MultiloadApplet *ma) total_graphs = 0; last_graph = 0; - for (i = 0; i < NGRAPHS; i++) + for (i = 0; i < graph_n; i++) if (ma->graphs[i]->visible) { last_graph = i; @@ -122,7 +122,7 @@ property_toggled_cb(GtkWidget *widget, gpointer name) if (active) { - for (i = 0; i < NGRAPHS; i++) + for (i = 0; i < graph_n; i++) soft_set_sensitive(ma->check_boxes[i], TRUE); gtk_widget_show_all (ma->graphs[prop_type]->main_widget); ma->graphs[prop_type]->visible = TRUE; @@ -154,7 +154,7 @@ spin_button_changed_cb(GtkWidget *widget, gpointer name) { case PROP_SPEED: g_settings_set_int (ma->settings, (gchar *)name, value); - for (i = 0; i < NGRAPHS; i++) + for (i = 0; i < graph_n; i++) { load_graph_stop(ma->graphs[i]); ma->graphs[i]->speed = value; @@ -165,7 +165,7 @@ spin_button_changed_cb(GtkWidget *widget, gpointer name) break; case PROP_SIZE: - for (i = 0; i < NGRAPHS; i++) + for (i = 0; i < graph_n; i++) { g_settings_set_int (ma->settings, (gchar *)name, value); ma->graphs[i]->size = value ; -- cgit v1.2.1