(phpconftw2012) PHP as a Middleware in Embedded Systems

Post on 23-Jun-2015

1760 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

It is used by sosorry at PHPConf Taiwan 2012. In this presentation, we will see that why we need a middleware in embedded systems, and how PHP can play this role. Besides, some tasks about architecture design, porting libraries, development & debug, and performance tunning would be included.

Transcript

PHP as a Middleware in Embedded Systems

@phpconftw2012

Hi, I'm sosorry

不可能的電視

系統廠工程師

本次分享

僅代表個人意見

不代表公司立場

故事開始

2012 年 , 連網電視正夯

老闆想在電視上新增一個網路 App

該如何開始?

先來測個 API 吧

● Request the developer account to 3rd party● Get the technical document● Build the test environment● Download sample code● Test all APIs with JAVA● … OK

杯具發生了 ...

板子上沒有 JVM

歡迎來到嵌入式系統的世界

An embedded system is a computer system designed for specific control functions within a larger system, often with real-time computing constraints.

Embedded System

● No APT, no YUM● Hardware limitation● ! Scale● Deploy/update not easy

Back to the basics

Q1:

app 在嵌入式系統是如何顯示的?

Display System

Framebuffer

Hardware

Application

GUI-ToolKit

In Embedded System

/dev/fb driver

SoC

application 受限於 GUI-Toolkit

Q2:

是否可以讓 UI 只做 UI ?

Software Glue

source: http://www.furniturefromturkey.com/

Middleware

Turnkey/Driver

Hardware

Application

Middleware

Turnkey/Driver

Hardware

Application

Middleware

Turnkey/Driver

Hardware

Application

interface

interface

Middleware

Turnkey/Driver

Hardware

Application

Portable Layerinterface

interface

Q3:

要能實做 3rd Party App 的基本需求?

● RESTFul API● JSON/XML data format● Symmetric/asymmetric encryption● SSL communication● Data storage● Multi-byte encoding

PHP as Software Glue in Embedded Systems

Why PHP

● Tiny, low resource usage● Community contribution (PEAR/PECL)● 3rd party API (Youtube/Twitter/Facebook...)● Integration with lots of libraries

● RESTFul API /cURL● JSON/XML data format /LibeXpat, Libxml2● Symmetric/asymmetric encryption /Libmcrypt● SSL communication /OpenSSL● Data storage /SQLite3● Multi-byte encoding /Libiconv● Inter-Process Communication /Socket, SHM● Core /PHP-CLI

PHP as a Middleware

Turnkey/Driver

Hardware

Application

● Architecture design● Porting libraries● Development & debug● Performance tunning

Tasks

Architecture Design

Web Server + PHP ?

● Apache MPM prefork + mod_php● Nginx + FastCGI● Lighttpd + PHP-FPM● Boa + PHP-CGI

杯具又發生了 ...

● 資源吃很兇● 不容易移植● 記憶體洩漏● 常常連不上

砍掉重練

PHP Inter-Process Communication

PHP

Native Code

data exchangeinvoke

● Shared library

PHP

Native Code

data exchangeinvoke

● Shared library● Embedded build (--enable-embed)

PHP

Native Code

data exchangeinvoke

● Shared library● Embedded build (--enable-embed) ● Shell command

PHP

Native Code

data exchangeinvoke

● Shared library● Embedded build (--enable-embed) ● Shell command● Socket

PHP

Native Code

data exchangeinvoke

Socket Server

● Concurrent server (No multi-thread in PHP)● IPC mechanism (broadcast/multicast/unicast)● Client/Server command protocol● No framework, all things handmade

Design

PHP Work Flow

Turnkey/Driver

Hardware

Turnkey/Driver

Hardware

application interface

storagesocket server

Turnkey/Driver

Hardware

application interface

storagesocket server

Turnkey/Driver

Hardware

application interface

storagesocket server

Turnkey/Driver

Hardware

application interfaceYou Tube

storagesocket server

Turnkey/Driver

Hardware

application interface

Pandora

You Tube

storagesocket server

Turnkey/Driver

Hardware

application interface

Pandora

Twitter

You Tube

storagesocket server

Turnkey/Driver

Hardware

application interface

Pandora

Twitter

You Tube

storagesocket server

Turnkey/Driver

Hardware

application interface

Pandora

Twitter

You Tube

storagesocket server

Porting Libraries

● Prepare toolchain● Configure with minimized features● Resolve dependency & version conflict● Resolve compilation error● Test all functions in the target machine● Reduce library size

Development & Debug

● Ubuntu /OS● Vim & plugin /editor● Shell script /automation tool● Meld /visual diff and merge tool● SVN /version control system● Jira /bug tracking system

Development Environment

● print_r(), var_dump()● Xdebug & xdebug_debug_zval()● log_errors & error_log in php.ini● debugging level in config.ini

Console Debug

● Set breakpoint● Step into/step over/step out● Watch variables● Display call stack

IDE Debug

Performance Tunning

● Xdebug ● Kcachegrind● VLD (Vulcan Logic Disassembler)

Profiling

● Stack/function traces● Code coverage analysis● Time tracking● Memory usage

Xdebug

● Aggregate information from xdebug● Sort by time/memory/calls

tracefile-analyser

● Analyze xdebug profiling data● Call graph viewer(callers/callees)

Kcachegrind

● An opcode dumper

VLD

A simple comparison

array_push

array assignment

# of Opcodes = 16

# of Opcodes = 10

Tunning

Alternative PHP Cache (APC)

source: http://www.slideshare.net/shire/php-tek-2008-apc-facebook

DB is always the bottleneck

● 買好一點的硬體● 優化 SQL 語法

● 關掉同步 commit● 增加寫入 buffer 的大小

● 增加 wait write 的 delay time● … 等等

常見招數

● Configuration● SQLITE_MAX_LENGTH=2147483647● SQLITE_MAX_SQL_LENGTH=1073741824

● lock/unlock● $handle->busyTimeout($time)

● Single statement● Concat SQL string● Begin; SQL; Commit;

SQLite3 Tunning

● --enable-debug --enable-maintainer-zts ● ext_skel

$ vi ext/$extname/config.m4

$ ./buildconf

$ ./configure –[with|enable]-$extname

$ make

$ ./php -f ext/$extname/$extname.php

$ vi ext/$extname/$extname.c

$ make

Write an Extension

● Change debugging level during runtime ● Dump debug information to a specific usb drive● inotify + blkid

Extension for Debugging

Integration

這又是另外一個故事了

Thanks

● Jeffrey Chen● Simon Wang

Special Thanks

Q & A

top related