Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/workflows/e2e-java-tracer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: E2E - Java Tracer

on:
pull_request:
paths:
- 'codeflash/languages/java/**'
- 'codeflash/languages/base.py'
- 'codeflash/languages/registry.py'
- 'codeflash/tracer.py'
- 'codeflash/benchmarking/function_ranker.py'
- 'codeflash/discovery/functions_to_optimize.py'
- 'codeflash/optimization/**'
- 'codeflash/verification/**'
- 'codeflash-java-runtime/**'
- 'tests/test_languages/fixtures/java_tracer_e2e/**'
- 'tests/scripts/end_to_end_test_java_tracer.py'
- '.github/workflows/e2e-java-tracer.yaml'

workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref_name }}
cancel-in-progress: true

jobs:
java-tracer-e2e:
environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }}

runs-on: ubuntu-latest
env:
CODEFLASH_AIS_SERVER: prod
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
COLUMNS: 110
MAX_RETRIES: 3
RETRY_DELAY: 5
EXPECTED_IMPROVEMENT_PCT: 10
CODEFLASH_END_TO_END: 1
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Validate PR
env:
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_STATE: ${{ github.event.pull_request.state }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
if git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -q "^.github/workflows/"; then
echo "⚠️ Workflow changes detected."
echo "PR Author: $PR_AUTHOR"
if [[ "$PR_AUTHOR" == "misrasaurabh1" || "$PR_AUTHOR" == "KRRT7" ]]; then
echo "✅ Authorized user ($PR_AUTHOR). Proceeding."
elif [[ "$PR_STATE" == "open" ]]; then
echo "✅ PR is open. Proceeding."
else
echo "⛔ Unauthorized user ($PR_AUTHOR) attempting to modify workflows. Exiting."
exit 1
fi
else
echo "✅ No workflow file changes detected. Proceeding."
fi

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
cache: maven

- name: Set up Python 3.11 for CLI
uses: astral-sh/setup-uv@v6
with:
python-version: 3.11.6

- name: Install dependencies (CLI)
run: uv sync

- name: Build codeflash-runtime JAR
run: |
cd codeflash-java-runtime
mvn clean package -q -DskipTests
mvn install -q -DskipTests

- name: Verify Java installation
run: |
java -version
mvn --version

- name: Run Java tracer e2e test
run: |
uv run python tests/scripts/end_to_end_test_java_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
*/
public class AgentDispatcher {

static boolean isTracerMode(String agentArgs) {
return agentArgs != null
&& (agentArgs.startsWith("trace=") || agentArgs.contains(",trace="));
}

static boolean isProfilerMode(String agentArgs) {
return agentArgs != null
&& (agentArgs.startsWith("config=") || agentArgs.contains(",config="));
}

public static void premain(String agentArgs, Instrumentation inst) throws Exception {
if (isProfilerMode(agentArgs)) {
if (isTracerMode(agentArgs)) {
com.codeflash.tracer.TracerAgent.premain(agentArgs, inst);
} else if (isProfilerMode(agentArgs)) {
com.codeflash.profiler.ProfilerAgent.premain(agentArgs, inst);
} else {
org.jacoco.agent.rt.internal_0e20598.PreMain.premain(agentArgs, inst);
Expand Down
116 changes: 116 additions & 0 deletions codeflash-java-runtime/src/main/java/com/codeflash/ReplayHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.codeflash;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.objectweb.asm.Type;

public class ReplayHelper {

private final Connection db;

public ReplayHelper(String traceDbPath) {
try {
this.db = DriverManager.getConnection("jdbc:sqlite:" + traceDbPath);
} catch (SQLException e) {
throw new RuntimeException("Failed to open trace database: " + traceDbPath, e);
}
}

public void replay(String className, String methodName, String descriptor, int invocationIndex) throws Exception {
// Query the function_calls table for this method at the given index
byte[] argsBlob;
try (PreparedStatement stmt = db.prepareStatement(
"SELECT args FROM function_calls " +
"WHERE classname = ? AND function = ? AND descriptor = ? " +
"ORDER BY time_ns LIMIT 1 OFFSET ?")) {
stmt.setString(1, className);
stmt.setString(2, methodName);
stmt.setString(3, descriptor);
stmt.setInt(4, invocationIndex);

try (ResultSet rs = stmt.executeQuery()) {
if (!rs.next()) {
throw new RuntimeException("No invocation found at index " + invocationIndex
+ " for " + className + "." + methodName + descriptor);
}
argsBlob = rs.getBytes("args");
}
}

// Deserialize args
Object deserialized = Serializer.deserialize(argsBlob);
if (!(deserialized instanceof Object[])) {
throw new RuntimeException("Deserialized args is not Object[], got: "
+ (deserialized == null ? "null" : deserialized.getClass().getName()));
}
Object[] allArgs = (Object[]) deserialized;

// Load the target class
Class<?> targetClass = Class.forName(className);

// Parse descriptor to find parameter types
Type[] paramTypes = Type.getArgumentTypes(descriptor);
Class<?>[] paramClasses = new Class<?>[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
paramClasses[i] = typeToClass(paramTypes[i]);
}

// Find the method
Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
method.setAccessible(true);

boolean isStatic = Modifier.isStatic(method.getModifiers());

if (isStatic) {
method.invoke(null, allArgs);
} else {
// Args contain only explicit parameters (no 'this').
// Create a default instance via no-arg constructor or Kryo.
Object instance;
try {
java.lang.reflect.Constructor<?> ctor = targetClass.getDeclaredConstructor();
ctor.setAccessible(true);
instance = ctor.newInstance();
} catch (NoSuchMethodException e) {
// Fall back to Objenesis instantiation (no constructor needed)
instance = new org.objenesis.ObjenesisStd().newInstance(targetClass);
}
method.invoke(instance, allArgs);
}
}

private static Class<?> typeToClass(Type type) throws ClassNotFoundException {
switch (type.getSort()) {
case Type.BOOLEAN: return boolean.class;
case Type.BYTE: return byte.class;
case Type.CHAR: return char.class;
case Type.SHORT: return short.class;
case Type.INT: return int.class;
case Type.LONG: return long.class;
case Type.FLOAT: return float.class;
case Type.DOUBLE: return double.class;
case Type.VOID: return void.class;
case Type.ARRAY:
Class<?> elementClass = typeToClass(type.getElementType());
return java.lang.reflect.Array.newInstance(elementClass, 0).getClass();
case Type.OBJECT:
return Class.forName(type.getClassName());
default:
throw new ClassNotFoundException("Unknown type: " + type);
}
}

public void close() {
try {
if (db != null) db.close();
} catch (SQLException e) {
System.err.println("Error closing ReplayHelper: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.codeflash.tracer;

import com.codeflash.Serializer;

import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

public final class TraceRecorder {

private static volatile TraceRecorder instance;

private static final long SERIALIZATION_TIMEOUT_MS = 500;

private final TracerConfig config;
private final TraceWriter writer;
private final ConcurrentHashMap<String, AtomicInteger> functionCounts = new ConcurrentHashMap<>();
private final int maxFunctionCount;
private final ExecutorService serializerExecutor;

// Reentrancy guard: prevent recursive tracing when serialization triggers class loading
private static final ThreadLocal<Boolean> RECORDING = ThreadLocal.withInitial(() -> Boolean.FALSE);

private TraceRecorder(TracerConfig config) {
this.config = config;
this.writer = new TraceWriter(config.getDbPath());
this.maxFunctionCount = config.getMaxFunctionCount();
this.serializerExecutor = Executors.newCachedThreadPool(r -> {
Thread t = new Thread(r, "codeflash-serializer");
t.setDaemon(true);
return t;
});
}

public static void initialize(TracerConfig config) {
instance = new TraceRecorder(config);
}

public static TraceRecorder getInstance() {
return instance;
}

public static boolean isRecording() {
return Boolean.TRUE.equals(RECORDING.get());
}

public void onEntry(String className, String methodName, String descriptor,
int lineNumber, String sourceFile, Object[] args) {
// Reentrancy guard
if (RECORDING.get()) {
return;
}
RECORDING.set(Boolean.TRUE);
try {
onEntryImpl(className, methodName, descriptor, lineNumber, sourceFile, args);
} finally {
RECORDING.set(Boolean.FALSE);
}
}

private void onEntryImpl(String className, String methodName, String descriptor,
int lineNumber, String sourceFile, Object[] args) {
String qualifiedName = className + "." + methodName + descriptor;

// Check per-method count limit
AtomicInteger count = functionCounts.computeIfAbsent(qualifiedName, k -> new AtomicInteger(0));
if (count.get() >= maxFunctionCount) {
return;
}

// Serialize args with timeout to prevent deep object graph traversal from blocking
byte[] argsBlob;
Future<byte[]> future = serializerExecutor.submit(() -> Serializer.serialize(args));
try {
argsBlob = future.get(SERIALIZATION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
future.cancel(true);
System.err.println("[codeflash-tracer] Serialization timed out for " + className + "."
+ methodName);
return;
} catch (Exception e) {
Throwable cause = e.getCause() != null ? e.getCause() : e;
System.err.println("[codeflash-tracer] Serialization failed for " + className + "."
+ methodName + ": " + cause.getClass().getSimpleName() + ": " + cause.getMessage());
return;
}

long timeNs = System.nanoTime();
count.incrementAndGet();

writer.recordFunctionCall("call", methodName, className, sourceFile,
lineNumber, descriptor, timeNs, argsBlob);
}

public void flush() {
serializerExecutor.shutdownNow();
// Write metadata
Map<String, String> metadata = new LinkedHashMap<>();
metadata.put("projectRoot", config.getProjectRoot());
metadata.put("timestamp", Instant.now().toString());
metadata.put("totalFunctions", String.valueOf(functionCounts.size()));

int totalCaptures = 0;
for (AtomicInteger count : functionCounts.values()) {
totalCaptures += count.get();
}
metadata.put("totalCaptures", String.valueOf(totalCaptures));

writer.writeMetadata(metadata);
writer.flush();
writer.close();

System.err.println("[codeflash-tracer] Captured " + totalCaptures
+ " invocations across " + functionCounts.size() + " methods");
}
}
Loading
Loading