-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-4808. Add Genesis benchmark for various CRC implementations #1910
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
Changes from 4 commits
8ff98ad
852b873
e5ad91f
05143ac
06a7dc9
9b1b0bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void update(byte[] b, int off, int len) { | ||
| checksum.update(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public void update(int i) { | ||
| checksum.update(i); | ||
| } | ||
|
|
||
| @Override | ||
| public long getValue() { | ||
| return checksum.getValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| checksum.reset(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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.NotImplementedException; | ||
| import org.apache.hadoop.util.NativeCRC32Wrapper; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * This is a partial implementation to be used only in benchmarks. | ||
| * | ||
| * The Hadoop Native checksum libraries do not allow for updating a checksum | ||
| * as the java.util.zip.Checksum dictates in its update(...) method. | ||
| * | ||
| * This class allows the Native Hadoop CRC32 implementations to be called to | ||
| * generate checksums, provided only a single call is made to the update(...) | ||
| * method. | ||
| * | ||
| */ | ||
| public class NativeCheckSumCRC32 implements java.util.zip.Checksum { | ||
|
|
||
| // 1 for crc32, 2 for crc32c - see NativeCRC32Wrapper | ||
| private int checksumType; | ||
| private int bytesPerSum; | ||
|
|
||
| private ByteBuffer checksum = ByteBuffer.allocate(4); | ||
| private boolean needsReset = false; | ||
|
|
||
| public NativeCheckSumCRC32(int checksumType, int bytesPerSum) { | ||
| this.checksumType = checksumType; | ||
| this.bytesPerSum = bytesPerSum; | ||
| } | ||
|
|
||
| @Override | ||
| public void update(int b) { | ||
| throw new NotImplementedException("Update method is not implemented"); | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the checksum. Note the checksum is not updatable. You should | ||
| * make a single call to this method and then call getValue() to retrive the | ||
| * value. | ||
| * @param b A byte array whose contents will be used to calculate a CRC32(C) | ||
| * @param off The offset in the byte array to start reading. | ||
| * @param len The number of bytes in the byte array to read. | ||
| */ | ||
| @Override | ||
| public void update(byte[] b, int off, int len) { | ||
| if (needsReset) { | ||
| throw new IllegalArgumentException( | ||
| "This checksum implementation is not updatable"); | ||
| } | ||
| NativeCRC32Wrapper.calculateChunkedSumsByteArray(bytesPerSum, checksumType, | ||
| checksum.array(), 0, b, off, len); | ||
| needsReset = true; | ||
| } | ||
|
|
||
| @Override | ||
| public long getValue() { | ||
| checksum.position(0); | ||
| return checksum.getInt(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| checksum.clear(); | ||
| needsReset = false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * 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.util; | ||
|
|
||
| import org.apache.hadoop.fs.ChecksumException; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * This class wraps the NativeCRC32 class in hadoop-common, because the class | ||
| * is package private there. The intention of making this class available | ||
| * in Ozone is to allow the native libraries to be benchmarked alongside other | ||
| * implementations. At the current time, the hadoop native CRC is not used | ||
| * anywhere in Ozone except for benchmarks. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important to call this out in the jira description as well as the PR. With the changes in this patch could Ozone start making use of the native CRC implementation?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not unless you get the compiled shared library from a hadoop build and then add it to the java.library.path. However to be able to benchmark the native libs, we need this code here. The classes inside Hadoop common are marked private, which is why I needed to wrap them. |
||
| */ | ||
| public final class NativeCRC32Wrapper { | ||
|
|
||
| public static final int CHECKSUM_CRC32 = NativeCrc32.CHECKSUM_CRC32; | ||
| public static final int CHECKSUM_CRC32C = NativeCrc32.CHECKSUM_CRC32C; | ||
|
|
||
| // Private constructor | ||
| private NativeCRC32Wrapper() { | ||
| } | ||
|
|
||
| public static boolean isAvailable() { | ||
| return NativeCrc32.isAvailable(); | ||
| } | ||
|
|
||
| public static void verifyChunkedSums(int bytesPerSum, int checksumType, | ||
| ByteBuffer sums, ByteBuffer data, String fileName, long basePos) | ||
| throws ChecksumException { | ||
| NativeCrc32.verifyChunkedSums(bytesPerSum, checksumType, sums, data, | ||
| fileName, basePos); | ||
| } | ||
|
|
||
| @SuppressWarnings("checkstyle:parameternumber") | ||
| public static void verifyChunkedSumsByteArray(int bytesPerSum, | ||
| int checksumType, byte[] sums, int sumsOffset, byte[] data, | ||
| int dataOffset, int dataLength, String fileName, long basePos) | ||
| throws ChecksumException { | ||
| NativeCrc32.verifyChunkedSumsByteArray(bytesPerSum, checksumType, sums, | ||
| sumsOffset, data, dataOffset, dataLength, fileName, basePos); | ||
| } | ||
|
|
||
| public static void calculateChunkedSums(int bytesPerSum, int checksumType, | ||
| ByteBuffer sums, ByteBuffer data) { | ||
| NativeCrc32.calculateChunkedSums(bytesPerSum, checksumType, sums, data); | ||
| } | ||
|
|
||
| public static void calculateChunkedSumsByteArray(int bytesPerSum, | ||
| int checksumType, byte[] sums, int sumsOffset, byte[] data, | ||
| int dataOffset, int dataLength) { | ||
| NativeCrc32.calculateChunkedSumsByteArray(bytesPerSum, checksumType, sums, | ||
| sumsOffset, data, dataOffset, dataLength); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * This package contains class that wrap private classes in hadoop-common | ||
| * util. | ||
| */ | ||
| package org.apache.hadoop.util; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()))); | ||
sodonnel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (NativeCRC32Wrapper.isAvailable()) { | ||
| impls.add(new ChecksumByteBufferImpl(new NativeCheckSumCRC32(2, bpc))); | ||
| } | ||
| assertEquals(true, validateImpls(data, impls, bpc)); | ||
| } | ||
| } | ||
|
|
||
| private boolean validateImpls(ByteBuffer buf, List<ChecksumByteBuffer> impls, | ||
| int bpc) { | ||
| for (int i = 0; i < buf.capacity(); i += bpc) { | ||
| buf.position(i); | ||
| buf.limit(i + bpc); | ||
| impls.get(0).update(buf); | ||
| int res = (int) impls.get(0).getValue(); | ||
| impls.get(0).reset(); | ||
| for (int j = 1; j < impls.size(); j++) { | ||
| ChecksumByteBuffer csum = impls.get(j); | ||
| buf.position(i); | ||
| buf.limit(i + bpc); | ||
| csum.update(buf); | ||
| if ((int) csum.getValue() != res) { | ||
| return false; | ||
| } | ||
| csum.reset(); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.