Menu

multiple ping requests in multithread environment on windows hangs execution

Sham
2017-03-06
2017-03-06
  • Sham

    Sham - 2017-03-06

    Below is the code I trying to execute

    ArrayList<String> devices = new ArrayList<String>();
    for(int i=0; i<255; i++) {
    ip="198.127.1." + i;
    devices.add(ip);
    }
    ExecutorService es = Executors.newFixedThreadPool(devices.size());
    for(final String device : devices) {
    es.submit(new Runnable() {
    @Override
    public void run() {
    ConcurrentHashMap<String, Object=""> devDataMap = new ConcurrentHashMap<>();
    // Ping
    try {
    final IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
    request.setHost(device);
    final IcmpPingResponse respIcmp = IcmpPingUtil.executePingRequest(request);
    if(respIcmp.getSuccessFlag()) {
    devDataMap.put("status", "up");
    devDataMap.put("latenancy", respIcmp.getRtt());
    } else {
    devDataMap.put("status", "down");
    devDataMap.put("error", respIcmp.getErrorMessage());
    }
    } catch(Exception e) {
    devDataMap.put("status", "down");
    devDataMap.put("error", e);
    }
    System.out.println(devDataMap);
    }
    });
    }
    es.shutdown();
    while(!es.isTerminated()) {}
    }

    When I ping 231 ips simultaneousely, it works though it takes long time around 15 mins. But if I ping ips more than 231 then code execution hangs. Same code works fine on linux for 231 or more ips. Can anyone help me to fix this on windows platform?

     
  • laurent buhler

    laurent buhler - 2017-03-06

    Hey Sham,
    not that I want to dodge the question :) but are you sure you want to create a pool with 254 threads !!
    It seems overkill whatever platform you are running on
    L.

     
  • Sham

    Sham - 2017-03-07

    Hey laurent,
    My requirement is to ping 254 or even more ips simultaneousely, because serially it will take long time. Can you please suggest me how to do this in other way, if a pool of 254 threads will overkill.

     
  • Sal Ingrilli

    Sal Ingrilli - 2017-03-07

    Sham,
    The icmp4j Windows implementation uses the Windows IcmpSendEcho native API, which is single-threaded. So you could use 1 milion thread - they will all exectuve one at a time!

    The only way to perform parallel windows pings with icmp4j, is by doing this:
    Icmp4jUtil.initialize ();
    final NativeBridge nativeBridge = new WindowsProcessNativeBridge ();
    nativeBridge.initialize ();
    Icmp4jUtil.setNativeBridge (nativeBridge);

    This tells icmp4j to perform pings using "ping.exe". You will then get parallelism, and if you are running with low-grade systems with not much memory/cpu, I do recommend you bring your thread pool size down to say 64. Only you will be able to tell if your hardware can handle 64 ping.exe processes running at the same time (I am sure it can - it just may cause a little thrashing).

    ALTERNATIVELY, if you want to execute ICMP pings in parallel, try the shortpasta-icmp library:
    https://sourceforge.net/projects/shortpasta-icmp/?source=directory
    This library implements ICMP over raw sockets, and it is capable of executing ICMP pings in parallel.
    The problem with raw sockets, however, is that your process needs to have privileges (running as administrator), so you will probably have to escalate your command prompt to administrator, or set your service user appropriately.

    As for the number of threads, 254, nothing wrong with that (at least in Windows) - especially for async network connections. The threads will mostly spend their time waiting for the response, so this is a perfect use of threads, even if you only have 1 CPU core.

    If your company wants to sponsor the development of multi-threaded Windows pings natively, we are willing to give it a try for you. We can at least add raw packet ICMP support to icmp4j, and ideally we would be able to do it with the win32 IcmpSendEcho2 function. Depending on which OSs you need to support. If that's the case, we can talk about it in a private thread...

    Sal.

     

Log in to post a comment.