You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2017/08/10 22:33:32 UTC
[3/5] incubator-mnemonic git commit: MNEMONIC-330: Refactor service
package name
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImpl.java b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImpl.java
new file mode 100644
index 0000000..2aa8c6d
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImpl.java
@@ -0,0 +1,238 @@
+/*
+ * 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.mnemonic.service.memory.internal;
+
+import org.apache.mnemonic.ConfigurationException;
+import org.apache.mnemonic.service.memory.MemoryServiceFeature;
+import org.apache.mnemonic.service.memory.VolatileMemoryAllocatorService;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
+
+public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
+
+ protected Map<Long, Long> m_info = Collections.synchronizedMap(new HashMap<Long, Long>());
+ protected ArrayList<RandomAccessFile> mem_pool = new ArrayList<RandomAccessFile>();
+
+ @Override
+ public String getServiceId() {
+ return "javavmem";
+ }
+
+ @Override
+ public long init(long capacity, String uri, boolean isnew) {
+ FileChannel channel = null;
+ RandomAccessFile mappedFile = null;
+ long cp = -1;
+ long ret = -1;
+
+
+ if (uri == null || uri.length() == 0) {
+ throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
+ }
+ if (capacity <= 0) {
+ throw new ConfigurationException("Please supply the capacity");
+ }
+
+ File file = new File(uri);
+
+ if (file.exists() && !file.isFile()) {
+ throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
+ }
+ if (file.isFile() && file.length() <= 0) {
+ throw new ConfigurationException("File length should be more than zero.");
+ }
+
+ if (isnew) {
+ if (file.exists()) {
+ if (!file.delete()) {
+ throw new ConfigurationException(String.format("Failed to delete the file: %s.", uri));
+ }
+ }
+ try {
+ mappedFile = new RandomAccessFile(file, "rw");
+ mappedFile.setLength(capacity);
+ cp = mappedFile.length();
+ } catch (Exception ex) { }
+ } else {
+ if (!file.exists()) {
+ throw new ConfigurationException(String.format("File doesn't exist under the specifiled uri: %s", uri));
+ }
+ try {
+ mappedFile = new RandomAccessFile(file, "rw");
+ cp = mappedFile.length();
+ } catch (Exception ex) { }
+ }
+
+
+ if (mappedFile != null) {
+ mem_pool.add(mappedFile);
+ ret = mem_pool.size() - 1;
+ m_info.put(ret, cp);
+ }
+
+ return ret;
+ }
+
+ @Override
+ public long adjustCapacity(long id, long reserve) {
+ throw new UnsupportedOperationException("Unsupported to reduce capacity of this memory service");
+ }
+
+ @Override
+ public void close(long id) {
+ int idx = (int) id;
+ if (mem_pool.get(idx) != null) {
+ try {
+ mem_pool.get(idx).close();
+ } catch (IOException e) {
+ } finally {
+ mem_pool.set(idx, null);
+ }
+ }
+ }
+
+ @Override
+ public void sync(long id, long addr, long length, boolean autodetect) {
+ throw new UnsupportedOperationException("Unsupported to synchronization operation");
+ }
+
+ @Override
+ public long capacity(long id) {
+ return m_info.get(id);
+ }
+
+ @Override
+ public long allocate(long id, long size, boolean initzero) {
+ return 1L; //need detail
+ }
+
+ @Override
+ public long reallocate(long id, long addr, long size, boolean initzero) {
+ return 1L; //need detail
+ }
+
+ @Override
+ public void free(long id, long addr) {
+ ///mem_pool.get(id) = null;//need change//allocateVS free
+ }
+
+ @Override
+ public ByteBuffer createByteBuffer(long id, long size) {
+ ByteBuffer myByteBuffer = null;
+ /*try {
+ MapMode mapMode = readWrite ? MapMode.READ_WRITE : MapMode.READ_ONLY;
+ FileChannel channel = mem_pool.get(id).getChannel();
+ myByteBuffers = channel.map(mapMode, XXXXX, size);
+ } catch (Exception e) {
+ myBytebuffers = null;
+ }*///need change
+
+ return myByteBuffer;
+ }
+
+ @Override
+ public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
+ ByteBuffer myByteBuffer = null;
+ return myByteBuffer; //need change
+ }
+
+ @Override
+ public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
+ //more detail
+ }
+
+ @Override
+ public ByteBuffer retrieveByteBuffer(long id, long handler) {
+ ByteBuffer myByteBuffer = null;
+ return myByteBuffer; //need change
+ }
+
+ @Override
+ public long retrieveSize(long id, long handler) {
+ return 1L; //need change
+ }
+
+ @Override
+ public long getByteBufferHandler(long id, ByteBuffer buf) {
+ return 1L; //need change
+ }
+
+ @Override
+ public void setHandler(long id, long key, long handler) {
+ throw new UnsupportedOperationException("Unsupported to set handler");
+ }
+
+ @Override
+ public long getHandler(long id, long key) {
+ throw new UnsupportedOperationException("Unsupported to get handler");
+ }
+
+ @Override
+ public long handlerCapacity(long id) {
+ return 255;
+ }
+
+ @Override
+ public long getBaseAddress(long id) {
+ return 1L; //need change
+ }
+
+ @Override
+ public void beginTransaction(boolean readOnly) {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public void commitTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public void abortTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public boolean isInTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public Set<MemoryServiceFeature> getFeatures() {
+ Set<MemoryServiceFeature> ret = new HashSet<MemoryServiceFeature>();
+ ret.add(MemoryServiceFeature.VOLATILE);
+ return ret;
+ }
+
+ public Map<Long, Long> getMInfo() {
+ return this.m_info;
+ }
+ public ArrayList<RandomAccessFile> getMemPool() {
+ return this.mem_pool;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
deleted file mode 100644
index 3a402eb..0000000
--- a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImpl.java
+++ /dev/null
@@ -1,238 +0,0 @@
-/*
- * 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.mnemonic.service.memoryservice.internal;
-
-import org.apache.mnemonic.ConfigurationException;
-import org.apache.mnemonic.service.memoryservice.MemoryServiceFeature;
-import org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.HashSet;
-
-public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
-
- protected Map<Long, Long> m_info = Collections.synchronizedMap(new HashMap<Long, Long>());
- protected ArrayList<RandomAccessFile> mem_pool = new ArrayList<RandomAccessFile>();
-
- @Override
- public String getServiceId() {
- return "javavmem";
- }
-
- @Override
- public long init(long capacity, String uri, boolean isnew) {
- FileChannel channel = null;
- RandomAccessFile mappedFile = null;
- long cp = -1;
- long ret = -1;
-
-
- if (uri == null || uri.length() == 0) {
- throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
- }
- if (capacity <= 0) {
- throw new ConfigurationException("Please supply the capacity");
- }
-
- File file = new File(uri);
-
- if (file.exists() && !file.isFile()) {
- throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
- }
- if (file.isFile() && file.length() <= 0) {
- throw new ConfigurationException("File length should be more than zero.");
- }
-
- if (isnew) {
- if (file.exists()) {
- if (!file.delete()) {
- throw new ConfigurationException(String.format("Failed to delete the file: %s.", uri));
- }
- }
- try {
- mappedFile = new RandomAccessFile(file, "rw");
- mappedFile.setLength(capacity);
- cp = mappedFile.length();
- } catch (Exception ex) { }
- } else {
- if (!file.exists()) {
- throw new ConfigurationException(String.format("File doesn't exist under the specifiled uri: %s", uri));
- }
- try {
- mappedFile = new RandomAccessFile(file, "rw");
- cp = mappedFile.length();
- } catch (Exception ex) { }
- }
-
-
- if (mappedFile != null) {
- mem_pool.add(mappedFile);
- ret = mem_pool.size() - 1;
- m_info.put(ret, cp);
- }
-
- return ret;
- }
-
- @Override
- public long adjustCapacity(long id, long reserve) {
- throw new UnsupportedOperationException("Unsupported to reduce capacity of this memory service");
- }
-
- @Override
- public void close(long id) {
- int idx = (int) id;
- if (mem_pool.get(idx) != null) {
- try {
- mem_pool.get(idx).close();
- } catch (IOException e) {
- } finally {
- mem_pool.set(idx, null);
- }
- }
- }
-
- @Override
- public void sync(long id, long addr, long length, boolean autodetect) {
- throw new UnsupportedOperationException("Unsupported to synchronization operation");
- }
-
- @Override
- public long capacity(long id) {
- return m_info.get(id);
- }
-
- @Override
- public long allocate(long id, long size, boolean initzero) {
- return 1L; //need detail
- }
-
- @Override
- public long reallocate(long id, long addr, long size, boolean initzero) {
- return 1L; //need detail
- }
-
- @Override
- public void free(long id, long addr) {
- ///mem_pool.get(id) = null;//need change//allocateVS free
- }
-
- @Override
- public ByteBuffer createByteBuffer(long id, long size) {
- ByteBuffer myByteBuffer = null;
- /*try {
- MapMode mapMode = readWrite ? MapMode.READ_WRITE : MapMode.READ_ONLY;
- FileChannel channel = mem_pool.get(id).getChannel();
- myByteBuffers = channel.map(mapMode, XXXXX, size);
- } catch (Exception e) {
- myBytebuffers = null;
- }*///need change
-
- return myByteBuffer;
- }
-
- @Override
- public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
- ByteBuffer myByteBuffer = null;
- return myByteBuffer; //need change
- }
-
- @Override
- public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
- //more detail
- }
-
- @Override
- public ByteBuffer retrieveByteBuffer(long id, long handler) {
- ByteBuffer myByteBuffer = null;
- return myByteBuffer; //need change
- }
-
- @Override
- public long retrieveSize(long id, long handler) {
- return 1L; //need change
- }
-
- @Override
- public long getByteBufferHandler(long id, ByteBuffer buf) {
- return 1L; //need change
- }
-
- @Override
- public void setHandler(long id, long key, long handler) {
- throw new UnsupportedOperationException("Unsupported to set handler");
- }
-
- @Override
- public long getHandler(long id, long key) {
- throw new UnsupportedOperationException("Unsupported to get handler");
- }
-
- @Override
- public long handlerCapacity(long id) {
- return 255;
- }
-
- @Override
- public long getBaseAddress(long id) {
- return 1L; //need change
- }
-
- @Override
- public void beginTransaction(boolean readOnly) {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public void commitTransaction() {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public void abortTransaction() {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public boolean isInTransaction() {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public Set<MemoryServiceFeature> getFeatures() {
- Set<MemoryServiceFeature> ret = new HashSet<MemoryServiceFeature>();
- ret.add(MemoryServiceFeature.VOLATILE);
- return ret;
- }
-
- public Map<Long, Long> getMInfo() {
- return this.m_info;
- }
- public ArrayList<RandomAccessFile> getMemPool() {
- return this.mem_pool;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.VolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.VolatileMemoryAllocatorService b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.VolatileMemoryAllocatorService
new file mode 100644
index 0000000..ffcb626
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.VolatileMemoryAllocatorService
@@ -0,0 +1 @@
+org.apache.mnemonic.service.memory.internal.JavaVMemServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService b/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService
deleted file mode 100644
index a92f591..0000000
--- a/mnemonic-memory-services/mnemonic-java-vmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.mnemonic.service.memoryservice.internal.JavaVMemServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImplNGTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImplNGTest.java b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImplNGTest.java
new file mode 100644
index 0000000..b0d9907
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memory/internal/JavaVMemServiceImplNGTest.java
@@ -0,0 +1,155 @@
+/*
+ * 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.mnemonic.service.memory.internal;
+
+
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import static org.testng.Assert.assertTrue;
+
+/**
+ * test the functionalities of JavaVMemServiceImpl class
+ *
+ */
+public class JavaVMemServiceImplNGTest {
+ private String testFile = "./jvmstest.dat";
+ /**
+ * test to verify the initial of memory service successes when isnew = true
+ * regardless of whether the file exists or not (file exists)
+ */
+ @Test
+ public void testToInitByCreateNewFile1() throws IOException, ClassNotFoundException {
+ //Ensure it doesn't impact the test file
+ String dest = "./jvmstestCopy.dat";
+ Files.copy(Paths.get(testFile), Paths.get(dest));
+
+ JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+ long cap = 10 * 1024 * 1024;
+ long idx = jvms.init(10 * 1024 * 1024, dest, true);
+ assertTrue(idx != -1);
+ assertTrue(idx == 0);
+ assertTrue(jvms.getMInfo().get(idx).equals(cap));
+ jvms.close(idx);
+ Files.delete(Paths.get(dest));
+ }
+
+ /**
+ * test to verify the initial of memory service successes when isnew = true
+ * regardless of whether the file exists or not (file doesn't exist)
+ */
+ @Test
+ public void testToInitByCreateNewFile2() throws IOException, ClassNotFoundException {
+ JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+ long cap = 10 * 1024 * 1024;
+ long idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", true);
+ assertTrue(idx != -1);
+ assertTrue(idx == 0);
+ assertTrue(jvms.getMInfo().get(idx).equals(cap));
+
+ //Delete the new created test file
+ File file = new File("./jvmstestnotexist.dat");
+ file.delete();
+ jvms.close(idx);
+ }
+
+
+
+ /**
+ * test to verify the initial of memory service fails when isnew = true
+ * and the specifiled uri is not a file
+ */
+ @Test
+ public void testToInitFailWhenNofile() throws IOException, ClassNotFoundException {
+ long idx = -1;
+ boolean thrown = false;
+ JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+ try {
+ idx = jvms.init(10 * 1024 * 1024, ".", true);
+ } catch (Exception e) {
+ thrown = true;
+ } finally {
+ assertTrue(thrown);
+ if (idx >= 0) {
+ jvms.close(idx);
+ }
+ }
+ }
+
+ /**
+ * test to verify the initial of memory service successes when isnew = false
+ * and the specifiled file exists.
+ */
+ @Test
+ public void testToInitWhenFileExists() throws IOException, ClassNotFoundException {
+ JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+ File file = new File(testFile);
+ long cap = file.length();
+ long idx = jvms.init(10 * 1024 * 1024, testFile, false);
+ assertTrue(idx != -1);
+ assertTrue(idx == 0);
+ assertTrue(jvms.getMInfo().get(idx).equals(cap));
+ jvms.close(idx);
+ }
+
+ /**
+ * test to verify the initial of memory service fails when isnew = false
+ * and the specifiled file doesn't exist.
+ */
+ @Test
+ public void testToInitFailWhenFileNotExists() throws IOException, ClassNotFoundException {
+ long idx = -1;
+ boolean thrown = false;
+ JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+ try {
+ idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", false);
+ } catch (Exception e) {
+ thrown = true;
+ } finally {
+ assertTrue(thrown);
+ if (idx >= 0) {
+ jvms.close(idx);
+ }
+ }
+ }
+
+ /**
+ * test to verify the initial of memory service fails when isnew = false
+ * and the specifiled uri is not a file.
+ */
+ @Test
+ public void testToInitFailWhenNotAFile() throws IOException, ClassNotFoundException {
+ long idx = -1;
+ boolean thrown = false;
+ JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
+ try {
+ idx = jvms.init(10 * 1024 * 1024, ".", false);
+ } catch (Exception e) {
+ thrown = true;
+ } finally {
+ assertTrue(thrown);
+ if (idx >= 0) {
+ jvms.close(idx);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java
deleted file mode 100644
index d923954..0000000
--- a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/java/org/apache/mnemonic/service/memoryservice/internal/JavaVMemServiceImplNGTest.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * 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.mnemonic.service.memoryservice.internal;
-
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-
-import org.testng.annotations.Test;
-import static org.testng.Assert.assertTrue;
-
-/**
- * test the functionalities of JavaVMemServiceImpl class
- *
- */
-public class JavaVMemServiceImplNGTest {
- private String testFile = "./jvmstest.dat";
- /**
- * test to verify the initial of memory service successes when isnew = true
- * regardless of whether the file exists or not (file exists)
- */
- @Test
- public void testToInitByCreateNewFile1() throws IOException, ClassNotFoundException {
- //Ensure it doesn't impact the test file
- String dest = "./jvmstestCopy.dat";
- Files.copy(Paths.get(testFile), Paths.get(dest));
-
- JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
- long cap = 10 * 1024 * 1024;
- long idx = jvms.init(10 * 1024 * 1024, dest, true);
- assertTrue(idx != -1);
- assertTrue(idx == 0);
- assertTrue(jvms.getMInfo().get(idx).equals(cap));
- jvms.close(idx);
- Files.delete(Paths.get(dest));
- }
-
- /**
- * test to verify the initial of memory service successes when isnew = true
- * regardless of whether the file exists or not (file doesn't exist)
- */
- @Test
- public void testToInitByCreateNewFile2() throws IOException, ClassNotFoundException {
- JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
- long cap = 10 * 1024 * 1024;
- long idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", true);
- assertTrue(idx != -1);
- assertTrue(idx == 0);
- assertTrue(jvms.getMInfo().get(idx).equals(cap));
-
- //Delete the new created test file
- File file = new File("./jvmstestnotexist.dat");
- file.delete();
- jvms.close(idx);
- }
-
-
-
- /**
- * test to verify the initial of memory service fails when isnew = true
- * and the specifiled uri is not a file
- */
- @Test
- public void testToInitFailWhenNofile() throws IOException, ClassNotFoundException {
- long idx = -1;
- boolean thrown = false;
- JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
- try {
- idx = jvms.init(10 * 1024 * 1024, ".", true);
- } catch (Exception e) {
- thrown = true;
- } finally {
- assertTrue(thrown);
- if (idx >= 0) {
- jvms.close(idx);
- }
- }
- }
-
- /**
- * test to verify the initial of memory service successes when isnew = false
- * and the specifiled file exists.
- */
- @Test
- public void testToInitWhenFileExists() throws IOException, ClassNotFoundException {
- JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
- File file = new File(testFile);
- long cap = file.length();
- long idx = jvms.init(10 * 1024 * 1024, testFile, false);
- assertTrue(idx != -1);
- assertTrue(idx == 0);
- assertTrue(jvms.getMInfo().get(idx).equals(cap));
- jvms.close(idx);
- }
-
- /**
- * test to verify the initial of memory service fails when isnew = false
- * and the specifiled file doesn't exist.
- */
- @Test
- public void testToInitFailWhenFileNotExists() throws IOException, ClassNotFoundException {
- long idx = -1;
- boolean thrown = false;
- JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
- try {
- idx = jvms.init(10 * 1024 * 1024, "./jvmstestnotexist.dat", false);
- } catch (Exception e) {
- thrown = true;
- } finally {
- assertTrue(thrown);
- if (idx >= 0) {
- jvms.close(idx);
- }
- }
- }
-
- /**
- * test to verify the initial of memory service fails when isnew = false
- * and the specifiled uri is not a file.
- */
- @Test
- public void testToInitFailWhenNotAFile() throws IOException, ClassNotFoundException {
- long idx = -1;
- boolean thrown = false;
- JavaVMemServiceImpl jvms = new JavaVMemServiceImpl();
- try {
- idx = jvms.init(10 * 1024 * 1024, ".", false);
- } catch (Exception e) {
- thrown = true;
- } finally {
- assertTrue(thrown);
- if (idx >= 0) {
- jvms.close(idx);
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
index e0279cb..d93dff6 100644
--- a/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
+++ b/mnemonic-memory-services/mnemonic-java-vmem-service/src/test/resources/testng.xml
@@ -22,7 +22,7 @@
<suite name="Mnenomic Memory Service test Suite" verbose="1" parallel="tests" thread-count="1">
<test name="Java Memory Service" >
<classes>
- <class name="org.apache.mnemonic.service.memoryservice.internal.JavaVMemServiceImplNGTest" />
+ <class name="org.apache.mnemonic.service.memory.internal.JavaVMemServiceImplNGTest" />
</classes>
</test>
</suite>
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/PMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/PMemServiceImpl.java b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/PMemServiceImpl.java
new file mode 100644
index 0000000..25b4388
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/PMemServiceImpl.java
@@ -0,0 +1,215 @@
+/*
+ * 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.mnemonic.service.memory.internal;
+
+import org.apache.mnemonic.service.memory.MemoryServiceFeature;
+import org.apache.mnemonic.service.memory.NonVolatileMemoryAllocatorService;
+import org.flowcomputing.commons.primitives.NativeLibraryLoader;
+
+import java.nio.ByteBuffer;
+import java.util.HashSet;
+import java.util.Set;
+
+public class PMemServiceImpl implements NonVolatileMemoryAllocatorService {
+ static {
+ try {
+ NativeLibraryLoader.loadFromJar("pmemallocator");
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+
+ @Override
+ public String getServiceId() {
+ return "pmem";
+ }
+
+ @Override
+ public long init(long capacity, String uri, boolean isnew) {
+ return ninit(capacity, uri, isnew);
+ }
+
+ @Override
+ public long adjustCapacity(long id, long reserve) {
+ throw new UnsupportedOperationException("Unsupported to reduce capacity of this memory service");
+ }
+
+ @Override
+ public void close(long id) {
+ nclose(id);
+ }
+
+ @Override
+ public void sync(long id, long addr, long length, boolean autodetect) {
+ nsync(id, addr, length, autodetect);
+ }
+
+ @Override
+ public long capacity(long id) {
+ return ncapacity(id);
+ }
+
+ @Override
+ public long allocate(long id, long size, boolean initzero) {
+ return nallocate(id, size, initzero);
+ }
+
+ @Override
+ public long reallocate(long id, long addr, long size, boolean initzero) {
+ return nreallocate(id, addr, size, initzero);
+ }
+
+ @Override
+ public void free(long id, long addr) {
+ nfree(id, addr);
+ }
+
+ @Override
+ public ByteBuffer createByteBuffer(long id, long size) {
+ return ncreateByteBuffer(id, size);
+ }
+
+ @Override
+ public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
+ return nresizeByteBuffer(id, bytebuf, size);
+ }
+
+ @Override
+ public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
+ ndestroyByteBuffer(id, bytebuf);
+ }
+
+ @Override
+ public ByteBuffer retrieveByteBuffer(long id, long handler) {
+ return nretrieveByteBuffer(id, handler);
+ }
+
+ @Override
+ public long retrieveSize(long id, long handler) {
+ return nretrieveSize(id, handler);
+ }
+
+ @Override
+ public long getByteBufferHandler(long id, ByteBuffer buf) {
+ return ngetByteBufferHandler(id, buf);
+ }
+
+ @Override
+ public void setHandler(long id, long key, long handler) {
+ nsetHandler(id, key, handler);
+ }
+
+ @Override
+ public long getHandler(long id, long key) {
+ return ngetHandler(id, key);
+ }
+
+ @Override
+ public long handlerCapacity(long id) {
+ return nhandlerCapacity(id);
+ }
+
+ @Override
+ public void persist(long id, long addr, long length, boolean autodetect) {
+ npersist(id, addr, length, autodetect);
+ }
+
+ @Override
+ public void flush(long id, long addr, long length, boolean autodetect) {
+ nflush(id, addr, length, autodetect);
+ }
+
+ @Override
+ public void drain(long id) {
+ ndrain(id);
+ }
+
+ @Override
+ public long getBaseAddress(long id) {
+ return ngetBaseAddress(id);
+ }
+
+ @Override
+ public void beginTransaction(boolean readOnly) {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public void commitTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public void abortTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public boolean isInTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public Set<MemoryServiceFeature> getFeatures() {
+ Set<MemoryServiceFeature> ret = new HashSet<MemoryServiceFeature>();
+ ret.add(MemoryServiceFeature.VOLATILE);
+ ret.add(MemoryServiceFeature.NONVOLATILE);
+ return ret;
+ }
+
+ protected native long ninit(long capacity, String uri, boolean isnew);
+
+ protected native void nclose(long id);
+
+ protected native void nsync(long id, long addr, long length, boolean autodetect);
+
+ protected native long ncapacity(long id);
+
+ protected native long nallocate(long id, long size, boolean initzero);
+
+ protected native long nreallocate(long id, long addr, long size, boolean initzero);
+
+ protected native void nfree(long id, long addr);
+
+ protected native ByteBuffer ncreateByteBuffer(long id, long size);
+
+ protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
+
+ protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
+
+ protected native ByteBuffer nretrieveByteBuffer(long id, long handler);
+
+ protected native long nretrieveSize(long id, long handler);
+
+ protected native long ngetByteBufferHandler(long id, ByteBuffer buf);
+
+ protected native void nsetHandler(long id, long key, long handler);
+
+ protected native long ngetHandler(long id, long key);
+
+ protected native long nhandlerCapacity(long id);
+
+ protected native void npersist(long id, long addr, long length, boolean autodetect);
+
+ protected native void nflush(long id, long addr, long length, boolean autodetect);
+
+ protected native void ndrain(long id);
+
+ protected native long ngetBaseAddress(long id);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/PMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/PMemServiceImpl.java b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/PMemServiceImpl.java
deleted file mode 100644
index 0e0b7a5..0000000
--- a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/java/org/apache/mnemonic/service/memoryservice/internal/PMemServiceImpl.java
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- * 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.mnemonic.service.memoryservice.internal;
-
-import org.apache.mnemonic.service.memoryservice.MemoryServiceFeature;
-import org.apache.mnemonic.service.memoryservice.NonVolatileMemoryAllocatorService;
-import org.flowcomputing.commons.primitives.NativeLibraryLoader;
-
-import java.nio.ByteBuffer;
-import java.util.HashSet;
-import java.util.Set;
-
-public class PMemServiceImpl implements NonVolatileMemoryAllocatorService {
- static {
- try {
- NativeLibraryLoader.loadFromJar("pmemallocator");
- } catch (Exception e) {
- throw new Error(e);
- }
- }
-
- @Override
- public String getServiceId() {
- return "pmem";
- }
-
- @Override
- public long init(long capacity, String uri, boolean isnew) {
- return ninit(capacity, uri, isnew);
- }
-
- @Override
- public long adjustCapacity(long id, long reserve) {
- throw new UnsupportedOperationException("Unsupported to reduce capacity of this memory service");
- }
-
- @Override
- public void close(long id) {
- nclose(id);
- }
-
- @Override
- public void sync(long id, long addr, long length, boolean autodetect) {
- nsync(id, addr, length, autodetect);
- }
-
- @Override
- public long capacity(long id) {
- return ncapacity(id);
- }
-
- @Override
- public long allocate(long id, long size, boolean initzero) {
- return nallocate(id, size, initzero);
- }
-
- @Override
- public long reallocate(long id, long addr, long size, boolean initzero) {
- return nreallocate(id, addr, size, initzero);
- }
-
- @Override
- public void free(long id, long addr) {
- nfree(id, addr);
- }
-
- @Override
- public ByteBuffer createByteBuffer(long id, long size) {
- return ncreateByteBuffer(id, size);
- }
-
- @Override
- public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
- return nresizeByteBuffer(id, bytebuf, size);
- }
-
- @Override
- public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
- ndestroyByteBuffer(id, bytebuf);
- }
-
- @Override
- public ByteBuffer retrieveByteBuffer(long id, long handler) {
- return nretrieveByteBuffer(id, handler);
- }
-
- @Override
- public long retrieveSize(long id, long handler) {
- return nretrieveSize(id, handler);
- }
-
- @Override
- public long getByteBufferHandler(long id, ByteBuffer buf) {
- return ngetByteBufferHandler(id, buf);
- }
-
- @Override
- public void setHandler(long id, long key, long handler) {
- nsetHandler(id, key, handler);
- }
-
- @Override
- public long getHandler(long id, long key) {
- return ngetHandler(id, key);
- }
-
- @Override
- public long handlerCapacity(long id) {
- return nhandlerCapacity(id);
- }
-
- @Override
- public void persist(long id, long addr, long length, boolean autodetect) {
- npersist(id, addr, length, autodetect);
- }
-
- @Override
- public void flush(long id, long addr, long length, boolean autodetect) {
- nflush(id, addr, length, autodetect);
- }
-
- @Override
- public void drain(long id) {
- ndrain(id);
- }
-
- @Override
- public long getBaseAddress(long id) {
- return ngetBaseAddress(id);
- }
-
- @Override
- public void beginTransaction(boolean readOnly) {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public void commitTransaction() {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public void abortTransaction() {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public boolean isInTransaction() {
- throw new UnsupportedOperationException("Not support transaction");
- }
-
- @Override
- public Set<MemoryServiceFeature> getFeatures() {
- Set<MemoryServiceFeature> ret = new HashSet<MemoryServiceFeature>();
- ret.add(MemoryServiceFeature.VOLATILE);
- ret.add(MemoryServiceFeature.NONVOLATILE);
- return ret;
- }
-
- protected native long ninit(long capacity, String uri, boolean isnew);
-
- protected native void nclose(long id);
-
- protected native void nsync(long id, long addr, long length, boolean autodetect);
-
- protected native long ncapacity(long id);
-
- protected native long nallocate(long id, long size, boolean initzero);
-
- protected native long nreallocate(long id, long addr, long size, boolean initzero);
-
- protected native void nfree(long id, long addr);
-
- protected native ByteBuffer ncreateByteBuffer(long id, long size);
-
- protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
-
- protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
-
- protected native ByteBuffer nretrieveByteBuffer(long id, long handler);
-
- protected native long nretrieveSize(long id, long handler);
-
- protected native long ngetByteBufferHandler(long id, ByteBuffer buf);
-
- protected native void nsetHandler(long id, long key, long handler);
-
- protected native long ngetHandler(long id, long key);
-
- protected native long nhandlerCapacity(long id);
-
- protected native void npersist(long id, long addr, long length, boolean autodetect);
-
- protected native void nflush(long id, long addr, long length, boolean autodetect);
-
- protected native void ndrain(long id);
-
- protected native long ngetBaseAddress(long id);
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/CMakeLists.txt b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/CMakeLists.txt
index 2876ba7..ca27352 100644
--- a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/CMakeLists.txt
+++ b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/CMakeLists.txt
@@ -48,7 +48,7 @@ if (NOT LIBPMEMOBJ_LIBRARIES)
message(FATAL_ERROR "not found pmemobj library")
endif (NOT LIBPMEMOBJ_LIBRARIES)
-add_library(pmemallocator SHARED common.c org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.c)
+add_library(pmemallocator SHARED common.c org_apache_mnemonic_service_memory_internal_PMemServiceImpl.c)
target_include_directories(pmemallocator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(pmemallocator ${LIBPMEMOBJ_LIBRARIES} ${LIBPMEM_LIBRARIES})
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.c
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.c b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.c
new file mode 100644
index 0000000..ce72559
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.c
@@ -0,0 +1,414 @@
+/**
+ * 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.
+ */
+
+#include "org_apache_mnemonic_service_memory_internal_PMemServiceImpl.h"
+#include "libpmem.h"
+
+static PMPool *g_pmpool_arr = NULL;
+static size_t g_pmpool_count = 0;
+
+static pthread_rwlock_t g_pmp_rwlock = PTHREAD_RWLOCK_INITIALIZER;
+
+/******************************************************************************
+ ** JNI implementations
+ *****************************************************************************/
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nallocate(JNIEnv* env,
+ jobject this, jlong id, jlong size, jboolean initzero) {
+ PMPool *pool;
+ jlong ret = 0L;
+ void *p;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ p = prealloc(pool, NULL, size, initzero ? 1 : 0);
+ if (NULL != p) {
+ ret = addr_to_java(p);
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nreallocate(JNIEnv* env,
+ jobject this, jlong id, jlong addr, jlong size, jboolean initzero) {
+ PMPool *pool;
+ jlong ret = 0L;
+ void *p;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ p = addr_from_java(addr);
+ p = prealloc(pool, p, size, initzero ? 1 : 0);
+ if (NULL != p) {
+ ret = addr_to_java(p);
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nfree(
+ JNIEnv* env,
+ jobject this, jlong id,
+ jlong addr)
+{
+ PMPool *pool;
+ void* nativebuf;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ //fprintf(stderr, "nfree Get Called %ld, %X\n", id, addr);
+ nativebuf = addr_from_java(addr);
+ pfree(pool, nativebuf);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nsync(
+ JNIEnv* env,
+ jobject this, jlong id, jlong addr, jlong len, jboolean autodetect)
+{
+ PMPool *pool;
+ size_t capa;
+ void* nativebuf;
+ void* p = addr_from_java(addr);
+ if (autodetect) {
+ if (NULL != p) {
+ nativebuf = p - PMBHSZ;
+ pmem_msync(nativebuf, ((PMBHeader *) nativebuf)->size);
+ } else {
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ capa = pool->capacity;
+ pmem_msync(pool->base, capa);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ }
+ } else {
+ if (NULL != p && len > 0L) {
+ pmem_msync(p, len);
+ }
+ }
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_npersist(
+ JNIEnv* env,
+ jobject this, jlong id, jlong addr, jlong len, jboolean autodetect)
+{
+ PMPool *pool;
+ size_t capa;
+ void* nativebuf;
+ void* p = addr_from_java(addr);
+ if (autodetect) {
+ if (NULL != p) {
+ nativebuf = p - PMBHSZ;
+ pmem_persist(nativebuf, ((PMBHeader *) nativebuf)->size);
+ } else {
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ capa = pool->capacity;
+ pmem_persist(pool->base, capa);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ }
+ } else {
+ if (NULL != p && len > 0L) {
+ pmem_persist(p, len);
+ }
+ }
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nflush(
+ JNIEnv* env,
+ jobject this, jlong id, jlong addr, jlong len, jboolean autodetect)
+{
+ PMPool *pool;
+ size_t capa;
+ void* nativebuf;
+ void* p = addr_from_java(addr);
+ if (autodetect) {
+ if (NULL != p) {
+ nativebuf = p - PMBHSZ;
+ pmem_flush(nativebuf, ((PMBHeader *) nativebuf)->size);
+ } else {
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ capa = pool->capacity;
+ pmem_flush(pool->base, capa);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ }
+ } else {
+ if (NULL != p && len > 0L) {
+ pmem_flush(p, len);
+ }
+ }
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ndrain(
+ JNIEnv* env,
+ jobject this, jlong id)
+{
+ pmem_drain();
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ncapacity(
+ JNIEnv* env,
+ jobject this, jlong id)
+{
+ PMPool *pool;
+ jlong ret;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ ret = pool->capacity;
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ncreateByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jlong size) {
+ PMPool *pool;
+ jobject ret = NULL;
+ void* nativebuf = NULL;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ nativebuf = prealloc(pool, NULL, size, 0);
+ if (NULL != nativebuf) {
+ ret = (*env)->NewDirectByteBuffer(env, nativebuf, size);
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nretrieveByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jlong addr) {
+ jobject ret = NULL;
+ void* p = addr_from_java(addr);
+ if (NULL != p) {
+ void* nativebuf = p - PMBHSZ;
+ ret = (*env)->NewDirectByteBuffer(env, p, ((PMBHeader *) nativebuf)->size - PMBHSZ);
+ }
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nretrieveSize(JNIEnv *env,
+ jobject this, jlong id, jlong addr) {
+ jlong ret = 0L;
+ void* p = addr_from_java(addr);
+ if (NULL != p) {
+ void* nativebuf = p - PMBHSZ;
+ ret = ((PMBHeader *) nativebuf)->size - PMBHSZ;
+// fprintf(stderr, "### nretrieveSize size: %lld, %X ###, header size: %ld \n",
+// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmpool_arr + id)), PMBHSZ);
+ }
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ngetByteBufferHandler(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf) {
+// fprintf(stderr, "ngetByteBufferAddress Get Called %X, %X\n", env, bytebuf);
+ jlong ret = 0L;
+ if (NULL != bytebuf) {
+ void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
+// fprintf(stderr, "ngetByteBufferAddress Get Native addr %X\n", nativebuf);
+ ret = addr_to_java(nativebuf);
+ }
+// fprintf(stderr, "ngetByteBufferAddress returned addr %016lx\n", ret);
+ return ret;
+}
+
+JNIEXPORT
+jobject JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nresizeByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf, jlong size) {
+ PMPool *pool;
+ jobject ret = NULL;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ if (NULL != bytebuf) {
+ void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
+ if (nativebuf != NULL) {
+ nativebuf = prealloc(pool, nativebuf, size, 0);
+ if (NULL != nativebuf) {
+ ret = (*env)->NewDirectByteBuffer(env, nativebuf, size);
+ }
+ }
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ndestroyByteBuffer(
+ JNIEnv *env, jobject this, jlong id, jobject bytebuf)
+{
+ PMPool *pool;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ if (NULL != bytebuf) {
+ void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
+ pfree(pool, nativebuf);
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nsetHandler(
+ JNIEnv *env, jobject this, jlong id, jlong key, jlong value)
+{
+ PMPool *pool;
+ TOID(struct pmem_root) root;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ root = POBJ_ROOT(pool->pop, struct pmem_root);
+ if (key < MAX_HANDLER_STORE_LEN && key >= 0) {
+ D_RW(root)->hdl_buf[key] = value;
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ngetHandler(JNIEnv *env,
+ jobject this, jlong id, jlong key) {
+ PMPool *pool;
+ TOID(struct pmem_root) root;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ root = POBJ_ROOT(pool->pop, struct pmem_root);
+ jlong ret = (key < MAX_HANDLER_STORE_LEN && key >= 0) ? D_RO(root)->hdl_buf[key] : 0L;
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nhandlerCapacity(
+ JNIEnv *env, jobject this) {
+ return MAX_HANDLER_STORE_LEN;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ngetBaseAddress(JNIEnv *env,
+ jobject this, jlong id) {
+ PMPool *pool;
+ jlong ret;
+ pthread_rwlock_rdlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ ret = addr_to_java(pool->base);
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+jlong JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_ninit(JNIEnv *env,
+ jclass this, jlong capacity, jstring pathname, jboolean isnew) {
+ PMPool *pool;
+ TOID(struct pmem_root) root;
+ int needcreate;
+ jlong ret = -1;
+ PMEMobjpool *pop = NULL;
+ pthread_rwlock_wrlock(&g_pmp_rwlock);
+ const char* mpathname = (*env)->GetStringUTFChars(env, pathname, NULL);
+ if (NULL == mpathname) {
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ throw(env, "Big memory path not specified!");
+ }
+ needcreate = access(mpathname, F_OK);
+ if (isnew && !needcreate) {
+ if(0 != unlink(mpathname)) {
+ throw(env, "Failure to delete file to create new one.");
+ }
+ needcreate = 1;
+ }
+ if (needcreate && capacity < PMEMOBJ_MIN_POOL) {
+ capacity = PMEMOBJ_MIN_POOL;
+ }
+ if (needcreate) {
+ pop = pmemobj_create(mpathname, POBJ_LAYOUT_NAME(memory_service), capacity, S_IRUSR | S_IWUSR);
+ } else {
+ pop = pmemobj_open(mpathname, POBJ_LAYOUT_NAME(memory_service));
+ }
+ if (pop == NULL) {
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ throw(env, "Big memory init failure!");
+ }
+ (*env)->ReleaseStringUTFChars(env, pathname, mpathname);
+ g_pmpool_arr = realloc(g_pmpool_arr, (g_pmpool_count + 1) * sizeof(PMPool));
+ if (NULL != g_pmpool_arr) {
+ pool = g_pmpool_arr + g_pmpool_count;
+ pool->pop = pop;
+ root = POBJ_ROOT(pool->pop, struct pmem_root);
+ pool->uuid_lo = root.oid.pool_uuid_lo;
+ pool->base = (void*)pop;
+ if (needcreate) {
+ pool->capacity = capacity;
+ D_RW(root)->capacity = capacity;
+ } else {
+ pool->capacity = D_RO(root)->capacity;
+ }
+ ret = g_pmpool_count;
+ ++g_pmpool_count;
+ } else {
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ throw(env, "Big memory init Out of memory!");
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ return ret;
+}
+
+JNIEXPORT
+void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_nclose
+(JNIEnv *env, jobject this, jlong id)
+{
+ PMPool *pool;
+ pthread_rwlock_wrlock(&g_pmp_rwlock);
+ pool = g_pmpool_arr + id;
+ if (NULL != pool->pop) {
+ pmemobj_close(pool->pop);
+ pool->pop = NULL;
+ pool->base = NULL;
+ pool->uuid_lo = 0;
+ pool->capacity = 0;
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+}
+
+__attribute__((destructor)) void fini(void) {
+ int i;
+ PMPool *pool;
+ pthread_rwlock_wrlock(&g_pmp_rwlock);
+ if (NULL != g_pmpool_arr) {
+ for (i = 0; i < g_pmpool_count; ++i) {
+ pool = g_pmpool_arr + i;
+ if (NULL != pool->pop) {
+ pmemobj_close(pool->pop);
+ pool->pop = NULL;
+ pool->base = NULL;
+ pool->capacity = 0;
+ pool->uuid_lo = 0;
+ }
+ }
+ free(g_pmpool_arr);
+ g_pmpool_arr = NULL;
+ g_pmpool_count = 0;
+ }
+ pthread_rwlock_unlock(&g_pmp_rwlock);
+ pthread_rwlock_destroy(&g_pmp_rwlock);
+}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.h
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.h b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.h
new file mode 100644
index 0000000..3957a92
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memory_internal_PMemServiceImpl.h
@@ -0,0 +1,48 @@
+/**
+ * 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.
+ */
+
+#include <jni.h>
+/* Header for class org_apache_mnemonic_service_memory_internal_PMemServiceImpl */
+
+#include "common.h"
+
+#ifndef _Included_org_apache_mnemonic_service_memory_internal_PMemServiceImpl
+#define _Included_org_apache_mnemonic_service_memory_internal_PMemServiceImpl
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_apache_mnemonic_service_memory_internal_PMemServiceImpl
+ * Method: jniInit
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_jniInit
+(JNIEnv *, jobject, jint, jint);
+
+/*
+ * Class: org_apache_mnemonic_service_memory_internal_PMemServiceImpl
+ * Method: jniTerm
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_memory_internal_PMemServiceImpl_jniTerm
+(JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.c
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.c b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.c
deleted file mode 100644
index 86d4145..0000000
--- a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.c
+++ /dev/null
@@ -1,414 +0,0 @@
-/**
- * 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.
- */
-
-#include "org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.h"
-#include "libpmem.h"
-
-static PMPool *g_pmpool_arr = NULL;
-static size_t g_pmpool_count = 0;
-
-static pthread_rwlock_t g_pmp_rwlock = PTHREAD_RWLOCK_INITIALIZER;
-
-/******************************************************************************
- ** JNI implementations
- *****************************************************************************/
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nallocate(JNIEnv* env,
- jobject this, jlong id, jlong size, jboolean initzero) {
- PMPool *pool;
- jlong ret = 0L;
- void *p;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- p = prealloc(pool, NULL, size, initzero ? 1 : 0);
- if (NULL != p) {
- ret = addr_to_java(p);
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nreallocate(JNIEnv* env,
- jobject this, jlong id, jlong addr, jlong size, jboolean initzero) {
- PMPool *pool;
- jlong ret = 0L;
- void *p;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- p = addr_from_java(addr);
- p = prealloc(pool, p, size, initzero ? 1 : 0);
- if (NULL != p) {
- ret = addr_to_java(p);
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nfree(
- JNIEnv* env,
- jobject this, jlong id,
- jlong addr)
-{
- PMPool *pool;
- void* nativebuf;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- //fprintf(stderr, "nfree Get Called %ld, %X\n", id, addr);
- nativebuf = addr_from_java(addr);
- pfree(pool, nativebuf);
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nsync(
- JNIEnv* env,
- jobject this, jlong id, jlong addr, jlong len, jboolean autodetect)
-{
- PMPool *pool;
- size_t capa;
- void* nativebuf;
- void* p = addr_from_java(addr);
- if (autodetect) {
- if (NULL != p) {
- nativebuf = p - PMBHSZ;
- pmem_msync(nativebuf, ((PMBHeader *) nativebuf)->size);
- } else {
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- capa = pool->capacity;
- pmem_msync(pool->base, capa);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- }
- } else {
- if (NULL != p && len > 0L) {
- pmem_msync(p, len);
- }
- }
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_npersist(
- JNIEnv* env,
- jobject this, jlong id, jlong addr, jlong len, jboolean autodetect)
-{
- PMPool *pool;
- size_t capa;
- void* nativebuf;
- void* p = addr_from_java(addr);
- if (autodetect) {
- if (NULL != p) {
- nativebuf = p - PMBHSZ;
- pmem_persist(nativebuf, ((PMBHeader *) nativebuf)->size);
- } else {
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- capa = pool->capacity;
- pmem_persist(pool->base, capa);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- }
- } else {
- if (NULL != p && len > 0L) {
- pmem_persist(p, len);
- }
- }
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nflush(
- JNIEnv* env,
- jobject this, jlong id, jlong addr, jlong len, jboolean autodetect)
-{
- PMPool *pool;
- size_t capa;
- void* nativebuf;
- void* p = addr_from_java(addr);
- if (autodetect) {
- if (NULL != p) {
- nativebuf = p - PMBHSZ;
- pmem_flush(nativebuf, ((PMBHeader *) nativebuf)->size);
- } else {
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- capa = pool->capacity;
- pmem_flush(pool->base, capa);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- }
- } else {
- if (NULL != p && len > 0L) {
- pmem_flush(p, len);
- }
- }
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ndrain(
- JNIEnv* env,
- jobject this, jlong id)
-{
- pmem_drain();
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ncapacity(
- JNIEnv* env,
- jobject this, jlong id)
-{
- PMPool *pool;
- jlong ret;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- ret = pool->capacity;
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jobject JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ncreateByteBuffer(
- JNIEnv *env, jobject this, jlong id, jlong size) {
- PMPool *pool;
- jobject ret = NULL;
- void* nativebuf = NULL;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- nativebuf = prealloc(pool, NULL, size, 0);
- if (NULL != nativebuf) {
- ret = (*env)->NewDirectByteBuffer(env, nativebuf, size);
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jobject JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nretrieveByteBuffer(
- JNIEnv *env, jobject this, jlong id, jlong addr) {
- jobject ret = NULL;
- void* p = addr_from_java(addr);
- if (NULL != p) {
- void* nativebuf = p - PMBHSZ;
- ret = (*env)->NewDirectByteBuffer(env, p, ((PMBHeader *) nativebuf)->size - PMBHSZ);
- }
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nretrieveSize(JNIEnv *env,
- jobject this, jlong id, jlong addr) {
- jlong ret = 0L;
- void* p = addr_from_java(addr);
- if (NULL != p) {
- void* nativebuf = p - PMBHSZ;
- ret = ((PMBHeader *) nativebuf)->size - PMBHSZ;
-// fprintf(stderr, "### nretrieveSize size: %lld, %X ###, header size: %ld \n",
-// ((PMBHeader *)nativebuf)->size, nativebuf-b_addr(*(g_pmpool_arr + id)), PMBHSZ);
- }
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ngetByteBufferHandler(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf) {
-// fprintf(stderr, "ngetByteBufferAddress Get Called %X, %X\n", env, bytebuf);
- jlong ret = 0L;
- if (NULL != bytebuf) {
- void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
-// fprintf(stderr, "ngetByteBufferAddress Get Native addr %X\n", nativebuf);
- ret = addr_to_java(nativebuf);
- }
-// fprintf(stderr, "ngetByteBufferAddress returned addr %016lx\n", ret);
- return ret;
-}
-
-JNIEXPORT
-jobject JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nresizeByteBuffer(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf, jlong size) {
- PMPool *pool;
- jobject ret = NULL;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- if (NULL != bytebuf) {
- void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
- if (nativebuf != NULL) {
- nativebuf = prealloc(pool, nativebuf, size, 0);
- if (NULL != nativebuf) {
- ret = (*env)->NewDirectByteBuffer(env, nativebuf, size);
- }
- }
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ndestroyByteBuffer(
- JNIEnv *env, jobject this, jlong id, jobject bytebuf)
-{
- PMPool *pool;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- if (NULL != bytebuf) {
- void* nativebuf = (*env)->GetDirectBufferAddress(env, bytebuf);
- pfree(pool, nativebuf);
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nsetHandler(
- JNIEnv *env, jobject this, jlong id, jlong key, jlong value)
-{
- PMPool *pool;
- TOID(struct pmem_root) root;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- root = POBJ_ROOT(pool->pop, struct pmem_root);
- if (key < MAX_HANDLER_STORE_LEN && key >= 0) {
- D_RW(root)->hdl_buf[key] = value;
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ngetHandler(JNIEnv *env,
- jobject this, jlong id, jlong key) {
- PMPool *pool;
- TOID(struct pmem_root) root;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- root = POBJ_ROOT(pool->pop, struct pmem_root);
- jlong ret = (key < MAX_HANDLER_STORE_LEN && key >= 0) ? D_RO(root)->hdl_buf[key] : 0L;
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nhandlerCapacity(
- JNIEnv *env, jobject this) {
- return MAX_HANDLER_STORE_LEN;
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ngetBaseAddress(JNIEnv *env,
- jobject this, jlong id) {
- PMPool *pool;
- jlong ret;
- pthread_rwlock_rdlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- ret = addr_to_java(pool->base);
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-jlong JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_ninit(JNIEnv *env,
- jclass this, jlong capacity, jstring pathname, jboolean isnew) {
- PMPool *pool;
- TOID(struct pmem_root) root;
- int needcreate;
- jlong ret = -1;
- PMEMobjpool *pop = NULL;
- pthread_rwlock_wrlock(&g_pmp_rwlock);
- const char* mpathname = (*env)->GetStringUTFChars(env, pathname, NULL);
- if (NULL == mpathname) {
- pthread_rwlock_unlock(&g_pmp_rwlock);
- throw(env, "Big memory path not specified!");
- }
- needcreate = access(mpathname, F_OK);
- if (isnew && !needcreate) {
- if(0 != unlink(mpathname)) {
- throw(env, "Failure to delete file to create new one.");
- }
- needcreate = 1;
- }
- if (needcreate && capacity < PMEMOBJ_MIN_POOL) {
- capacity = PMEMOBJ_MIN_POOL;
- }
- if (needcreate) {
- pop = pmemobj_create(mpathname, POBJ_LAYOUT_NAME(memory_service), capacity, S_IRUSR | S_IWUSR);
- } else {
- pop = pmemobj_open(mpathname, POBJ_LAYOUT_NAME(memory_service));
- }
- if (pop == NULL) {
- pthread_rwlock_unlock(&g_pmp_rwlock);
- throw(env, "Big memory init failure!");
- }
- (*env)->ReleaseStringUTFChars(env, pathname, mpathname);
- g_pmpool_arr = realloc(g_pmpool_arr, (g_pmpool_count + 1) * sizeof(PMPool));
- if (NULL != g_pmpool_arr) {
- pool = g_pmpool_arr + g_pmpool_count;
- pool->pop = pop;
- root = POBJ_ROOT(pool->pop, struct pmem_root);
- pool->uuid_lo = root.oid.pool_uuid_lo;
- pool->base = (void*)pop;
- if (needcreate) {
- pool->capacity = capacity;
- D_RW(root)->capacity = capacity;
- } else {
- pool->capacity = D_RO(root)->capacity;
- }
- ret = g_pmpool_count;
- ++g_pmpool_count;
- } else {
- pthread_rwlock_unlock(&g_pmp_rwlock);
- throw(env, "Big memory init Out of memory!");
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- return ret;
-}
-
-JNIEXPORT
-void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_nclose
-(JNIEnv *env, jobject this, jlong id)
-{
- PMPool *pool;
- pthread_rwlock_wrlock(&g_pmp_rwlock);
- pool = g_pmpool_arr + id;
- if (NULL != pool->pop) {
- pmemobj_close(pool->pop);
- pool->pop = NULL;
- pool->base = NULL;
- pool->uuid_lo = 0;
- pool->capacity = 0;
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
-}
-
-__attribute__((destructor)) void fini(void) {
- int i;
- PMPool *pool;
- pthread_rwlock_wrlock(&g_pmp_rwlock);
- if (NULL != g_pmpool_arr) {
- for (i = 0; i < g_pmpool_count; ++i) {
- pool = g_pmpool_arr + i;
- if (NULL != pool->pop) {
- pmemobj_close(pool->pop);
- pool->pop = NULL;
- pool->base = NULL;
- pool->capacity = 0;
- pool->uuid_lo = 0;
- }
- }
- free(g_pmpool_arr);
- g_pmpool_arr = NULL;
- g_pmpool_count = 0;
- }
- pthread_rwlock_unlock(&g_pmp_rwlock);
- pthread_rwlock_destroy(&g_pmp_rwlock);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.h
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.h b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.h
deleted file mode 100644
index 543fee2..0000000
--- a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/native/org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/**
- * 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.
- */
-
-#include <jni.h>
-/* Header for class org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl */
-
-#include "common.h"
-
-#ifndef _Included_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl
-#define _Included_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl
-#ifdef __cplusplus
-extern "C" {
-#endif
-/*
- * Class: org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl
- * Method: jniInit
- * Signature: (II)V
- */
-JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_jniInit
-(JNIEnv *, jobject, jint, jint);
-
-/*
- * Class: org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl
- * Method: jniTerm
- * Signature: ()V
- */
-JNIEXPORT void JNICALL Java_org_apache_mnemonic_service_memoryservice_internal_PMemServiceImpl_jniTerm
-(JNIEnv *, jobject);
-
-#ifdef __cplusplus
-}
-#endif
-#endif
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.NonVolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.NonVolatileMemoryAllocatorService b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.NonVolatileMemoryAllocatorService
new file mode 100644
index 0000000..eb35454
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memory.NonVolatileMemoryAllocatorService
@@ -0,0 +1 @@
+org.apache.mnemonic.service.memory.internal.PMemServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.NonVolatileMemoryAllocatorService
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.NonVolatileMemoryAllocatorService b/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.NonVolatileMemoryAllocatorService
deleted file mode 100644
index e4b40da..0000000
--- a/mnemonic-memory-services/mnemonic-nvml-pmem-service/src/main/resources/META-INF/services/org.apache.mnemonic.service.memoryservice.NonVolatileMemoryAllocatorService
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.mnemonic.service.memoryservice.internal.PMemServiceImpl
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/a4bb10eb/mnemonic-memory-services/mnemonic-nvml-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/VMemServiceImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-memory-services/mnemonic-nvml-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/VMemServiceImpl.java b/mnemonic-memory-services/mnemonic-nvml-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/VMemServiceImpl.java
new file mode 100644
index 0000000..6f2969d
--- /dev/null
+++ b/mnemonic-memory-services/mnemonic-nvml-vmem-service/src/main/java/org/apache/mnemonic/service/memory/internal/VMemServiceImpl.java
@@ -0,0 +1,201 @@
+/*
+ * 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.mnemonic.service.memory.internal;
+
+import org.apache.mnemonic.service.memory.MemoryServiceFeature;
+import org.apache.mnemonic.service.memory.VolatileMemoryAllocatorService;
+import org.flowcomputing.commons.primitives.NativeLibraryLoader;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
+
+public class VMemServiceImpl implements VolatileMemoryAllocatorService {
+ static {
+ try {
+ NativeLibraryLoader.loadFromJar("vmemallocator");
+ } catch (Exception e) {
+ throw new Error(e);
+ }
+ }
+
+ protected Map<Long, Long> m_info = Collections.synchronizedMap(new HashMap<Long, Long>());
+
+ @Override
+ public String getServiceId() {
+ return "vmem";
+ }
+
+ @Override
+ public long init(long capacity, String uri, boolean isnew) {
+ long ret = ninit(capacity, uri, isnew);
+ m_info.put(ret, capacity);
+ return ret;
+ }
+
+ @Override
+ public long adjustCapacity(long id, long reserve) {
+ throw new UnsupportedOperationException("Unsupported to reduce capacity of this memory service");
+ }
+
+ @Override
+ public void close(long id) {
+ nclose(id);
+ }
+
+ @Override
+ public void sync(long id, long addr, long length, boolean autodetect) {
+ nsync(id, addr, length, autodetect);
+ }
+
+ @Override
+ public long capacity(long id) {
+ return m_info.get(id);
+ }
+
+ @Override
+ public long allocate(long id, long size, boolean initzero) {
+ return nallocate(id, size, initzero);
+ }
+
+ @Override
+ public long reallocate(long id, long addr, long size, boolean initzero) {
+ return nreallocate(id, addr, size, initzero);
+ }
+
+ @Override
+ public void free(long id, long addr) {
+ nfree(id, addr);
+ }
+
+ @Override
+ public ByteBuffer createByteBuffer(long id, long size) {
+ return ncreateByteBuffer(id, size);
+ }
+
+ @Override
+ public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
+ return nresizeByteBuffer(id, bytebuf, size);
+ }
+
+ @Override
+ public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
+ ndestroyByteBuffer(id, bytebuf);
+ }
+
+ @Override
+ public ByteBuffer retrieveByteBuffer(long id, long handler) {
+ return nretrieveByteBuffer(id, handler);
+ }
+
+ @Override
+ public long retrieveSize(long id, long handler) {
+ return nretrieveSize(id, handler);
+ }
+
+ @Override
+ public long getByteBufferHandler(long id, ByteBuffer buf) {
+ return ngetByteBufferHandler(id, buf);
+ }
+
+ @Override
+ public void setHandler(long id, long key, long handler) {
+ nsetHandler(id, key, handler);
+ }
+
+ @Override
+ public long getHandler(long id, long key) {
+ return ngetHandler(id, key);
+ }
+
+ @Override
+ public long handlerCapacity(long id) {
+ return nhandlerCapacity(id);
+ }
+
+ @Override
+ public long getBaseAddress(long id) {
+ return 0L;
+ //return ngetBaseAddress(id);
+ }
+
+ @Override
+ public void beginTransaction(boolean readOnly) {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public void commitTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public void abortTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public boolean isInTransaction() {
+ throw new UnsupportedOperationException("Not support transaction");
+ }
+
+ @Override
+ public Set<MemoryServiceFeature> getFeatures() {
+ Set<MemoryServiceFeature> ret = new HashSet<MemoryServiceFeature>();
+ ret.add(MemoryServiceFeature.VOLATILE);
+ return ret;
+ }
+
+ protected native long ninit(long capacity, String uri, boolean isnew);
+
+ protected native void nclose(long id);
+
+ protected native void nsync(long id, long addr, long length, boolean autodetect);
+
+ protected native long ncapacity(long id);
+
+ protected native long nallocate(long id, long size, boolean initzero);
+
+ protected native long nreallocate(long id, long addr, long size, boolean initzero);
+
+ protected native void nfree(long id, long addr);
+
+ protected native ByteBuffer ncreateByteBuffer(long id, long size);
+
+ protected native ByteBuffer nresizeByteBuffer(long id, ByteBuffer bytebuf, long size);
+
+ protected native void ndestroyByteBuffer(long id, ByteBuffer bytebuf);
+
+ protected native ByteBuffer nretrieveByteBuffer(long id, long handler);
+
+ protected native long nretrieveSize(long id, long handler);
+
+ protected native long ngetByteBufferHandler(long id, ByteBuffer buf);
+
+ protected native void nsetHandler(long id, long key, long handler);
+
+ protected native long ngetHandler(long id, long key);
+
+ protected native long nhandlerCapacity(long id);
+
+ protected native long ngetBaseAddress(long id);
+
+}