Home / Class/ PerThreadState Class — netty Architecture

PerThreadState Class — netty Architecture

Architecture documentation for the PerThreadState class in BurstCostExecutorsBenchmark.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  896af492_7d6f_2782_9162_a58f217dc636["PerThreadState"]
  b13bc1c1_6503_8c67_bdc7_874bea111591["BurstCostExecutorsBenchmark.java"]
  896af492_7d6f_2782_9162_a58f217dc636 -->|defined in| b13bc1c1_6503_8c67_bdc7_874bea111591
  11bdb437_0419_7580_3709_276c968eb1f1["setup()"]
  896af492_7d6f_2782_9162_a58f217dc636 -->|method| 11bdb437_0419_7580_3709_276c968eb1f1
  ceb292d6_44ba_a8b2_76fb_0df3236e3a9a["resetCompleted()"]
  896af492_7d6f_2782_9162_a58f217dc636 -->|method| ceb292d6_44ba_a8b2_76fb_0df3236e3a9a
  cc81d8c1_743a_4ebb_a711_f38d36ab5698["spinWaitCompletionOf()"]
  896af492_7d6f_2782_9162_a58f217dc636 -->|method| cc81d8c1_743a_4ebb_a711_f38d36ab5698

Relationship Graph

Source Code

microbench/src/main/java/io/netty/microbench/concurrent/BurstCostExecutorsBenchmark.java lines 235–297

    @State(Scope.Thread)
    public static class PerThreadState {
        //To reduce the benchmark noise we avoid using AtomicInteger that would
        //suffer of false sharing while reading/writing the counter due to the surrounding
        //instances on heap: thanks to JMH the "completed" field will be padded
        //avoiding false-sharing for free
        private static final AtomicIntegerFieldUpdater<PerThreadState> DONE_UPDATER =
                AtomicIntegerFieldUpdater.newUpdater(PerThreadState.class, "completed");
        private volatile int completed;

        private Runnable completeTask;

        @Setup
        public void setup(BurstCostExecutorsBenchmark bench) {
            final int work = bench.work;
            if (work > 0) {
                completeTask = new Runnable() {
                    @Override
                    public void run() {
                        Blackhole.consumeCPU(work);
                        //We can avoid the full barrier cost of a volatile set given that the
                        //benchmark is focusing on executors with a single threaded consumer:
                        //it would reduce the cost on consumer side while allowing to focus just
                        //to the threads hand-off/wake-up cost
                        DONE_UPDATER.lazySet(PerThreadState.this, completed + 1);
                    }
                };
            } else {
                completeTask = new Runnable() {
                    @Override
                    public void run() {
                        //We can avoid the full barrier cost of a volatile set given that the
                        //benchmark is focusing on executors with a single threaded consumer:
                        //it would reduce the cost on consumer side while allowing to focus just
                        //to the threads hand-off/wake-up cost
                        DONE_UPDATER.lazySet(PerThreadState.this, completed + 1);
                    }
                };
            }
        }

        /**
         * Single-writer reset of completed counter.
         */
        public void resetCompleted() {
            //We can avoid the full barrier cost of a volatile set given that
            //the counter can be reset from a single thread and it should be reset
            //only after any submitted tasks are completed
            DONE_UPDATER.lazySet(this, 0);
        }

        /**
         * It would spin-wait until at least {@code value} tasks are being completed.
         */
        public int spinWaitCompletionOf(int value) {
            while (true) {
                final int lastRead = this.completed;
                if (lastRead >= value) {
                    return lastRead;
                }
            }
        }
    }

Frequently Asked Questions

What is the PerThreadState class?
PerThreadState is a class in the netty codebase, defined in microbench/src/main/java/io/netty/microbench/concurrent/BurstCostExecutorsBenchmark.java.
Where is PerThreadState defined?
PerThreadState is defined in microbench/src/main/java/io/netty/microbench/concurrent/BurstCostExecutorsBenchmark.java at line 235.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free