add ptql support to proc methods

This commit is contained in:
Doug MacEachern 2009-03-15 12:33:54 -07:00
parent 63141c38b9
commit 4bd7310f9a
4 changed files with 53 additions and 7 deletions

View File

@ -2350,7 +2350,7 @@ sub generate_class {
if ($func->{num_args} == 1) {
my $arg_type;
if ($func->{is_proc}) {
$arg_type = 'NUM2UINT';
$arg_type = 'OBJ2PID';
}
else {
$arg_type = 'StringValuePtr';

View File

@ -14,5 +14,5 @@ end
sigar = Sigar.new
ARGV.each do |pid|
output(sigar, pid.to_i)
output(sigar, pid)
end

View File

@ -10,5 +10,5 @@ end
sigar = Sigar.new
ARGV.each do |pid|
output(sigar, pid.to_i)
output(sigar, pid)
end

View File

@ -21,9 +21,11 @@
#include "sigar.h"
#include "sigar_fileinfo.h"
#include "sigar_format.h"
#include "sigar_ptql.h"
#define RB_SIGAR_CROAK rb_raise(rb_eArgError, "%s", sigar_strerror(sigar, status))
#define NUM2PID NUM2UINT
#define RB_SIGAR_RAISE(msg) rb_raise(rb_eArgError, "%s", msg)
#define RB_SIGAR_CROAK RB_SIGAR_RAISE(sigar_strerror(sigar, status))
#define OBJ2PID(pid) rb_sigar_pid_get(sigar, pid)
#ifndef RSTRING_PTR
#define RSTRING_PTR(s) RSTRING(s)->ptr
@ -40,6 +42,50 @@ static sigar_t *rb_sigar_get(VALUE obj)
return sigar;
}
#define sigar_isdigit(c) \
(isdigit(((unsigned char)(c))))
static sigar_pid_t rb_sigar_pid_get(sigar_t *sigar, VALUE obj)
{
if (TYPE(obj) == T_STRING) {
char *pid = StringValuePtr(obj);
if (sigar_isdigit(*pid)) {
obj = rb_str2inum(obj, 10);
/* fallthru */
}
else if ((RSTRING_LEN(obj) == 2) &&
(*pid == '$') && (*(pid + 1) == '$'))
{
return sigar_pid_get(sigar);
}
else {
/* XXX cache queries */
sigar_ptql_query_t *query;
sigar_ptql_error_t error;
int status =
sigar_ptql_query_create(&query, (char *)pid, &error);
if (status == SIGAR_OK) {
sigar_pid_t qpid;
status = sigar_ptql_query_find_process(sigar, query, &qpid);
sigar_ptql_query_destroy(query);
if (status == SIGAR_OK) {
return qpid;
}
else {
RB_SIGAR_RAISE(sigar_strerror(sigar, status));
}
}
else {
RB_SIGAR_RAISE(error.message);
}
}
}
return NUM2UINT(obj);
}
static void rb_sigar_free(void *obj)
{
free(obj);
@ -359,7 +405,7 @@ static VALUE rb_sigar_proc_args(VALUE obj, VALUE pid)
sigar_proc_args_t args;
VALUE RETVAL;
status = sigar_proc_args_get(sigar, NUM2PID(pid), &args);
status = sigar_proc_args_get(sigar, OBJ2PID(pid), &args);
if (status != SIGAR_OK) {
RB_SIGAR_CROAK;
@ -393,7 +439,7 @@ static VALUE rb_sigar_proc_env(VALUE obj, VALUE pid)
procenv.env_getter = rb_sigar_env_getall;
procenv.data = &RETVAL;
status = sigar_proc_env_get(sigar, NUM2PID(pid), &procenv);
status = sigar_proc_env_get(sigar, OBJ2PID(pid), &procenv);
if (status != SIGAR_OK) {
RB_SIGAR_CROAK;
}