Program: 105151
Project: 105151
Buggy source:
public VmStatsEntry getVmStat(final Connect conn,final String vmName) throws LibvirtException {
  Domain dm=null;
  try {
    dm=getDomain(conn,vmName);
    final DomainInfo info=dm.getInfo();
    final MemoryStatistic[] mems=dm.memoryStats(NUMMEMSTATS);
    final VmStatsEntry stats=new VmStatsEntry();
    stats.setNumCPUs(info.nrVirtCpu);
    stats.setEntityType("String_Node_Str");
    stats.setMemoryKBs(info.maxMem);
    stats.setTargetMemoryKBs(info.memory);
    stats.setIntFreeMemoryKBs((double)mems[0].getValue());
    VmStats oldStats=null;
    final Calendar now=Calendar.getInstance();
    oldStats=_vmStats.get(vmName);
    long elapsedTime=0;
    if (oldStats != null) {
      elapsedTime=now.getTimeInMillis() - oldStats._timestamp.getTimeInMillis();
      double utilization=(info.cpuTime - oldStats._usedTime) / ((double)elapsedTime * 1000000);
      final NodeInfo node=conn.nodeInfo();
      utilization=utilization / node.cpus;
      if (utilization > 0) {
        stats.setCPUUtilization(utilization * 100);
      }
    }
    final List<InterfaceDef> vifs=getInterfaces(conn,vmName);
    long rx=0;
    long tx=0;
    for (    final InterfaceDef vif : vifs) {
      final DomainInterfaceStats ifStats=dm.interfaceStats(vif.getDevName());
      rx+=ifStats.rx_bytes;
      tx+=ifStats.tx_bytes;
    }
    if (oldStats != null) {
      final double deltarx=rx - oldStats._rx;
      if (deltarx > 0) {
        stats.setNetworkReadKBs(deltarx / 1024);
      }
      final double deltatx=tx - oldStats._tx;
      if (deltatx > 0) {
        stats.setNetworkWriteKBs(deltatx / 1024);
      }
    }
    final List<DiskDef> disks=getDisks(conn,vmName);
    long io_rd=0;
    long io_wr=0;
    long bytes_rd=0;
    long bytes_wr=0;
    for (    final DiskDef disk : disks) {
      final DomainBlockStats blockStats=dm.blockStats(disk.getDiskLabel());
      io_rd+=blockStats.rd_req;
      io_wr+=blockStats.wr_req;
      bytes_rd+=blockStats.rd_bytes;
      bytes_wr+=blockStats.wr_bytes;
    }
    if (oldStats != null) {
      final long deltaiord=io_rd - oldStats._ioRead;
      if (deltaiord > 0) {
        stats.setDiskReadIOs(deltaiord);
      }
      final long deltaiowr=io_wr - oldStats._ioWrote;
      if (deltaiowr > 0) {
        stats.setDiskWriteIOs(deltaiowr);
      }
      final double deltabytesrd=bytes_rd - oldStats._bytesRead;
      if (deltabytesrd > 0) {
        stats.setDiskReadKBs(deltabytesrd / 1024);
      }
      final double deltabyteswr=bytes_wr - oldStats._bytesWrote;
      if (deltabyteswr > 0) {
        stats.setDiskWriteKBs(deltabyteswr / 1024);
      }
    }
    final VmStats newStat=new VmStats();
    newStat._usedTime=info.cpuTime;
    newStat._rx=rx;
    newStat._tx=tx;
    newStat._ioRead=io_rd;
    newStat._ioWrote=io_wr;
    newStat._bytesRead=bytes_rd;
    newStat._bytesWrote=bytes_wr;
    newStat._timestamp=now;
    newStat._intmemfree=mems[0].getValue();
    newStat._memory=info.memory;
    newStat._maxmemory=info.maxMem;
    _vmStats.put(vmName,newStat);
    return stats;
  }
  finally {
    if (dm != null) {
      dm.free();
    }
  }
}

Fixed version (ground truth):
public VmStatsEntry getVmStat(final Connect conn,final String vmName) throws LibvirtException {
  Domain dm=null;
  try {
    dm=getDomain(conn,vmName);
    if (dm == null) {
      return null;
    }
    DomainInfo info=dm.getInfo();
    final VmStatsEntry stats=new VmStatsEntry();
    stats.setNumCPUs(info.nrVirtCpu);
    stats.setEntityType("String_Node_Str");
    stats.setMemoryKBs(info.maxMem);
    stats.setTargetMemoryKBs(info.memory);
    stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
    VmStats oldStats=null;
    final Calendar now=Calendar.getInstance();
    oldStats=_vmStats.get(vmName);
    long elapsedTime=0;
    if (oldStats != null) {
      elapsedTime=now.getTimeInMillis() - oldStats._timestamp.getTimeInMillis();
      double utilization=(info.cpuTime - oldStats._usedTime) / ((double)elapsedTime * 1000000);
      final NodeInfo node=conn.nodeInfo();
      utilization=utilization / node.cpus;
      if (utilization > 0) {
        stats.setCPUUtilization(utilization * 100);
      }
    }
    final List<InterfaceDef> vifs=getInterfaces(conn,vmName);
    long rx=0;
    long tx=0;
    for (    final InterfaceDef vif : vifs) {
      final DomainInterfaceStats ifStats=dm.interfaceStats(vif.getDevName());
      rx+=ifStats.rx_bytes;
      tx+=ifStats.tx_bytes;
    }
    if (oldStats != null) {
      final double deltarx=rx - oldStats._rx;
      if (deltarx > 0) {
        stats.setNetworkReadKBs(deltarx / 1024);
      }
      final double deltatx=tx - oldStats._tx;
      if (deltatx > 0) {
        stats.setNetworkWriteKBs(deltatx / 1024);
      }
    }
    final List<DiskDef> disks=getDisks(conn,vmName);
    long io_rd=0;
    long io_wr=0;
    long bytes_rd=0;
    long bytes_wr=0;
    for (    final DiskDef disk : disks) {
      final DomainBlockStats blockStats=dm.blockStats(disk.getDiskLabel());
      io_rd+=blockStats.rd_req;
      io_wr+=blockStats.wr_req;
      bytes_rd+=blockStats.rd_bytes;
      bytes_wr+=blockStats.wr_bytes;
    }
    if (oldStats != null) {
      final long deltaiord=io_rd - oldStats._ioRead;
      if (deltaiord > 0) {
        stats.setDiskReadIOs(deltaiord);
      }
      final long deltaiowr=io_wr - oldStats._ioWrote;
      if (deltaiowr > 0) {
        stats.setDiskWriteIOs(deltaiowr);
      }
      final double deltabytesrd=bytes_rd - oldStats._bytesRead;
      if (deltabytesrd > 0) {
        stats.setDiskReadKBs(deltabytesrd / 1024);
      }
      final double deltabyteswr=bytes_wr - oldStats._bytesWrote;
      if (deltabyteswr > 0) {
        stats.setDiskWriteKBs(deltabyteswr / 1024);
      }
    }
    final VmStats newStat=new VmStats();
    newStat._usedTime=info.cpuTime;
    newStat._rx=rx;
    newStat._tx=tx;
    newStat._ioRead=io_rd;
    newStat._ioWrote=io_wr;
    newStat._bytesRead=bytes_rd;
    newStat._bytesWrote=bytes_wr;
    newStat._timestamp=now;
    _vmStats.put(vmName,newStat);
    return stats;
  }
  finally {
    if (dm != null) {
      dm.free();
    }
  }
}