Skip to content

Commit

Permalink
disk io works using cache - also pid cache cleanup and pid cache dump
Browse files Browse the repository at this point in the history
  • Loading branch information
nira11 committed Sep 15, 2013
1 parent f2ba707 commit 07b221a
Show file tree
Hide file tree
Showing 20 changed files with 408 additions and 42 deletions.
30 changes: 30 additions & 0 deletions bindings/SigarWrapper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ my %has_name_arg = map { $_, 1 } qw(FileSystemUsage DiskUsage
FileAttrs DirStat DirUsage
NetInterfaceConfig NetInterfaceStat);


my %proc_no_arg = map { $_, 1 } qw(stat);

my %get_not_impl = map { $_, 1 } qw(net_address net_route net_connection net_stat cpu_perc
Expand Down Expand Up @@ -527,6 +528,7 @@ use vars qw(%classes %cmds);
plat => '*'
},
],

ProcMem => [
{
name => 'size', type => 'Long',
Expand Down Expand Up @@ -641,9 +643,37 @@ use vars qw(%classes %cmds);
{
name => 'bytes_total', type => 'Long',
desc => 'Bytes Total',
plat => 'LWAHS'
}
],

ProcCumulativeDiskIO => [
{
name => 'bytes_read', type => 'Long',
desc => 'Bytes Read from Start',
plat => 'LW'
},
{
name => 'bytes_written', type => 'Long',
desc => 'Bytes Written from Start',
plat => 'LW'
},
{
name => 'bytes_total', type => 'Long',
desc => 'Bytes Total from Start',
plat => 'LWAHS'
}
],

DumpPidCache => [
{
name => 'dummy', type => 'Long',
desc => 'Dummy',
plat => 'LWAHS'
}
],


ProcState => [
{
name => 'state', type => 'Char',
Expand Down
30 changes: 30 additions & 0 deletions bindings/java/src/org/hyperic/sigar/Sigar.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class Sigar implements SigarProxy {
private static String loadError = null;

public static final long FIELD_NOTIMPL = -1;
public static final int PID_PROC_CPU_CACHE = 1;
public static final int PID_PROC_IO_CACHE = 2;

/**
* The Sigar java version.
Expand Down Expand Up @@ -661,6 +663,34 @@ public ProcDiskIO getProcDiskIO(String pid) throws SigarException {
return getProcDiskIO(convertPid(pid));
}

/**
* Get process cumulative disk IO info.
* @param pid THe process id.
* @exception SigarException on failure.
*/
public ProcCumulativeDiskIO getProcCumulativeDiskIO(long pid) throws SigarException {
try {
return ProcCumulativeDiskIO.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 ProcCumulativeDiskIO getProcCumulativeDiskIO(String pid) throws SigarException {
return getProcCumulativeDiskIO(convertPid(pid));
}

public DumpPidCache dumpPidCache() throws SigarException {
return DumpPidCache.fetch(this);
}


/**
* Get the cumulative cpu time for the calling thread.
*/
Expand Down
6 changes: 6 additions & 0 deletions bindings/java/src/org/hyperic/sigar/SigarProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public interface SigarProxy {

public ProcDiskIO getProcDiskIO(String pid) throws SigarException;

public ProcCumulativeDiskIO getProcCumulativeDiskIO(long pid) throws SigarException;

public ProcCumulativeDiskIO getProcCumulativeDiskIO(String pid) throws SigarException;

public DumpPidCache dumpPidCache() throws SigarException;

public FileSystem[] getFileSystemList() throws SigarException;

public FileSystemMap getFileSystemMap() throws SigarException;
Expand Down
51 changes: 51 additions & 0 deletions bindings/java/src/org/hyperic/sigar/cmd/PidCacheInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2006-2007 Hyperic, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hyperic.sigar.cmd;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarPermissionDeniedException;

/**
* Display all pid cache information.
*/
public class PidCacheInfo extends SigarCommandBase {


public PidCacheInfo(Shell shell) {
super(shell);
}

public PidCacheInfo() {
super();
}

protected boolean validateArgs(String[] args) {
return true;
}

public String getUsageShort() {
return "Display cache info for CPU cache and for IO cache";
}

public boolean isPidCompleter() {
return false;
}

public void output(String[] args) throws SigarException {
sigar.dumpPidCache();
}
}
5 changes: 5 additions & 0 deletions bindings/java/src/org/hyperic/sigar/cmd/ProcInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public void output(String pid) throws SigarException {
try {
println("diskio=" + sigar.getProcDiskIO(pid));
} catch (SigarException e) {}

try {
println("cumulative diskio=" + sigar.getProcCumulativeDiskIO(pid));
} catch (SigarException e) {}

}

public static void main(String[] args) throws Exception {
Expand Down
2 changes: 2 additions & 0 deletions bindings/java/src/org/hyperic/sigar/cmd/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public void registerCommands() throws ShellCommandInitException {
registerCommandHandler("time", new Time(this));
registerCommandHandler("ulimit", new Ulimit(this));
registerCommandHandler("who", new Who(this));
registerCommandHandler("pid_cache_info", new PidCacheInfo(this));

if (SigarLoader.IS_WIN32) {
registerCommandHandler("service", new Win32Service(this));
registerCommandHandler("fversion", new FileVersionInfo(this));
Expand Down
16 changes: 16 additions & 0 deletions bindings/java/src/org/hyperic/sigar/jmx/SigarProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperic.sigar.ProcFd;
import org.hyperic.sigar.ProcMem;
import org.hyperic.sigar.ProcUtil;
import org.hyperic.sigar.ProcDiskIO;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;
Expand Down Expand Up @@ -76,6 +77,16 @@ private synchronized ProcCpu getCpu() {
}
}


private synchronized ProcDiskIO getDiskIO() {
try {
return this.sigar.getProcDiskIO(getPid());
} catch (SigarException e) {
throw unexpectedError("DiskIO", e);
}
}


private synchronized ProcFd getFd() throws SigarException {
return this.sigar.getProcFd(getPid());
}
Expand Down Expand Up @@ -153,4 +164,9 @@ public Long getOpenFd() {
return NOTIMPL;
}
}

public Double getBytesReadWriteTotal() {
return new Double(getDiskIO().getBytesTotal());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public interface SigarProcessMBean {
public Double getCpuUsage();

public Long getOpenFd();

public Double getBytesReadWriteTotal();
}
33 changes: 32 additions & 1 deletion include/sigar.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ SIGAR_DECLARE(int) sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_mem_t *procmem);

typedef struct {
sigar_uint64_t
sigar_uint64_t
bytes_read,
bytes_written,
bytes_total;
Expand All @@ -302,6 +302,37 @@ typedef struct {
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_uint64_t
bytes_read,
bytes_written,
bytes_total;
sigar_uint64_t last_time;
sigar_uint64_t
bytes_read_diff,
bytes_written_diff,
bytes_total_diff;
} sigar_cached_proc_disk_io_t;


typedef struct {
sigar_uint64_t
bytes_read,
bytes_written,
bytes_total;
} sigar_proc_cumulative_disk_io_t;

SIGAR_DECLARE(int) sigar_proc_cumulative_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cumulative_disk_io_t *proc_cumulative_disk_io);


typedef struct {
sigar_uint64_t dummy;
}sigar_dump_pid_cache_t;

SIGAR_DECLARE(int) sigar_dump_pid_cache_get(sigar_t *sigar, sigar_dump_pid_cache_t *info);


typedef struct {
sigar_uid_t uid;
sigar_gid_t gid;
Expand Down
9 changes: 7 additions & 2 deletions include/sigar_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
sigar_cache_t *proc_cpu; \
sigar_cache_t *net_listen; \
sigar_cache_t *net_services_tcp; \
sigar_cache_t *net_services_udp
sigar_cache_t *net_services_udp;\
sigar_cache_t *proc_io;

#if defined(WIN32)
# define SIGAR_INLINE __inline
Expand Down Expand Up @@ -398,11 +399,15 @@ int sigar_get_iftype(const char *name, int *type, int *inst);
#define SIGAR_NIC_SIT "IPv6-in-IPv4"
#define SIGAR_NIC_IRDA "IrLAP"
#define SIGAR_NIC_EC "Econet"

#define PID_CACHE_CLEANUP_PERIOD 1000*60*10 /* 10 minutes */
#define PID_CACHE_ENTRY_EXPIRE_PERIOD 1000*60*20 /* 20 minutes */
#ifndef WIN32
#include <netdb.h>
#endif

#define PROC_PID_CPU_CACHE 1
#define PROC_PID_IO_CACHE 2

#define SIGAR_HOSTENT_LEN 1024
#if defined(_AIX)
#define SIGAR_HAS_HOSTENT_DATA
Expand Down
6 changes: 6 additions & 0 deletions include/sigar_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,21 @@ struct sigar_cache_entry_t {
sigar_cache_entry_t *next;
sigar_uint64_t id;
void *value;
sigar_uint64_t last_access_time;
};

typedef struct {
sigar_cache_entry_t **entries;
unsigned int count, size;
void (*free_value)(void *ptr);
sigar_uint64_t entry_expire_period;
sigar_uint64_t cleanup_period_millis;
sigar_uint64_t last_cleanup_time;
} sigar_cache_t;

sigar_cache_t *sigar_cache_new(int size);
sigar_cache_t *sigar_expired_cache_new(int size, sigar_uint64_t cleanup_period_millis, sigar_uint64_t entry_expire_period);
void sigar_cache_dump(sigar_cache_t *table);

sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
sigar_uint64_t key);
Expand Down
12 changes: 5 additions & 7 deletions src/os/aix/aix_sigar.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,22 +754,20 @@ int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
return SIGAR_OK;
}

int sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_disk_io_t *proc_disk_io)
int sigar_proc_cumulative_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cumulative_disk_io_t *cumulative_proc_disk_io)
{
int status = sigar_getprocs(sigar, pid);
struct procsinfo64 *pinfo = sigar->pinfo;

if (status != SIGAR_OK) {
return status;
}
proc_disk_io->bytes_read = SIGAR_FIELD_NOTIMPL;
proc_disk_io->bytes_written = SIGAR_FIELD_NOTIMPL;
proc_disk_io->bytes_total = pinfo->pi_ioch;
cumulative_proc_disk_io->bytes_read = SIGAR_FIELD_NOTIMPL;
cumulative_proc_disk_io->bytes_written = SIGAR_FIELD_NOTIMPL;
cumulative_proc_disk_io->bytes_total = pinfo->pi_ioch;

return SIGAR_OK;


}


Expand Down
4 changes: 2 additions & 2 deletions src/os/darwin/darwin_sigar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,8 @@ int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
return SIGAR_OK;
}

int sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_disk_io_t *proc_disk_io)
int sigar_proc_cumulative_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cumulative_disk_io_t *proc_cumulative_disk_io)
{
return SIGAR_ENOTIMPL;
}
Expand Down
10 changes: 5 additions & 5 deletions src/os/hpux/hpux_sigar.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
return SIGAR_OK;
}

int sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_disk_io_t *proc_disk_io)
int sigar_proc_cumulative_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cumulative_disk_io_t *proc_cumulative_disk_io)
{

int status = sigar_pstat_getproc(sigar, pid);
Expand All @@ -317,9 +317,9 @@ int sigar_proc_disk_io_get(sigar_t *sigar, sigar_pid_t pid,
if (status != SIGAR_OK) {
return status;
}
proc_disk_io->bytes_read = SIGAR_FIELD_NOTIMPL;
proc_disk_io->bytes_written = SIGAR_FIELD_NOTIMPL;
proc_disk_io->bytes_total = pinfo->pst_ioch;
proc_cumulative_disk_io->bytes_read = SIGAR_FIELD_NOTIMPL;
proc_cumulative_disk_io->bytes_written = SIGAR_FIELD_NOTIMPL;
proc_cumulative_disk_io->bytes_total = pinfo->pst_ioch;


return SIGAR_OK;
Expand Down
Loading

0 comments on commit 07b221a

Please sign in to comment.