Added Sigar.getProcDiskIO to Java API on Linux

Returns the total bytes read and written for a process using /proc/pid/io.
This commit is contained in:
nettag 2013-07-30 10:37:01 +03:00
parent ec489fb336
commit a62558af50
7 changed files with 122 additions and 1 deletions

View File

@ -627,6 +627,18 @@ use vars qw(%classes %cmds);
plat => '*'
},
],
ProcDiskIO => [
{
name => 'bytes_read', type => 'Long',
desc => 'Bytes Read',
plat => 'L'
},
{
name => 'bytes_written', type => 'Long',
desc => 'Bytes Written',
plat => 'L'
}
],
ProcState => [
{
name => 'state', type => 'Char',

View File

@ -440,7 +440,7 @@ public class Sigar implements SigarProxy {
return this.processFinder.findSingleProcess(pid);
}
}
/**
* Get process memory info.
* @param pid The process id.
@ -638,6 +638,29 @@ public class Sigar implements SigarProxy {
Integer.parseInt(port));
}
/**
* Get process disk IO info.
* @param pid THe process id.
* @exception SigarException on failure.
*/
public ProcDiskIO getProcDiskIO(long pid) throws SigarException {
try {
return ProcDiskIO.fetch(this, pid);
} catch (UnsatisfiedLinkError linkErrorException) {
// We want to handle exceptions gracefully even if the linked
// shared library is older and isn't compiled with the ProcDiskIO APIs.
// The downside of this is that we throw SigarNotImplemented exception
// also when the shared library can't be loaded.
SigarException sigarException = new SigarNotImplementedException();
sigarException.initCause(linkErrorException);
throw sigarException;
}
}
public ProcDiskIO getProcDiskIO(String pid) throws SigarException {
return getProcDiskIO(convertPid(pid));
}
/**
* Get the cumulative cpu time for the calling thread.
*/

View File

@ -106,6 +106,10 @@ public interface SigarProxy {
public long getProcPort(String protocol, String port) throws SigarException;
public ProcDiskIO getProcDiskIO(long pid) throws SigarException;
public ProcDiskIO getProcDiskIO(String pid) throws SigarException;
public FileSystem[] getFileSystemList() throws SigarException;
public FileSystemMap getFileSystemMap() throws SigarException;

View File

@ -100,6 +100,9 @@ public class ProcInfo extends SigarCommandBase {
try {
println("credname=" + sigar.getProcCredName(pid));
} catch (SigarException e) {}
try {
println("diskio=" + sigar.getProcDiskIO(pid));
} catch (SigarException e) {}
}
public static void main(String[] args) throws Exception {

View File

@ -0,0 +1,43 @@
package org.hyperic.sigar.test;
import org.hyperic.sigar.ProcDiskIO;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
public class TestProcDiskIO extends SigarTestCase {
public TestProcDiskIO(String name) {
super(name);
}
private void traceDiskIO(Sigar sigar, long pid) throws Exception {
ProcDiskIO procDiskIO;
try {
procDiskIO = sigar.getProcDiskIO(pid);
} catch (SigarException e) {
traceln("pid " + pid + ": " + e.getMessage());
return;
}
traceln("Pid=" + pid);
traceln("Bytes Read=" + Sigar.formatSize(procDiskIO.getBytesRead()));
traceln("Bytes Written=" + Sigar.formatSize(procDiskIO.getBytesWritten()));
}
public void testCreate() throws Exception {
Sigar sigar = getSigar();
try {
sigar.getProcDiskIO(getInvalidPid());
} catch (SigarException e) {
}
long[] pids = sigar.getProcList();
for (int i=0; i<pids.length; i++) {
traceDiskIO(sigar, pids[i]);
}
}
}

View File

@ -292,6 +292,15 @@ typedef struct {
SIGAR_DECLARE(int) sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_mem_t *procmem);
typedef struct {
sigar_uint64_t
bytes_read,
bytes_written;
} sigar_proc_disk_io_t;
SIGAR_DECLARE(int) sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_disk_io_t *proc_disk_io);
typedef struct {
sigar_uid_t uid;
sigar_gid_t gid;

View File

@ -768,6 +768,33 @@ int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
return SIGAR_OK;
}
SIGAR_INLINE sigar_uint64_t get_named_proc_token(char *buffer,
char *token) {
char *ptr = strstr(buffer, token);
if (!ptr) {
return SIGAR_FIELD_NOTIMPL;
}
ptr = sigar_skip_token(ptr);
return sigar_strtoul(ptr);
}
int sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_disk_io_t *proc_disk_io)
{
char buffer[BUFSIZ], *ptr=buffer;
int status = SIGAR_PROC_FILE2STR(buffer, pid, "/io");
if (status != SIGAR_OK) {
return status;
}
proc_disk_io->bytes_read = get_named_proc_token(buffer, "\nread_bytes");
proc_disk_io->bytes_written = get_named_proc_token(buffer, "\nwrite_bytes");
return SIGAR_OK;
}
#define NO_ID_MSG "[proc_cred] /proc/%lu" PROC_PSTATUS " missing "
int sigar_proc_cred_get(sigar_t *sigar, sigar_pid_t pid,