what does IN_NATIVE mean in jstack file ?

Simon Su picture Simon Su · Feb 13, 2017 · Viewed 7.8k times · Source
    Thread 10296: (state = IN_NATIVE)
 - sun.nio.ch.Net.connect0(boolean, java.io.FileDescriptor, java.net.InetAddress, int) @bci=0 (Interpreted frame)
 - sun.nio.ch.Net.connect(java.net.ProtocolFamily, java.io.FileDescriptor, java.net.InetAddress, int) @bci=25, line=465 (Interpreted frame)
 - sun.nio.ch.Net.connect(java.io.FileDescriptor, java.net.InetAddress, int) @bci=6, line=457 (Interpreted frame)
 - sun.nio.ch.SocketChannelImpl.connect(java.net.SocketAddress) @bci=225, line=670 (Interpreted frame)
 - kafka.network.BlockingChannel.connect() @bci=135 (Interpreted frame)
 - kafka.producer.SyncProducer.connect() @bci=21 (Interpreted frame)
 - kafka.producer.SyncProducer.getOrMakeConnection() @bci=11 (Interpreted frame)
 - kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(kafka.api.RequestOrResponse, boolean) @bci=13 (Interpreted frame)
 - kafka.producer.SyncProducer.send(kafka.api.TopicMetadataRequest) @bci=6 (Interpreted frame)
 - kafka.client.ClientUtils$.fetchTopicMetadata(scala.collection.Set, scala.collection.Seq, kafka.producer.ProducerConfig, int, kafka.auth.KafkaUser) @bci=189 (Interpreted frame)
 - kafka.producer.BrokerPartitionInfo.updateInfo(scala.collection.immutable.Set, int) @bci=24 (Interpreted frame)
 - kafka.producer.async.DefaultEventHandler$$anonfun$handle$2.apply$mcV$sp() @bci=54 (Interpreted frame)
 - kafka.utils.Utils$.swallow(scala.Function2, scala.Function0) @bci=1 (Interpreted frame)
 - kafka.utils.Logging$class.swallowError(kafka.utils.Logging, scala.Function0) @bci=12 (Interpreted frame)
 - kafka.utils.Utils$.swallowError(scala.Function0) @bci=2 (Interpreted frame)
 - kafka.producer.async.DefaultEventHandler.handle(scala.collection.Seq) @bci=269 (Interpreted frame)
 - kafka.producer.Producer.send(scala.collection.Seq) @bci=45 (Interpreted frame)
 - kafka.javaapi.producer.Producer.send(kafka.producer.KeyedMessage) @bci=21 (Interpreted frame)
 - com.tmri.cld.impl.kafka.SjKafkaClientUtil.sendMessage(java.lang.String, java.lang.String, byte[]) @bci=40, line=141 (Interpreted frame)
 - com.tmri.stream.handle.thread.w.SendLogDataThreadW.run() @bci=356, line=78 (Interpreted frame)
 - java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) @bci=95, line=1145 (Interpreted frame)
 - java.util.concurrent.ThreadPoolExecutor$Worker.run() @bci=5, line=615 (Interpreted frame)
 - java.lang.Thread.run() @bci=11, line=745 (Interpreted frame)

As show as above, I'm using jstack to get output and for debugging, I have googled and still not get the clear answer , can anybody tell me what does IN_NATIVE status mean in jstack output file ?

Answer

James Fry picture James Fry · Feb 13, 2017

At some point most code has to interact with the operating system and 'physical' hardware. Java does this through native code, and the JStack state of IN_NATIVE reflects this - it is running system "native" code rather than Java code (eg to write data to a file). See the answers to this question for definitions of native code.

The states you see are VM dependent, but most likely you are running something with Hotspot or similar (OpenJDK, Oracle JDK etc) so the following definitions should hold:

  • UNINITIALIZED - Should never happen (missing initialization)
  • NEW - Just starting up, i.e., in process of being initialized
  • IN_NATIVE - Running in native code
  • IN_NATIVE_TRANS - Corresponding transition state
  • IN_VM - Running in VM
  • IN_VM_TRANS - Corresponding transition state
  • IN_JAVA - Running in Java or in stub code
  • BLOCKED - Blocked in vm
  • BLOCKED_TRANS - Corresponding transition state

The _TRANS suffix is used to indicate threads are transitioning between states. Note the states IN_JAVA_TRANS and NEW_TRANS are not used, so I've excluded them from the above list.