Unveiling Design Patterns: A Visual Guide with UML Diagrams
1032 cs208 g operation system ip camera case share.v0.2
1. Chung Yuan Christian University
Department of Information and Computer Engineering
(1032 CS208G Operation System)
Teacher: Bin Shyan Jong
Student: Wen Chih Ho
Student ID: 9877606
2. Usage Scenarios
IP Camera Software Stack
Linux Kernel
Thread in User Space Application
Web Server
IP Cam Main Program
Linux Thread Experiment
Android Mobile APP
I/O- versus CPU-Bound
Linux Boot Time
Video Decode Performance
7. Trace system call and signals
Allocate new space to load the information of programmer
sets the end of the data segment
File containing an ordered list of libraries found in the directories
specified in /etc/ld.so.conf. This file is not in human readable format, and
is not intended to be edited. This file is created by ldconfig command.
Shared libraries stores in /lib, /usr/lib, /usr/lib64, /lib64,
/usr/local/lib directories.
The arch_prctl() function sets architecture-specific process or thread state.
ARCH_SET_FS
Set the 64-bit base for the FS register to addr.
the 386 architecture introduced two new general segment registers FS,GS.
8.
9. #include <stdio.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/reg.h>
int main(){
puts("Parent started");
pid_t pid;
pid=fork();
if (pid<0){
puts("fork() failed");
return(-1);
}
if (pid==0){
ptrace(PTRACE_TRACEME,0,0,0);
puts("Child sleeping...");
sleep(1);
puts("Child exec...");
execlp("./hello","hello",NULL);
}else{
printf("Child PiD == %dn",pid);
int sta=0;
struct rusage ru;
wait4(pid,&sta,0,&ru);
long rax_rt=ptrace(PTRACE_PEEKUSER,pid,8*RAX,0);
printf("Child execve() returned with %ldn",rax_rt);
ptrace(PTRACE_SYSCALL,pid,0,0);
int intocall=1;
while(1){
wait4(pid,&sta,0,&ru);
if (WIFEXITED(sta)){
puts("Child Exited");
break;
}
long
_ORIG_RAX=ptrace(PTRACE_PEEKUSER,pid,8*ORIG_RAX,0);
long _RAX=ptrace(PTRACE_PEEKUSER,pid,8*RAX,0);
if (intocall){
printf("Entering SYSCALL %ld .... ",_ORIG_RAX);
intocall=0;
}else{
printf("Exited with %ldn",_RAX);
intocall=1;
}
ptrace(PTRACE_SYSCALL,pid,0,0);
}
}
}
Process can be traced
Ref: Programming with PTRACE, Part2
15. Web
Server
Web
Server
daemon
Open Sockets for server
Accept connection
pthread_create for client
fork()
Client thread provide function
GET /?action=snapshot
GET /?action=stream
GET /input
GET /output
GET /?action=command
GET /command.cgi?action=…
File Download
16. IP Camera Program
Grab Video Frame
Send Video to client
Send Sensor
information to client
Save snapshot or
MP4 File
Main
Program
Grab Camera
Frame Thread
Client Connection
Thread
Save MP4 File
Thread
input
output
output
Sensor Data
input
17.
18. Ref: Small example of pthreads
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* A task that takes some time to complete. The id identifies distinct
tasks for printed messages. */
void *task(int id) {
printf("Task%d startedn", id);
int i;
double result = 0.0;
for (i = 0; i < 100000000; i++) {
result = result + sin(i) * tan(i);
}
printf("Task%d completed with result %en", id, result);
}
/* Same as 'task', but meant to be called from different threads. */
void *threaded_task(void *t) {
long id = (long) t;
printf("Thread %ld startedn", id);
task(id);
printf("Thread %ld donen", id);
pthread_exit(0);
}
/* Run 'task' num_tasks times serially. */
void *serial(int num_tasks) {
int i;
for (i = 0; i < num_tasks; i++) {
task(i);
}
}
/* Run 'task' num_tasks times, creating a separate thread for each call to 'task'. */
void *parallel(int num_tasks){
int num_threads = num_tasks;
pthread_t thread[num_threads];
int rc;
long t;
for (t = 0; t < num_threads; t++) {
printf("Creating thread %ldn", t);
rc = pthread_create(&thread[t], NULL,threaded_task, (void *)t);
if (rc) {
printf("ERROR: return code from pthread_create() is %dn", rc);
exit(-1);
}
}
}
void *print_usage(int argc, char *argv[]) {
printf("Usage: %s serial|parallel num_tasksn", argv[0]);
exit(1);
}
int main(int argc, char *argv[]) {
if (argc != 3) {print_usage(argc, argv);}
int num_tasks = atoi(argv[2]);
if (!strcmp(argv[1], "serial")) {
serial(num_tasks);
} else if (!strcmp(argv[1], "parallel")) {
parallel(num_tasks);
}
else {
print_usage(argc, argv);
}
printf("Main completedn");
pthread_exit(NULL);
}
22. Android Mobile App
Connect to IP Camera
Receive Video/Audio
Data
Play Video/Audio Data
Main
Program
Network
video/audio/
command transfer
OpenGL Video
Render Thread
Video Decode
Thread
Input/
output
output
output
UI Thread
input
23. All application threads are based on the native
pthreads in Linux with a Thread representation in
Java, but the platform still assigns special properties
to threads that make them differ. From an application
perspective, the thread types are UI, binder, and back
ground threads.
UI Thread
The UI thread is started on application start and stays alive during the lifetime of the Linux process. The UI thread
is the main thread of the application, used for executing Android components and updating the UI elements on the
screen.
Binder Thread
Binder threads are used for communicating between threads in different processes.Each process maintains a set
of threads, called a thread pool, that is never terminated or recreated, but can run tasks at the request of another
thread in the process.
Background Threads
All the threads that an application explicitly creates are background threads. This means that they have no
predefined purpose, but are empty execution environments waiting to execute any task.
Ref Book: OReilly Efficient Android Threading
24. USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 480 348 ffffffff 00000000 S /init
root 174 1 649496 38368 ffffffff 00000000 S zygote
u0_a77 12895 174 682124 51656 ffffffff 00000000 S com.example.mythread
Garbage collection
com.example.mythead
Java debug wire protocol
Android Just-In-Time
Android IPC Binder
25. Example
OS Layer
System Boot Time
Application Layer
Video Decode Performance
34. So, we are using ffmpeg software decode
library in our IP Camera App.
But that will cause two big issue otherwise the video will lag
(1)We only can decode one VGA video stream
(VGA=640x480)
(2)We can not decode HD or higher video stream
(HD=1280x720)
But, now everybody are using HD video and even has the 4K TV.
What we can do?
35. Receive H264
video raw data
from network
ffmpeg library
decode and output
RGB pixel data
OpenGL draw RGB
pixel data
Why we can see the video play is smoothly?
FPS : frame per second( 30 fps or 25 fps)
If 30 fps that is means you have finished all process in 33ms for each frame
If 25 fps that is means you have finished all process in 40ms for each frame
avcode_decode_video2()
sws_getContext()
sws_scale()
We need to profiling those function to find out what happen on our problem.
37. 1) ARM/Thumb instruction set
2) Concurrent process video data
3) Ask GPU for help (CPU vs GPU)
4) Use video hardware decode component
5) …
Requirement:
Android/Apple mobile and tablet must all support
38. One Frame Decode YUV to RGB OpenGL Draw Total
XIAOMI 18~33ms 33ms 4.2~4.8ms 51~70ms
GPlus 27ms 64ms 17ms 108ms
RockChip 29ms 74ms 28ms 131ms
4~2xms 3x~5xms 3x~7xms
H/W Decode GPU Shader support
So this is a CPU bound issue.