Performance of Three, Six, Nine and Twelve Sector sites in

Download Report

Transcript Performance of Three, Six, Nine and Twelve Sector sites in

Some considerations when
doing real time multi threaded
applications on Android
April 15, 2011
Martin Ohlsson
School of Electrical Engineering
1
Real time multi threaded
applications on Android
• Will not discuss everything to
consider
• Background on RTS/OS/Java
• Android
• Specific situations in Java/Android
2
Real time systems/hardware
drivers
• Computer architecture
• Low level
• Interrupt driven
• Shared memory/DMA
• Up to programmer
3
Operating systems
• Low level hardware drivers
• High level libraries
• Resource management (memory,
disk, screen)
• Processes/threads, preemptive
multi tasking
4
Java
• Higher level/abstractions
• Automatic memory management,
garbage collection
• Simple to use threading and
concurrency
• Easy to use/easy to fall into pits
5
Android
• Linux kernel
• Dalvik virtual machine
• Android java libraries
• Dalvik
• Native
6
Specific situations
• Preallocated vs dynamic memory
• Simultaneous object access from
threads
• Synchronous/Asynchronous
execution
7
Preallocated vs dynamic
memory
List<short[]> sampleList = new ArrayList<short[]>();
void sound_in(long time, short[] samples, int length) {
short keepSamples = new short[length];
for(int i=0;i<length;i++) keepSamples[i] = samples[i];
sampleList.add(keep_samples);
}
8
Preallocated vs dynamic
memory
short circularSampleBuffer = new short[22050*delay+extra];
int circularPointer = 0;
void sound_in(long time, short[] samples, int length) {
System.arraycopy(samples, 0,
circularSampleBuffer, circularPointer,
length);
circularPointer += length;
}
9
Simultaneous object access
from threads
List<short[]> sampleList = new ArrayList<short[]>();
void sound_in(long time, short[] samples, int length) {
…
sampleList.add(keepSamples);
}
void process() {
…
sampleList.removeFirst();
}
10
Simultaneous object access
from threads/critical sections
synchronized(sampleList) {
…
sampleList.add(keepSamples);
}
synchronized(sampleList) {
…
sampleList. removeFirst();
}
11
Simultaneous object access
from threads/read write locks
ReadWriteLock dataLock = new ReentrantReadWriteLock();
dataLock.writeLock().lock();
…
sampleList.add(keepSamples);
dataLock.writeLock().unlock();
…
dataLock.readLock().lock();
currentBuffer = sampleList.get(0);
dataLock.readLock().unlock();
…
12
Simultaneous object access
from threads/atomic
operation
int buffersRecorded = 0;
int lastPositionPlayed = 0;
void process() {
int localBuffersRecorded = buffersRecorded; // Atomic
int buffersToplay = localBuffersRecorded lastPositionPlayed;
lastPositionPlayed = localBuffersRecorded;
…
}
void sound_in(long time, short[] samples, int length) {
…
buffersRecorded++;
}
13
Synchronous/Asynchronous
execution
Boolean newData = false;
while(true) {
if(newData)
processData(data);
}
14
Synchronous/Asynchronous
execution
int numberOfReceivedData = 0;
int numberOfProcessedData = 0;
void process() {
int localNumberOfReceivedData = numberOfReceivedData;
int numberOfDataToProcess = localNumberOfReceivedData –
numberOfProcessedData;
numberOfProcessedData = localNumberOfReceivedData;
if(numberOfDataToProcess == 0)
return;
processData(data,numberOfDataToProcess);
}
15
Questions?
16