New API to retrieve process io stats, with Linux implementation

This commit is contained in:
Robert Roth
2017-02-12 17:59:08 +02:00
parent 1a103bf142
commit dff7c5588e
21 changed files with 292 additions and 6 deletions

1
.gitignore vendored
View File

@@ -70,6 +70,7 @@ examples/smp
examples/sysdeps
examples/timings
examples/wd
examples/diskio
gtk-doc.make
install-sh
lib/GTop-2.0.gir

View File

@@ -11,7 +11,7 @@ AM_LDFLAGS = $(LIBGTOP_EXTRA_LIBS)
noinst_PROGRAMS = first second pprint procargs df netlist \
mountlist procmap netload sysdeps timings \
openfiles smp proclist free wd affinity
openfiles smp proclist free wd affinity diskio
first_SOURCES = first.c
first_LDADD = $(top_builddir)/lib/libgtop-2.0.la
@@ -43,6 +43,7 @@ pprint_LDADD = $(top_builddir)/lib/libgtop-2.0.la
procargs_SOURCES = procargs.c
procargs_LDADD = $(top_builddir)/lib/libgtop-2.0.la
df_SOURCES = df.c
df_LDADD = $(top_builddir)/lib/libgtop-2.0.la
@@ -63,3 +64,6 @@ free_LDADD = $(top_builddir)/lib/libgtop-2.0.la
affinity_SOURCES = affinity.c
affinity_LDADD = $(top_builddir)/lib/libgtop-2.0.la
diskio_SOURCES = diskio.c
diskio_LDADD = $(top_builddir)/lib/libgtop-2.0.la

39
examples/diskio.c Normal file
View File

@@ -0,0 +1,39 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glibtop.h>
#include <glibtop/procdiskio.h>
#include <stdlib.h>
#include <unistd.h>
static void show_diskio(pid_t pid)
{
glibtop_proc_diskio diskio;
glibtop_get_proc_diskio (&diskio, pid);
printf("<%ld>\t", (long)pid);
printf("rchar : %lu, wchar : %lu, read_bytes : %lu, write_bytes : %lu\n", diskio.rchar, diskio.wchar, diskio.rbytes, diskio.wbytes);
}
int main(int argc, char **argv)
{
glibtop_init();
while(*++argv)
{
pid_t pid = strtol(*argv, NULL, 10);
show_diskio(pid);
}
glibtop_close();
return 0;
}

View File

@@ -24,4 +24,5 @@ void|ppp|ushort(device)
char **|netlist
char **|proc_wd|pid_t(pid)
guint16*|proc_affinity|pid_t(pid)
void|proc_diskio|pid_t(pid)

View File

@@ -9,6 +9,7 @@ glibtop_HEADERS = close.h loadavg.h prockernel.h procstate.h \
parameter.h mountlist.h fsusage.h procmap.h signal.h \
sysinfo.h ppp.h procargs.h netload.h \
procwd.h procaffinity.h \
procdiskio.h \
netlist.h procopenfiles.h open.h
noinst_HEADERS = error.h write.h read_data.h read.h init_hooks.h machine.h \

View File

@@ -59,8 +59,9 @@ G_BEGIN_DECLS
#define GLIBTOP_CMND_PROC_OPEN_FILES 25
#define GLIBTOP_CMND_PROC_WD 26
#define GLIBTOP_CMND_PROC_AFFINITY 27
#define GLIBTOP_CMND_PROC_DISKIO 28
#define GLIBTOP_MAX_CMND 28
#define GLIBTOP_MAX_CMND 29
#define _GLIBTOP_PARAM_SIZE 16

View File

@@ -0,0 +1,90 @@
/* Copyright (C) 2017 Robert Roth
This file is part of LibGTop.
Contributed by Robert Roth <robert.roth.off@gmail.com>, February 2017.
LibGTop 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.
LibGTop 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 LibGTop; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef __GLIBTOP_PROCDISKIO_H__
#define __GLIBTOP_PROCDISKIO_H__
#include <glibtop.h>
#include <glibtop/global.h>
G_BEGIN_DECLS
#define GLIBTOP_PROC_DISKIO_RCHAR 0
#define GLIBTOP_PROC_DISKIO_WCHAR 1
#define GLIBTOP_PROC_DISKIO_RBYTES 2
#define GLIBTOP_PROC_DISKIO_WBYTES 3
#define GLIBTOP_MAX_PROC_TIME 3
typedef struct _glibtop_proc_diskio glibtop_proc_diskio;
/* Time section */
/**
* glibtop_proc_diskio:
* @rchar: The number of bytes which this task has caused to be read from storage. This
* is simply the sum of bytes which this process passed to read() and pread(), also including tty IO,
* and it is unaffected by whether or not actual physical disk IO was required.
* @wchar: The number of bytes which this task has caused, or shall cause to be written
* to disk. Similar caveats apply here as with rchar.
* @rbytes: Attempt to count the number of bytes which this process really did cause to
* be fetched from the storage layer. Done at the submit_bio() level, so it is
* accurate for block-backed filesystems.
* @wbytes: Attempt to count the number of bytes which this process caused to be sent to
* the storage layer. This is done at page-dirtying time.
*
* Process disk io data filled by glibtop_get_proc_diskio().
*
*/
struct _glibtop_proc_diskio
{
/*< private >*/
guint64 flags;
/*< public >*/
guint64 rchar;
guint64 wchar;
guint64 rbytes;
guint64 wbytes;
};
void glibtop_get_proc_diskio (glibtop_proc_diskio *buf, pid_t pid);
#if GLIBTOP_SUID_PROC_DISKIO
#define glibtop_get_proc_diskio_r glibtop_get_proc_diskio_p
#else
#define glibtop_get_proc_diskio_r glibtop_get_proc_diskio_s
#endif
void glibtop_get_proc_diskio_l (glibtop *server, glibtop_proc_diskio *buf, pid_t pid);
#if GLIBTOP_SUID_PROC_DISKIO
void _glibtop_init_proc_diskio_p (glibtop *server);
void glibtop_get_proc_diskio_p (glibtop *server, glibtop_proc_diskio *buf, pid_t pid);
#else
void _glibtop_init_proc_diskio_s (glibtop *server);
void glibtop_get_proc_diskio_s (glibtop *server, glibtop_proc_diskio *buf, pid_t pid);
#endif
G_END_DECLS
#endif

View File

@@ -53,8 +53,9 @@ G_BEGIN_DECLS
#define GLIBTOP_SYSDEPS_PROC_OPEN_FILES 24
#define GLIBTOP_SYSDEPS_PROC_WD 25
#define GLIBTOP_SYSDEPS_PROC_AFFINITY 26
#define GLIBTOP_SYSDEPS_PROC_DISKIO 27
#define GLIBTOP_MAX_SYSDEPS 27
#define GLIBTOP_MAX_SYSDEPS 28
/* The 'features' args to glibtop_init_* is an unsigned long */
G_STATIC_ASSERT((1UL << (GLIBTOP_MAX_SYSDEPS - 1)) <= ULONG_MAX);
@@ -93,6 +94,7 @@ struct _glibtop_sysdeps
guint64 ppp; /* glibtop_ppp */
guint64 proc_wd; /* glibtop_proc_wd */
guint64 proc_affinity; /* glibtop_proc_affinity */
guint64 proc_diskio; /* glibtop_proc_diskio */
};
void glibtop_get_sysdeps (glibtop_sysdeps *buf);

View File

@@ -44,6 +44,7 @@
#include <glibtop/procopenfiles.h>
#include <glibtop/procwd.h>
#include <glibtop/procaffinity.h>
#include <glibtop/procdiskio.h>
#include <glibtop/mountlist.h>
#include <glibtop/fsusage.h>
@@ -84,6 +85,7 @@ union _glibtop_union
glibtop_proc_open_files proc_open_files;
glibtop_proc_wd proc_wd;
glibtop_proc_affinity proc_affinity;
glibtop_proc_diskio proc_diskio;
};
G_END_DECLS

View File

@@ -35,7 +35,7 @@ introspection_sources = $(libgtop_2_0_la_SOURCES) lib.c ../glibtop.h ../libgtopc
../include/glibtop/procsignal.h ../include/glibtop/union.h ../include/glibtop/gnuserv.h \
../include/glibtop/parameter.h ../include/glibtop/mountlist.h ../include/glibtop/fsusage.h ../include/glibtop/procmap.h ../include/glibtop/signal.h \
../include/glibtop/sysinfo.h ../include/glibtop/ppp.h ../include/glibtop/procargs.h ../include/glibtop/netload.h \
../include/glibtop/procwd.h ../include/glibtop/procaffinity.h \
../include/glibtop/procwd.h ../include/glibtop/procaffinity.h ../include/glibtop/procdiskio.h \
../include/glibtop/netlist.h ../include/glibtop/procopenfiles.h ../include/glibtop/open.h
GTop-2.0.gir: libgtop-2.0.la

View File

@@ -68,6 +68,7 @@ glibtop_call_l (glibtop *server, unsigned command, size_t send_size,
CHECK_CMND(GLIBTOP_CMND_PROC_OPEN_FILES);
CHECK_CMND(GLIBTOP_CMND_PROC_WD);
CHECK_CMND(GLIBTOP_CMND_PROC_AFFINITY);
CHECK_CMND(GLIBTOP_CMND_PROC_DISKIO);
default:
glibtop_error_r(server, "CALL: command UNKNOWN(%d) sending %lu bytes", command, (unsigned long)send_size); break;
}

View File

@@ -52,6 +52,8 @@ glibtop_get_shm_limits
glibtop_get_shm_limits_l
glibtop_get_proc_affinity
glibtop_get_proc_affinity_l
glibtop_get_proc_diskio
glibtop_get_proc_diskio_l
glibtop_get_swap
glibtop_get_swap_l
glibtop_get_sysdeps

View File

@@ -48,7 +48,8 @@ GLIBTOP_SUID_NETLOAD +
GLIBTOP_SUID_NETLIST +
GLIBTOP_SUID_PROC_WD +
GLIBTOP_SUID_PROC_AFFINITY +
GLIBTOP_SUID_PPP;
GLIBTOP_SUID_PPP +
GLIBTOP_SUID_PROC_DISKIO;
const _glibtop_init_func_t _glibtop_init_hook_s [] = {
#if !GLIBTOP_SUID_CPU
@@ -119,6 +120,9 @@ const _glibtop_init_func_t _glibtop_init_hook_s [] = {
#endif
#if !GLIBTOP_SUID_PPP
_glibtop_init_ppp_s,
#endif
#if !GLIBTOP_SUID_PROC_DISKIO
_glibtop_init_proc_diskio_s,
#endif
NULL
};
@@ -192,6 +196,9 @@ const _glibtop_init_func_t _glibtop_init_hook_p [] = {
#endif
#if GLIBTOP_SUID_PPP
_glibtop_init_ppp_p,
#endif
#if GLIBTOP_SUID_PROC_DISKIO
_glibtop_init_proc_diskio_p,
#endif
NULL
};

View File

@@ -221,6 +221,12 @@ handle_parent_connection (int s)
do_output (s, resp, _offset_data (fsusage),
0, NULL);
break;
case GLIBTOP_CMND_PROC_DISKIO:
glibtop_get_proc_diskio_l
(server, &resp->u.data.proc_diskio, parameter);
do_output (s, resp, _offset_data (proc_diskio),
0, NULL);
break;
case GLIBTOP_CMND_PPP:
memcpy (&device, parameter, sizeof (device));
glibtop_get_ppp_l

View File

@@ -431,3 +431,18 @@ glibtop_get_proc_affinity(glibtop_proc_affinity *buf, pid_t pid)
{
return glibtop_get_proc_affinity_l(glibtop_global_server, buf, pid);
}
/**
* glibtop_get_proc_diskio: Get the disk io stats for the given pid
* @buf: Buffer where the result will be given
* @pid: Process id to get the io stats for
*
* Get the io stats for a process
*
* Returns: A list of processor ID of 'buf.number' elements.
*/
void
glibtop_get_proc_diskio(glibtop_proc_diskio *buf, pid_t pid)
{
return glibtop_get_proc_diskio_l(glibtop_global_server, buf, pid);
}

View File

@@ -89,6 +89,9 @@ const _glibtop_init_func_t _glibtop_init_hook_p [] = {
#endif
#if GLIBTOP_SUID_PPP
_glibtop_init_ppp_p,
#endif
#if GLIBTOP_SUID_PROC_DISKIO
_glibtop_init_proc_diskio_p,
#endif
NULL
};

View File

@@ -10,7 +10,7 @@ libgtop_sysdeps_2_0_la_SOURCES = open.c close.c cpu.c mem.c swap.c \
proctime.c procmem.c procsignal.c prockernel.c \
procsegment.c procargs.c procmap.c siglist.c \
sysinfo.c netload.c ppp.c glibtop_private.c \
mountlist.c procaffinity.c \
mountlist.c procaffinity.c procdiskio.c \
fsusage.c netlist.c procopenfiles.c procwd.c
libgtop_sysdeps_2_0_la_LIBADD = @GLIB_LIBS@

View File

@@ -46,5 +46,6 @@
#define GLIBTOP_SUID_PROC_AFFINITY 0
#define GLIBTOP_SUID_PPP 0
#define GLIBTOP_SUID_PROC_FILE 0
#define GLIBTOP_SUID_PROC_DISKIO 0
#endif /* __LINUX__GLIBTOP_SERVER_H__ */

View File

@@ -0,0 +1,66 @@
/* Copyright (C) 2017 Robert Roth
This file is part of LibGTop.
Contributed by Robert Roth <robert.roth.off@gmail.com>, February 2017.
LibGTop 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.
LibGTop 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 LibGTop; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procdiskio.h>
#include "glibtop_private.h"
static const unsigned long _glibtop_sysdeps_proc_diskio =
(1L << GLIBTOP_PROC_DISKIO_RCHAR) + (1L << GLIBTOP_PROC_DISKIO_WCHAR) +
(1L << GLIBTOP_PROC_DISKIO_RBYTES) + (1L << GLIBTOP_PROC_DISKIO_WBYTES);
/* Init function. */
void
_glibtop_init_proc_diskio_s (glibtop *server)
{
server->sysdeps.proc_diskio = _glibtop_sysdeps_proc_diskio;
}
/* Provides detailed information about a process. */
void
glibtop_get_proc_diskio_s (glibtop *server, glibtop_proc_diskio *buf, pid_t pid)
{
char buffer [BUFSIZ], *p;
memset (buf, 0, sizeof (glibtop_proc_diskio));
if (proc_file_to_buffer(buffer, sizeof buffer, "/proc/%d/io", pid))
return;
p = skip_token (buffer);
buf->rchar = g_ascii_strtoull (p, &p, 10);
p = skip_line (p);
p = skip_token (p);
buf->wchar = g_ascii_strtoull (p, &p, 10);
p = skip_line (p);
p = skip_line (p);
p = skip_line (p);
p = skip_token (p);
buf->rbytes = g_ascii_strtoull (p, &p, 10);
p = skip_line (p);
p = skip_token (p);
buf->wbytes = g_ascii_strtoull (p, &p, 10);
}

43
sysdeps/stub/procdiskio.c Normal file
View File

@@ -0,0 +1,43 @@
/* Copyright (C) 2017 Robert Roth
This file is part of LibGTop.
Contributed by Robert Roth <robert.roth.off@gmail.com>, February 2017.
LibGTop 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.
LibGTop 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 LibGTop; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <glibtop.h>
#include <glibtop/procdiskio.h>
static const unsigned long _glibtop_sysdeps_proc_diskio = 0;
/* Init function. */
void
_glibtop_init_proc_diskio_s (glibtop *server)
{
server->sysdeps.proc_diskio = _glibtop_sysdeps_proc_diskio;
}
/* Provides detailed information about a process. */
void
glibtop_get_proc_diskio_s (glibtop *server, glibtop_proc_diskio *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_diskio));
}

View File

@@ -45,6 +45,7 @@ G_BEGIN_DECLS
#define GLIBTOP_SUID_NETLOAD (1 << GLIBTOP_SYSDEPS_NETLOAD)
#define GLIBTOP_SUID_NETLIST 0
#define GLIBTOP_SUID_PPP (1 << GLIBTOP_SYSDEPS_PPP)
#define GLIBTOP_SUID_PROC_DISKIO (1 << GLIBTOP_SYSDEPS_PROC_DISKIO)
G_END_DECLS