create maven plugin for generator
diff --git a/asterix-maven-plugins/pom.xml b/asterix-maven-plugins/pom.xml
index 56bc697..b26a6d6 100644
--- a/asterix-maven-plugins/pom.xml
+++ b/asterix-maven-plugins/pom.xml
@@ -35,5 +35,6 @@
<modules>
<module>lexer-generator-maven-plugin</module>
+ <module>record-manager-generator-maven-plugin</module>
</modules>
</project>
diff --git a/asterix-maven-plugins/record-manager-generator-maven-plugin/pom.xml b/asterix-maven-plugins/record-manager-generator-maven-plugin/pom.xml
new file mode 100644
index 0000000..9ffc8d2
--- /dev/null
+++ b/asterix-maven-plugins/record-manager-generator-maven-plugin/pom.xml
@@ -0,0 +1,56 @@
+<!--
+ ! Copyright 2009-2013 by The Regents of the University of California
+ ! Licensed under the Apache License, Version 2.0 (the "License");
+ ! you may not use this file except in compliance with the License.
+ ! you may obtain a copy of the License from
+ !
+ ! 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.
+ !-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>edu.uci.ics.asterix</groupId>
+ <artifactId>record-manager-generator-maven-plugin</artifactId>
+ <parent>
+ <artifactId>asterix-maven-plugins</artifactId>
+ <groupId>edu.uci.ics.asterix</groupId>
+ <version>0.8.1-SNAPSHOT</version>
+ </parent>
+
+ <packaging>maven-plugin</packaging>
+ <name>record-manager-generator-maven-plugin</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.7</source>
+ <target>1.7</target>
+ <fork>true</fork>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0.2</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/Generator.java b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/Generator.java
similarity index 74%
rename from asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/Generator.java
rename to asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/Generator.java
index a87d321..66f366e 100644
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/Generator.java
+++ b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/Generator.java
@@ -1,13 +1,34 @@
-package edu.uci.ics.asterix.transaction.management.service.locking;
+/*
+ * Copyright 2013 by The Regents of the University of California
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * you may obtain a copy of the License from
+ *
+ * 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 edu.uci.ics.asterix.recordmanagergenerator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import edu.uci.ics.asterix.transaction.management.service.locking.RecordType.Field;
+import edu.uci.ics.asterix.recordmanagergenerator.RecordType.Field;
public class Generator {
+
+ public enum Manager {
+ RECORD,
+ ARENA
+ }
+
public static void main(String args[]) {
RecordType resource = new RecordType("Resource");
@@ -30,19 +51,28 @@
StringBuilder sb = new StringBuilder();
- generateMemoryManagerSource(request, sb);
+ //generateMemoryManagerSource(request, sb);
//generateMemoryManagerSource(resource, sb);
//generateArenaManagerSource(request, sb);
//generateArenaManagerSource(resource, sb);
System.out.println(sb.toString());
}
+
+ public static void generateSource(Manager mgr, RecordType rec, InputStream is, StringBuilder sb) {
+ switch (mgr) {
+ case RECORD:
+ generateMemoryManagerSource(rec, is, sb);
+ break;
+ case ARENA:
+ generateArenaManagerSource(rec, is, sb);
+ break;
+ default:
+ throw new IllegalArgumentException();
+ }
+ }
- private static void generateMemoryManagerSource(RecordType resource, StringBuilder sb) {
- InputStream is = resource.getClass().getResourceAsStream("StructuredMemoryManager.java.txt");
- if (is == null) {
- throw new IllegalStateException();
- }
+ private static void generateMemoryManagerSource(RecordType resource, InputStream is, StringBuilder sb) {
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = null;
@@ -83,11 +113,7 @@
}
}
- private static void generateArenaManagerSource(RecordType resource, StringBuilder sb) {
- InputStream is = resource.getClass().getResourceAsStream("ArenaManager.java.txt");
- if (is == null) {
- throw new IllegalStateException();
- }
+ private static void generateArenaManagerSource(RecordType resource, InputStream is, StringBuilder sb) {
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = null;
diff --git a/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/RecordManagerGeneratorMojo.java b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/RecordManagerGeneratorMojo.java
new file mode 100644
index 0000000..7c932f4
--- /dev/null
+++ b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/RecordManagerGeneratorMojo.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2013 by The Regents of the University of California
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * you may obtain a copy of the License from
+ *
+ * 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 edu.uci.ics.asterix.recordmanagergenerator;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+
+/**
+ * @goal generate-record-manager
+ * @phase generate-sources
+ * @requiresDependencyResolution compile
+ */
+public class RecordManagerGeneratorMojo extends AbstractMojo {
+ /**
+ * parameter injected from pom.xml
+ *
+ * @parameter
+ */
+ private String arenaManagerTemplate;
+ /**
+ * parameter injected from pom.xml
+ *
+ * @parameter
+ */
+ private String recordManagerTemplate;
+ /**
+ * parameter injected from pom.xml
+ *
+ * @parameter
+ * @required
+ */
+ private String[] recordTypes;
+ /**
+ * parameter injected from pom.xml
+ *
+ * @parameter
+ * @required
+ */
+ private File outputDir;
+
+ private Map<String, RecordType> typeMap;
+
+ public RecordManagerGeneratorMojo() {
+ typeMap = new HashMap<String, RecordType>();
+
+ RecordType resource = new RecordType("Resource");
+ resource.addField("last holder", RecordType.Type.INT, "-1");
+ resource.addField("first waiter", RecordType.Type.INT, "-1");
+ resource.addField("first upgrader", RecordType.Type.INT, "-1");
+ resource.addField("max mode", RecordType.Type.INT, "LockMode.NL");
+ resource.addField("dataset id", RecordType.Type.INT, null);
+ resource.addField("pk hash val", RecordType.Type.INT, null);
+ resource.addField("next", RecordType.Type.INT, null);
+
+ typeMap.put(resource.name, resource);
+
+ RecordType request = new RecordType("Request");
+ request.addField("resource id", RecordType.Type.INT, null);
+ request.addField("lock mode", RecordType.Type.INT, null);
+ request.addField("job id", RecordType.Type.INT, null);
+ request.addField("prev job request", RecordType.Type.INT, null);
+ request.addField("next job request", RecordType.Type.INT, null);
+ request.addField("next request", RecordType.Type.INT, null);
+
+ typeMap.put(request.name, request);
+ }
+
+ public void execute() throws MojoExecutionException {
+ if (!outputDir.exists()) {
+ outputDir.mkdirs();
+ }
+
+ for (int i = 0; i < recordTypes.length; ++i) {
+ final String recordType = recordTypes[i];
+
+ if (recordManagerTemplate != null) {
+ generateSource(Generator.Manager.RECORD, recordManagerTemplate, recordType);
+ }
+
+ if (arenaManagerTemplate != null) {
+ generateSource(Generator.Manager.ARENA, arenaManagerTemplate, recordType);
+ }
+ }
+ }
+
+ private void generateSource(Generator.Manager mgrType, String template, String recordType) {
+ try {
+ InputStream is = getClass().getClassLoader().getResourceAsStream(template);
+ if (is == null) {
+ throw new IllegalStateException();
+ }
+
+ StringBuilder sb = new StringBuilder();
+ Generator.generateSource(mgrType, typeMap.get(recordType), is, sb);
+
+ is.close();
+
+ File outputFile = new File(outputDir, recordType + template);
+ getLog().info("generating " + outputFile.toString());
+
+ FileWriter outWriter = new FileWriter(outputFile);
+ outWriter.write(sb.toString());
+ outWriter.close();
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ }
+ }
+}
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RecordType.java b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/RecordType.java
similarity index 93%
rename from asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RecordType.java
rename to asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/RecordType.java
index 0b6c255..5212885 100644
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RecordType.java
+++ b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/java/edu/uci/ics/asterix/recordmanagergenerator/RecordType.java
@@ -1,4 +1,19 @@
-package edu.uci.ics.asterix.transaction.management.service.locking;
+/*
+ * Copyright 2013 by The Regents of the University of California
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * you may obtain a copy of the License from
+ *
+ * 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 edu.uci.ics.asterix.recordmanagergenerator;
import java.util.ArrayList;
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ArenaManager.java.txt b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/resources/ArenaManager.java
similarity index 65%
rename from asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ArenaManager.java.txt
rename to asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/resources/ArenaManager.java
index cf14280..744b0f9 100644
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ArenaManager.java.txt
+++ b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/resources/ArenaManager.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2013 by The Regents of the University of California
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * you may obtain a copy of the License from
+ *
+ * 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 edu.uci.ics.asterix.transaction.management.service.locking;
import java.util.ArrayList;
@@ -5,13 +20,13 @@
public class @E@ArenaManager {
private final int noArenas;
- private ArrayList<@E@MemoryManager> arenas;
+ private ArrayList<@E@RecordManager> arenas;
private volatile int nextArena;
private ThreadLocal<LocalManager> local;
public @E@ArenaManager() {
noArenas = Runtime.getRuntime().availableProcessors() * 2;
- arenas = new ArrayList<@E@MemoryManager>(noArenas);
+ arenas = new ArrayList<@E@RecordManager>(noArenas);
nextArena = 0;
local = new ThreadLocal<LocalManager>() {
@Override
@@ -43,9 +58,9 @@
public synchronized LocalManager getNext() {
if (nextArena >= arenas.size()) {
- arenas.add(new @E@MemoryManager());
+ arenas.add(new @E@RecordManager());
}
- @E@MemoryManager mgr = arenas.get(nextArena);
+ @E@RecordManager mgr = arenas.get(nextArena);
LocalManager res = new LocalManager();
res.mgr = mgr;
res.arenaId = nextArena;
@@ -53,11 +68,11 @@
return res;
}
- public @E@MemoryManager get(int i) {
+ public @E@RecordManager get(int i) {
return arenas.get(i);
}
- public @E@MemoryManager local() {
+ public @E@RecordManager local() {
return local.get().mgr;
}
@@ -65,7 +80,7 @@
static class LocalManager {
int arenaId;
- @E@MemoryManager mgr;
+ @E@RecordManager mgr;
}
StringBuffer append(StringBuffer sb) {
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/StructuredMemoryManager.java.txt b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/resources/RecordManager.java
similarity index 90%
rename from asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/StructuredMemoryManager.java.txt
rename to asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/resources/RecordManager.java
index 0f82d72..c8c64ab 100644
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/StructuredMemoryManager.java.txt
+++ b/asterix-maven-plugins/record-manager-generator-maven-plugin/src/main/resources/RecordManager.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2013 by The Regents of the University of California
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * you may obtain a copy of the License from
+ *
+ * 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 edu.uci.ics.asterix.transaction.management.service.locking;
import java.nio.ByteBuffer;
@@ -5,7 +20,7 @@
import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionManagementConstants.LockManagerConstants.LockMode;
-public class @E@MemoryManager {
+public class @E@RecordManager {
public static final int SHRINK_TIMER_THRESHOLD = 120000; //2min
@@ -20,7 +35,7 @@
private long shrinkTimer;
private boolean isShrinkTimerOn;
- public @E@MemoryManager() {
+ public @E@RecordManager() {
buffers = new ArrayList<Buffer>();
buffers.add(new Buffer());
current = 0;
@@ -238,4 +253,4 @@
}
}
-}
\ No newline at end of file
+}
diff --git a/asterix-transactions/pom.xml b/asterix-transactions/pom.xml
index 3123008..67dc5a0 100644
--- a/asterix-transactions/pom.xml
+++ b/asterix-transactions/pom.xml
@@ -33,6 +33,44 @@
<fork>true</fork>
</configuration>
</plugin>
+ <plugin>
+ <groupId>edu.uci.ics.asterix</groupId>
+ <artifactId>record-manager-generator-maven-plugin</artifactId>
+ <version>0.8.1-SNAPSHOT</version>
+ <configuration>
+ <arenaManagerTemplate>ArenaManager.java</arenaManagerTemplate>
+ <recordManagerTemplate>RecordManager.java</recordManagerTemplate>
+ <recordTypes><param>Request</param><param>Resource</param></recordTypes>
+ <outputDir>${project.build.directory}/generated-sources/java/edu/uci/ics/asterix/transaction/management/service/locking</outputDir>
+ </configuration>
+ <executions>
+ <execution>
+ <id>generate-record-manager</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>generate-record-manager</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>add-source</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>add-source</goal>
+ </goals>
+ <configuration>
+ <sources>
+ <source>${project.build.directory}/generated-sources/java/</source>
+ </sources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RequestArenaManager.java b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RequestArenaManager.java
deleted file mode 100644
index 0247dc7..0000000
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RequestArenaManager.java
+++ /dev/null
@@ -1,190 +0,0 @@
-package edu.uci.ics.asterix.transaction.management.service.locking;
-
-import java.util.ArrayList;
-
-public class RequestArenaManager {
-
- private final int noArenas;
- private ArrayList<RequestMemoryManager> arenas;
- private volatile int nextArena;
- private ThreadLocal<LocalManager> local;
-
- public RequestArenaManager() {
- noArenas = Runtime.getRuntime().availableProcessors() * 2;
- arenas = new ArrayList<RequestMemoryManager>(noArenas);
- nextArena = 0;
- local = new ThreadLocal<LocalManager>() {
- @Override
- protected LocalManager initialValue() {
- return getNext();
- }
- };
- }
-
- public static int arenaId(int i) {
- return (i >> 24) & 0xff;
- }
-
- public static int localId(int i) {
- return i & 0xffffff;
- }
-
- public int allocate() {
- final LocalManager localManager = local.get();
- int result = localManager.arenaId << 24;
- result |= localManager.mgr.allocate();
- return result;
- }
-
- public void deallocate(int slotNum) {
- final int arenaId = arenaId(slotNum);
- get(arenaId).deallocate(localId(slotNum));
- }
-
- public synchronized LocalManager getNext() {
- if (nextArena >= arenas.size()) {
- arenas.add(new RequestMemoryManager());
- }
- RequestMemoryManager mgr = arenas.get(nextArena);
- LocalManager res = new LocalManager();
- res.mgr = mgr;
- res.arenaId = nextArena;
- nextArena = (nextArena + 1) % noArenas;
- return res;
- }
-
- public RequestMemoryManager get(int i) {
- return arenas.get(i);
- }
-
- public RequestMemoryManager local() {
- return local.get().mgr;
- }
-
- public int getResourceId(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getResourceId(localId(slotNum));
- }
-
- public void setResourceId(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setResourceId(localId(slotNum), value);
- }
-
- public int getLockMode(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getLockMode(localId(slotNum));
- }
-
- public void setLockMode(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setLockMode(localId(slotNum), value);
- }
-
- public int getJobId(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getJobId(localId(slotNum));
- }
-
- public void setJobId(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setJobId(localId(slotNum), value);
- }
-
- public int getPrevJobRequest(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getPrevJobRequest(localId(slotNum));
- }
-
- public void setPrevJobRequest(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setPrevJobRequest(localId(slotNum), value);
- }
-
- public int getNextJobRequest(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getNextJobRequest(localId(slotNum));
- }
-
- public void setNextJobRequest(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setNextJobRequest(localId(slotNum), value);
- }
-
- public int getNextRequest(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getNextRequest(localId(slotNum));
- }
-
- public void setNextRequest(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setNextRequest(localId(slotNum), value);
- }
-
-
- static class LocalManager {
- int arenaId;
- RequestMemoryManager mgr;
- }
-
- StringBuffer append(StringBuffer sb) {
- for (int i = 0; i < arenas.size(); ++i) {
- sb.append("++++ arena ").append(i).append(" ++++\n");
- arenas.get(i).append(sb);
- }
- return sb;
- }
-
- public String toString() {
- return append(new StringBuffer()).toString();
- }
-}
-
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RequestMemoryManager.java b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RequestMemoryManager.java
deleted file mode 100644
index 2d47185..0000000
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/RequestMemoryManager.java
+++ /dev/null
@@ -1,361 +0,0 @@
-package edu.uci.ics.asterix.transaction.management.service.locking;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-
-import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionManagementConstants.LockManagerConstants.LockMode;
-
-public class RequestMemoryManager {
-
- public static final int SHRINK_TIMER_THRESHOLD = 120000; //2min
-
- static final int NO_SLOTS = 10;
- static final int NEXT_FREE_SLOT_OFFSET = 0;
-
- public static int ITEM_SIZE = 24;
- public static int RESOURCE_ID_OFF = 0;
- public static int LOCK_MODE_OFF = 4;
- public static int JOB_ID_OFF = 8;
- public static int PREV_JOB_REQUEST_OFF = 12;
- public static int NEXT_JOB_REQUEST_OFF = 16;
- public static int NEXT_REQUEST_OFF = 20;
-
-
- private ArrayList<Buffer> buffers;
- private int current;
- private int occupiedSlots;
- private long shrinkTimer;
- private boolean isShrinkTimerOn;
-
- public RequestMemoryManager() {
- buffers = new ArrayList<Buffer>();
- buffers.add(new Buffer());
- current = 0;
- }
-
- public int allocate() {
- if (buffers.get(current).isFull()) {
- int size = buffers.size();
- boolean needNewBuffer = true;
- for (int i = 0; i < size; i++) {
- Buffer buffer = buffers.get(i);
- if (! buffer.isInitialized()) {
- buffer.initialize();
- current = i;
- needNewBuffer = false;
- break;
- }
- }
-
- if (needNewBuffer) {
- buffers.add(new Buffer());
- current = buffers.size() - 1;
- }
- }
- ++occupiedSlots;
- return buffers.get(current).allocate() + current * NO_SLOTS;
- }
-
- void deallocate(int slotNum) {
- buffers.get(slotNum / NO_SLOTS).deallocate(slotNum % NO_SLOTS);
- --occupiedSlots;
-
- if (needShrink()) {
- shrink();
- }
- }
-
- /**
- * Shrink policy:
- * Shrink when the resource under-utilization lasts for a certain amount of time.
- * TODO Need to figure out which of the policies is better
- * case1.
- * buffers status : O x x x x x O (O is initialized, x is deinitialized)
- * In the above status, 'CURRENT' needShrink() returns 'TRUE'
- * even if there is nothing to shrink or deallocate.
- * It doesn't distinguish the deinitialized children from initialized children
- * by calculating totalNumOfSlots = buffers.size() * ChildEntityLockInfoArrayManager.NUM_OF_SLOTS.
- * In other words, it doesn't subtract the deinitialized children's slots.
- * case2.
- * buffers status : O O x x x x x
- * However, in the above case, if we subtract the deinitialized children's slots,
- * needShrink() will return false even if we shrink the buffers at this case.
- *
- * @return
- */
- private boolean needShrink() {
- int size = buffers.size();
- int usedSlots = occupiedSlots;
- if (usedSlots == 0) {
- usedSlots = 1;
- }
-
- if (size > 1 && size * NO_SLOTS / usedSlots >= 3) {
- if (isShrinkTimerOn) {
- if (System.currentTimeMillis() - shrinkTimer >= SHRINK_TIMER_THRESHOLD) {
- isShrinkTimerOn = false;
- return true;
- }
- } else {
- //turn on timer
- isShrinkTimerOn = true;
- shrinkTimer = System.currentTimeMillis();
- }
- } else {
- //turn off timer
- isShrinkTimerOn = false;
- }
-
- return false;
- }
-
- /**
- * Shrink() may
- * deinitialize(:deallocates ByteBuffer of child) Children(s) or
- * shrink buffers according to the deinitialized children's contiguity status.
- * It doesn't deinitialze or shrink more than half of children at a time.
- */
- private void shrink() {
- int i;
- int removeCount = 0;
- int size = buffers.size();
- int maxDecreaseCount = size / 2;
- Buffer buffer;
-
- //The first buffer never be deinitialized.
- for (i = 1; i < size; i++) {
- if (buffers.get(i).isEmpty()) {
- buffers.get(i).deinitialize();
- }
- }
-
- //remove the empty buffers from the end
- for (i = size - 1; i >= 1; i--) {
- buffer = buffers.get(i);
- if (! buffer.isInitialized()) {
- buffers.remove(i);
- if (++removeCount == maxDecreaseCount) {
- break;
- }
- } else {
- break;
- }
- }
-
- //reset allocChild to the first buffer
- current = 0;
-
- isShrinkTimerOn = false;
- }
-
- public int getResourceId(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + RESOURCE_ID_OFF);
- }
-
- public void setResourceId(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + RESOURCE_ID_OFF, value);
- }
-
- public int getLockMode(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + LOCK_MODE_OFF);
- }
-
- public void setLockMode(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + LOCK_MODE_OFF, value);
- }
-
- public int getJobId(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + JOB_ID_OFF);
- }
-
- public void setJobId(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + JOB_ID_OFF, value);
- }
-
- public int getPrevJobRequest(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + PREV_JOB_REQUEST_OFF);
- }
-
- public void setPrevJobRequest(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + PREV_JOB_REQUEST_OFF, value);
- }
-
- public int getNextJobRequest(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + NEXT_JOB_REQUEST_OFF);
- }
-
- public void setNextJobRequest(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + NEXT_JOB_REQUEST_OFF, value);
- }
-
- public int getNextRequest(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + NEXT_REQUEST_OFF);
- }
-
- public void setNextRequest(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + NEXT_REQUEST_OFF, value);
- }
-
-
- StringBuffer append(StringBuffer sb) {
- sb.append("+++ current: ")
- .append(current)
- .append(" no occupied slots: ")
- .append(occupiedSlots)
- .append(" +++\n");
- for (int i = 0; i < buffers.size(); ++i) {
- buffers.get(i).append(sb);
- sb.append("\n");
- }
- return sb;
- }
-
- public String toString() {
- StringBuffer sb = new StringBuffer();
- append(sb);
- return sb.toString();
- }
-
- static class Buffer {
- private ByteBuffer bb;
- private int freeSlotNum;
- private int occupiedSlots = -1; //-1 represents 'deinitialized' state.
-
- Buffer() {
- initialize();
- }
-
- void initialize() {
- bb = ByteBuffer.allocate(NO_SLOTS * ITEM_SIZE);
- freeSlotNum = 0;
- occupiedSlots = 0;
-
- for (int i = 0; i < NO_SLOTS - 1; i++) {
- setNextFreeSlot(i, i + 1);
- }
- setNextFreeSlot(NO_SLOTS - 1, -1); //-1 represents EOL(end of link)
- }
-
- public void deinitialize() {
- bb = null;
- occupiedSlots = -1;
- }
-
- public boolean isInitialized() {
- return occupiedSlots >= 0;
- }
-
- public boolean isFull() {
- return occupiedSlots == NO_SLOTS;
- }
-
- public boolean isEmpty() {
- return occupiedSlots == 0;
- }
-
- public int allocate() {
- int currentSlot = freeSlotNum;
- freeSlotNum = getNextFreeSlot(currentSlot);
- occupiedSlots++;
- return currentSlot;
- }
-
- public void deallocate(int slotNum) {
- setNextFreeSlot(slotNum, freeSlotNum);
- freeSlotNum = slotNum;
- occupiedSlots--;
- }
-
- public int getNextFreeSlot(int slotNum) {
- return bb.getInt(slotNum * ITEM_SIZE + NEXT_FREE_SLOT_OFFSET);
- }
-
- public void setNextFreeSlot(int slotNum, int nextFreeSlot) {
- bb.putInt(slotNum * ITEM_SIZE + NEXT_FREE_SLOT_OFFSET, nextFreeSlot);
- }
-
- StringBuffer append(StringBuffer sb) {
- sb.append("++ free slot: ")
- .append(freeSlotNum)
- .append(" no occupied slots: ")
- .append(occupiedSlots)
- .append(" ++\n");
- sb.append("resource id | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + RESOURCE_ID_OFF);
- sb.append(String.format("%1$2x", RequestArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", RequestArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("lock mode | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + LOCK_MODE_OFF);
- sb.append(String.format("%1$2x", RequestArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", RequestArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("job id | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + JOB_ID_OFF);
- sb.append(String.format("%1$2x", RequestArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", RequestArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("prev job request | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + PREV_JOB_REQUEST_OFF);
- sb.append(String.format("%1$2x", RequestArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", RequestArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("next job request | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + NEXT_JOB_REQUEST_OFF);
- sb.append(String.format("%1$2x", RequestArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", RequestArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("next request | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + NEXT_REQUEST_OFF);
- sb.append(String.format("%1$2x", RequestArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", RequestArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
-
- return sb;
- }
-
- public String toString() {
- StringBuffer sb = new StringBuffer();
- append(sb);
- return sb.toString();
- }
- }
-
-}
-
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ResourceArenaManager.java b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ResourceArenaManager.java
deleted file mode 100644
index f38b6ce..0000000
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ResourceArenaManager.java
+++ /dev/null
@@ -1,208 +0,0 @@
-package edu.uci.ics.asterix.transaction.management.service.locking;
-
-import java.util.ArrayList;
-
-public class ResourceArenaManager {
-
- private final int noArenas;
- private ArrayList<ResourceMemoryManager> arenas;
- private volatile int nextArena;
- private ThreadLocal<LocalManager> local;
-
- public ResourceArenaManager() {
- noArenas = Runtime.getRuntime().availableProcessors() * 2;
- arenas = new ArrayList<ResourceMemoryManager>(noArenas);
- nextArena = 0;
- local = new ThreadLocal<LocalManager>() {
- @Override
- protected LocalManager initialValue() {
- return getNext();
- }
- };
- }
-
- public static int arenaId(int i) {
- return (i >> 24) & 0xff;
- }
-
- public static int localId(int i) {
- return i & 0xffffff;
- }
-
- public int allocate() {
- final LocalManager localManager = local.get();
- int result = localManager.arenaId << 24;
- result |= localManager.mgr.allocate();
- return result;
- }
-
- public void deallocate(int slotNum) {
- final int arenaId = arenaId(slotNum);
- get(arenaId).deallocate(localId(slotNum));
- }
-
- public synchronized LocalManager getNext() {
- if (nextArena >= arenas.size()) {
- arenas.add(new ResourceMemoryManager());
- }
- ResourceMemoryManager mgr = arenas.get(nextArena);
- LocalManager res = new LocalManager();
- res.mgr = mgr;
- res.arenaId = nextArena;
- nextArena = (nextArena + 1) % noArenas;
- return res;
- }
-
- public ResourceMemoryManager get(int i) {
- return arenas.get(i);
- }
-
- public ResourceMemoryManager local() {
- return local.get().mgr;
- }
-
- public int getLastHolder(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getLastHolder(localId(slotNum));
- }
-
- public void setLastHolder(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setLastHolder(localId(slotNum), value);
- }
-
- public int getFirstWaiter(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getFirstWaiter(localId(slotNum));
- }
-
- public void setFirstWaiter(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setFirstWaiter(localId(slotNum), value);
- }
-
- public int getFirstUpgrader(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getFirstUpgrader(localId(slotNum));
- }
-
- public void setFirstUpgrader(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setFirstUpgrader(localId(slotNum), value);
- }
-
- public int getMaxMode(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getMaxMode(localId(slotNum));
- }
-
- public void setMaxMode(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setMaxMode(localId(slotNum), value);
- }
-
- public int getDatasetId(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getDatasetId(localId(slotNum));
- }
-
- public void setDatasetId(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setDatasetId(localId(slotNum), value);
- }
-
- public int getPkHashVal(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getPkHashVal(localId(slotNum));
- }
-
- public void setPkHashVal(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setPkHashVal(localId(slotNum), value);
- }
-
- public int getNext(int slotNum) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- return local.get().mgr.getNext(localId(slotNum));
- }
-
- public void setNext(int slotNum, int value) {
- final int arenaId = arenaId(slotNum);
- if (arenaId != local.get().arenaId) {
- local.get().arenaId = arenaId;
- local.get().mgr = get(arenaId);
- }
- local.get().mgr.setNext(localId(slotNum), value);
- }
-
-
- static class LocalManager {
- int arenaId;
- ResourceMemoryManager mgr;
- }
-
- StringBuffer append(StringBuffer sb) {
- for (int i = 0; i < arenas.size(); ++i) {
- sb.append("++++ arena ").append(i).append(" ++++\n");
- arenas.get(i).append(sb);
- }
- return sb;
- }
-
- public String toString() {
- return append(new StringBuffer()).toString();
- }
-}
-
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ResourceMemoryManager.java b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ResourceMemoryManager.java
deleted file mode 100644
index aeeb395..0000000
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/locking/ResourceMemoryManager.java
+++ /dev/null
@@ -1,385 +0,0 @@
-package edu.uci.ics.asterix.transaction.management.service.locking;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-
-import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionManagementConstants.LockManagerConstants.LockMode;
-
-public class ResourceMemoryManager {
-
- public static final int SHRINK_TIMER_THRESHOLD = 120000; //2min
-
- static final int NO_SLOTS = 10;
- static final int NEXT_FREE_SLOT_OFFSET = 0;
-
- public static int ITEM_SIZE = 28;
- public static int LAST_HOLDER_OFF = 0;
- public static int FIRST_WAITER_OFF = 4;
- public static int FIRST_UPGRADER_OFF = 8;
- public static int MAX_MODE_OFF = 12;
- public static int DATASET_ID_OFF = 16;
- public static int PK_HASH_VAL_OFF = 20;
- public static int NEXT_OFF = 24;
-
-
- private ArrayList<Buffer> buffers;
- private int current;
- private int occupiedSlots;
- private long shrinkTimer;
- private boolean isShrinkTimerOn;
-
- public ResourceMemoryManager() {
- buffers = new ArrayList<Buffer>();
- buffers.add(new Buffer());
- current = 0;
- }
-
- public int allocate() {
- if (buffers.get(current).isFull()) {
- int size = buffers.size();
- boolean needNewBuffer = true;
- for (int i = 0; i < size; i++) {
- Buffer buffer = buffers.get(i);
- if (! buffer.isInitialized()) {
- buffer.initialize();
- current = i;
- needNewBuffer = false;
- break;
- }
- }
-
- if (needNewBuffer) {
- buffers.add(new Buffer());
- current = buffers.size() - 1;
- }
- }
- ++occupiedSlots;
- return buffers.get(current).allocate() + current * NO_SLOTS;
- }
-
- void deallocate(int slotNum) {
- buffers.get(slotNum / NO_SLOTS).deallocate(slotNum % NO_SLOTS);
- --occupiedSlots;
-
- if (needShrink()) {
- shrink();
- }
- }
-
- /**
- * Shrink policy:
- * Shrink when the resource under-utilization lasts for a certain amount of time.
- * TODO Need to figure out which of the policies is better
- * case1.
- * buffers status : O x x x x x O (O is initialized, x is deinitialized)
- * In the above status, 'CURRENT' needShrink() returns 'TRUE'
- * even if there is nothing to shrink or deallocate.
- * It doesn't distinguish the deinitialized children from initialized children
- * by calculating totalNumOfSlots = buffers.size() * ChildEntityLockInfoArrayManager.NUM_OF_SLOTS.
- * In other words, it doesn't subtract the deinitialized children's slots.
- * case2.
- * buffers status : O O x x x x x
- * However, in the above case, if we subtract the deinitialized children's slots,
- * needShrink() will return false even if we shrink the buffers at this case.
- *
- * @return
- */
- private boolean needShrink() {
- int size = buffers.size();
- int usedSlots = occupiedSlots;
- if (usedSlots == 0) {
- usedSlots = 1;
- }
-
- if (size > 1 && size * NO_SLOTS / usedSlots >= 3) {
- if (isShrinkTimerOn) {
- if (System.currentTimeMillis() - shrinkTimer >= SHRINK_TIMER_THRESHOLD) {
- isShrinkTimerOn = false;
- return true;
- }
- } else {
- //turn on timer
- isShrinkTimerOn = true;
- shrinkTimer = System.currentTimeMillis();
- }
- } else {
- //turn off timer
- isShrinkTimerOn = false;
- }
-
- return false;
- }
-
- /**
- * Shrink() may
- * deinitialize(:deallocates ByteBuffer of child) Children(s) or
- * shrink buffers according to the deinitialized children's contiguity status.
- * It doesn't deinitialze or shrink more than half of children at a time.
- */
- private void shrink() {
- int i;
- int removeCount = 0;
- int size = buffers.size();
- int maxDecreaseCount = size / 2;
- Buffer buffer;
-
- //The first buffer never be deinitialized.
- for (i = 1; i < size; i++) {
- if (buffers.get(i).isEmpty()) {
- buffers.get(i).deinitialize();
- }
- }
-
- //remove the empty buffers from the end
- for (i = size - 1; i >= 1; i--) {
- buffer = buffers.get(i);
- if (! buffer.isInitialized()) {
- buffers.remove(i);
- if (++removeCount == maxDecreaseCount) {
- break;
- }
- } else {
- break;
- }
- }
-
- //reset allocChild to the first buffer
- current = 0;
-
- isShrinkTimerOn = false;
- }
-
- public int getLastHolder(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + LAST_HOLDER_OFF);
- }
-
- public void setLastHolder(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + LAST_HOLDER_OFF, value);
- }
-
- public int getFirstWaiter(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + FIRST_WAITER_OFF);
- }
-
- public void setFirstWaiter(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + FIRST_WAITER_OFF, value);
- }
-
- public int getFirstUpgrader(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + FIRST_UPGRADER_OFF);
- }
-
- public void setFirstUpgrader(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + FIRST_UPGRADER_OFF, value);
- }
-
- public int getMaxMode(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + MAX_MODE_OFF);
- }
-
- public void setMaxMode(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + MAX_MODE_OFF, value);
- }
-
- public int getDatasetId(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + DATASET_ID_OFF);
- }
-
- public void setDatasetId(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + DATASET_ID_OFF, value);
- }
-
- public int getPkHashVal(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + PK_HASH_VAL_OFF);
- }
-
- public void setPkHashVal(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + PK_HASH_VAL_OFF, value);
- }
-
- public int getNext(int slotNum) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- return b.getInt((slotNum % NO_SLOTS) * ITEM_SIZE + NEXT_OFF);
- }
-
- public void setNext(int slotNum, int value) {
- final ByteBuffer b = buffers.get(slotNum / NO_SLOTS).bb;
- b.putInt((slotNum % NO_SLOTS) * ITEM_SIZE + NEXT_OFF, value);
- }
-
-
- StringBuffer append(StringBuffer sb) {
- sb.append("+++ current: ")
- .append(current)
- .append(" no occupied slots: ")
- .append(occupiedSlots)
- .append(" +++\n");
- for (int i = 0; i < buffers.size(); ++i) {
- buffers.get(i).append(sb);
- sb.append("\n");
- }
- return sb;
- }
-
- public String toString() {
- StringBuffer sb = new StringBuffer();
- append(sb);
- return sb.toString();
- }
-
- static class Buffer {
- private ByteBuffer bb;
- private int freeSlotNum;
- private int occupiedSlots = -1; //-1 represents 'deinitialized' state.
-
- Buffer() {
- initialize();
- }
-
- void initialize() {
- bb = ByteBuffer.allocate(NO_SLOTS * ITEM_SIZE);
- freeSlotNum = 0;
- occupiedSlots = 0;
-
- for (int i = 0; i < NO_SLOTS - 1; i++) {
- setNextFreeSlot(i, i + 1);
- }
- setNextFreeSlot(NO_SLOTS - 1, -1); //-1 represents EOL(end of link)
- }
-
- public void deinitialize() {
- bb = null;
- occupiedSlots = -1;
- }
-
- public boolean isInitialized() {
- return occupiedSlots >= 0;
- }
-
- public boolean isFull() {
- return occupiedSlots == NO_SLOTS;
- }
-
- public boolean isEmpty() {
- return occupiedSlots == 0;
- }
-
- public int allocate() {
- int currentSlot = freeSlotNum;
- freeSlotNum = getNextFreeSlot(currentSlot);
- bb.putInt(currentSlot * ITEM_SIZE + LAST_HOLDER_OFF, -1);
- bb.putInt(currentSlot * ITEM_SIZE + FIRST_WAITER_OFF, -1);
- bb.putInt(currentSlot * ITEM_SIZE + FIRST_UPGRADER_OFF, -1);
- bb.putInt(currentSlot * ITEM_SIZE + MAX_MODE_OFF, LockMode.NL);
- occupiedSlots++;
- return currentSlot;
- }
-
- public void deallocate(int slotNum) {
- setNextFreeSlot(slotNum, freeSlotNum);
- freeSlotNum = slotNum;
- occupiedSlots--;
- }
-
- public int getNextFreeSlot(int slotNum) {
- return bb.getInt(slotNum * ITEM_SIZE + NEXT_FREE_SLOT_OFFSET);
- }
-
- public void setNextFreeSlot(int slotNum, int nextFreeSlot) {
- bb.putInt(slotNum * ITEM_SIZE + NEXT_FREE_SLOT_OFFSET, nextFreeSlot);
- }
-
- StringBuffer append(StringBuffer sb) {
- sb.append("++ free slot: ")
- .append(freeSlotNum)
- .append(" no occupied slots: ")
- .append(occupiedSlots)
- .append(" ++\n");
- sb.append("last holder | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + LAST_HOLDER_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("first waiter | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + FIRST_WAITER_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("first upgrader | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + FIRST_UPGRADER_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("max mode | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + MAX_MODE_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("dataset id | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + DATASET_ID_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("pk hash val | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + PK_HASH_VAL_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
- sb.append("next | ");
- for (int i = 0; i < occupiedSlots; ++i) {
- int value = bb.getInt(i * ITEM_SIZE + NEXT_OFF);
- sb.append(String.format("%1$2x", ResourceArenaManager.arenaId(value)));
- sb.append(" ");
- sb.append(String.format("%1$6x", ResourceArenaManager.localId(value)));
- sb.append(" | ");
- }
- sb.append("\n");
-
- return sb;
- }
-
- public String toString() {
- StringBuffer sb = new StringBuffer();
- append(sb);
- return sb.toString();
- }
- }
-
-}
-