Skip to content

Add FIFO and Llama Services #1339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# fast build
mvn -DskipTests package -o

# execute
mvn exec:java -Dexec.mainClass=org.myrobotlab.service.Runtime -Dexec.args="-s webgui WebGui intro Intro python Python"

# specific test
mvn test -Dtest="org.myrobotlab.service.WebGuiTest#postTest"

Expand Down Expand Up @@ -614,6 +617,15 @@
</dependency>
<!-- LeapMotion end -->

<!-- Llama begin -->
<dependency>
<groupId>de.kherud</groupId>
<artifactId>llama</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- Llama end -->

<!-- LocalSpeech begin -->
<dependency>
<groupId>org.myrobotlab.audio</groupId>
Expand Down Expand Up @@ -1018,6 +1030,12 @@
<artifactId>jovr</artifactId>
<version>1.8.0.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>slick-util</groupId>
Expand Down Expand Up @@ -1382,6 +1400,11 @@
<version>3.9.0</version>
</dependency>
<!-- Duplicate entry for io.netty-netty-all-4.1.82.Final skipping -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.4.5</version>
</dependency>
<!-- Runtime end -->

<!-- Serial begin -->
Expand Down Expand Up @@ -1655,6 +1678,15 @@
<!-- Duplicate entry for org.myrobotlab.audio-voice-effects-1.0 skipping -->
<!-- WebkitSpeechSynthesis end -->

<!-- Whisper begin -->
<dependency>
<groupId>io.github.givimad</groupId>
<artifactId>whisper-jni</artifactId>
<version>1.4.2-6</version>
<scope>provided</scope>
</dependency>
<!-- Whisper end -->

<!-- Wii begin -->
<!-- Duplicate entry for wiiusej-wiiusej-wiiusej skipping -->
<!-- Wii end -->
Expand Down
41 changes: 38 additions & 3 deletions src/main/java/org/myrobotlab/framework/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.slf4j.Logger;
import oshi.SystemInfo;

/**
* The purpose of this class is to retrieve all the detailed information
Expand Down Expand Up @@ -85,6 +86,10 @@ public class Platform implements Serializable {

String shortCommit;

int numLogicalProcessors;

int numPhysicalProcessors;

static Platform localInstance;

/**
Expand All @@ -108,11 +113,11 @@ public static Platform getLocalInstance() {

// === OS ===
platform.os = System.getProperty("os.name").toLowerCase();
if (platform.os.indexOf("win") >= 0) {
if (platform.os.contains("win")) {
platform.os = OS_WINDOWS;
} else if (platform.os.indexOf("mac") >= 0) {
} else if (platform.os.contains("mac")) {
platform.os = OS_MAC;
} else if (platform.os.indexOf("linux") >= 0) {
} else if (platform.os.contains("linux")) {
platform.os = OS_LINUX;
}

Expand Down Expand Up @@ -248,6 +253,13 @@ public static Platform getLocalInstance() {
} catch (Exception e) {
}

// Logical and physical processor detection

// availableProcessors returns the number of logical cores dedicated to the JVM
platform.numLogicalProcessors = java.lang.Runtime.getRuntime().availableProcessors();

platform.numPhysicalProcessors = new SystemInfo().getHardware().getProcessor().getPhysicalProcessorCount();

localInstance = platform;
}
return localInstance;
Expand Down Expand Up @@ -497,6 +509,29 @@ public Date getStartTime() {
return startTime;
}

/**
* Get the number of logical cores
* available to the VM. May be different
* from the number of logical cores in the
* system if the user only allocates
* some of them to the VM.
* @return The number of available logical cores
*/
public int getNumLogicalProcessors() {
return numLogicalProcessors;
}

/**
* Get the number of physical cores in the system.
* This may be different from the number of cores allocated
* to the JVM, and on x86 will usually be different from
* the number of logical cores in the system.
* @return The number of physical cores in the system.
*/
public int getNumPhysicalProcessors() {
return numPhysicalProcessors;
}

/**
* @return true if running in virtual mode
*
Expand Down
154 changes: 154 additions & 0 deletions src/main/java/org/myrobotlab/service/AutoEjectFIFO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.myrobotlab.service;

import org.myrobotlab.framework.Service;
import org.myrobotlab.service.config.ServiceConfig;

import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* A simple service that acts as a circular FIFO queue.
* This queue can store a number of items, but once
* its max capacity is reached, any attempt to add more
* items ejects the oldest element, i.e. the head.
* <p></p>
* This queue is not typed, i.e. it can store any type
* of object, with the downside that no type checking is
* performed. This is to allow the fifo to be used
* in any situation, since we don't currently have a way
* to create generic services.
*
* @author AutonomicPerfectionist
*/
public class AutoEjectFIFO extends Service<ServiceConfig> {
public static final int DEFAULT_MAX_SIZE = 50;


/**
* Lock used to protect the fifo queue,
* used instead of synchronized block to allow
* multiple simultaneous readers so long as there
* is no writer writing to the queue.
*/
private final ReadWriteLock lock = new ReentrantReadWriteLock();

/**
* The actual queue, whose initial maximum size is set to
* {@link #DEFAULT_MAX_SIZE}.
*/
private BlockingDeque<Object> fifo = new LinkedBlockingDeque<>(DEFAULT_MAX_SIZE);


/**
* Constructor of service, reservedkey typically is a services name and inId
* will be its process id
*
* @param reservedKey the service name
* @param inId process id
*/
public AutoEjectFIFO(String reservedKey, String inId) {
super(reservedKey, inId);
}


/**
* Sets the size at which the FIFO will begin evicting
* elements. If smaller than the current number of items,
* then elements will be silently evicted.
* @param size The new max size
*/
public void setMaxSize(int size) {
lock.writeLock().lock();
BlockingDeque<Object> newFifo = new LinkedBlockingDeque<>(size);
newFifo.addAll(fifo);
fifo = newFifo;
lock.writeLock().unlock();
}

/**
* Add a new element to the FIFO, if
* it's full then this will trigger an
* eviction
* @param item The new item to be added to the tail
*/
public void add(Object item) {
lock.writeLock().lock();
try {
if (!fifo.offer(item)) {
Object head = fifo.removeFirst();
invoke("publishEviction", head);
fifo.add(item);
}
invoke("publishItemAdded", item);
} catch (Exception e) {
error(e);
} finally {
lock.writeLock().unlock();
}


}

public void clear() {
lock.writeLock().lock();
fifo.clear();
lock.writeLock().unlock();
invoke("publishClear");
}

public List<Object> getAll() {
lock.readLock().lock();
List<Object> ret = List.copyOf(fifo);
lock.readLock().unlock();
invoke("publishAll", ret);

return ret;
}

public Object getHead() {
lock.readLock().lock();
Object head = fifo.peek();
lock.readLock().unlock();
invoke("publishHead", head);
return head;

}

public Object getTail() {
lock.readLock().lock();
Object tail = fifo.peekLast();
lock.readLock().unlock();
invoke("publishTail", tail);
return tail;
}

public Object publishItemAdded(Object item) {
return item;
}

public void publishClear() {
// Do nothing
}

public List<Object> publishAll(List<Object> items) {
return items;
}

public Object publishHead(Object head) {
return head;
}

public Object publishTail(Object tail) {
return tail;
}

public Object publishEviction(Object evicted) {
return evicted;
}



}
Loading