Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl: Factor Thread Storage into pluggable provider #177

Merged
merged 3 commits into from
Nov 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void moduleElement_oldClass() throws Exception {
@Test
public void rewriteClass_task() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();
String expectedValue =
new StackTraceElement("loadername", "modulename", "moduleversion", "classname", "methodname", "filename", -1)
.toString();
Expand Down Expand Up @@ -109,7 +109,7 @@ public MethodVisitor visitMethod(
@Test
public void rewriteClass_closeable() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();
String expectedValue =
new StackTraceElement("loadername", "modulename", "moduleversion", "classname", "methodname", "filename", -1)
.toString();
Expand Down Expand Up @@ -139,6 +139,7 @@ public MethodVisitor visitMethod(
Truth.assertThat(start).isNotEqualTo(end);
}

@SuppressWarnings("UnusedMethod")
private static final class ClzToRewrite {
public static void task() {
// These two calls MUST happen on separate lines to ensure line reading works.
Expand Down
28 changes: 14 additions & 14 deletions agent/src/test/java/io/perfmark/agent/PerfMarkTransformerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void deriveFileName_innerClass() {
public void transform_autoAnnotate() throws Exception {
// This test currently depends on the transformer treating this test class specially.
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzAutoRecord.class);
Constructor<?> ctor = clz.getConstructor();
Expand All @@ -73,7 +73,7 @@ public void transform_autoAnnotate() throws Exception {
@Test
public void transform_record() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.SomeRecord.class);
Constructor<?> ctor = clz.getConstructor(int.class);
Expand All @@ -97,7 +97,7 @@ public void transform_record() throws Exception {
@Test
public void transform_lambda() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzCtorLambda.class);
Constructor<?> ctor = clz.getConstructor();
Expand All @@ -121,7 +121,7 @@ public void transform_lambda() throws Exception {
@Test
public void transform_methodRef() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithMethodRefs.class);
Constructor<?> ctor = clz.getConstructor();
Expand All @@ -135,7 +135,7 @@ public void transform_methodRef() throws Exception {
@Test
public void transform_interface() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz =
transformAndLoad(TransformerTestClasses.Bar.class, TransformerTestClasses.InterfaceWithDefaults.class);
Expand Down Expand Up @@ -178,7 +178,7 @@ public void transform_interface() throws Exception {
@Test
public void transform_link() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithLinks.class);
Constructor<?> ctor = clz.getConstructor();
Expand All @@ -205,7 +205,7 @@ public void transform_link() throws Exception {
@Test
public void transform_closeable() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithCloseable.class);
Constructor<?> ctor = clz.getConstructor();
Expand All @@ -232,7 +232,7 @@ public void transform_wrongCloseable() throws Exception {
// If the wrong static type is used, the agent won't be able to instrument it. Add a test to document this
// behavior.
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithWrongCloseable.class);
Constructor<?> ctor = clz.getConstructor();
Expand All @@ -255,7 +255,7 @@ public void transform_wrongCloseable_autoCloseable() throws Exception {
// If the wrong static type is used, the agent won't be able to instrument it. Add a test to document this
// behavior.
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<? extends Closeable> clz = transformAndLoad(TaskCloseable.class).asSubclass(Closeable.class);
Constructor<? extends Closeable> ctor = clz.getDeclaredConstructor();
Expand All @@ -271,7 +271,7 @@ public void transform_wrongCloseable_autoCloseable() throws Exception {
@Test
public void transform_ctor() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithCtor.class);
Constructor<?> ctor = clz.getConstructor();
Expand Down Expand Up @@ -312,7 +312,7 @@ public void transform_ctor() throws Exception {
@Test
public void transform_init() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithInit.class);
Constructor<?> ctor = clz.getDeclaredConstructor();
Expand Down Expand Up @@ -353,7 +353,7 @@ public void transform_init() throws Exception {
@Test
public void transform_clinit() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(TransformerTestClasses.ClzWithClinit.class);
Constructor<?> ctor = clz.getDeclaredConstructor();
Expand Down Expand Up @@ -394,7 +394,7 @@ public void transform_clinit() throws Exception {
@Test
public void transform_toplevel() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(ClzFooter.class);
Constructor<?> ctor = clz.getDeclaredConstructor();
Expand Down Expand Up @@ -435,7 +435,7 @@ public void transform_toplevel() throws Exception {
@Test
public void transform_anonymousClass() throws Exception {
PerfMark.setEnabled(true);
Storage.resetForTest();
Storage.clearLocalStorage();

Class<?> clz = transformAndLoad(new Runnable() {
// avoid IntelliJ thinking this should be a lambda.
Expand Down
3 changes: 2 additions & 1 deletion api/src/test/java/io/perfmark/CompatibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class CompatibilityTest {
"0.25.0");

@Parameterized.Parameters(name = "version v{0}")
@SuppressWarnings("StringSplitter")
public static Iterable<Object[]> params() {
List<Object[]> params = new ArrayList<>();
for (var version : VERSIONS) {
Expand Down Expand Up @@ -102,7 +103,7 @@ public void setUp() throws Exception {
@After
public void tearDown() throws Exception {
if (storageClz != null) {
storageClz.getMethod("resetForTest").invoke(null);
storageClz.getMethod("clearLocalStorage").invoke(null);
}
}

Expand Down
10 changes: 5 additions & 5 deletions api/src/test/java/io/perfmark/PerfMarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void checkPermission(Permission perm) {

@Test
public void allMethodForward_taskName() {
Storage.resetForTest();
Storage.clearLocalStorage();
PerfMark.setEnabled(true);

long gen = getGen();
Expand Down Expand Up @@ -319,7 +319,7 @@ public void allMethodForward_taskName() {

@Test
public void attachTag_nullFunctionFailsSilently() {
Storage.resetForTest();
Storage.clearLocalStorage();
PerfMark.setEnabled(true);

PerfMark.attachTag("name", "extra2", null);
Expand All @@ -330,7 +330,7 @@ public void attachTag_nullFunctionFailsSilently() {

@Test
public void attachTag_functionFailureSucceeds() {
Storage.resetForTest();
Storage.clearLocalStorage();
PerfMark.setEnabled(true);

PerfMark.attachTag(
Expand All @@ -346,7 +346,7 @@ public void attachTag_functionFailureSucceeds() {

@Test
public void attachTag_functionFailureObjectFailureSucceeds() {
Storage.resetForTest();
Storage.clearLocalStorage();
PerfMark.setEnabled(true);
Object o =
new Object() {
Expand All @@ -369,7 +369,7 @@ public String toString() {

@Test
public void attachTag_doubleFunctionFailureSucceeds() {
Storage.resetForTest();
Storage.clearLocalStorage();
PerfMark.setEnabled(true);

PerfMark.attachTag(
Expand Down
41 changes: 41 additions & 0 deletions impl/src/main/java/io/perfmark/impl/LocalMarkHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 Carl Mastrangelo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.perfmark.impl;

/**
* A local MarkHolder is a class that gets the "current" MarkHolder based on context. For example, a thread local
* MarkHolder could use this class to pull the local MarkHolder from the threadlocal variable. Other implementations
* are possible as well.
*/
public abstract class LocalMarkHolder {

/**
* Removes the local markholder storage.
*/
public abstract void clear();

/**
* Get's the current MarkHolder for mutation. Only called from a tracing thread.
*/
public abstract MarkHolder acquire();

/**
* Releases the MarkHolder from being written too. Usually called very shortly after {@link #acquire()}.
* This method is meant to be overridden and should not be called from subclasses.
*/
public void release(MarkHolder markHolder) {}
}