Java Code Examples for io.perfmark.PerfMark#linkOut()

The following examples show how to use io.perfmark.PerfMark#linkOut() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyServerStream.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public void runOnTransportThread(final Runnable r) {
  if (eventLoop.inEventLoop()) {
    r.run();
  } else {
    final Link link = PerfMark.linkOut();
    eventLoop.execute(new Runnable() {
      @Override
      public void run() {
        PerfMark.startTask("NettyServerStream$TransportState.runOnTransportThread", tag);
        PerfMark.linkIn(link);
        try {
          r.run();
        } finally {
          PerfMark.stopTask("NettyServerStream$TransportState.runOnTransportThread", tag);
        }
      }
    });
  }
}
 
Example 2
Source File: PerfMarkTransformerTest.java    From perfmark with Apache License 2.0 5 votes vote down vote up
@Test
public void transform_link() throws Exception {
  PerfMark.setEnabled(true);
  Storage.resetForTest();

  final class ClzLocal {
    public ClzLocal() {
      PerfMark.startTask("task");
      Link link = PerfMark.linkOut();
      PerfMark.linkIn(link);
      PerfMark.stopTask("task");
    }
  }

  Class<?> clz = transformAndLoad(ClzLocal.class);
  Constructor<?> ctor = clz.getConstructor(PerfMarkTransformerTest.class);
  ctor.setAccessible(true);
  ctor.newInstance(this);
  List<Mark> marks = Storage.readForTest();
  assertThat(marks).hasSize(4);
  for (Mark mark : marks) {
    assertNotNull(mark.getMarker());
    StackTraceElement element = Internal.getElement(mark.getMarker());
    assertThat(element.getClassName()).isEqualTo(ClzLocal.class.getName());
    assertThat(element.getMethodName()).isEqualTo("<init>");
    assertThat(element.getFileName()).isEqualTo("PerfMarkTransformerTest.java");
    assertThat(element.getLineNumber()).isGreaterThan(0);
  }
}
 
Example 3
Source File: ServerImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void messagesAvailable(final MessageProducer producer) {
  PerfMark.startTask("ServerStreamListener.messagesAvailable", tag);
  final Link link = PerfMark.linkOut();

  final class MessagesAvailable extends ContextRunnable {

    MessagesAvailable() {
      super(context);
    }

    @Override
    public void runInContext() {
      PerfMark.startTask("ServerCallListener(app).messagesAvailable", tag);
      PerfMark.linkIn(link);
      try {
        getListener().messagesAvailable(producer);
      } catch (Throwable t) {
        internalClose(t);
        throw t;
      } finally {
        PerfMark.stopTask("ServerCallListener(app).messagesAvailable", tag);
      }
    }
  }

  try {
    callExecutor.execute(new MessagesAvailable());
  } finally {
    PerfMark.stopTask("ServerStreamListener.messagesAvailable", tag);
  }
}
 
Example 4
Source File: ServerImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void halfClosed() {
  PerfMark.startTask("ServerStreamListener.halfClosed", tag);
  final Link link = PerfMark.linkOut();

  final class HalfClosed extends ContextRunnable {
    HalfClosed() {
      super(context);
    }

    @Override
    public void runInContext() {
      PerfMark.startTask("ServerCallListener(app).halfClosed", tag);
      PerfMark.linkIn(link);
      try {
        getListener().halfClosed();
      } catch (Throwable t) {
        internalClose(t);
        throw t;
      } finally {
        PerfMark.stopTask("ServerCallListener(app).halfClosed", tag);
      }
    }
  }

  try {
    callExecutor.execute(new HalfClosed());
  } finally {
    PerfMark.stopTask("ServerStreamListener.halfClosed", tag);
  }
}
 
Example 5
Source File: ServerImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void closedInternal(final Status status) {
  // For cancellations, promptly inform any users of the context that their work should be
  // aborted. Otherwise, we can wait until pending work is done.
  if (!status.isOk()) {
    // The callExecutor might be busy doing user work. To avoid waiting, use an executor that
    // is not serializing.
    cancelExecutor.execute(new ContextCloser(context, status.getCause()));
  }
  final Link link = PerfMark.linkOut();

  final class Closed extends ContextRunnable {
    Closed() {
      super(context);
    }

    @Override
    public void runInContext() {
      PerfMark.startTask("ServerCallListener(app).closed", tag);
      PerfMark.linkIn(link);
      try {
        getListener().closed(status);
      } finally {
        PerfMark.stopTask("ServerCallListener(app).closed", tag);
      }
    }
  }

  callExecutor.execute(new Closed());
}
 
Example 6
Source File: ServerImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void onReady() {
  PerfMark.startTask("ServerStreamListener.onReady", tag);
  final Link link = PerfMark.linkOut();
  final class OnReady extends ContextRunnable {
    OnReady() {
      super(context);
    }

    @Override
    public void runInContext() {
      PerfMark.startTask("ServerCallListener(app).onReady", tag);
      PerfMark.linkIn(link);
      try {
        getListener().onReady();
      } catch (Throwable t) {
        internalClose(t);
        throw t;
      } finally {
        PerfMark.stopTask("ServerCallListener(app).onReady", tag);
      }
    }
  }

  try {
    callExecutor.execute(new OnReady());
  } finally {
    PerfMark.stopTask("ServerStreamListener.onReady", tag);
  }
}
 
Example 7
Source File: MigratingThreadDeframer.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void request(final int numMessages) {
  class RequestOp implements Op {
    @Override public void run(boolean isDeframerOnTransportThread) {
      if (isDeframerOnTransportThread) {
        final Link link = PerfMark.linkOut();
        // We may not be currently on the transport thread, so jump over to it and then do the
        // necessary processing
        transportExecutor.runOnTransportThread(new Runnable() {
          @Override public void run() {
            PerfMark.startTask("MigratingThreadDeframer.request");
            PerfMark.linkIn(link);
            try {
              // Since processing continues from transport thread while this runnable was
              // enqueued, the state may have changed since we ran runOnTransportThread. So we
              // must make sure deframerOnTransportThread==true
              requestFromTransportThread(numMessages);
            } finally {
              PerfMark.stopTask("MigratingThreadDeframer.request");
            }
          }
        });
        return;
      }
      PerfMark.startTask("MigratingThreadDeframer.request");
      try {
        deframer.request(numMessages);
      } catch (Throwable t) {
        appListener.deframeFailed(t);
        deframer.close(); // unrecoverable state
      } finally {
        PerfMark.stopTask("MigratingThreadDeframer.request");
      }
    }
  }

  runWhereAppropriate(new RequestOp(), false);
}
 
Example 8
Source File: AbstractStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Called to request the given number of messages from the deframer. May be called from any
 * thread.
 */
private void requestMessagesFromDeframer(final int numMessages) {
  if (deframer instanceof ThreadOptimizedDeframer) {
    PerfMark.startTask("AbstractStream.request");
    try {
      deframer.request(numMessages);
    } finally {
      PerfMark.stopTask("AbstractStream.request");
    }
    return;
  }
  final Link link = PerfMark.linkOut();
  class RequestRunnable implements Runnable {
    @Override public void run() {
      PerfMark.startTask("AbstractStream.request");
      PerfMark.linkIn(link);
      try {
        deframer.request(numMessages);
      } catch (Throwable t) {
        deframeFailed(t);
      } finally {
        PerfMark.stopTask("AbstractStream.request");
      }
    }
  }

  runOnTransportThread(new RequestRunnable());
}
 
Example 9
Source File: SendGrpcFrameCommand.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
SendGrpcFrameCommand(StreamIdHolder stream, ByteBuf content, boolean endStream) {
  super(content);
  this.stream = stream;
  this.endStream = endStream;
  this.link = PerfMark.linkOut();
}
 
Example 10
Source File: WriteQueue.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
public RunnableCommand(Runnable runnable) {
  this.link = PerfMark.linkOut();
  this.runnable = runnable;
}
 
Example 11
Source File: WriteQueue.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
AbstractQueuedCommand() {
  this.link = PerfMark.linkOut();
}
 
Example 12
Source File: ClientCallImpl.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void headersRead(final Metadata headers) {
  PerfMark.startTask("ClientStreamListener.headersRead", tag);
  final Link link = PerfMark.linkOut();

  final class HeadersRead extends ContextRunnable {
    HeadersRead() {
      super(context);
    }

    @Override
    public void runInContext() {
      PerfMark.startTask("ClientCall$Listener.headersRead", tag);
      PerfMark.linkIn(link);
      try {
        runInternal();
      } finally {
        PerfMark.stopTask("ClientCall$Listener.headersRead", tag);
      }
    }

    private void runInternal() {
      if (closed) {
        return;
      }
      try {
        observer.onHeaders(headers);
      } catch (Throwable t) {
        Status status =
            Status.CANCELLED.withCause(t).withDescription("Failed to read headers");
        stream.cancel(status);
        close(status, new Metadata());
      }
    }
  }

  try {
    callExecutor.execute(new HeadersRead());
  } finally {
    PerfMark.stopTask("ClientStreamListener.headersRead", tag);
  }
}
 
Example 13
Source File: ClientCallImpl.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onReady() {
  if (method.getType().clientSendsOneMessage()) {
    return;
  }

  PerfMark.startTask("ClientStreamListener.onReady", tag);
  final Link link = PerfMark.linkOut();

  final class StreamOnReady extends ContextRunnable {
    StreamOnReady() {
      super(context);
    }

    @Override
    public void runInContext() {
      PerfMark.startTask("ClientCall$Listener.onReady", tag);
      PerfMark.linkIn(link);
      try {
        runInternal();
      } finally {
        PerfMark.stopTask("ClientCall$Listener.onReady", tag);
      }
    }

    private void runInternal() {
      try {
        observer.onReady();
      } catch (Throwable t) {
        Status status =
            Status.CANCELLED.withCause(t).withDescription("Failed to call onReady.");
        stream.cancel(status);
        close(status, new Metadata());
      }
    }
  }

  try {
    callExecutor.execute(new StreamOnReady());
  } finally {
    PerfMark.stopTask("ClientStreamListener.onReady", tag);
  }
}
 
Example 14
Source File: MigratingThreadDeframer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private boolean runWhereAppropriate(Op op, boolean currentThreadIsTransportThread) {
  boolean deframerOnTransportThreadCopy;
  boolean alreadyEnqueued;
  synchronized (lock) {
    deframerOnTransportThreadCopy = deframerOnTransportThread;
    alreadyEnqueued = messageProducerEnqueued;
    if (!deframerOnTransportThreadCopy) {
      opQueue.offer(op);
      messageProducerEnqueued = true;
    }
  }
  if (deframerOnTransportThreadCopy) {
    op.run(/*isDeframerOnTransportThread=*/true);
    return true;
  } else {
    if (!alreadyEnqueued) {
      if (currentThreadIsTransportThread) {
        PerfMark.startTask("MigratingThreadDeframer.messageAvailable");
        try {
          transportListener.messagesAvailable(messageProducer);
        } finally {
          PerfMark.stopTask("MigratingThreadDeframer.messageAvailable");
        }
      } else {
        final Link link = PerfMark.linkOut();
        // SLOW path. This is the "normal" thread-hopping approach for request() when _not_ using
        // MigratingThreadDeframer
        transportExecutor.runOnTransportThread(new Runnable() {
          @Override public void run() {
            PerfMark.startTask("MigratingThreadDeframer.messageAvailable");
            PerfMark.linkIn(link);
            try {
              transportListener.messagesAvailable(messageProducer);
            } finally {
              PerfMark.stopTask("MigratingThreadDeframer.messageAvailable");
            }
          }
        });
      }
    }
    return false;
  }
}