forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log the sizes allocated through the SimpleMemoryPool for analysis.
- Loading branch information
Showing
9 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
clients/src/main/java/org/apache/kafka/common/memory/MemoryPoolStatsStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.apache.kafka.common.memory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
public class MemoryPoolStatsStore { | ||
private static final Logger log = LoggerFactory.getLogger(MemoryPoolStatsStore.class); | ||
|
||
private final AtomicInteger[] histogram; | ||
private final int maxSizeBytes; | ||
private final int segmentSizeBytes; | ||
|
||
public static class Range { | ||
public final int startInclusive; | ||
public final int endInclusive; | ||
|
||
public Range(int startInclusive, int endInclusive) { | ||
this.startInclusive = startInclusive; | ||
this.endInclusive = endInclusive; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Range{" + "startInclusive=" + startInclusive + ", endInclusive=" + endInclusive + '}'; | ||
} | ||
} | ||
|
||
public MemoryPoolStatsStore(int segments, int maxSizeBytes) { | ||
histogram = new AtomicInteger[segments]; | ||
this.maxSizeBytes = maxSizeBytes; | ||
segmentSizeBytes = (int) Math.ceil((double) maxSizeBytes / segments); | ||
for (int segmentIndex = 0; segmentIndex < segments; segmentIndex++) { | ||
histogram[segmentIndex] = new AtomicInteger(); | ||
} | ||
} | ||
|
||
private int getSegmentIndexForBytes(int bytes) { | ||
if (bytes == 0) { | ||
throw new IllegalArgumentException("Requested zero bytes for allocation."); | ||
} | ||
if (bytes > maxSizeBytes) { | ||
log.debug("Requested bytes {} for allocation exceeds maximum recorded value {}", bytes, maxSizeBytes); | ||
return -1; | ||
} else { | ||
return (bytes - 1) / segmentSizeBytes; | ||
} | ||
} | ||
|
||
public void recordAllocation(int bytes) { | ||
try { | ||
final int segmentIndex = getSegmentIndexForBytes(bytes); | ||
if (segmentIndex != -1) { | ||
histogram[segmentIndex].incrementAndGet(); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
log.error("Encountered error when trying to record memory allocation for request", e); | ||
} | ||
} | ||
|
||
public synchronized Map<Range, Integer> getFrequencies() { | ||
Map<Range, Integer> frequenciesMap = new HashMap<>(); | ||
for (int segmentIndex = 0; segmentIndex < histogram.length; segmentIndex++) { | ||
frequenciesMap.put(new Range( | ||
segmentIndex * segmentSizeBytes + 1, | ||
segmentIndex * segmentSizeBytes + segmentSizeBytes | ||
), histogram[segmentIndex].intValue()); | ||
} | ||
return frequenciesMap; | ||
} | ||
|
||
public synchronized void clear() { | ||
for (AtomicInteger atomicInteger : histogram) { | ||
atomicInteger.set(0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/src/main/scala/kafka/server/MemoryPoolStatsLogger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package kafka.server | ||
|
||
import com.typesafe.scalalogging.Logger | ||
import kafka.utils.Logging | ||
import org.apache.kafka.common.memory.MemoryPoolStatsStore | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
object MemoryPoolStatsLogger { | ||
private val logger = Logger("memory.pool.stats.logger") | ||
} | ||
|
||
class MemoryPoolStatsLogger extends Logging { | ||
override lazy val logger = MemoryPoolStatsLogger.logger | ||
|
||
def logStats(memoryPoolStatsStore: MemoryPoolStatsStore): Unit = { | ||
val frequencyList = memoryPoolStatsStore.getFrequencies.asScala.toSeq.sortBy(_._1.startInclusive) | ||
frequencyList.foreach { | ||
case (range, frequency) => | ||
info(s"[${range.startInclusive}-${range.endInclusive}] = $frequency") | ||
} | ||
} | ||
} |