/*
 * Copyright (c) 2006-2008 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;

import java.util.HashMap;
import java.util.Map;

import org.hyperic.sigar.ptql.ProcessFinder;

/**
 * Provide multi process cpu metrics.
 */
public class MultiProcCpu extends ProcCpu {

    private long pid;
    private int nproc = 0;
    private static Map ptable = new HashMap();

    static synchronized MultiProcCpu get(Sigar sigar, String query)
        throws SigarException {

        MultiProcCpu cpu;

        cpu = (MultiProcCpu)ptable.get(query);

        if (cpu == null) {
            cpu = new MultiProcCpu();
            cpu.pid = query.hashCode(); //for equals()
            ptable.put(query, cpu);
        }

        long timeNow = System.currentTimeMillis();
        double diff = timeNow - cpu.lastTime;
        if (diff == 0) {
            return cpu; //we were just called within < 1 second ago.
        }

        cpu.lastTime = timeNow;

        long otime = cpu.total;

        cpu.total = 0;
        cpu.user  = 0;
        cpu.sys   = 0;
        cpu.nproc = 0;

        long[] pids = ProcessFinder.find(sigar, query);
        cpu.nproc = pids.length;

        for (int i=0; i<pids.length; i++) {
            ProcTime time;
            try {
                time = sigar.getProcTime(pids[i]);
            } catch (SigarException e) {
                //process may have gone away or EPERM
                continue;
            }
            cpu.total += time.total;
            cpu.user  += time.user;
            cpu.sys   += time.sys;
        }

        if (otime == 0) {
            //XXX could/should pause first time called.
            return cpu;
        }

        cpu.percent = ((cpu.total - otime) / diff);
        if (cpu.percent < 0.0) {
            //counter wrapped
            cpu.percent = (0.0 - cpu.percent);
        }
        if (cpu.percent >= 1.0) {
            cpu.percent = 0.99;
        }

        return cpu;
    }

    /**
     * @return Processes CPU usage percentage.
     */
    public double getPercent() {
        return this.percent;
    }

    /**
     * @return Number of processes matched by query.
     */
    public int getProcesses() {
        return this.nproc;
    }

    /**
     * @return Pid of the process.
     */
    public int hashCode() {
        return (int)this.pid;
    }

    public boolean equals(Object cpu) {
        if (!(cpu instanceof MultiProcCpu)) {
            return false;
        }

        return ((MultiProcCpu)cpu).pid == this.pid;
    }
}

@@ -867,54 +867,8 @@
  0;%0A
-    private static Map ptable = new HashMap();
 %0A%0A  
@@ -933,16 +933,24 @@
  query)%0A
+        
         
@@ -999,101 +999,8 @@
 Cpu 
-cpu;%0A%0A        cpu = (MultiProcCpu)ptable.get(query);%0A%0A        if (cpu == null) %7B%0A            
 cpu 
@@ -1017,28 +1017,24 @@
 iProcCpu();%0A
-    
         cpu.
@@ -1075,322 +1075,8 @@
 ls()
-%0A            ptable.put(query, cpu);%0A        %7D%0A%0A        long timeNow = System.currentTimeMillis();%0A        double diff = timeNow - cpu.lastTime;%0A        if (diff == 0) %7B%0A            return cpu; //we were just called within %3C 1 second ago.%0A        %7D%0A%0A        cpu.lastTime = timeNow;%0A%0A        long otime = cpu.total;
 %0A%0A  
@@ -1154,25 +1154,30 @@
     cpu.
-nproc
+percent
  = 0
+.0D
 ;%0A%0A     
@@ -1309,35 +1309,8 @@
 ) %7B%0A
-            ProcTime time;%0A
     
@@ -1339,20 +1339,31 @@
         
-time
+ProcCpu procCpu
  = sigar
@@ -1374,12 +1374,11 @@
 Proc
-Time
+Cpu
 (pid
@@ -1400,168 +1400,47 @@
     
-%7D catch (SigarException e) %7B%0A                //process may have gone away or EPERM%0A                continue;%0A            %7D%0A            cpu.total += time.total;%0A
+    cpu.total += procCpu.getTotal();%0A  
     
@@ -1439,32 +1439,34 @@
 ;%0A              
+  
 cpu.user  += tim
@@ -1462,23 +1462,34 @@
 ser 
- 
 += 
-time.user;%0A
+procCpu.getUser();%0A    
     
@@ -1508,138 +1508,27 @@
 sys 
-  
 += 
-time.sys;%0A        %7D%0A%0A        if (otime == 0) %7B%0A            //XXX could/should pause first time called.%0A            return cpu
+procCpu.getSys()
 ;%0A  
@@ -1525,35 +1525,32 @@
 tSys();%0A        
-%7D%0A%0A
         cpu.perc
@@ -1557,149 +1557,127 @@
 ent 
++
 = 
-((cpu.total - otime) / diff);%0A        if (cpu.percent %3C 0.0) %7B%0A            //counter wrapped%0A            cpu.percent = (0.0 - cpu.percent);
+procCpu.getPercent();%0A            %7D catch (SigarException e) %7B%0A                //process may have gone away or EPERM
 %0A   
@@ -1685,18 +1685,16 @@
     
-%7D%0A
         
 if (
@@ -1693,65 +1693,31 @@
     
-if (cpu.percent %3E= 1.0) %7B%0A            cpu.percent = 0.99;
+continue;%0A            %7D
 %0A   
@@ -1806,32 +1806,46 @@
 entage.%0A     */%0A
+    @Override%0A
     public doubl
@@ -2075,32 +2075,46 @@
 rocess.%0A     */%0A
+    @Override%0A
     public int h
@@ -2158,24 +2158,38 @@
 pid;%0A    %7D%0A%0A
+    @Override%0A
     public b

fix bug 1361953(bugzilla)/HQ-4820(jira) - changing the way MultiProcess cpu usage(%) is calculated