Skip to content

Commit

Permalink
Introduce per-Priority Context with different policies
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozlov committed Jun 6, 2023
1 parent cdf4c35 commit 6f403ea
Show file tree
Hide file tree
Showing 29 changed files with 367 additions and 635 deletions.
9 changes: 2 additions & 7 deletions src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import jdk.crac.Resource;
import jdk.internal.access.JavaIOFileDescriptorAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.crac.Core;
import jdk.internal.crac.JDKResource;
import jdk.internal.crac.JDKResource.Priority;

import java.io.FileDescriptor;
import java.io.IOException;
Expand Down Expand Up @@ -136,7 +136,7 @@ private void initFDs() throws IOException {
initFDs();
// trigger FileDispatcherImpl initialization
new FileDispatcherImpl();
jdk.internal.crac.Core.getJDKContext().register(this);
Core.Priority.EPOLLSELECTOR.getContext().register(this);
}

private void ensureOpen() {
Expand Down Expand Up @@ -412,9 +412,4 @@ public void afterRestore(Context<? extends Resource> context) throws Exception {
}
}
}

@Override
public Priority getPriority() {
return Priority.EPOLLSELECTOR;
}
}
22 changes: 6 additions & 16 deletions src/java.base/share/classes/java/io/FileDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package java.io;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;

Expand Down Expand Up @@ -65,8 +64,6 @@ class Resource implements jdk.internal.crac.JDKResource {
final Exception stackTraceHolder;

Resource() {
JDKContext jdkContext = Core.getJDKContext();
jdkContext.register(this);
if (JDKContext.Properties.COLLECT_FD_STACKTRACES) {
// About the timestamp: we cannot format it nicely since this
// exception is sometimes created too early in the VM lifecycle
Expand All @@ -76,6 +73,7 @@ class Resource implements jdk.internal.crac.JDKResource {
} else {
stackTraceHolder = null;
}
Core.Priority.FILE_DESCRIPTORS.getContext().register(this);
}

@Override
Expand All @@ -90,11 +88,6 @@ public void afterRestore(Context<? extends jdk.crac.Resource> context) throws Ex
FileDescriptor.this.afterRestore();
}

@Override
public Priority getPriority() {
return Priority.FILE_DESCRIPTORS;
}

@Override
public String toString() {
return getClass().getName() + "(FD " + fd + ")";
Expand All @@ -111,15 +104,10 @@ public String toString() {
static {
initIDs();

Core.getJDKContext().register(checkpointListener = new JDKResource() {
@Override
public Priority getPriority() {
return Priority.NORMAL;
}

JDKResource resource = new JDKResource() {
@Override
public void beforeCheckpoint(Context<? extends jdk.crac.Resource> context) {
JDKContext ctx = (JDKContext) context;
JDKContext ctx = Core.getJDKContext();
ctx.claimFd(in, "System.in");
ctx.claimFd(out, "System.out");
ctx.claimFd(err, "System.err");
Expand All @@ -128,7 +116,9 @@ public void beforeCheckpoint(Context<? extends jdk.crac.Resource> context) {
@Override
public void afterRestore(Context<? extends jdk.crac.Resource> context) {
}
});
};
checkpointListener = resource;
Core.Priority.NORMAL.getContext().register(resource);
}

// Set up JavaIOFileDescriptorAccess in SharedSecrets
Expand Down
6 changes: 1 addition & 5 deletions src/java.base/share/classes/java/io/RandomAccessFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private class Resource implements JDKResource {
private static final JavaIOFileDescriptorAccess fdAccess = SharedSecrets.getJavaIOFileDescriptorAccess();

Resource() {
Core.getJDKContext().register(this);
Core.Priority.PRE_FILE_DESRIPTORS.getContext().register(this);
}

@Override
Expand All @@ -114,10 +114,6 @@ public void afterRestore(Context<? extends jdk.crac.Resource> context) throws Ex

}

@Override
public Priority getPriority() {
return Priority.PRE_FILE_DESRIPTORS;
}
}

Resource resource = new Resource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@

import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.crac.JDKResource;
import jdk.internal.ref.CleanerFactory;
import jdk.internal.ref.CleanerImpl;
import sun.invoke.util.Wrapper;

import java.lang.invoke.MethodHandles.Lookup;
import java.lang.ref.Cleaner;
import java.lang.reflect.Field;

import static java.lang.invoke.MethodHandleNatives.Constants.*;
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/lang/ref/Cleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

package java.lang.ref;

import jdk.internal.crac.JDKResource;
import jdk.internal.crac.Core;
import jdk.internal.ref.CleanerImpl;

import java.util.Objects;
Expand Down Expand Up @@ -218,13 +218,13 @@ public static Cleaner create(ThreadFactory threadFactory) {
public Cleanable register(Object obj, Runnable action) {
Objects.requireNonNull(obj, "obj");
Objects.requireNonNull(action, "action");
return new CleanerImpl.PhantomCleanableRef(obj, this, action, JDKResource.Priority.CLEANERS);
return new CleanerImpl.PhantomCleanableRef(obj, this, action, Core.Priority.CLEANERS);
}

/**
* Register an object and action and also register the underlying Reference with a CRaC priority.
*/
/*non-public*/ Cleanable register(Object obj, Runnable action, JDKResource.Priority priority) {
/*non-public*/ Cleanable register(Object obj, Runnable action, Core.Priority priority) {
Objects.requireNonNull(obj, "obj");
Objects.requireNonNull(action, "action");
return new CleanerImpl.PhantomCleanableRef(obj, this, action, priority);
Expand Down
10 changes: 3 additions & 7 deletions src/java.base/share/classes/java/lang/ref/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import jdk.crac.Context;
import jdk.crac.Resource;
import jdk.internal.crac.Core;
import jdk.internal.crac.JDKResource;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
Expand Down Expand Up @@ -333,17 +334,12 @@ public void runFinalization() {
}

@Override
public java.lang.ref.Cleaner.Cleanable cleanerRegisterWithPriority(java.lang.ref.Cleaner cleaner, Object obj, Runnable action, JDKResource.Priority priority) {
public java.lang.ref.Cleaner.Cleanable cleanerRegisterWithPriority(java.lang.ref.Cleaner cleaner, Object obj, Runnable action, Core.Priority priority) {
return cleaner.register(obj, action, priority);
}
});

referenceHandlerResource = new JDKResource() {
@Override
public Priority getPriority() {
return Priority.REFERENCE_HANDLER;
}

@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
System.gc();
Expand All @@ -355,7 +351,7 @@ public void beforeCheckpoint(Context<? extends Resource> context) throws Excepti
public void afterRestore(Context<? extends Resource> context) throws Exception {
}
};
jdk.internal.crac.Core.getJDKContext().register(referenceHandlerResource);
Core.Priority.REFERENCE_HANDLER.getContext().register(referenceHandlerResource);
}

/* -- Referent accessor and setters -- */
Expand Down
9 changes: 2 additions & 7 deletions src/java.base/share/classes/java/net/InetAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@

import jdk.crac.Context;
import jdk.crac.Resource;
import jdk.internal.crac.Core;
import jdk.internal.access.JavaNetInetAddressAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.crac.Core;
import jdk.internal.crac.JDKResource;
import sun.security.action.*;
import sun.net.InetAddressCachePolicy;
Expand Down Expand Up @@ -351,11 +351,6 @@ public byte[] addressBytes(Inet6Address inet6Address) {
// DNS cache is cleared before the checkpoint; application restored at a later point
// or in a different environment should query DNS again.
checkpointListener = new JDKResource() {
@Override
public Priority getPriority() {
return Priority.NORMAL;
}

@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
cache.clear();
Expand All @@ -366,7 +361,7 @@ public void beforeCheckpoint(Context<? extends Resource> context) throws Excepti
public void afterRestore(Context<? extends Resource> context) throws Exception {
}
};
Core.getJDKContext().register(checkpointListener);
Core.Priority.NORMAL.getContext().register(checkpointListener);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/java.base/share/classes/javax/crac/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

package javax.crac;

import jdk.crac.impl.BlockingOrderedContext;
import jdk.crac.impl.OrderedContext;

/**
Expand All @@ -37,7 +38,7 @@ public class Core {
private Core() {
}

private static final Context<Resource> globalContext = new ContextWrapper(new OrderedContext<>("Global Context (javax)"));
private static final Context<Resource> globalContext = new ContextWrapper(new BlockingOrderedContext<>());
static {
jdk.crac.Core.getGlobalContext().register(new ResourceWrapper(null, globalContext));
}
Expand Down
10 changes: 5 additions & 5 deletions src/java.base/share/classes/jdk/crac/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

import jdk.crac.impl.*;
import jdk.internal.crac.JDKContext;
import jdk.internal.crac.JDKResource;
import jdk.internal.crac.LoggerContainer;

import sun.security.action.GetBooleanAction;
import sun.security.action.GetPropertyAction;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -70,11 +72,7 @@ private FlagsHolder() {}
GetBooleanAction.privilegedGetProperty("jdk.crac.trace-startup-time");
}

private static final Context<Resource> globalContext = new OrderedContext<>("GlobalContext");
static {
// force JDK context initialization
jdk.internal.crac.Core.getJDKContext();
}
private static final Context<Resource> globalContext = new BlockingOrderedContext<>();

/** This class is not instantiable. */
private Core() {
Expand Down Expand Up @@ -123,6 +121,7 @@ private static void checkpointRestore1(long jcmdStream) throws
LoggerContainer.debug("Starting checkpoint at epoch:{0}", System.currentTimeMillis());

try {
jdk.internal.crac.Core.getJDKContext().beforeCheckpoint(null);
globalContext.beforeCheckpoint(null);
} catch (CheckpointException ce) {
checkpointException = new CheckpointException();
Expand Down Expand Up @@ -196,6 +195,7 @@ public String run() {
RestoreException restoreException = null;
try {
globalContext.afterRestore(null);
jdk.internal.crac.Core.getJDKContext().afterRestore(null);
} catch (RestoreException re) {
if (checkpointException == null) {
restoreException = re;
Expand Down
81 changes: 81 additions & 0 deletions src/java.base/share/classes/jdk/crac/impl/AbstractContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2019, 2023, Azul Systems, Inc. All rights reserved.
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.crac.impl;

import jdk.crac.*;
import jdk.internal.crac.LoggerContainer;

import java.util.List;

/**
* An abstract context with few utilities.
* @param <R> Type of Resource managed by the context.
*/
public abstract class AbstractContext<R extends Resource> extends Context<R> {
protected abstract List<R> checkpointSnapshot();
protected abstract List<R> restoreSnapshot();

protected void invokeBeforeCheckpoint(Resource resource) throws Exception {
LoggerContainer.debug("beforeCheckpoint {0}", resource);
resource.beforeCheckpoint(this);
}

protected void invokeAfterRestore(Resource resource) throws Exception {
LoggerContainer.debug("afterRestore {0}", resource);
resource.afterRestore(this);
}

@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws CheckpointException {
ExceptionHolder<CheckpointException> checkpointException =
new ExceptionHolder<>(CheckpointException::new);
List<R> resources = checkpointSnapshot();
for (R r : resources) {
try {
invokeBeforeCheckpoint(r);
} catch (Exception e) {
checkpointException.handle(e);
}
}
checkpointException.throwIfAny();
}

@Override
public void afterRestore(Context<? extends Resource> context) throws RestoreException {
ExceptionHolder<RestoreException> restoreException =
new ExceptionHolder<>(RestoreException::new);
List<R> resources = restoreSnapshot();
for (R r : resources) {
try {
invokeAfterRestore(r);
} catch (Exception e) {
restoreException.handle(e);
}
}
restoreException.throwIfAny();
}
}
Loading

0 comments on commit 6f403ea

Please sign in to comment.