elec comp 177 fall 2015
play

ELEC / COMP 177 Fall 2015 Some slides from Kurose - PowerPoint PPT Presentation

ELEC / COMP 177 Fall 2015 Some slides from Kurose and Ross, Computer Networking , 5 th Edition Project 2 Python HTTP Server v2 Starts


  1. ELEC ¡/ ¡COMP ¡177 ¡– ¡Fall ¡2015 ¡ Some ¡slides ¡from ¡Kurose ¡and ¡Ross, ¡ Computer ¡Networking , ¡5 th ¡Edition ¡

  2. ¡ Project ¡2 ¡– ¡Python ¡HTTP ¡Server ¡v2 ¡ § Starts ¡today! ¡ ¡ Checkpoint ¡1 ¡-­‑ ¡ Due ¡Oct ¡4 th ¡ ¡ ¡ Checkpoint ¡1 ¡-­‑ ¡ Due ¡Oct ¡11 th ¡ ¡ ¡ Final ¡Project ¡-­‑ ¡ Due ¡Oct ¡18 th ¡ ¡ 2 ¡

  3. ¡ Presentations ¡on ¡Thursday ¡ 1. Craig ¡Goble ¡ -­‑ ¡XMPP ¡(Jabber) ¡ 2. Taylor ¡Yatogo ¡ -­‑ ¡Internet ¡of ¡Things ¡(CoAP) ¡ 3. Tyler ¡Fernandez ¡ -­‑ ¡Secure ¡Shell ¡(SSH) ¡ 4. Kenneth ¡Thompson ¡ -­‑ ¡Building ¡Automation ¡and ¡ Control ¡Networks ¡(BACnet) ¡ 5. Marcus ¡Barnes ¡ -­‑ ¡Real ¡Time ¡Messaging ¡Protocol ¡ (RTMP) ¡ 6. Yunpeng ¡Zhang ¡ -­‑ ¡Post ¡Office ¡Protocol ¡(POP) ¡ 7. Alexander ¡Murray ¡ -­‑ ¡BitTorrent ¡Protocol ¡ 3 ¡

  4. 4 ¡

  5. ¡ Survey: ¡ § ¡Who ¡has ¡done ¡parallel ¡programming ¡before? ¡ § What ¡did ¡you ¡do? ¡ 5 ¡

  6. ¡ Why ¡do ¡I ¡need ¡concurrency ¡in ¡a ¡web ¡server? ¡ § Many ¡clients ¡making ¡requests ¡in ¡parallel ¡ § What ¡if ¡several ¡clients ¡each ¡attempt ¡to ¡download ¡ a ¡large ¡file? ¡ ▪ Ugly ¡to ¡make ¡everyone ¡wait ¡on ¡the ¡first ¡user ¡to ¡finish ¡ ▪ Eventually ¡other ¡clients ¡would ¡timeout ¡and ¡fail ¡ § A ¡multi-­‑CPU ¡server ¡should ¡use ¡all ¡its ¡resources ¡ (multiple ¡cores) ¡to ¡satisfy ¡multiple ¡clients ¡ 6 ¡

  7. MAXIMIZE ¡ MINIMIZE ¡ ¡ Request ¡throughput ¡ ¡ Response ¡times ¡ (#/sec) ¡ (ms) ¡ ¡ Raw ¡data ¡throughput ¡ ¡ Server ¡CPU ¡utilization ¡ (Mbps) ¡ ¡ Server ¡memory ¡usage ¡ ¡ Number ¡of ¡concurrent ¡ connections ¡ 7 ¡

  8. ¡ We’ll ¡use ¡the ¡ recv() ¡function ¡for ¡today’s ¡examples ¡ User-­‑space ¡ App ¡1 ¡ App ¡2 ¡ App ¡n ¡ . ¡. ¡. ¡ Buffer ¡ recv() Kernel-­‑space ¡ (OS) ¡ TCP ¡ (per-­‑socket ¡struct) ¡ Buffer ¡ recv() ¡copies ¡data ¡from ¡kernel ¡space ¡to ¡user-­‑space. ¡ If ¡data ¡is ¡available, ¡the ¡function ¡returns ¡immediately ¡with ¡data ¡ 8 ¡

  9. recv() ¡copies ¡data ¡from ¡kernel ¡space ¡to ¡user-­‑space. ¡ If ¡data ¡is ¡available, ¡the ¡function ¡returns ¡immediately ¡with ¡data ¡ BLOCKING ¡ NON-­‑BLOCKING ¡ ¡ Standard ¡mode ¡ ¡ Special ¡mode ¡for ¡many ¡ ¡ When ¡your ¡program ¡calls ¡ socket ¡calls, ¡including ¡ recv() , ¡if ¡no ¡data ¡is ¡ recv() available, ¡the ¡OS ¡puts ¡your ¡ ¡ When ¡your ¡program ¡calls ¡ program ¡to ¡ sleep ¡ ¡ recv() , ¡if ¡no ¡data ¡is ¡ ¡ Your ¡program ¡is ¡“blocked” ¡ available, ¡ recv() ¡ on ¡ recv() immediately ¡returns ¡ 9 ¡

  10. SYNCHRONOUS ¡ ASYNCHRONOUS ¡ ¡ “With ¡Synchronization” ¡ ¡ “Without ¡Synchronization” ¡ ¡ One ¡operation ¡at ¡a ¡time… ¡ ¡ Function ¡calls ¡to ¡OS ¡ ¡ Function ¡calls ¡to ¡OS ¡ services ¡return ¡ services ¡do ¡not ¡return ¡until ¡ immediately, ¡while ¡OS ¡ action ¡is ¡complete ¡ action ¡can ¡proceed ¡ independently ¡of ¡user ¡ program ¡ 10 ¡

  11. Synchronous ¡ Synchronous ¡ ¡ Blocking ¡I/O ¡ Non-­‑Blocking ¡I/O ¡ Asynchronous ¡ Asynchronous ¡ ¡ Blocking ¡I/O ¡ Non-­‑Blocking ¡I/O ¡ 11 ¡

  12. Pseudo-­‑code: ¡ ¡ Program ¡requests ¡ ¡ data ¡from ¡OS ¡ data = socket1.recv() # Data now available ¡ recv() ¡only ¡returns ¡ once ¡data ¡is ¡available ¡ ¡ Works ¡fine ¡for ¡ managing ¡one ¡socket ¡ § How ¡about ¡ two ¡ sockets ¡with ¡different ¡ clients? ¡ 12 ¡

  13. Pseudo-­‑code: ¡ ¡ Program ¡requests ¡ ¡ data ¡from ¡OS ¡ socket1.blocking(off) data = socket1.recv() ¡ recv() ¡will ¡return ¡ while(!data) data = socket1.recv() immediately, ¡but ¡may ¡ # Data now available not ¡have ¡any ¡data ¡ ¡ Busy-­‑wait ¡loop ¡ wastes ¡CPU ¡time ¡ ¡ How ¡would ¡this ¡work ¡if ¡we ¡had ¡ two ¡sockets ¡ to ¡manage? ¡ 13 ¡

  14. ¡ recv() ¡still ¡blocking ¡ Pseudo-­‑code: ¡ ¡ ¡ Busy-­‑wait ¡loop ¡ list_recv = (socket1) replaced ¡with ¡ new ¡ list = select(list_recv) select() ¡ function ¡ ready_sock = list[0] data = ready_sock.recv() that ¡ tests ¡multiple ¡ # Data now available sockets ¡at ¡once ¡ ¡ Give ¡ select() ¡ separate ¡list ¡of ¡sockets ¡ ¡ select() ¡returns ¡ the ¡subset ¡of ¡lists ¡that ¡ § Want ¡to ¡ recv() are ¡ ready ¡ ¡ § Want ¡to ¡ send() (for ¡send/recv/err) ¡ § Check ¡for ¡error ¡ ¡ Not ¡the ¡most ¡efficient ¡ function… ¡ 14 ¡

  15. Pseudo-­‑code: ¡ ¡ recv() ¡returns ¡ ¡ immediately ¡ data = socket.q_recv(done) # Do something else ¡ In ¡background, ¡OS ¡ # in program performs ¡ recv() ¡ fun done() # When called, data work ¡ # is available ¡ When ¡ready, ¡OS ¡calls ¡ a ¡“callback” ¡function ¡ in ¡your ¡program ¡ 15 ¡

  16. Process ¡ Thread ¡ What’s ¡the ¡difference? ¡ 16 ¡

  17. PROCESSES ¡ THREADS ¡ ¡ Use ¡multi ¡cores/CPUs ¡ ¡ Use ¡multi ¡cores/CPUs ¡ ¡ Same ¡memory ¡ space ¡ ¡ Separate ¡ memory ¡space ¡ ¡ Can ¡communicate ¡with ¡other ¡ ¡ Can ¡communicate ¡with ¡ threads ¡by ¡ shared ¡memory ¡ other ¡processes ¡only ¡by ¡ IPC ¡ ¡ “Harder” ¡ to ¡program ¡(other ¡ (inter-­‑program ¡comm.) ¡ buggy ¡threads ¡can ¡easily ¡ ¡ “Safer” ¡ to ¡program ¡(other ¡ corrupt ¡your ¡memory ¡+ ¡ processes ¡can’t ¡hurt ¡you) ¡ synchronization ¡is ¡hard!) ¡ ¡ “Heavy-­‑weight” ¡ -­‑ ¡Slower ¡ ¡ “Light-­‑weight” ¡ -­‑ ¡Fast ¡to ¡start ¡ to ¡start ¡a ¡new ¡process ¡ a ¡new ¡thread ¡ ¡ ¡(lots ¡of ¡OS ¡work) ¡ (minimal ¡OS ¡work) ¡ 17 ¡

  18. PROCESSES ¡ THREADS ¡ ¡ Slow ¡start? ¡ ¡ Fast ¡start? ¡ § Typical ¡servers ¡start ¡a ¡“pool” ¡ § OK ¡to ¡start ¡threads ¡“on ¡ of ¡processes ¡when ¡launched ¡ demand” ¡ § Requests ¡are ¡quickly ¡assigned ¡ ¡ Shared ¡data? ¡ to ¡an ¡already-­‑running ¡process ¡ § Need ¡synchronization ¡(locks, ¡ when ¡received ¡ semaphores, ¡etc…) ¡to ¡prevent ¡ ¡ Shared ¡data? ¡ corruption ¡of ¡shared ¡data ¡ § Need ¡to ¡use ¡OS ¡IPC ¡ mechanisms ¡to ¡communicate ¡ § Needed ¡to ¡assign ¡requests ¡to ¡ processes, ¡store ¡log ¡data ¡from ¡ processes ¡to ¡single ¡file, ¡… ¡ 18 ¡

  19. Processes ¡or ¡ ¡Threads ¡ Non-­‑blocking ¡sockets ¡ with ¡blocking ¡sockets ¡ Synchronous ¡ Synchronous ¡ ¡ Blocking ¡I/O ¡ Non-­‑Blocking ¡I/O ¡ Asynchronous ¡ Asynchronous ¡ ¡ Blocking ¡I/O ¡ Non-­‑Blocking ¡I/O ¡ Single ¡process ¡ Single ¡process, ¡ ¡ with ¡select() ¡ Event ¡driven ¡ 19 ¡

  20. And ¡now, ¡a ¡note ¡ about ¡Python… ¡ 20 ¡

  21. (Only ¡if ¡Google ¡helps…) ¡ Pro ¡ Intermediate ¡ Novice ¡ 21 ¡

  22. So ¡before ¡assigning ¡class ¡ projects, ¡I ¡wrote ¡a ¡Python ¡ web ¡server ¡using ¡ threads . ¡ ¡ Once ¡working, ¡I ¡measured ¡ its ¡performance… ¡ 22 ¡

  23. Results ¡were ¡“sub ¡optimal” ¡ Not ¡this ¡bad, ¡but ¡it ¡certainly ¡did ¡not ¡scale ¡ well ¡as ¡the ¡number ¡of ¡concurrent ¡clients ¡ increased… ¡ 23 ¡

  24. ¡ Python ¡is ¡an ¡ interpreted ¡language ¡ § Several ¡different ¡interpreters ¡exist… ¡ § Most ¡common ¡interpreter ¡is ¡written ¡in ¡C ¡(“ CPython ”) ¡ ¡ CPython ¡has ¡a ¡global ¡lock ¡ ¡ (GIL ¡= ¡Global ¡Interpreter ¡Lock) ¡ ¡ § Lock ¡prevents ¡two ¡threads ¡from ¡running ¡in ¡the ¡ interpreter ¡and ¡manipulating ¡memory ¡at ¡same ¡time ¡ § Allows ¡interpreter ¡to ¡run ¡safely ¡(correctly), ¡perform ¡ garbage ¡collection, ¡etc… ¡ 24 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend