move ProcessFinder.find to native code
This commit is contained in:
		
							parent
							
								
									2d0943c5a6
								
							
						
					
					
						commit
						503298c5cd
					
				@ -1264,6 +1264,64 @@ JNIEXPORT jlong SIGAR_JNI(ptql_SigarProcessQuery_findProcess)
 | 
			
		||||
    return pid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
JNIEXPORT jlongArray SIGAR_JNI(ptql_SigarProcessQuery_findProcesses)
 | 
			
		||||
(JNIEnv *env, jobject obj, jobject sigar_obj)
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
    jlongArray procarray;
 | 
			
		||||
    sigar_proc_list_t proclist;
 | 
			
		||||
    jlong *pids = NULL;
 | 
			
		||||
    jni_ptql_re_data_t re;
 | 
			
		||||
    sigar_ptql_query_t *query =
 | 
			
		||||
        (sigar_ptql_query_t *)sigar_get_pointer(env, obj);
 | 
			
		||||
    dSIGAR(NULL);
 | 
			
		||||
 | 
			
		||||
    if ((status = sigar_proc_list_get(sigar, &proclist)) != SIGAR_OK) {
 | 
			
		||||
        sigar_throw_error(env, jsigar, status);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    re_impl_set(env, sigar, obj, &re);
 | 
			
		||||
 | 
			
		||||
    status = sigar_ptql_query_find_processes(sigar, query, &proclist);
 | 
			
		||||
 | 
			
		||||
    sigar_ptql_re_impl_set(sigar, NULL, NULL);
 | 
			
		||||
 | 
			
		||||
    if (status < 0) {
 | 
			
		||||
        sigar_throw_ptql_malformed(env, sigar->errbuf);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    else if (status != SIGAR_OK) {
 | 
			
		||||
        sigar_throw_error(env, jsigar, status);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    procarray = JENV->NewLongArray(env, proclist.number);
 | 
			
		||||
 | 
			
		||||
    if (sizeof(jlong) == sizeof(sigar_pid_t)) {
 | 
			
		||||
        pids = (jlong *)proclist.data;
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        unsigned int i;
 | 
			
		||||
        pids = (jlong *)malloc(sizeof(jlong) * proclist.number);
 | 
			
		||||
 | 
			
		||||
        for (i=0; i<proclist.number; i++) {
 | 
			
		||||
            pids[i] = proclist.data[i];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    JENV->SetLongArrayRegion(env, procarray, 0,
 | 
			
		||||
                             proclist.number, pids);
 | 
			
		||||
 | 
			
		||||
    if (pids != (jlong *)proclist.data) {
 | 
			
		||||
        free(pids);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sigar_proc_list_destroy(sigar, &proclist);
 | 
			
		||||
 | 
			
		||||
    return procarray;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "sigar_getline.h"
 | 
			
		||||
 | 
			
		||||
JNIEXPORT jboolean SIGAR_JNI(util_Getline_isatty)
 | 
			
		||||
 | 
			
		||||
@ -62,7 +62,7 @@ public class ProcessFinder {
 | 
			
		||||
            return ((SigarProcessQuery)query).findProcess(this.proxy);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new MalformedQueryException();
 | 
			
		||||
        throw new SigarNotImplementedException();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static long[] find(Sigar sigar, String query)
 | 
			
		||||
@ -105,52 +105,11 @@ public class ProcessFinder {
 | 
			
		||||
    public long[] find(ProcessQuery query)
 | 
			
		||||
        throws SigarException, SigarNotImplementedException {
 | 
			
		||||
 | 
			
		||||
        int matches=0, lastMatch=-1;
 | 
			
		||||
 | 
			
		||||
        //because we modify the array below.  XXX this sucks.
 | 
			
		||||
        long[] pids = (long[])this.proxy.getProcList().clone();
 | 
			
		||||
 | 
			
		||||
        for (int i=0; i<pids.length; i++) {
 | 
			
		||||
            long pid = pids[i];
 | 
			
		||||
            boolean add = false;
 | 
			
		||||
 | 
			
		||||
            try {
 | 
			
		||||
                add = query.match(this.proxy, pid);
 | 
			
		||||
            } catch (SigarNotImplementedException e) {
 | 
			
		||||
                throw e; //let caller know query is invalid.
 | 
			
		||||
            } catch (SigarException e) {
 | 
			
		||||
                //ok, e.g. permission denied.
 | 
			
		||||
        if (query instanceof SigarProcessQuery) {
 | 
			
		||||
            return ((SigarProcessQuery)query).findProcesses(this.proxy);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            if (add) {
 | 
			
		||||
                ++matches;
 | 
			
		||||
                lastMatch = i;
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                pids[i] = -1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (matches == 1) {
 | 
			
		||||
            /* avoid loop below */
 | 
			
		||||
            ONE_MATCH[0] = pids[lastMatch];
 | 
			
		||||
            return ONE_MATCH;
 | 
			
		||||
        }
 | 
			
		||||
        else if (matches == 0) {
 | 
			
		||||
            return NO_MATCHES;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        long[] matched = new long[matches];
 | 
			
		||||
 | 
			
		||||
        //XXX this is clunky
 | 
			
		||||
        for (int i=0, j=0; i<=lastMatch; i++) {
 | 
			
		||||
            if (pids[i] == -1) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            matched[j++] = pids[i];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return matched;
 | 
			
		||||
        throw new SigarNotImplementedException();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
@ -55,6 +55,15 @@ public class SigarProcessQuery implements ProcessQuery {
 | 
			
		||||
        return findProcess(SigarProxyCache.getSigar(sigar));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public native long[] findProcesses(Sigar sigar)
 | 
			
		||||
        throws SigarException, SigarNotImplementedException;
 | 
			
		||||
 | 
			
		||||
    public long[] findProcesses(SigarProxy sigar)
 | 
			
		||||
        throws SigarException, SigarNotImplementedException {
 | 
			
		||||
 | 
			
		||||
        return findProcesses(SigarProxyCache.getSigar(sigar));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static boolean re(String haystack, String needle) {
 | 
			
		||||
        if (haystack == null) {
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
@ -41,4 +41,8 @@ SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar,
 | 
			
		||||
                                                 sigar_ptql_query_t *query,
 | 
			
		||||
                                                 sigar_pid_t *pid);
 | 
			
		||||
 | 
			
		||||
SIGAR_DECLARE(int) sigar_ptql_query_find_processes(sigar_t *sigar,
 | 
			
		||||
                                                   sigar_ptql_query_t *query,
 | 
			
		||||
                                                   sigar_proc_list_t *proclist);
 | 
			
		||||
 | 
			
		||||
#endif /*SIGAR_PTQL_H*/
 | 
			
		||||
 | 
			
		||||
@ -1403,3 +1403,43 @@ SIGAR_DECLARE(int) sigar_ptql_query_find_process(sigar_t *sigar,
 | 
			
		||||
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SIGAR_DECLARE(int) sigar_ptql_query_find_processes(sigar_t *sigar,
 | 
			
		||||
                                                   sigar_ptql_query_t *query,
 | 
			
		||||
                                                   sigar_proc_list_t *proclist)
 | 
			
		||||
{
 | 
			
		||||
    sigar_proc_list_t pids;
 | 
			
		||||
    int status;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    status = sigar_proc_list_get(sigar, &pids);
 | 
			
		||||
    if (status != SIGAR_OK) {
 | 
			
		||||
        return status;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sigar_proc_list_create(proclist);
 | 
			
		||||
 | 
			
		||||
    for (i=0; i<pids.number; i++) {
 | 
			
		||||
        int query_status =
 | 
			
		||||
            sigar_ptql_query_match(sigar, query, pids.data[i]);
 | 
			
		||||
 | 
			
		||||
        if (query_status == SIGAR_OK) {
 | 
			
		||||
            SIGAR_PROC_LIST_GROW(proclist);
 | 
			
		||||
            proclist->data[proclist->number++] = pids.data[i];
 | 
			
		||||
        }
 | 
			
		||||
        else if (query_status == SIGAR_ENOTIMPL) {
 | 
			
		||||
            /* let caller know query is invalid. */
 | 
			
		||||
            status = query_status;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sigar_proc_list_destroy(sigar, &pids);
 | 
			
		||||
 | 
			
		||||
    if (status != SIGAR_OK) {
 | 
			
		||||
        sigar_proc_list_create(proclist);
 | 
			
		||||
        return status;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return SIGAR_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user