Package systeminformationmonitor.system

Source Code of systeminformationmonitor.system.Process

/*
*  Copyright 2009 Leo Xu.
*
*  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.
*  under the License.
*/
package systeminformationmonitor.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.ProcCpu;
import org.hyperic.sigar.ProcCredName;
import org.hyperic.sigar.ProcMem;
import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.ProcTime;
import org.hyperic.sigar.ProcUtil;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;
import systeminformationmonitor.swing.model.ProcessTableModel;
import systeminformationmonitor.system.object.ProcessObject;

/**
* Process class generates all process related information
* @author Leo Xu
*/
public class Process extends AbstractSystemUpdater {

    private ProcessTableModel processModel;

    public Process() {
        processModel = ProcessTableModel.getInstance();
    }

    @Override
    public void update() {
        List<ProcessObject> pList = getProcessList();
        threadLock.lock();
        try {
            processModel.setRowData(pList);
            processModel.fireTableDataChanged();
        } finally {
            threadLock.unlock();
        }
    }

    private List<ProcessObject> getProcessList() {
        // list holder for all processes
        List<ProcessObject> pList = new Vector<ProcessObject>(60);
        try {
            long[] pids = sigar.getProcList();
            for (int i = 0; i < pids.length; i++) {
                long pid = pids[i];
                try {
                    pList.add(getProcessInfo(sigar, pid));
                } catch (SigarException e) {
                }
            }
        } catch (SigarException ex) {
            Logger.getLogger(
                    Process.class.getName()).log(Level.SEVERE, null, ex);
        }

        return pList;
    }

    @SuppressWarnings("deprecation")
    private ProcessObject getProcessInfo(SigarProxy sigar, long pid)
            throws SigarException {

        ProcessObject pObj = new ProcessObject(pid);
        ProcState state = sigar.getProcState(pid);
        ProcTime time = null;

        pObj.setPid(pid);

        // get process credential
        try {
            ProcCredName cred = sigar.getProcCredName(pid);
            pObj.setprocessCredentialName(cred.getUser());
        } catch (SigarException e) {
        }

        // get process time
        try {
            time = sigar.getProcTime(pid);
            pObj.setProcessTime(getStartTime(time.getStartTime()));
        } catch (SigarException e) {
        }

        // get process memory related information
        try {
            ProcMem mem = sigar.getProcMem(pid);
            pObj.setMemorySize(mem.getSize());
            pObj.setRssSize(mem.getRss());
            pObj.setSharedMemory(mem.getShare());
            pObj.setResident(mem.getResident());
        } catch (SigarException e) {
        }

        pObj.setProcessState(String.valueOf(state.getState()));

        try {
            ProcCpu cpu = sigar.getProcCpu(pid);
            int cpuCores = sigar.getCpuInfoList()[0].getTotalCores();
            int cpuSockets = sigar.getCpuInfoList()[0].getTotalSockets();

            if(cpuSockets == 0 || cpuCores == 0){
                cpuCores = cpuSockets = 1;
            }
           
            pObj.setProcessCPUPercentage((cpu.getPercent() /
                    (cpuCores * cpuSockets)));
           
        } catch (SigarException e) {           
        }

        if (time != null) {
            pObj.setCPUTime(getCpuTime(time));
        }

        pObj.setProcessDescription(ProcUtil.getDescription(sigar, pid));

        return pObj;
    }

    private static String getCpuTime(long total) {
        long t = total / 1000;
        return t / 60 + ":" + t % 60;
    }

    private static String getCpuTime(ProcTime time) {
        return getCpuTime(time.getTotal());
    }

    private static String getStartTime(long time) {
        if (time == 0) {
            return "00:00";
        }
        long timeNow = System.currentTimeMillis();
        String fmt = "MMMd";

        if ((timeNow - time) < ((60 * 60 * 24) * 1000)) {
            fmt = "HH:mm";
        }

        return new SimpleDateFormat(fmt).format(new Date(time));
    }
}
TOP

Related Classes of systeminformationmonitor.system.Process

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.