Skip to content

Commit

Permalink
Added Sigar.getProcDiskIO to Java API on Linux
Browse files Browse the repository at this point in the history
Returns the total bytes read and written for a process using /proc/pid/io.
  • Loading branch information
nettag committed Jul 30, 2013
1 parent ec489fb commit a62558a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
12 changes: 12 additions & 0 deletions bindings/SigarWrapper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
25 changes: 24 additions & 1 deletion bindings/java/src/org/hyperic/sigar/Sigar.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ else if (Character.isDigit(pid.charAt(0))) {
return this.processFinder.findSingleProcess(pid);
}
}

/**
* Get process memory info.
* @param pid The process id.
Expand Down Expand Up @@ -638,6 +638,29 @@ public long getProcPort(String protocol, String port)
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.
*/
Expand Down
4 changes: 4 additions & 0 deletions bindings/java/src/org/hyperic/sigar/SigarProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions bindings/java/src/org/hyperic/sigar/cmd/ProcInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public void output(String pid) throws SigarException {
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 {
Expand Down
43 changes: 43 additions & 0 deletions bindings/java/src/org/hyperic/sigar/test/TestProcDiskIO.java
Original file line number Diff line number Diff line change
@@ -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]);
}
}

}
9 changes: 9 additions & 0 deletions include/sigar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/os/linux/linux_sigar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a62558a

Please sign in to comment.