Top Banner
PHP as a Middleware in Embedded Systems @phpconftw2012
91

(phpconftw2012) PHP as a Middleware in Embedded Systems

Jun 23, 2015

Download

Technology

sosorry

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: (phpconftw2012) PHP as a Middleware in Embedded Systems

PHP as a Middleware in Embedded Systems

@phpconftw2012

Page 2: (phpconftw2012) PHP as a Middleware in Embedded Systems

Hi, I'm sosorry

Page 3: (phpconftw2012) PHP as a Middleware in Embedded Systems

不可能的電視

系統廠工程師

Page 4: (phpconftw2012) PHP as a Middleware in Embedded Systems

本次分享

僅代表個人意見

不代表公司立場

Page 5: (phpconftw2012) PHP as a Middleware in Embedded Systems

故事開始

Page 6: (phpconftw2012) PHP as a Middleware in Embedded Systems

2012 年 , 連網電視正夯

Page 7: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 8: (phpconftw2012) PHP as a Middleware in Embedded Systems

該如何開始?

Page 9: (phpconftw2012) PHP as a Middleware in Embedded Systems

先來測個 API 吧

Page 10: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 11: (phpconftw2012) PHP as a Middleware in Embedded Systems

杯具發生了 ...

Page 12: (phpconftw2012) PHP as a Middleware in Embedded Systems

板子上沒有 JVM

Page 13: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 14: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Embedded System

Page 15: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 16: (phpconftw2012) PHP as a Middleware in Embedded Systems

Back to the basics

Page 17: (phpconftw2012) PHP as a Middleware in Embedded Systems

Q1:

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

Page 18: (phpconftw2012) PHP as a Middleware in Embedded Systems

Display System

Framebuffer

Hardware

Application

GUI-ToolKit

Page 19: (phpconftw2012) PHP as a Middleware in Embedded Systems

In Embedded System

/dev/fb driver

SoC

Page 20: (phpconftw2012) PHP as a Middleware in Embedded Systems

application 受限於 GUI-Toolkit

Page 21: (phpconftw2012) PHP as a Middleware in Embedded Systems

Q2:

是否可以讓 UI 只做 UI ?

Page 22: (phpconftw2012) PHP as a Middleware in Embedded Systems

Software Glue

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

Page 23: (phpconftw2012) PHP as a Middleware in Embedded Systems

Middleware

Turnkey/Driver

Hardware

Application

Page 24: (phpconftw2012) PHP as a Middleware in Embedded Systems

Middleware

Turnkey/Driver

Hardware

Application

Page 25: (phpconftw2012) PHP as a Middleware in Embedded Systems

Middleware

Turnkey/Driver

Hardware

Application

interface

interface

Page 26: (phpconftw2012) PHP as a Middleware in Embedded Systems

Middleware

Turnkey/Driver

Hardware

Application

Portable Layerinterface

interface

Page 27: (phpconftw2012) PHP as a Middleware in Embedded Systems

Q3:

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

Page 28: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 29: (phpconftw2012) PHP as a Middleware in Embedded Systems

PHP as Software Glue in Embedded Systems

Page 30: (phpconftw2012) PHP as a Middleware in Embedded Systems

Why PHP

Page 31: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 32: (phpconftw2012) PHP as a Middleware in Embedded Systems

● 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

Page 33: (phpconftw2012) PHP as a Middleware in Embedded Systems

PHP as a Middleware

Turnkey/Driver

Hardware

Application

Page 34: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Tasks

Page 35: (phpconftw2012) PHP as a Middleware in Embedded Systems

Architecture Design

Page 36: (phpconftw2012) PHP as a Middleware in Embedded Systems

Web Server + PHP ?

Page 37: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 38: (phpconftw2012) PHP as a Middleware in Embedded Systems

杯具又發生了 ...

Page 39: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 40: (phpconftw2012) PHP as a Middleware in Embedded Systems

砍掉重練

Page 41: (phpconftw2012) PHP as a Middleware in Embedded Systems

PHP Inter-Process Communication

Page 42: (phpconftw2012) PHP as a Middleware in Embedded Systems

PHP

Native Code

data exchangeinvoke

Page 43: (phpconftw2012) PHP as a Middleware in Embedded Systems

● Shared library

PHP

Native Code

data exchangeinvoke

Page 44: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

PHP

Native Code

data exchangeinvoke

Page 45: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

PHP

Native Code

data exchangeinvoke

Page 46: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

PHP

Native Code

data exchangeinvoke

Page 47: (phpconftw2012) PHP as a Middleware in Embedded Systems

Socket Server

Page 48: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Design

Page 49: (phpconftw2012) PHP as a Middleware in Embedded Systems

PHP Work Flow

Page 50: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

Page 51: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

storagesocket server

Page 52: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

storagesocket server

Page 53: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

storagesocket server

Page 54: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interfaceYou Tube

storagesocket server

Page 55: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

Pandora

You Tube

storagesocket server

Page 56: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

Pandora

Twitter

You Tube

storagesocket server

Page 57: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

Pandora

Twitter

You Tube

storagesocket server

Page 58: (phpconftw2012) PHP as a Middleware in Embedded Systems

Turnkey/Driver

Hardware

application interface

Pandora

Twitter

You Tube

storagesocket server

Page 59: (phpconftw2012) PHP as a Middleware in Embedded Systems

Porting Libraries

Page 60: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Page 61: (phpconftw2012) PHP as a Middleware in Embedded Systems

Development & Debug

Page 62: (phpconftw2012) PHP as a Middleware in Embedded Systems

● 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

Page 63: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Console Debug

Page 64: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

IDE Debug

Page 65: (phpconftw2012) PHP as a Middleware in Embedded Systems
Page 66: (phpconftw2012) PHP as a Middleware in Embedded Systems

Performance Tunning

Page 67: (phpconftw2012) PHP as a Middleware in Embedded Systems

● Xdebug ● Kcachegrind● VLD (Vulcan Logic Disassembler)

Profiling

Page 68: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Xdebug

Page 69: (phpconftw2012) PHP as a Middleware in Embedded Systems
Page 70: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

tracefile-analyser

Page 71: (phpconftw2012) PHP as a Middleware in Embedded Systems
Page 72: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Kcachegrind

Page 73: (phpconftw2012) PHP as a Middleware in Embedded Systems
Page 74: (phpconftw2012) PHP as a Middleware in Embedded Systems

● An opcode dumper

VLD

Page 75: (phpconftw2012) PHP as a Middleware in Embedded Systems

A simple comparison

Page 76: (phpconftw2012) PHP as a Middleware in Embedded Systems

array_push

Page 77: (phpconftw2012) PHP as a Middleware in Embedded Systems

array assignment

Page 78: (phpconftw2012) PHP as a Middleware in Embedded Systems

# of Opcodes = 16

Page 79: (phpconftw2012) PHP as a Middleware in Embedded Systems

# of Opcodes = 10

Page 80: (phpconftw2012) PHP as a Middleware in Embedded Systems

Tunning

Page 81: (phpconftw2012) PHP as a Middleware in Embedded Systems

Alternative PHP Cache (APC)

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

Page 82: (phpconftw2012) PHP as a Middleware in Embedded Systems

DB is always the bottleneck

Page 83: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

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

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

常見招數

Page 84: (phpconftw2012) PHP as a Middleware in Embedded Systems

● 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

Page 85: (phpconftw2012) PHP as a Middleware in Embedded Systems

● --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

Page 86: (phpconftw2012) PHP as a Middleware in Embedded Systems

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

Extension for Debugging

Page 87: (phpconftw2012) PHP as a Middleware in Embedded Systems

Integration

Page 88: (phpconftw2012) PHP as a Middleware in Embedded Systems

這又是另外一個故事了

Page 89: (phpconftw2012) PHP as a Middleware in Embedded Systems

Thanks

Page 90: (phpconftw2012) PHP as a Middleware in Embedded Systems

● Jeffrey Chen● Simon Wang

Special Thanks

Page 91: (phpconftw2012) PHP as a Middleware in Embedded Systems

Q & A