diff options
author | rbuj <[email protected]> | 2020-07-14 11:35:07 +0200 |
---|---|---|
committer | raveit65 <[email protected]> | 2020-07-29 19:28:08 +0200 |
commit | 3733313e90b81cb171d3ece44c537c435f42c090 (patch) | |
tree | 659c45ad69490ab6f6191b48eac88834bee8aab7 /multiload | |
parent | 1dafc4423968a404273e4a9372b42bad8390bad3 (diff) | |
download | mate-applets-3733313e90b81cb171d3ece44c537c435f42c090.tar.bz2 mate-applets-3733313e90b81cb171d3ece44c537c435f42c090.tar.xz |
multiload: Display the percentage used accurately on tooltip
Diffstat (limited to 'multiload')
-rw-r--r-- | multiload/global.h | 15 | ||||
-rw-r--r-- | multiload/linux-proc.c | 41 | ||||
-rw-r--r-- | multiload/main.c | 57 |
3 files changed, 65 insertions, 48 deletions
diff --git a/multiload/global.h b/multiload/global.h index 3f1aa69b..635c9652 100644 --- a/multiload/global.h +++ b/multiload/global.h @@ -76,11 +76,20 @@ struct _MultiloadApplet GtkWidget *notebook; int last_clicked; - long cpu_time [NCPUSTATES]; - long cpu_last [NCPUSTATES]; - int cpu_initialized; + float cpu_used_ratio; + long cpu_time [NCPUSTATES]; + long cpu_last [NCPUSTATES]; + int cpu_initialized; double loadavg1; + + float memload_user_ratio; + float memload_cache_ratio; + + float swapload_used_ratio; + + float diskload_used_ratio; + NetSpeed *netspeed_in; NetSpeed *netspeed_out; guint net_threshold1; diff --git a/multiload/linux-proc.c b/multiload/linux-proc.c index b7bdec8b..e150346e 100644 --- a/multiload/linux-proc.c +++ b/multiload/linux-proc.c @@ -55,12 +55,12 @@ GetLoad (int Maximum, int data [5], LoadGraph *g) glibtop_cpu cpu; MultiloadApplet *multiload; - multiload = g->multiload; - glibtop_get_cpu (&cpu); g_return_if_fail ((cpu.flags & needed_cpu_flags) == needed_cpu_flags); + multiload = g->multiload; + multiload->cpu_time [0] = cpu.user; multiload->cpu_time [1] = cpu.nice; multiload->cpu_time [2] = cpu.sys; @@ -81,6 +81,7 @@ GetLoad (int Maximum, int data [5], LoadGraph *g) total = usr + nice + sys + free + iowait; memcpy(multiload->cpu_last, multiload->cpu_time, sizeof multiload->cpu_last); + multiload->cpu_used_ratio = (float)(usr + nice + sys + iowait) / (float)total; usr = rint (Maximum * (float)(usr) / total); nice = rint (Maximum * (float)(nice) / total); @@ -202,6 +203,8 @@ GetDiskLoad (int Maximum, int data [3], LoadGraph *g) max = autoscaler_get_max(&scaler, readdiff + writediff); + 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]); @@ -259,17 +262,25 @@ void GetMemory (int Maximum, int data [5], LoadGraph *g) { int user, shared, buffer, cached; - glibtop_mem mem; + float user_ratio, cache_ratio; + MultiloadApplet *multiload; glibtop_get_mem (&mem); g_return_if_fail ((mem.flags & needed_mem_flags) == needed_mem_flags); - user = rint (Maximum * (float)mem.user / (float)mem.total); - shared = rint (Maximum * (float)mem.shared / (float)mem.total); - buffer = rint (Maximum * (float)mem.buffer / (float)mem.total); - cached = rint (Maximum * (float)mem.cached / (float)mem.total); + 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; @@ -282,17 +293,24 @@ void GetSwap (int Maximum, int data [2], LoadGraph *g) { int used; - + MultiloadApplet *multiload; glibtop_swap swap; glibtop_get_swap (&swap); g_return_if_fail ((swap.flags & needed_swap_flags) == needed_swap_flags); + multiload = g->multiload; + if (swap.total == 0) { used = 0; + multiload->swapload_used_ratio = 0.0f; } else { - used = rint (Maximum * (float)swap.used / swap.total); + float ratio; + + ratio = (float)swap.used / (float)swap.total; + used = rint ((float) Maximum * ratio); + multiload->swapload_used_ratio = ratio; } data [0] = used; @@ -305,15 +323,14 @@ GetLoadAvg (int Maximum, int data [2], LoadGraph *g) glibtop_loadavg loadavg; MultiloadApplet *multiload; - multiload = g->multiload; glibtop_get_loadavg (&loadavg); g_return_if_fail ((loadavg.flags & needed_loadavg_flags) == needed_loadavg_flags); - /* multiload->loadavg1 represents %used */ + multiload = g->multiload; multiload->loadavg1 = loadavg.loadavg[0]; - data [0] = rint ((float) Maximum * multiload->loadavg1); + data [0] = rint ((float) Maximum * loadavg.loadavg[0]); data [1] = Maximum - data[0]; } diff --git a/multiload/main.c b/multiload/main.c index 45756891..18f151ef 100644 --- a/multiload/main.c +++ b/multiload/main.c @@ -294,27 +294,21 @@ multiload_applet_tooltip_update(LoadGraph *g) g_assert_not_reached(); if (!strncmp(g->name, "memload", strlen("memload"))) { - guint mem_user, mem_cache, user_percent, cache_percent; - mem_user = g->data[1][0]; - mem_cache = g->data[1][1] + g->data[1][2] + g->data[1][3]; - user_percent = 100.0f * mem_user / g->draw_height; - cache_percent = 100.0f * mem_cache / g->draw_height; - user_percent = MIN(user_percent, 100); - cache_percent = MIN(cache_percent, 100); - - /* xgettext: use and cache are > 1 most of the time, - please assume that they always are. - */ - tooltip_text = g_strdup_printf(_("%s:\n" - "%u%% in use by programs\n" - "%u%% in use as cache"), - name, - user_percent, - cache_percent); + 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); + 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; @@ -328,22 +322,19 @@ multiload_applet_tooltip_update(LoadGraph *g) g_free(tx_in); g_free(tx_out); } else { - const char *msg; - guint i, total_used, percent; + float ratio = 1.0f; + float percent; - for (i = 0, total_used = 0; i < (g->n - 1); i++) - total_used += g->data[1][i]; + 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 = 100.0f * total_used / g->draw_height; - percent = MIN(percent, 100); - - msg = ngettext("%s:\n" - "%u%% in use", - "%s:\n" - "%u%% in use", - percent); - - tooltip_text = g_strdup_printf(msg, + percent = CLAMP (ratio * 100.0f, 0.0f, 100.0f); + tooltip_text = g_strdup_printf(_("%s:\n" + "%.01f%% in use"), name, percent); } |