/** Constructs a PrintGCStat object to monitor a remote JVM. */
  public PrintGCStat(MBeanServerConnection server) throws IOException {
    // Create the platform mxbean proxies
    this.rmbean = newPlatformMXBeanProxy(server, RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    this.mmbean = newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    ObjectName poolName = null;
    ObjectName gcName = null;
    try {
      poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
      gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
    } catch (MalformedObjectNameException e) {
      // should not reach here
      assert (false);
    }

    Set<ObjectName> mbeans = server.queryNames(poolName, null);
    if (mbeans != null) {
      pools = new ArrayList<MemoryPoolMXBean>();
      for (ObjectName objName : mbeans) {
        MemoryPoolMXBean p =
            newPlatformMXBeanProxy(server, objName.getCanonicalName(), MemoryPoolMXBean.class);
        pools.add(p);
      }
    }

    mbeans = server.queryNames(gcName, null);
    if (mbeans != null) {
      gcmbeans = new ArrayList<GarbageCollectorMXBean>();
      for (ObjectName objName : mbeans) {
        GarbageCollectorMXBean gc =
            newPlatformMXBeanProxy(
                server, objName.getCanonicalName(), GarbageCollectorMXBean.class);
        gcmbeans.add(gc);
      }
    }
  }
  private static void checkGarbageCollectionNotificationInfoContent(
      GarbageCollectionNotificationInfo notif) throws Exception {
    System.out.println("GC notification for " + notif.getGcName());
    System.out.print("Action: " + notif.getGcAction());
    System.out.println(" Cause: " + notif.getGcCause());
    GcInfo info = notif.getGcInfo();
    System.out.print("GC Info #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc();

    List<String> pnames = new ArrayList<String>();
    for (Map.Entry entry : usage.entrySet()) {
      String poolname = (String) entry.getKey();
      pnames.add(poolname);
      MemoryUsage busage = (MemoryUsage) entry.getValue();
      MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
      if (ausage == null) {
        throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname);
      }
      System.out.println("Usage for pool " + poolname);
      System.out.println("   Before GC: " + busage);
      System.out.println("   After GC: " + ausage);
    }

    // check if memory usage for all memory pools are returned
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean p : pools) {
      if (!pnames.contains(p.getName())) {
        throw new RuntimeException(
            "GcInfo does not contain " + "memory usage for pool " + p.getName());
      }
    }
  }
Beispiel #3
0
  public synchronized Collection<GarbageCollectorMXBean> getGarbageCollectorMXBeans()
      throws IOException {

    // TODO: How to deal with changes to the list??
    if (garbageCollectorMBeans == null) {
      ObjectName gcName = null;
      try {
        gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
      } catch (MalformedObjectNameException e) {
        // should not reach here
        assert (false);
      }
      Set<ObjectName> mbeans = server.queryNames(gcName, null);
      if (mbeans != null) {
        garbageCollectorMBeans = new ArrayList<GarbageCollectorMXBean>();
        Iterator<ObjectName> iterator = mbeans.iterator();
        while (iterator.hasNext()) {
          ObjectName on = (ObjectName) iterator.next();
          String name = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",name=" + on.getKeyProperty("name");

          GarbageCollectorMXBean mBean =
              newPlatformMXBeanProxy(server, name, GarbageCollectorMXBean.class);
          garbageCollectorMBeans.add(mBean);
        }
      }
    }
    return garbageCollectorMBeans;
  }
Beispiel #4
0
  public static void main(String[] args) throws InterruptedException, NoSuchMethodException {
    int nthreads = Integer.parseInt(args[0]);
    int ncounters = Integer.parseInt(args[1]);
    String type = args[2];
    int nexecutions = Integer.parseInt(args[3]);

    List<Counter> counters = new ArrayList<Counter>();
    for (int i = 0; i < ncounters; i++) {
      Lock lock;
      if (type.equals("s")) lock = new safe.ReentrantLock();
      else if (type.equals("d")) lock = new safe.ReentrantLockDirect();
      else lock = new java.util.concurrent.locks.ReentrantLock();
      counters.add(new Counter(lock));
    }

    runExecutionBoundedTest(nthreads, counters, nexecutions);
  }
 List<Edge> detectDeadlock() {
   Collection<Map<String, MapLockState>> collection =
       (Collection<Map<String, MapLockState>>) callOnAllMembers(new LockInformationCallable());
   List<Vertex> graph = new ArrayList<Vertex>();
   for (Map<String, MapLockState> mapLockStateMap : collection) {
     for (MapLockState map : mapLockStateMap.values()) {
       for (Object key : map.getLockOwners().keySet()) {
         Vertex owner = new Vertex(map.getLockOwners().get(key));
         Vertex requester = new Vertex(map.getLockRequested().get(key));
         int index = graph.indexOf(owner);
         if (index >= 0) {
           owner = graph.get(index);
         } else {
           graph.add(owner);
         }
         index = graph.indexOf(requester);
         if (index >= 0) {
           requester = graph.get(index);
         } else {
           graph.add(requester);
         }
         Edge edge = new Edge();
         edge.from = requester;
         edge.to = owner;
         edge.key = key;
         edge.mapName = map.getMapName();
         edge.globalLock = map.isGlobalLock();
         owner.addIncoming(edge);
         requester.addOutgoing(edge);
       }
     }
   }
   List<Edge> list = new ArrayList<Edge>();
   if (graph != null && graph.size() > 0) {
     try {
       graph.get(0).visit(list);
     } catch (RuntimeException e) {
     }
   }
   return list;
 }
Beispiel #6
0
  private static void runExecutionBoundedTest(int nthreads, List<Counter> counters, int nexecutions)
      throws InterruptedException {
    final List<CounterThread> threads = new ArrayList<CounterThread>();
    for (int i = 0; i < nthreads; i++) {
      CounterThread t;
      t = new CounterThread(counters.get(i % counters.size()), nexecutions);
      t.start();
      threads.add(t);
    }

    CounterThread.shoot(); // let all the threads go crazy at the same time

    for (int i = 0; i < nthreads; i++) {
      CounterThread t = threads.get(i);
      t.join(30000);
      if (t.isAlive()) {
        System.out.println("stuck thread name: " + t.toString());
        System.out.println("stuck thread state: " + t.getState());
        safe.ReentrantLock l = (safe.ReentrantLock) t.getLock();
        System.out.println("thread waiting lock: " + l);
        System.out.println("stuck thread increments: " + t.getExecutions());
        System.out.println("stuck thread counter value: " + t.getCounterCount());
        Thread other = l.getOwner();
        System.out.println("lock owner: " + other);
        if (other != null) {
          System.out.println("owner name: " + other.toString());
          System.out.println("state owner: " + other.getState());
        }
        // Keep program alive to dump thread stacks with: jstack -l $(pidof java)
        System.out.println(
            java.lang.management.ManagementFactory.getThreadMXBean().getPeakThreadCount());
        while (true) {}
      }
    }
    long sum = 0;
    for (int i = 0; i < counters.size(); ++i) {
      sum += counters.get(i).getCount();
    }
    System.out.println(sum);
    System.out.println(
        java.lang.management.ManagementFactory.getThreadMXBean().getPeakThreadCount());
  }
Beispiel #7
0
  public Collection<MemoryPoolProxy> getMemoryPoolProxies() throws IOException {

    // TODO: How to deal with changes to the list??
    if (memoryPoolProxies == null) {
      ObjectName poolName = null;
      try {
        poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
      } catch (MalformedObjectNameException e) {
        // should not reach here
        assert (false);
      }
      Set<ObjectName> mbeans = server.queryNames(poolName, null);
      if (mbeans != null) {
        memoryPoolProxies = new ArrayList<MemoryPoolProxy>();
        Iterator<ObjectName> iterator = mbeans.iterator();
        while (iterator.hasNext()) {
          ObjectName objName = (ObjectName) iterator.next();
          MemoryPoolProxy p = new MemoryPoolProxy(this, objName);
          memoryPoolProxies.add(p);
        }
      }
    }
    return memoryPoolProxies;
  }