You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2021/02/17 09:03:09 UTC

[GitHub] [ozone] szetszwo commented on a change in pull request #1910: HDDS-4808. Add Genesis benchmark for various CRC implementations

szetszwo commented on a change in pull request #1910:
URL: https://github.com/apache/ozone/pull/1910#discussion_r577397045



##########
File path: hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChecksumByteBufferImpl.java
##########
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.ozone.common;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.nio.ByteBuffer;
+import java.util.zip.Checksum;
+
+public class ChecksumByteBufferImpl implements ChecksumByteBuffer {
+
+  public static class Java9Crc32CFactory {
+    private static final MethodHandle NEW_CRC32C_MH;
+
+    static {
+      MethodHandle newCRC32C = null;
+      try {
+        newCRC32C = MethodHandles.publicLookup()
+            .findConstructor(
+                Class.forName("java.util.zip.CRC32C"),
+                MethodType.methodType(void.class)
+            );
+      } catch (ReflectiveOperationException e) {
+        // Should not reach here.
+        throw new RuntimeException(e);
+      }
+      NEW_CRC32C_MH = newCRC32C;
+    }
+
+    public static java.util.zip.Checksum createChecksum() {
+      try {
+        // Should throw nothing
+        return (Checksum) NEW_CRC32C_MH.invoke();
+      } catch (Throwable t) {
+        throw (t instanceof RuntimeException) ? (RuntimeException) t
+            : new RuntimeException(t);
+      }
+    }
+  };
+
+  private Checksum checksum;
+
+  public ChecksumByteBufferImpl(Checksum impl) {
+    this.checksum = impl;
+  }
+
+  @Override
+  public void update(ByteBuffer buffer) {
+    if (buffer.hasArray()) {
+      checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset(),
+          buffer.remaining());
+    } else {
+      byte[] b = new byte[buffer.remaining()];
+      buffer.get(b);
+      checksum.update(b, 0, b.length);
+    }
+  }

Review comment:
       Since Java 9 Checksum supports `update(ByteBuffer)` https://docs.oracle.com/javase/9/docs/api/java/util/zip/Checksum.html#update-java.nio.ByteBuffer- , this method should call it when `checksum` is a Java. 9 Checksum object.

##########
File path: hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/genesis/BenchMarkCRCStreaming.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.ozone.genesis;
+
+import java.nio.ByteBuffer;
+
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.hadoop.ozone.common.ChecksumByteBuffer;
+import org.apache.hadoop.ozone.common.ChecksumByteBufferImpl;
+import org.apache.hadoop.ozone.common.NativeCheckSumCRC32;
+import org.apache.hadoop.ozone.common.PureJavaCrc32ByteBuffer;
+import org.apache.hadoop.ozone.common.PureJavaCrc32CByteBuffer;
+import org.apache.hadoop.util.NativeCRC32Wrapper;
+import org.apache.hadoop.util.PureJavaCrc32;
+import org.apache.hadoop.util.PureJavaCrc32C;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+import java.util.zip.CRC32;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+
+/**
+ * Class to benchmark various CRC implementations. This can be executed via
+ *
+ * ozone genesis -b BenchmarkCRC
+ *
+ * However there are some points to keep in mind. java.util.zip.CRC32C is not
+ * available until Java 9, therefore if the JVM has a lower version than 9, that
+ * implementation will not be tested.
+ *
+ * The hadoop native libraries will only be tested if libhadoop.so is found on
+ * the "-Djava.library.path". libhadoop.so is not currently bundled with Ozone,
+ * so it needs to be obtained from a Hadoop build and the test needs to be
+ * executed on a compatible OS (ie Linux x86):
+ *
+ * ozone --jvmargs -Djava.library.path=/home/sodonnell/native genesis -b
+ *     BenchmarkCRC
+ */
+public class BenchMarkCRCStreaming {
+
+  private static int dataSize = 64 * 1024 * 1024;
+
+  @State(Scope.Thread)
+  public static class BenchmarkState {
+
+    private final ByteBuffer data = ByteBuffer.allocate(dataSize);
+
+    @Param({"512", "1024", "2048", "4096", "32768", "1048576"})
+    private int checksumSize;
+
+    @Param({"pureCRC32", "pureCRC32C", "hadoopCRC32C", "hadoopCRC32",
+        "zipCRC32", "zipCRC32C", "nativeCRC32", "nativeCRC32C"})
+    private String crcImpl;
+
+    private ChecksumByteBuffer checksum;
+
+    public ChecksumByteBuffer checksum() {
+      return checksum;
+    }
+
+    public String crcImpl() {
+      return crcImpl;
+    }
+
+    public int checksumSize() {
+      return checksumSize;
+    }
+
+    @Setup(Level.Trial)
+    public void setUp() {
+      switch (crcImpl) {
+      case "pureCRC32":
+        checksum = new PureJavaCrc32ByteBuffer();
+        break;
+      case "pureCRC32C":
+        checksum = new PureJavaCrc32CByteBuffer();
+        break;
+      case "hadoopCRC32":
+        checksum = new ChecksumByteBufferImpl(new PureJavaCrc32());
+        break;
+      case "hadoopCRC32C":
+        checksum = new ChecksumByteBufferImpl(new PureJavaCrc32C());
+        break;
+      case "zipCRC32":
+        checksum = new ChecksumByteBufferImpl(new CRC32());
+        break;
+      case "zipCRC32C":
+        try {
+          checksum = new ChecksumByteBufferImpl(
+              ChecksumByteBufferImpl.Java9Crc32CFactory.createChecksum());
+        } catch (Throwable e) {
+          throw new RuntimeException("zipCRC32C is not available pre Java 9");
+        }
+        break;
+      case "nativeCRC32":
+        if (NativeCRC32Wrapper.isAvailable()) {
+          checksum = new ChecksumByteBufferImpl(new NativeCheckSumCRC32(
+              NativeCRC32Wrapper.CHECKSUM_CRC32, checksumSize));
+        } else {
+          throw new RuntimeException("Native library is not available");
+        }
+        break;
+      case "nativeCRC32C":
+        if (NativeCRC32Wrapper.isAvailable()) {
+          checksum = new ChecksumByteBufferImpl(new NativeCheckSumCRC32(
+              NativeCRC32Wrapper.CHECKSUM_CRC32C, checksumSize));
+        } else {
+          throw new RuntimeException("Native library is not available");
+        }
+        break;
+      default:
+      }
+      data.clear();
+      data.put(RandomUtils.nextBytes(data.remaining()));
+    }
+  }
+
+  @Benchmark
+  @Threads(1)
+  @Warmup(iterations = 3, time = 1000, timeUnit = MILLISECONDS)
+  @Fork(value = 1, warmups = 0)
+  @Measurement(iterations = 5, time = 2000, timeUnit = MILLISECONDS)
+  @BenchmarkMode(Mode.Throughput)
+  public void runCRC(Blackhole blackhole, BenchmarkState state) {
+    ByteBuffer data = state.data;
+    data.clear();

Review comment:
       Why calling clearing the data?  Typo?

##########
File path: hadoop-hdds/common/src/test/java/org/apache/hadoop/ozone/common/TestChecksumImplsComputeSameValues.java
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.ozone.common;
+
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.hadoop.util.NativeCRC32Wrapper;
+import org.apache.hadoop.util.PureJavaCrc32;
+import org.apache.hadoop.util.PureJavaCrc32C;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.zip.CRC32;
+
+import static junit.framework.TestCase.assertEquals;
+
+public class TestChecksumImplsComputeSameValues {
+
+  private int dataSize = 1024 * 1024 * 64;
+  private ByteBuffer data = ByteBuffer.allocate(dataSize);
+  private int[] bytesPerChecksum = {512, 1024, 2048, 4096, 32768, 1048576};
+
+  @Test
+  public void testCRC32ImplsMatch() {
+    data.clear();
+    data.put(RandomUtils.nextBytes(data.remaining()));
+    for (int bpc : bytesPerChecksum) {
+      List<ChecksumByteBuffer> impls = new ArrayList<>();
+      impls.add(new PureJavaCrc32ByteBuffer());
+      impls.add(new ChecksumByteBufferImpl(new PureJavaCrc32()));
+      impls.add(new ChecksumByteBufferImpl(new CRC32()));
+      if (NativeCRC32Wrapper.isAvailable()) {
+        impls.add(new ChecksumByteBufferImpl(new NativeCheckSumCRC32(1, bpc)));
+      }
+      assertEquals(true, validateImpls(data, impls, bpc));
+    }
+  }
+
+  @Test
+  public void testCRC32CImplsMatch() {
+    data.clear();
+    data.put(RandomUtils.nextBytes(data.remaining()));
+    for (int bpc : bytesPerChecksum) {
+      List<ChecksumByteBuffer> impls = new ArrayList<>();
+      impls.add(new PureJavaCrc32CByteBuffer());
+      impls.add(new ChecksumByteBufferImpl(new PureJavaCrc32C()));
+      // TODO - optional loaded java.util.zip.CRC32C if >= Java 9
+      // impls.add(new ChecksumByteBufferImpl(new CRC32C())));

Review comment:
       How about doing try-catch Java9Crc32CFactory.createChecksum()?  Ignore the exception if it is unavailable.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org