• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #3896

pending completion
#3896

push

github-actions

ben-manes
upgrade jamm library (memory meter)

7542 of 7616 relevant lines covered (99.03%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedBuffer.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.cache;
17

18
import java.lang.invoke.MethodHandles;
19
import java.lang.invoke.VarHandle;
20
import java.util.function.Consumer;
21

22
/**
23
 * A striped, non-blocking, bounded buffer.
24
 *
25
 * @author ben.manes@gmail.com (Ben Manes)
26
 * @param <E> the type of elements maintained by this buffer
27
 */
28
final class BoundedBuffer<E> extends StripedBuffer<E> {
1✔
29
  /*
30
   * A circular ring buffer stores the elements being transferred by the producers to the consumer.
31
   * The monotonically increasing count of reads and writes allow indexing sequentially to the next
32
   * element location based upon a power-of-two sizing.
33
   *
34
   * The producers race to read the counts, check if there is available capacity, and if so then try
35
   * once to CAS to the next write count. If the increment is successful then the producer lazily
36
   * publishes the element. The producer does not retry or block when unsuccessful due to a failed
37
   * CAS or the buffer being full.
38
   *
39
   * The consumer reads the counts and takes the available elements. The clearing of the elements
40
   * and the next read count are lazily set.
41
   *
42
   * This implementation is striped to further increase concurrency by rehashing and dynamically
43
   * adding new buffers when contention is detected, up to an internal maximum. When rehashing in
44
   * order to discover an available buffer, the producer may retry adding its element to determine
45
   * whether it found a satisfactory buffer or if resizing is necessary.
46
   */
47

48
  /** The maximum number of elements per buffer. */
49
  static final int BUFFER_SIZE = 16;
50
  static final int MASK = BUFFER_SIZE - 1;
51

52
  @Override
53
  protected Buffer<E> create(E e) {
54
    return new RingBuffer<>(e);
1✔
55
  }
56

57
  static final class RingBuffer<E> extends BBHeader.ReadAndWriteCounterRef implements Buffer<E> {
58
    static final VarHandle BUFFER = MethodHandles.arrayElementVarHandle(Object[].class);
1✔
59

60
    final Object[] buffer;
61

62
    public RingBuffer(E e) {
1✔
63
      buffer = new Object[BUFFER_SIZE];
1✔
64
      BUFFER.set(buffer, 0, e);
1✔
65
      WRITE.set(this, 1);
1✔
66
    }
1✔
67

68
    @Override
69
    public int offer(E e) {
70
      long head = readCounter;
1✔
71
      long tail = writeCounterOpaque();
1✔
72
      long size = (tail - head);
1✔
73
      if (size >= BUFFER_SIZE) {
1✔
74
        return Buffer.FULL;
1✔
75
      }
76
      if (casWriteCounter(tail, tail + 1)) {
1✔
77
        int index = (int) (tail & MASK);
1✔
78
        BUFFER.setRelease(buffer, index, e);
1✔
79
        return Buffer.SUCCESS;
1✔
80
      }
81
      return Buffer.FAILED;
1✔
82
    }
83

84
    @Override
85
    public void drainTo(Consumer<E> consumer) {
86
      long head = readCounter;
1✔
87
      long tail = writeCounterOpaque();
1✔
88
      long size = (tail - head);
1✔
89
      if (size == 0) {
1✔
90
        return;
1✔
91
      }
92
      do {
93
        int index = (int) (head & MASK);
1✔
94
        @SuppressWarnings("unchecked")
95
        E e = (E) BUFFER.getAcquire(buffer, index);
1✔
96
        if (e == null) {
1✔
97
          // not published yet
98
          break;
1✔
99
        }
100
        BUFFER.setRelease(buffer, index, null);
1✔
101
        consumer.accept(e);
1✔
102
        head++;
1✔
103
      } while (head != tail);
1✔
104
      setReadCounterOpaque(head);
1✔
105
    }
1✔
106

107
    @Override
108
    public long reads() {
109
      return readCounter;
1✔
110
    }
111

112
    @Override
113
    public long writes() {
114
      return writeCounter;
1✔
115
    }
116
  }
117
}
118

119
/** The namespace for field padding through inheritance. */
120
final class BBHeader {
×
121

122
  @SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
123
  abstract static class PadReadCounter {
1✔
124
    byte p000, p001, p002, p003, p004, p005, p006, p007;
125
    byte p008, p009, p010, p011, p012, p013, p014, p015;
126
    byte p016, p017, p018, p019, p020, p021, p022, p023;
127
    byte p024, p025, p026, p027, p028, p029, p030, p031;
128
    byte p032, p033, p034, p035, p036, p037, p038, p039;
129
    byte p040, p041, p042, p043, p044, p045, p046, p047;
130
    byte p048, p049, p050, p051, p052, p053, p054, p055;
131
    byte p056, p057, p058, p059, p060, p061, p062, p063;
132
    byte p064, p065, p066, p067, p068, p069, p070, p071;
133
    byte p072, p073, p074, p075, p076, p077, p078, p079;
134
    byte p080, p081, p082, p083, p084, p085, p086, p087;
135
    byte p088, p089, p090, p091, p092, p093, p094, p095;
136
    byte p096, p097, p098, p099, p100, p101, p102, p103;
137
    byte p104, p105, p106, p107, p108, p109, p110, p111;
138
    byte p112, p113, p114, p115, p116, p117, p118, p119;
139
  }
140

141
  /** Enforces a memory layout to avoid false sharing by padding the read count. */
142
  abstract static class ReadCounterRef extends PadReadCounter {
1✔
143
    volatile long readCounter;
144
  }
145

146
  abstract static class PadWriteCounter extends ReadCounterRef {
1✔
147
    byte p120, p121, p122, p123, p124, p125, p126, p127;
148
    byte p128, p129, p130, p131, p132, p133, p134, p135;
149
    byte p136, p137, p138, p139, p140, p141, p142, p143;
150
    byte p144, p145, p146, p147, p148, p149, p150, p151;
151
    byte p152, p153, p154, p155, p156, p157, p158, p159;
152
    byte p160, p161, p162, p163, p164, p165, p166, p167;
153
    byte p168, p169, p170, p171, p172, p173, p174, p175;
154
    byte p176, p177, p178, p179, p180, p181, p182, p183;
155
    byte p184, p185, p186, p187, p188, p189, p190, p191;
156
    byte p192, p193, p194, p195, p196, p197, p198, p199;
157
    byte p200, p201, p202, p203, p204, p205, p206, p207;
158
    byte p208, p209, p210, p211, p212, p213, p214, p215;
159
    byte p216, p217, p218, p219, p220, p221, p222, p223;
160
    byte p224, p225, p226, p227, p228, p229, p230, p231;
161
    byte p232, p233, p234, p235, p236, p237, p238, p239;
162
  }
163

164
  /** Enforces a memory layout to avoid false sharing by padding the write counter. */
165
  abstract static class ReadAndWriteCounterRef extends PadWriteCounter {
1✔
166
    static final VarHandle READ, WRITE;
167

168
    volatile long writeCounter;
169

170
    void setReadCounterOpaque(long count) {
171
      READ.setOpaque(this, count);
1✔
172
    }
1✔
173

174
    long writeCounterOpaque() {
175
      return (long) WRITE.getOpaque(this);
1✔
176
    }
177

178
    boolean casWriteCounter(long expect, long update) {
179
      return WRITE.weakCompareAndSet(this, expect, update);
1✔
180
    }
181

182
    static {
183
      var lookup = MethodHandles.lookup();
1✔
184
      try {
185
        READ = lookup.findVarHandle(ReadCounterRef.class, "readCounter", long.class);
1✔
186
        WRITE = lookup.findVarHandle(ReadAndWriteCounterRef.class, "writeCounter", long.class);
1✔
187
      } catch (ReflectiveOperationException e) {
×
188
        throw new ExceptionInInitializerError(e);
×
189
      }
1✔
190
    }
1✔
191
  }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc