(phpconftw2012) PHP as a Middleware in Embedded Systems

2,072 views

Published on

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.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,072
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
30
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

(phpconftw2012) PHP as a Middleware in Embedded Systems

  1. 1. PHP as a Middlewarein Embedded Systems @phpconftw2012
  2. 2. Hi, Im sosorry
  3. 3. 不可能的電視系統廠工程師
  4. 4. 本次分享僅代表個人意見不代表公司立場
  5. 5. 故事開始
  6. 6. 2012 年 , 連網電視正夯
  7. 7. 老闆想在電視上新增一個網路 App
  8. 8. 該如何開始?
  9. 9. 先來測個 API 吧
  10. 10. ● Request the developer account to 3rd party● Get the technical document● Build the test environment● Download sample code● Test all APIs with JAVA● … OK
  11. 11. 杯具發生了 ...
  12. 12. 板子上沒有 JVM
  13. 13. 歡迎來到嵌入式系統的世界
  14. 14. Embedded SystemAn embedded system is a computer systemdesigned for specific control functions within alarger system, often with real-time computingconstraints.
  15. 15. ● No APT, no YUM● Hardware limitation● ! Scale● Deploy/update not easy
  16. 16. Back to the basics
  17. 17. Q1:app 在嵌入式系統是如何顯示的?
  18. 18. Display System Application GUI-ToolKit Framebuffer Hardware
  19. 19. In Embedded System /dev/fb driver SoC
  20. 20. application 受限於 GUI-Toolkit
  21. 21. Q2:是否可以讓 UI 只做 UI ?
  22. 22. Software Gluesource: http://www.furniturefromturkey.com/
  23. 23. Middleware Application Turnkey/Driver Hardware
  24. 24. Middleware Application Turnkey/Driver Hardware
  25. 25. Middleware Application interface interface Turnkey/Driver Hardware
  26. 26. Middleware Application interfacePortable Layer interface Turnkey/Driver Hardware
  27. 27. Q3: rd要能實做 3 Party App 的基本需求?
  28. 28. ● RESTFul API● JSON/XML data format● Symmetric/asymmetric encryption● SSL communication● Data storage● Multi-byte encoding
  29. 29. PHP as Software Gluein Embedded Systems
  30. 30. Why PHP
  31. 31. ● Tiny, low resource usage● Community contribution (PEAR/PECL)● 3rd party API (Youtube/Twitter/Facebook...)● Integration with lots of libraries
  32. 32. ● 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
  33. 33. PHP as a Middleware Application Turnkey/Driver Hardware
  34. 34. Tasks● Architecture design● Porting libraries● Development & debug● Performance tunning
  35. 35. Architecture Design
  36. 36. Web Server + PHP ?
  37. 37. ● Apache MPM prefork + mod_php● Nginx + FastCGI● Lighttpd + PHP-FPM● Boa + PHP-CGI
  38. 38. 杯具又發生了 ...
  39. 39. ● 資源吃很兇● 不容易移植● 記憶體洩漏● 常常連不上
  40. 40. 砍掉重練
  41. 41. PHP Inter-Process Communication
  42. 42. PHPinvoke data exchangeNative Code
  43. 43. ● Shared library PHP invoke data exchange Native Code
  44. 44. ● Shared library● Embedded build (--enable-embed) PHP invoke data exchange Native Code
  45. 45. ● Shared library● Embedded build (--enable-embed)● Shell command PHP invoke data exchange Native Code
  46. 46. ● Shared library● Embedded build (--enable-embed)● Shell command● Socket PHP invoke data exchange Native Code
  47. 47. Socket Server
  48. 48. Design● Concurrent server (No multi-thread in PHP)● IPC mechanism (broadcast/multicast/unicast)● Client/Server command protocol● No framework, all things handmade
  49. 49. PHP Work Flow
  50. 50. Turnkey/Driver Hardware
  51. 51. application interfacesocket server storage Turnkey/Driver Hardware
  52. 52. application interfacesocket server storage Turnkey/Driver Hardware
  53. 53. application interfacesocket server storage Turnkey/Driver Hardware
  54. 54. You Tube application interface socket server storage Turnkey/Driver Hardware
  55. 55. You Tube application interfacePandora socket server storage Turnkey/Driver Hardware
  56. 56. You Tube application interfacePandora socket server storageTwitter Turnkey/Driver Hardware
  57. 57. You Tube application interfacePandora socket server storageTwitter Turnkey/Driver Hardware
  58. 58. You Tube application interfacePandora socket server storageTwitter Turnkey/Driver Hardware
  59. 59. Porting Libraries
  60. 60. ● Prepare toolchain● Configure with minimized features● Resolve dependency & version conflict● Resolve compilation error● Test all functions in the target machine● Reduce library size
  61. 61. Development & Debug
  62. 62. Development Environment● Ubuntu /OS● Vim & plugin /editor● Shell script /automation tool● Meld /visual diff and merge tool● SVN /version control system● Jira /bug tracking system
  63. 63. Console Debug● print_r(), var_dump()● Xdebug & xdebug_debug_zval()● log_errors & error_log in php.ini● debugging level in config.ini
  64. 64. IDE Debug● Set breakpoint● Step into/step over/step out● Watch variables● Display call stack
  65. 65. Performance Tunning
  66. 66. Profiling● Xdebug● Kcachegrind● VLD (Vulcan Logic Disassembler)
  67. 67. Xdebug● Stack/function traces● Code coverage analysis● Time tracking● Memory usage
  68. 68. tracefile-analyser● Aggregate information from xdebug● Sort by time/memory/calls
  69. 69. Kcachegrind● Analyze xdebug profiling data● Call graph viewer(callers/callees)
  70. 70. VLD● An opcode dumper
  71. 71. A simple comparison
  72. 72. array_push
  73. 73. array assignment
  74. 74. # of Opcodes = 16
  75. 75. # of Opcodes = 10
  76. 76. Tunning
  77. 77. Alternative PHP Cache (APC)source: http://www.slideshare.net/shire/php-tek-2008-apc-facebook
  78. 78. DB is always the bottleneck
  79. 79. 常見招數● 買好一點的硬體● 優化 SQL 語法● 關掉同步 commit● 增加寫入 buffer 的大小● 增加 wait write 的 delay time● … 等等
  80. 80. SQLite3 Tunning● Configuration ● SQLITE_MAX_LENGTH=2147483647 ● SQLITE_MAX_SQL_LENGTH=1073741824● lock/unlock ● $handle->busyTimeout($time)● Single statement ● Concat SQL string ● Begin; SQL; Commit;
  81. 81. Write an Extension● --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
  82. 82. Extension for Debugging● Change debugging level during runtime● Dump debug information to a specific usb drive● inotify + blkid
  83. 83. Integration
  84. 84. 這又是另外一個故事了
  85. 85. Thanks
  86. 86. Special Thanks● Jeffrey Chen● Simon Wang
  87. 87. Q &A

×