[gnome-applets/wip/muktupavels/issue-12] multiload: redo disk I/O monitoring



commit 4f181b79f32e38056a099adeacd5f001971ec12b
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sun Apr 5 16:36:42 2020 +0300

    multiload: redo disk I/O monitoring
    
    https://gitlab.gnome.org/GNOME/gnome-applets/-/issues/12

 gnome-applets/multiload/Makefile.am     |  2 +
 gnome-applets/multiload/linux-proc.c    | 33 +-----------
 gnome-applets/multiload/ma-disk-stats.c | 96 +++++++++++++++++++++++++++++++++
 gnome-applets/multiload/ma-disk-stats.h | 30 +++++++++++
 4 files changed, 130 insertions(+), 31 deletions(-)
---
diff --git a/gnome-applets/multiload/Makefile.am b/gnome-applets/multiload/Makefile.am
index 8c0e7703d..da7cbd243 100644
--- a/gnome-applets/multiload/Makefile.am
+++ b/gnome-applets/multiload/Makefile.am
@@ -25,6 +25,8 @@ libmultiload_applet_la_SOURCES = \
        linux-proc.h \
        load-graph.h \
        load-graph.c \
+       ma-disk-stats.c \
+       ma-disk-stats.h \
        multiload-applet.c \
        multiload-applet.h \
        netspeed.c \
diff --git a/gnome-applets/multiload/linux-proc.c b/gnome-applets/multiload/linux-proc.c
index b4d8efcfb..bd1499aa7 100644
--- a/gnome-applets/multiload/linux-proc.c
+++ b/gnome-applets/multiload/linux-proc.c
@@ -13,10 +13,10 @@
 #include <glibtop/loadavg.h>
 #include <glibtop/netload.h>
 #include <glibtop/netlist.h>
-#include <glibtop/mountlist.h>
 #include <glibtop/fsusage.h>
 
 #include "linux-proc.h"
+#include "ma-disk-stats.h"
 #include "autoscaler.h"
 
 static const unsigned needed_cpu_flags =
@@ -100,47 +100,18 @@ GetDiskLoad (int Maximum, int data [3], LoadGraph *g)
        static guint64 lastread = 0, lastwrite = 0;
        static AutoScaler scaler;
 
-       glibtop_mountlist mountlist;
-       glibtop_mountentry *mountentries;
-       guint i;
        int max;
 
        guint64 read, write;
        guint64 readdiff, writediff;
 
-
        if(first_call)
        {
            autoscaler_init(&scaler, 60, 500);
        }
 
        read = write = 0;
-
-       mountentries = glibtop_get_mountlist (&mountlist, FALSE);
-
-       for (i = 0; i < mountlist.number; i++)
-       {
-               struct statvfs statresult;
-               glibtop_fsusage fsusage;
-
-               if (strcmp(mountentries[i].type, "smbfs") == 0
-                   || strcmp(mountentries[i].type, "nfs") == 0
-                   || strcmp(mountentries[i].type, "cifs") == 0)
-                       continue;
-
-
-               if (statvfs (mountentries[i].mountdir, &statresult) < 0)
-               {
-                       g_debug ("Failed to get statistics for mount entry: %s. Reason: %s. Skipping entry.",
-                                mountentries[i].mountdir, strerror(errno));
-                       continue;
-               }
-
-               glibtop_get_fsusage(&fsusage, mountentries[i].mountdir);
-               read += fsusage.read; write += fsusage.write;
-       }
-
-       g_free(mountentries);
+       ma_disk_stats_get_usage (&read, &write);
 
        readdiff  = read - lastread;
        writediff = write - lastwrite;
diff --git a/gnome-applets/multiload/ma-disk-stats.c b/gnome-applets/multiload/ma-disk-stats.c
new file mode 100644
index 000000000..472c3c1bc
--- /dev/null
+++ b/gnome-applets/multiload/ma-disk-stats.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2020 Alberts Muktupāvels
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "ma-disk-stats.h"
+
+#include <stdio.h>
+
+static void
+get_usage (const char    *filename,
+           unsigned long *sectors_read,
+           unsigned long *sectors_written)
+{
+  FILE *file;
+  int result;
+  unsigned long read_ios;
+  unsigned long read_sectors;
+  unsigned long write_ios;
+  unsigned long write_sectors;
+
+  *sectors_read = 0;
+  *sectors_written = 0;
+
+  file = fopen (filename, "r");
+  if (file == NULL)
+    return;
+
+  result = fscanf (file,
+                   /* read I/Os, merges, sectors, ticks */
+                   "%lu %*u %lu %*u "
+                   /* write I/Os, merges, sectors, ticks */
+                   "%lu %*u %lu %*u "
+                   /* in flight, io ticks, time in queue */
+                   "%*u %*u %*u "
+                   /* discard I/Os, merges, sectors, ticks */
+                   "%*u %*u %*u %*u "
+                   /* flush I/Os, flush ticks, */
+                   "%*u %*u",
+                   &read_ios,
+                   &read_sectors,
+                   &write_ios,
+                   &write_sectors);
+
+  if (result == 4 && read_ios != 0 && write_ios != 0)
+    {
+      *sectors_read = read_sectors;
+      *sectors_written = write_sectors;
+    }
+
+  fclose (file);
+}
+
+void
+ma_disk_stats_get_usage (unsigned long *sectors_read,
+                         unsigned long *sectors_written)
+{
+  GDir *dir;
+  const char *name;
+
+  *sectors_read = 0;
+  *sectors_written = 0;
+
+  dir = g_dir_open ("/sys/block", 0, NULL);
+  if (dir == NULL)
+    return;
+
+  while ((name = g_dir_read_name (dir)) != 0)
+    {
+      char *filename;
+      unsigned long block_sectors_read;
+      unsigned long block_sectors_written;
+
+      filename = g_strdup_printf ("/sys/block/%s/stat", name);
+      get_usage (filename, &block_sectors_read, &block_sectors_written);
+      g_free (filename);
+
+      *sectors_read += block_sectors_read;
+      *sectors_written += block_sectors_written;
+    }
+
+  g_dir_close (dir);
+}
diff --git a/gnome-applets/multiload/ma-disk-stats.h b/gnome-applets/multiload/ma-disk-stats.h
new file mode 100644
index 000000000..212ec2c2c
--- /dev/null
+++ b/gnome-applets/multiload/ma-disk-stats.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2020 Alberts Muktupāvels
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MA_DISK_STATS_H
+#define MA_DISK_STATS_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void ma_disk_stats_get_usage (unsigned long *sectors_read,
+                              unsigned long *sectors_written);
+
+G_END_DECLS
+
+#endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]