Skip to content

Commit

Permalink
Merge pull request #31459 from vespa-engine/jonmv/truncate-heap-size-…
Browse files Browse the repository at this point in the history
…for-hosted-app-containers

Truncate app container heap size to 4 significant bits (when hosted)
  • Loading branch information
jonmv authored Jun 5, 2024
2 parents 2dfea71 + 7aab863 commit 256c6bf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,25 @@ public void getConfig(MetricsProxyApiConfig.Builder builder) {
public void getConfig(QrStartConfig.Builder builder) {
super.getConfig(builder);
var memoryPct = getMemoryPercentage().orElse(null);
int heapsize = memoryPct != null && memoryPct.asAbsoluteGb().isPresent()
? (int) (memoryPct.asAbsoluteGb().getAsDouble() * 1024) : 1536;
int heapsize = truncateTo4SignificantBits(memoryPct != null && memoryPct.asAbsoluteGb().isPresent()
? (int) (memoryPct.asAbsoluteGb().getAsDouble() * 1024) : 1536);
builder.jvm.verbosegc(true)
.availableProcessors(0)
.compressedClassSpaceSize(0)
.minHeapsize(heapsize)
.minHeapsize(heapsize) // These cause restarts when changed, so we try to keep them stable.
.heapsize(heapsize);
if (memoryPct != null) builder.jvm.heapSizeAsPercentageOfPhysicalMemory(memoryPct.ofContainerAvailable());
}

static int truncateTo4SignificantBits(int i) {
if (i == Integer.MIN_VALUE) return i;
if (i < 0) return -truncateTo4SignificantBits(-i);
if (i <= 16) return i;
int mask = Integer.highestOneBit(i);
mask += mask - (mask >> 3);
return i & mask;
}

@Override
public void getConfig(ZookeeperServerConfig.Builder builder) {
if (getParent() instanceof ConfigserverCluster) return; // Produces its own config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static com.yahoo.config.provision.SystemName.main;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand Down Expand Up @@ -498,4 +499,28 @@ private static String expectedJvmArgs(boolean isHosted, String extra) {
(extra.isEmpty() ? "" : " " + extra);
}

@Test
void testTruncationTo4Bits() {
assertEquals(0, ApplicationContainerCluster.truncateTo4SignificantBits(0));
assertEquals(1, ApplicationContainerCluster.truncateTo4SignificantBits(1));
assertEquals(15, ApplicationContainerCluster.truncateTo4SignificantBits(15));
assertEquals(16, ApplicationContainerCluster.truncateTo4SignificantBits(16));
assertEquals(16, ApplicationContainerCluster.truncateTo4SignificantBits(17));
assertEquals(18, ApplicationContainerCluster.truncateTo4SignificantBits(18));
assertEquals(18, ApplicationContainerCluster.truncateTo4SignificantBits(19));
assertEquals(30, ApplicationContainerCluster.truncateTo4SignificantBits(30));
assertEquals(30, ApplicationContainerCluster.truncateTo4SignificantBits(31));
assertEquals(32, ApplicationContainerCluster.truncateTo4SignificantBits(32));
assertEquals(32, ApplicationContainerCluster.truncateTo4SignificantBits(33));
assertEquals(32, ApplicationContainerCluster.truncateTo4SignificantBits(34));
assertEquals(32, ApplicationContainerCluster.truncateTo4SignificantBits(35));
assertEquals(36, ApplicationContainerCluster.truncateTo4SignificantBits(36));
assertEquals(0x78000000, ApplicationContainerCluster.truncateTo4SignificantBits(0x78000000));
assertEquals(0x78000000, ApplicationContainerCluster.truncateTo4SignificantBits(0x7fffffff));
assertEquals(0x80000000, ApplicationContainerCluster.truncateTo4SignificantBits(0x80000000));
assertEquals(0b10001000000000000000000000000000, ApplicationContainerCluster.truncateTo4SignificantBits(0b10000000000000000000000000000001));
assertEquals(0b11000100000000000000000000000000, ApplicationContainerCluster.truncateTo4SignificantBits(0b11000000000000000000000000000001));
assertEquals(0b11111111111111111111111111100010, ApplicationContainerCluster.truncateTo4SignificantBits(0b11111111111111111111111111100001));
}

}

0 comments on commit 256c6bf

Please sign in to comment.