Program: 106343
Project: 106343
Buggy source:
private HashMap<String,VmStatsEntry> getVmStats(List<String> vmNames) throws Exception {
  VmwareHypervisorHost hyperHost=getHyperHost(getServiceContext());
  HashMap<String,VmStatsEntry> vmResponseMap=new HashMap<String,VmStatsEntry>();
  ManagedObjectReference perfMgr=getServiceContext().getServiceContent().getPerfManager();
  VimPortType service=getServiceContext().getService();
  PerfCounterInfo rxPerfCounterInfo=null;
  PerfCounterInfo txPerfCounterInfo=null;
  PerfCounterInfo[] cInfo=(PerfCounterInfo[])getServiceContext().getVimClient().getDynamicProperty(perfMgr,"String_Node_Str");
  for (int i=0; i < cInfo.length; ++i) {
    if ("String_Node_Str".equalsIgnoreCase(cInfo[i].getGroupInfo().getKey())) {
      if ("String_Node_Str".equalsIgnoreCase(cInfo[i].getNameInfo().getKey())) {
        txPerfCounterInfo=cInfo[i];
      }
      if ("String_Node_Str".equalsIgnoreCase(cInfo[i].getNameInfo().getKey())) {
        rxPerfCounterInfo=cInfo[i];
      }
    }
  }
  ObjectContent[] ocs=hyperHost.getVmPropertiesOnHyperHost(new String[]{"String_Node_Str","String_Node_Str","String_Node_Str"});
  if (ocs != null && ocs.length > 0) {
    for (    ObjectContent oc : ocs) {
      List<DynamicProperty> objProps=oc.getPropSet();
      if (objProps != null) {
        String name=null;
        String numberCPUs=null;
        String maxCpuUsage=null;
        for (        DynamicProperty objProp : objProps) {
          if (objProp.getName().equals("String_Node_Str")) {
            name=objProp.getVal().toString();
          }
 else           if (objProp.getName().equals("String_Node_Str")) {
            numberCPUs=objProp.getVal().toString();
          }
 else           if (objProp.getName().equals("String_Node_Str")) {
            maxCpuUsage=objProp.getVal().toString();
          }
        }
        if (!vmNames.contains(name)) {
          continue;
        }
        ManagedObjectReference vmMor=hyperHost.findVmOnHyperHost(name).getMor();
        assert(vmMor != null);
        ArrayList vmNetworkMetrics=new ArrayList();
        List<PerfMetricId> perfMetrics=service.queryAvailablePerfMetric(perfMgr,vmMor,null,null,null);
        if (perfMetrics != null) {
          for (int index=0; index < perfMetrics.size(); ++index) {
            if (((rxPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == rxPerfCounterInfo.getKey())) || ((txPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == txPerfCounterInfo.getKey()))) {
              vmNetworkMetrics.add(perfMetrics.get(index));
            }
          }
        }
        double networkReadKBs=0;
        double networkWriteKBs=0;
        long sampleDuration=0;
        if (vmNetworkMetrics.size() != 0) {
          PerfQuerySpec qSpec=new PerfQuerySpec();
          qSpec.setEntity(vmMor);
          PerfMetricId[] availableMetricIds=(PerfMetricId[])vmNetworkMetrics.toArray(new PerfMetricId[0]);
          qSpec.getMetricId().addAll(Arrays.asList(availableMetricIds));
          List<PerfQuerySpec> qSpecs=new ArrayList<PerfQuerySpec>();
          qSpecs.add(qSpec);
          List<PerfEntityMetricBase> values=service.queryPerf(perfMgr,qSpecs);
          for (int i=0; i < values.size(); ++i) {
            List<PerfSampleInfo> infos=((PerfEntityMetric)values.get(i)).getSampleInfo();
            int endMs=infos.get(infos.size() - 1).getTimestamp().getSecond() * 1000 + infos.get(infos.size() - 1).getTimestamp().getMillisecond();
            int beginMs=infos.get(0).getTimestamp().getSecond() * 1000 + infos.get(0).getTimestamp().getMillisecond();
            sampleDuration=(endMs - beginMs) / 1000;
            List<PerfMetricSeries> vals=((PerfEntityMetric)values.get(i)).getValue();
            for (int vi=0; ((vals != null) && (vi < vals.size())); ++vi) {
              if (vals.get(vi) instanceof PerfMetricIntSeries) {
                PerfMetricIntSeries val=(PerfMetricIntSeries)vals.get(vi);
                List<Long> perfValues=val.getValue();
                if (vals.get(vi).getId().getCounterId() == rxPerfCounterInfo.getKey()) {
                  networkReadKBs=sampleDuration * perfValues.get(3);
                }
                if (vals.get(vi).getId().getCounterId() == txPerfCounterInfo.getKey()) {
                  networkWriteKBs=sampleDuration * perfValues.get(3);
                }
              }
            }
          }
        }
        vmResponseMap.put(name,new VmStatsEntry(Integer.parseInt(maxCpuUsage),networkReadKBs,networkWriteKBs,Integer.parseInt(numberCPUs),"String_Node_Str"));
      }
    }
  }
  return vmResponseMap;
}

Fixed version (ground truth):
private HashMap<String,VmStatsEntry> getVmStats(List<String> vmNames) throws Exception {
  VmwareHypervisorHost hyperHost=getHyperHost(getServiceContext());
  HashMap<String,VmStatsEntry> vmResponseMap=new HashMap<String,VmStatsEntry>();
  ManagedObjectReference perfMgr=getServiceContext().getServiceContent().getPerfManager();
  VimPortType service=getServiceContext().getService();
  PerfCounterInfo rxPerfCounterInfo=null;
  PerfCounterInfo txPerfCounterInfo=null;
  List<PerfCounterInfo> cInfo=(List<PerfCounterInfo>)getServiceContext().getVimClient().getDynamicProperty(perfMgr,"String_Node_Str");
  for (  PerfCounterInfo info : cInfo) {
    if ("String_Node_Str".equalsIgnoreCase(info.getGroupInfo().getKey())) {
      if ("String_Node_Str".equalsIgnoreCase(info.getNameInfo().getKey())) {
        txPerfCounterInfo=info;
      }
      if ("String_Node_Str".equalsIgnoreCase(info.getNameInfo().getKey())) {
        rxPerfCounterInfo=info;
      }
    }
  }
  ObjectContent[] ocs=hyperHost.getVmPropertiesOnHyperHost(new String[]{"String_Node_Str","String_Node_Str","String_Node_Str"});
  if (ocs != null && ocs.length > 0) {
    for (    ObjectContent oc : ocs) {
      List<DynamicProperty> objProps=oc.getPropSet();
      if (objProps != null) {
        String name=null;
        String numberCPUs=null;
        String maxCpuUsage=null;
        for (        DynamicProperty objProp : objProps) {
          if (objProp.getName().equals("String_Node_Str")) {
            name=objProp.getVal().toString();
          }
 else           if (objProp.getName().equals("String_Node_Str")) {
            numberCPUs=objProp.getVal().toString();
          }
 else           if (objProp.getName().equals("String_Node_Str")) {
            maxCpuUsage=objProp.getVal().toString();
          }
        }
        if (!vmNames.contains(name)) {
          continue;
        }
        ManagedObjectReference vmMor=hyperHost.findVmOnHyperHost(name).getMor();
        assert(vmMor != null);
        ArrayList vmNetworkMetrics=new ArrayList();
        List<PerfMetricId> perfMetrics=service.queryAvailablePerfMetric(perfMgr,vmMor,null,null,null);
        if (perfMetrics != null) {
          for (int index=0; index < perfMetrics.size(); ++index) {
            if (((rxPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == rxPerfCounterInfo.getKey())) || ((txPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == txPerfCounterInfo.getKey()))) {
              vmNetworkMetrics.add(perfMetrics.get(index));
            }
          }
        }
        double networkReadKBs=0;
        double networkWriteKBs=0;
        long sampleDuration=0;
        if (vmNetworkMetrics.size() != 0) {
          PerfQuerySpec qSpec=new PerfQuerySpec();
          qSpec.setEntity(vmMor);
          PerfMetricId[] availableMetricIds=(PerfMetricId[])vmNetworkMetrics.toArray(new PerfMetricId[0]);
          qSpec.getMetricId().addAll(Arrays.asList(availableMetricIds));
          List<PerfQuerySpec> qSpecs=new ArrayList<PerfQuerySpec>();
          qSpecs.add(qSpec);
          List<PerfEntityMetricBase> values=service.queryPerf(perfMgr,qSpecs);
          for (int i=0; i < values.size(); ++i) {
            List<PerfSampleInfo> infos=((PerfEntityMetric)values.get(i)).getSampleInfo();
            int endMs=infos.get(infos.size() - 1).getTimestamp().getSecond() * 1000 + infos.get(infos.size() - 1).getTimestamp().getMillisecond();
            int beginMs=infos.get(0).getTimestamp().getSecond() * 1000 + infos.get(0).getTimestamp().getMillisecond();
            sampleDuration=(endMs - beginMs) / 1000;
            List<PerfMetricSeries> vals=((PerfEntityMetric)values.get(i)).getValue();
            for (int vi=0; ((vals != null) && (vi < vals.size())); ++vi) {
              if (vals.get(vi) instanceof PerfMetricIntSeries) {
                PerfMetricIntSeries val=(PerfMetricIntSeries)vals.get(vi);
                List<Long> perfValues=val.getValue();
                if (vals.get(vi).getId().getCounterId() == rxPerfCounterInfo.getKey()) {
                  networkReadKBs=sampleDuration * perfValues.get(3);
                }
                if (vals.get(vi).getId().getCounterId() == txPerfCounterInfo.getKey()) {
                  networkWriteKBs=sampleDuration * perfValues.get(3);
                }
              }
            }
          }
        }
        vmResponseMap.put(name,new VmStatsEntry(Integer.parseInt(maxCpuUsage),networkReadKBs,networkWriteKBs,Integer.parseInt(numberCPUs),"String_Node_Str"));
      }
    }
  }
  return vmResponseMap;
}