From a414b91a5991dc687b43b095823dc319ed02aae5 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Thu, 15 Feb 2018 18:08:19 -0800 Subject: [PATCH] context: remove context profiling --- context/src/main/java/io/grpc/Context.java | 86 ------------------- .../main/java/io/grpc/InternalContext.java | 36 -------- 2 files changed, 122 deletions(-) delete mode 100644 context/src/main/java/io/grpc/InternalContext.java diff --git a/context/src/main/java/io/grpc/Context.java b/context/src/main/java/io/grpc/Context.java index da13e1863b..600d0c25a2 100644 --- a/context/src/main/java/io/grpc/Context.java +++ b/context/src/main/java/io/grpc/Context.java @@ -17,16 +17,13 @@ package io.grpc; import java.io.Closeable; -import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.concurrent.atomic.AtomicLongArray; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; @@ -103,55 +100,6 @@ public class Context { private static final PersistentHashArrayMappedTrie, Object> EMPTY_ENTRIES = new PersistentHashArrayMappedTrie, Object>(); - static final AtomicLongArray withValueCounts; - /** - * Counts how many times a unique value added to the context. - */ - static final AtomicLongArray withValueUniqueCounts; - static final AtomicLongArray getCounts; - private static final Method threadLocalRandomCurrent; - - static { - Method localThreadLocalRandomCurrent = null; - try { - Class cls = Class.forName("java.util.concurrent.ThreadLocalRandom"); - localThreadLocalRandomCurrent = cls.getMethod("current"); - // call it once just to check. - localThreadLocalRandomCurrent.invoke(null); - } catch (Throwable t) { - log.log(Level.FINE, "Can't find TLR, skipping", t); - localThreadLocalRandomCurrent = null; - } - if (localThreadLocalRandomCurrent != null) { - withValueCounts = new AtomicLongArray(100); - withValueUniqueCounts = new AtomicLongArray(100); - getCounts = new AtomicLongArray(100); - threadLocalRandomCurrent = localThreadLocalRandomCurrent; - } else { - withValueCounts = new AtomicLongArray(0); - withValueUniqueCounts = new AtomicLongArray(0); - getCounts = new AtomicLongArray(0); - threadLocalRandomCurrent = null; - } - } - - private static boolean shouldSample() { - if (threadLocalRandomCurrent == null) { - return false; - } - Random r; - try { - r = (Random) threadLocalRandomCurrent.invoke(null); - } catch (Exception e) { - log.log(Level.FINE, "Can't get TLR", e); - return false; - } - if (r.nextInt(256) != 0) { - return false; - } - return true; - } - // Long chains of contexts are suspicious and usually indicate a misuse of Context. // The threshold is arbitrarily chosen. // VisibleForTesting @@ -369,13 +317,6 @@ public class Context { */ public Context withValue(Key k1, V v1) { PersistentHashArrayMappedTrie, Object> newKeyValueEntries = keyValueEntries.put(k1, v1); - if (shouldSample()) { - withValueUniqueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueUniqueCounts.length() - 1), - newKeyValueEntries.size() - keyValueEntries.size()); - withValueCounts.incrementAndGet( - Math.min(keyValueEntries.size(), withValueCounts.length() - 1)); - } return new Context(this, newKeyValueEntries); } @@ -386,14 +327,6 @@ public class Context { public Context withValues(Key k1, V1 v1, Key k2, V2 v2) { PersistentHashArrayMappedTrie, Object> newKeyValueEntries = keyValueEntries.put(k1, v1).put(k2, v2); - if (shouldSample()) { - withValueUniqueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueUniqueCounts.length() - 1), - newKeyValueEntries.size() - keyValueEntries.size()); - withValueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueCounts.length() - 1), - 2); - } return new Context(this, newKeyValueEntries); } @@ -404,14 +337,6 @@ public class Context { public Context withValues(Key k1, V1 v1, Key k2, V2 v2, Key k3, V3 v3) { PersistentHashArrayMappedTrie, Object> newKeyValueEntries = keyValueEntries.put(k1, v1).put(k2, v2).put(k3, v3); - if (shouldSample()) { - withValueUniqueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueUniqueCounts.length() - 1), - newKeyValueEntries.size() - keyValueEntries.size()); - withValueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueCounts.length() - 1), - 3); - } return new Context(this, newKeyValueEntries); } @@ -423,14 +348,6 @@ public class Context { Key k3, V3 v3, Key k4, V4 v4) { PersistentHashArrayMappedTrie, Object> newKeyValueEntries = keyValueEntries.put(k1, v1).put(k2, v2).put(k3, v3).put(k4, v4); - if (shouldSample()) { - withValueUniqueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueUniqueCounts.length() - 1), - newKeyValueEntries.size() - keyValueEntries.size()); - withValueCounts.addAndGet( - Math.min(keyValueEntries.size(), withValueCounts.length() - 1), - 4); - } return new Context(this, newKeyValueEntries); } @@ -740,9 +657,6 @@ public class Context { * Lookup the value for a key in the context inheritance chain. */ private Object lookup(Key key) { - if (shouldSample()) { - getCounts.incrementAndGet(Math.min(keyValueEntries.size(), getCounts.length() - 1)); - } return keyValueEntries.get(key); } diff --git a/context/src/main/java/io/grpc/InternalContext.java b/context/src/main/java/io/grpc/InternalContext.java deleted file mode 100644 index 9724e592e7..0000000000 --- a/context/src/main/java/io/grpc/InternalContext.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2018, gRPC Authors All rights reserved. - * - * 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.grpc; - -import java.util.concurrent.atomic.AtomicLongArray; - -/** - * Internal accessor for {@link InternalContext}. - */ -public final class InternalContext { - public static AtomicLongArray getCounts() { - return Context.getCounts; - } - - public static AtomicLongArray withValueCounts() { - return Context.withValueCounts; - } - - public static AtomicLongArray withValueUniqueCounts() { - return Context.withValueUniqueCounts; - } -}