SlideShare a Scribd company logo
1

                                  www.chenshuo.com


 __ __            _
| / |           | |
|  / |_    _ __| |_    _ ___
| |/| | | | |/ _` | | | |/ _ 
| | | | |_| | (_| | |_| | (_) |
|_| |_|__,_|__,_|__,_|___/




               NETWORK PROGRAMMING
               IN C++ WITH MUDUO
 2012/06       Shuo Chen
What is Muduo?
2


       non-blocking,
       event-driven,
       multi-core ready,
       modern (NYF’s*)
       C++ network library
       for Linux
       Buzz words!!!
       BSD License of course

    2012/06   * Not your father’s   www.chenshuo.com
Learn network programming
3
    in an afternoon? Let’s build greeting server/client
import socket, time                                    import socket, os

serversocket = socket.socket(                          sock = socket.socket(
   socket.AF_INET,                                        socket.AF_INET,
   socket.SOCK_STREAM)                                    socket.SOCK_STREAM)
# set SO_REUSEADDR                                     sock.connect((host, 8888))
serversocket.bind(('', 8888))                          sock.send(os.getlogin() + 'n')
serversocket.listen(5)                                 message = sock.recv(4096)
                                                       print message
while True:                                            sock.close()
  (clientsocket, address) = serversocket.accept()
  name = clientsocket.recv(4096)
  datetime = time.asctime()
  clientsocket.send('Hello ' + name)
  clientsocket.send('My time is ' + datetime + 'n')            ~10 Sockets APIs
  clientsocket.close()                                          Simple, huh ?
    2012/06                                                            www.chenshuo.com
Sockets API might be harder than
4
    you thought
       Run on local host                    Run on network
    $ ./hello-client.py localhost          $ ./hello-client.py atom
    Hello schen                            Hello schen
    My time is Sun May 13 12:56:44 2012

       Incomplete response!!! Why ?
       Standard libraries (C/Java/Python) do not
        provide higher abstractions than Sockets API
         Naive   implementation is most-likely wrong
           Sometimes hurt you after being deployed to prod env

       That’s why we need good network library 
    2012/06                                            www.chenshuo.com
Performance goals
5


       High performance? Hard to define
       Satisfactory (adequate) Performance
         Not     to be a/the bottleneck of the system
       Saturate GbE bandwidth
         Even      Python can do this
       50k concurrent connections
         No     special efforts needed on modern hardware
       n0k+ messages per second
         Distribute msg       to 30k clients in 0.99s (EC2 small)
    2012/06    40k clients in 0.75s (Atom D525 1.8GHz dualwww.chenshuo.com
                                                            core HT)
Caution: Unfair
                                                 Comparison




                  Nginx w/ echo module, not even static file
              http://weibo.com/1701018393/y8jw8AdUQ



6   2012/06                                    www.chenshuo.com
Muduo vs. Boost Asio
7




    http://www.cnblogs.com/Solstice/archive/2010/09/04/muduo_vs_asio.html

       Loopback device, because even Python can saturate 1GbE
     2012/06                                                    www.chenshuo.com
Muduo vs. libevent 2.0.x
8




    http://www.cnblogs.com/Solstice/archive/2010/09/05/muduo_vs_libevent.html


         Loopback device                * Libevent 2.1.x should be better
     2012/06                                                    www.chenshuo.com
http://www.cnblogs.com/Solstice/archive/2010/09/08/muduo_vs_libevent_bench.html
9    2012/06                                                   www.chenshuo.com
ZeroMQ local_lat, remote_lat
10




     2012/06                  www.chenshuo.com
Some performance metrics
11


        Use their own benchmarks
        Nginx          100k qps for in-memory reqs
        Asio           higher throughput, 800MiB+/s
        Libevent       ditto, same event handling speed
        pub sub        deliver msg to 40k clients in 1 sec
        RPC            100k qps @ 100c,
          260k~515k with   batching/pipelining
        At least proves “No obvious mistake made on
         critical path of Muduo”
     2012/06                                      www.chenshuo.com
Where does Muduo fit in the stack?
12


        General-purpose (neutral carrier) network library
          Let you focus on business logic
          Wraps sockets API, take care of IO complexity

          3.5 essential events (conn up/down, read, write complete)

        Libraries that share similar features/purposes
          C – libevent, C++ – ACE/ASIO, Java – Netty, Mina
          Python – twisted, Perl – POE, Ruby – EventMachine

        Not comparable to ‘frameworks’
           ICE            a RPC framework, see muduo-protorpc
          Tomcat, Node.js        built only/mainly for HTTP
          ZeroMQ
     2012/06                      4 messaging patternswww.chenshuo.com
Two major approaches to deal with
13
     many concurrent connections
        When ‘thread’ is cheap, 10k+ ‘thread’s in program
          Createone or two threads per connection, blocking IO
          Python gevent, Go goroutine/channel, Erlang actor

        When thread is expensive, a handful of threads
          Eachthread serves many connections
          Non-blocking IO with IO multiplexing (select/epoll)
                IO multiplexing is actually thread-reusing
          Event
               notification using callbacks
          Muduo, Netty, Python twisted, Node.js, libevent, etc.

        Not all libraries can make good use of multi-cores.
          But     Muduo can 
     2012/06                                                  www.chenshuo.com
Blocking IO is not always bad
14


        A socks proxy, TCP relay, port forwarding
          client   <-> proxy <-> server
      def forward(source, destination):
          while True:
              data = source.recv(4096)
              if data:
                  destination.sendall(data)
              else:
                  destination.shutdown(socket.SHUT_WR)
                  break
      thread.start_new_thread(forward, (clientsocket, sock))
      thread.start_new_thread(forward, (sock, clientsocket))

        OK to use blocking IO when interaction is simple
          Bandwidth/throttling is   done by kernel
     2012/06                                            www.chenshuo.com
Non-blocking IO
15


        Imagine writing a chat server with blocking IO
          Message from one connection needs to be sent to
           many connections
          Connections are up and down all the time
            How to keep the integrity of a message being forwarded
          How    many threads do you need for N connections ?
        Try non-blocking IO instead
          Essential   of event-driven network programming in
             30 lines of code
        Take a breath
     2012/06                                            www.chenshuo.com
# set up serversocket, socket()/bind()/listen(), as before
    poll = select.poll() # epoll() should work the same
    poll.register(serversocket.fileno(), select.POLLIN)
    connections = {}
    while True:      # The event loop              Demo only, not good quality
          events = poll.poll(10000)                IO multiplexing only
          for fileno, event in events:
              if fileno == serversocket.fileno():
                  (clientsocket, address) = serversocket.accept()
                  # clientsocket.setblocking(0) ??
                  poll.register(clientsocket.fileno(), select.POLLIN)
                  connections[clientsocket.fileno()] = clientsocket
              elif event & select.POLLIN:
                  clientsocket = connections[fileno]
                  data = clientsocket.recv(4096)      # incomplete msg ?
                  if data:
  Business
                      for (fd, othersocket) in connections.iteritems():
  logic
                           if othersocket != clientsocket:
                               othersocket.send(data) # partial sent ??
                  else:                                    chat server
                      poll.unregister(fileno)
                      clientsocket.close()
16    2012/06         del connections[fileno]                 www.chenshuo.com
# set up serversocket, socket()/bind()/listen(), as before
   poll = select.poll() # epoll() should work the same
   poll.register(serversocket.fileno(), select.POLLIN)
   connections = {}
   while True:     # The event loop              Demo only, not good quality
        events = poll.poll(10000)                IO multiplexing only
        for fileno, event in events:
            if fileno == serversocket.fileno():
                (clientsocket, address) = serversocket.accept()
                # clientsocket.setblocking(0) ??
                poll.register(clientsocket.fileno(), select.POLLIN)
                connections[clientsocket.fileno()] = clientsocket
            elif event & select.POLLIN:
                clientsocket = connections[fileno]
                data = clientsocket.recv(4096)
                if data:
 Business
                    clientsocket.send(data)         # partial sent ??
 logic


                else:                                      echo server
                    poll.unregister(fileno)
                    clientsocket.close()          Most code are identical
17   2012/06        del connections[fileno]                www.chenshuo.com
                                                  Make them a library
Pitfalls of non-blocking IO
18


        Partial write, how to deal with remaining data?
          You must use an output buffer per socket for next try,
           but when to watch POLLOUT event?
        Incomplete read, what if data arrives byte-by-byte
          TCP    is a byte stream, use an input buffer to decode
            Alternatively, use a state machine, which is more complex

        Connection management, Socket lifetime mgmt
          File   descriptors are small integers, prone to cross talk
        Muduo is aware of and well prepared for all above!
          Focus    on your business logic and let Muduo do the rest
     2012/06                                              www.chenshuo.com
Event loop (reactor), the heart of
19
     non-blocking network programming
        Dispatches IO event to callback functions
          Events:    socket is readable, writable, error, hang up
        Message loop in Win32 programming
         While (GetMessage(&msg, NULL, 0, 0) > 0) // epoll_wait()
         {
             TranslateMessage(&msg);
             DispatchMessage(&msg);               // here’s the beef
         }
                Cooperative   multitasking, blocking is unacceptable
        Muduo unifies event loop wakeup, timer queue,
         signal handler all with file read/write
          Also    make it non-portable
     2012/06                                               www.chenshuo.com
20   2012/06
               IO responses are instant, one CPU used
                                           www.chenshuo.com
               Events happen in sequence
One event loop with thread pool
21




     2012/06
                 Computational task is heavy, IO is light
                                         www.chenshuo.com
Any library function that accesses
22
     file or network can be blocking
        The whole C/Posix library is blocking/synchronous
          Disk   IO is blocking, use threads to make it cooperating
        ‘harmless’ functions could block current thread
          gethostbyname() could  read /etc/hosts or query DNS
          getpwuid() could read /etc/passwd or query NIS*

          localtime()/ctime() could read /etc/localtime

          Files could be on network mapped file system!

        What if this happens in a busy network IO thread?
          Server is   responseless for seconds, may cause trashing
     2012/06   * /var/db/passwd.db or LDAP             www.chenshuo.com
Non-blocking is a paradigm shift
23


        Have to pay the cost if you want to write high
         performance network application in traditional
         languages like C/C++/Java
          It’s   a mature technique for nearly 20 years
        Drivers/Adaptors needed for all operations
          Non-blocking DNSresolving, UDNS or c-ares
          Non-blocking HTTP client/server, curl and microhttpd
            Examples provided in muduo and muduo-udns

          Non-blocking database     query, libpq or libdrizzle
            Need drivers to make them work in   muduo
          Non-blocking logging,    in muduo 0.5.0
     2012/06                                             www.chenshuo.com
Event loop in multi-core era
24


        One loop per thread is usually a good model
          Before you   try any other fancy ‘pattern’
        Muduo supports both single/multi-thread usage
          Justassign TcpConnection to any EventLoop, all IO
           happens in that EventLoop thread
          The thread is predictable, EventLoop::runInLoop()

        Many other ‘event-driven’ libraries can’t make
         use of multi-cores, you have to run multiple
         processes
     2012/06                                            www.chenshuo.com
One event loop per thread
25




     2012/06   Prioritize connections with threads   www.chenshuo.com
Hybrid solution, versatile
26




               Decode/encode can be in IO thread
     2012/06                                       www.chenshuo.com
Object lifetime management
27


        Muduo classes are concrete & non-copyable
          And   have no base class or virtual destructor
        EventLoop, TcpServer, TcpClient are all long-live
         objects. Their ownership is clean, not shared.
        TcpConnection is vague
          TcpServer may    hold all alive connection objects
          You may also hold some/all of them for sending data

          It’s the only class managed by std::shared_ptr

        No ‘delete this’, it’s a joke
          muduo will not pass raw pointer to/from www.chenshuo.com
     2012/06                                       client code
class EchoServer { // non-copyable
  public:
   EchoServer(EventLoop* loop, const InetAddress& listenAddr)
     : server_(loop, listenAddr, "EchoServer") {
     server_.setConnectionCallback(
          boost::bind(&EchoServer::onConnection, this, _1));
     server_.setMessageCallback(
          boost::bind(&EchoServer::onMessage, this, _1, _2, _3));
     server_.setThreadNum(numThreads);
   }
  private:
   void onConnection(const TcpConnectionPtr& conn)   {
     // print, you may keep a copy of conn for further usage
   }

   void onMessage(const TcpConnectionPtr& conn,
                  Buffer* buf, Timestamp time) {
     string data(buf->retrieveAsString());
     conn->send(data);
   }                      But echo is too simple to be meaningful
    TcpServer server_; // a member, not base class. More is possible
28
  }; 2012/06                                            www.chenshuo.com
Muduo examples, all concurrent
29


        Boost.asio chat
          Codec   , length prefix message encoder/decoder
        Google Protocol Buffers codec
        Filetransfer
        Idle connection/max connection
        Hub/Multiplexer
        Pinpong/roundtrip
        socks4a      Business-oriented TCP network programming
        Many more Efficient multithreaded network programming
     2012/06                                        www.chenshuo.com
Format-less protocol, pure data
30




     2012/06                  www.chenshuo.com
Length header fmt, ‘messages’
31




     2012/06                 www.chenshuo.com
void onMessage(const muduo::net::TcpConnectionPtr& conn,
                muduo::net::Buffer* buf,
                muduo::Timestamp receiveTime) {
   while (buf->readableBytes() >= kHeaderLen) { // kHeaderLen == 4
     const void* data = buf->peek();
     int32_t be32 = *static_cast<const int32_t*>(data); // FIXME
     const int32_t len = muduo::net::sockets::networkToHost32(be32);
     if (len > 65536 || len < 0) {
       LOG_ERROR << "Invalid length " << len;
       conn->shutdown();
     } else if (buf->readableBytes() >= len + kHeaderLen) {
       buf->retrieve(kHeaderLen);
       std::string message(buf->peek(), len);
       messageCallback_(conn, message, receiveTime);
       buf->retrieve(len);
     } else {
       break;        Any grouping of input data should be decoded correctly
     }
   }       0x00, 0x00, 0x00, 0x05, ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, 0x00,
 }         0x00, 0x00, 0x08, ‘c’, ‘h’, ‘e’, ‘n’, ‘s’, ‘h’, ‘u’, ‘o’

32   2012/06                                                www.chenshuo.com
Protobuf format, message objects
33




     2012/06                                                   www.chenshuo.com
     http://www.cnblogs.com/Solstice/archive/2011/04/13/2014362.html
34   2012/06                                                   www.chenshuo.com
     http://www.cnblogs.com/Solstice/archive/2011/04/03/2004458.html
Design goals of Muduo
35


        Intranet, not Internet.
          Distributed system in a global company
          Use HTTP on internet, it’s the universal protocol

        Build network application with business logic,
         not writing well-known network server
          Not for building high-performance httpd, ntpd, ftpd,
           webproxy, bind
          Components in distributed system
                  master/chunk-server in GFS
        TCP long connections
            Muduo thread model is not optimized for short TCP
             connections, as accept(2) and IO in two loops
     2012/06                                            www.chenshuo.com
Muduo is NOT
36




     2012/06        www.chenshuo.com
Muduo doesn’t
37


        Support transport protocols other than TCPv4
          IPv6,UDP, Serial port, SNMP, ARP, RARP
          Build your own with muduo::Channel class
                Any thing that is ‘selectable’ can integrated into Muduo

          May      support SSL in future, but with low priority
                Use https for internet service, use VPN for info   security
        Support platforms other than Linux 2.6/3.x
          Never port to Windows
          Unlikely port to FreeBSD, Solaris

          However, it runs on ARM9 boards, with Linux 2.6.32
     2012/06                                                   www.chenshuo.com
List of muduo libraries
38


        Muduo                 The core library
          baselibrary (threading, logging, datetime)
          network library

          Many examples

        Muduo-udns Non-blocking DNS resolving
        Muduo-protorpc
          Asynchronous        bidirectional RPC based on Muduo
                Also has Java bindings with Netty

          Examples: zurg – a master/slaves service mgmt sys
          Paxos – a consensus algorithm* (to be written)
     2012/06                                           www.chenshuo.com
Check-ins per week
39


        From 2010-03 to 2012-06
         20
         18
         16
         14
         12
         10
          8
          6
          4
          2
          0




     2012/06                       www.chenshuo.com
Q&A
40


        Thank you!

        www.chenshuo.com
        github.com/chenshuo
        weibo.com/giantchen

        github.com/downloads/chenshuo/documents/
         MuduoManual.pdf

     2012/06                             www.chenshuo.com
Bonus Slides
41


        Synchronous vs. asynchronous
        Basic network performance metrics




                      Simply wrong and misleading
     2012/06                                    www.chenshuo.com
Synchronous vs. asynchronous IO
42


        Epoll is synchronous
          Select/poll/epoll are     O(N), but N stands differently
        Anything but aio_* are synchronous
          Non-blocking IO     is synchronous
             you call it, it returns. It never breaks/interrupt code flow
          The only thing that can be blocking in event-driven
           program are epoll_wait and pthread_cond_wait
             pthread_mutex_lock should almost not real block anything

        Asynchronous IO is not practical in Linux
          Either simulated with threads,
          Or notify with signal, not good for multithreaded app
      2012/06                                                www.chenshuo.com
TCP/IP over 1Gb Ethernet
43


        Ethernet frame           Raw b/w 125MB/s
        Preamble         8B      Packet per second
        MAC             12B        Max 1,488,000
        Type             2B        Min 81,274 (no jumbo)

        Payload 46~1500B         TCP/IP overhead
                                    IPheader 20B
        CRC              4B
                                    TCP header 20B
        Gap             12B
                                    TCP option 12B (TSopt)
        Total      84~1538B
                                  Max TCP throughput
     2012/06        112MB/s         81274*(1500-52)
                                               www.chenshuo.com
PPS vs. throughput
44

                                                          PPS vs. MB/s
               1400                                                                                            120


               1200
                                                                                                               100


               1000
                                                                                                               80

                800
                                                                                                               60
                600

                                                                                                               40
                400


                                                                                                               20
                200


                  0                                                                                            0
                      100   200   300   400   500   600   700    800   900 1000 1100 1200 1300 1400 1500

                                                                kPPS      MiB/s



     2012/06                                                                                               www.chenshuo.com
Back-of-the-envelope calculation
45


        Read 1MB from net, ~10ms
        Copy 1MB in memory, ~0.2ms on old E5150
        Copying is not a sin, CPU and memory are so fast
        Decode byte string to Message objects
          500MB/sdecoding in IO thread, pass ptr to calc thr
          50MB/s copy data to calc threads, decode there

        Compress or not ?     200MB/s 2x ratio 10MB
          10Mb ADSL 8s vs. 4.05s
          1000Mb LAN 0.08s vs. 0.09s

     2012/06             Redo for 10GbE, InfiniBand   www.chenshuo.com
High Performance ???
46


        Network application in user land
        Network service in kernel
        TCP/IP stack or network adaptor driver in kernel
        Network device (switch/router)
          Special purpose OS for network device (firmware)
          Special purpose chips for network device (NP)

        Control network adaptor with FPGAs
          Coding    in Verilog, hardwire logic

     2012/06                                      www.chenshuo.com

More Related Content

What's hot

IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...
IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...
IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...
Irontec
 
Performance Comparison of Mutex, RWLock and Atomic types in Rust
Performance Comparison of Mutex, RWLock and  Atomic types in RustPerformance Comparison of Mutex, RWLock and  Atomic types in Rust
Performance Comparison of Mutex, RWLock and Atomic types in Rust
Mitsunori Komatsu
 
Kotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is coming
Kirill Rozov
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)
Kentaro Ebisawa
 
Ansible - Crash course
Ansible - Crash courseAnsible - Crash course
Ansible - Crash course
Simone Soldateschi
 
Openstack+Ceph設定ガイド
Openstack+Ceph設定ガイドOpenstack+Ceph設定ガイド
Openstack+Ceph設定ガイド
OSSラボ株式会社
 
「var禁止」禁止
「var禁止」禁止「var禁止」禁止
「var禁止」禁止
Ryota Murohoshi
 
Reconnaissance of Virtio: What’s new and how it’s all connected?
Reconnaissance of Virtio: What’s new and how it’s all connected?Reconnaissance of Virtio: What’s new and how it’s all connected?
Reconnaissance of Virtio: What’s new and how it’s all connected?
Samsung Open Source Group
 
Hands-on ethernet driver
Hands-on ethernet driverHands-on ethernet driver
Hands-on ethernet driver
SUSE Labs Taipei
 
コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜
コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜
コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜
Retrieva inc.
 
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPFA Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
oholiab
 
Scaling WebRTC applications with Janus
Scaling WebRTC applications with JanusScaling WebRTC applications with Janus
Scaling WebRTC applications with Janus
Lorenzo Miniero
 
BPF: Tracing and more
BPF: Tracing and moreBPF: Tracing and more
BPF: Tracing and more
Brendan Gregg
 
Deeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay NetworksDeeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay Networks
Laurent Bernaille
 
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
Ernie Souhrada
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
Kernel TLV
 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet Processing
Michelle Holley
 
[232] 성능어디까지쥐어짜봤니 송태웅
[232] 성능어디까지쥐어짜봤니 송태웅[232] 성능어디까지쥐어짜봤니 송태웅
[232] 성능어디까지쥐어짜봤니 송태웅
NAVER D2
 
Linux Linux Traffic Control
Linux Linux Traffic ControlLinux Linux Traffic Control
Linux Linux Traffic Control
SUSE Labs Taipei
 

What's hot (20)

IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...
IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...
IVOZ Provider Open Source - La solución VoIP opensource para operadores e int...
 
Performance Comparison of Mutex, RWLock and Atomic types in Rust
Performance Comparison of Mutex, RWLock and  Atomic types in RustPerformance Comparison of Mutex, RWLock and  Atomic types in Rust
Performance Comparison of Mutex, RWLock and Atomic types in Rust
 
Kotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is coming
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)
 
Ansible - Crash course
Ansible - Crash courseAnsible - Crash course
Ansible - Crash course
 
Openstack+Ceph設定ガイド
Openstack+Ceph設定ガイドOpenstack+Ceph設定ガイド
Openstack+Ceph設定ガイド
 
「var禁止」禁止
「var禁止」禁止「var禁止」禁止
「var禁止」禁止
 
Reconnaissance of Virtio: What’s new and how it’s all connected?
Reconnaissance of Virtio: What’s new and how it’s all connected?Reconnaissance of Virtio: What’s new and how it’s all connected?
Reconnaissance of Virtio: What’s new and how it’s all connected?
 
Hands-on ethernet driver
Hands-on ethernet driverHands-on ethernet driver
Hands-on ethernet driver
 
コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜
コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜
コンテナ仮想、その裏側 〜user namespaceとrootlessコンテナ〜
 
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPFA Kernel of Truth: Intrusion Detection and Attestation with eBPF
A Kernel of Truth: Intrusion Detection and Attestation with eBPF
 
Scaling WebRTC applications with Janus
Scaling WebRTC applications with JanusScaling WebRTC applications with Janus
Scaling WebRTC applications with Janus
 
BPF: Tracing and more
BPF: Tracing and moreBPF: Tracing and more
BPF: Tracing and more
 
Deeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay NetworksDeeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay Networks
 
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet Processing
 
[232] 성능어디까지쥐어짜봤니 송태웅
[232] 성능어디까지쥐어짜봤니 송태웅[232] 성능어디까지쥐어짜봤니 송태웅
[232] 성능어디까지쥐어짜봤니 송태웅
 
Linux Linux Traffic Control
Linux Linux Traffic ControlLinux Linux Traffic Control
Linux Linux Traffic Control
 

Viewers also liked

Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverShuo Chen
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threads
Shuo Chen
 
Fork Yeah! The Rise and Development of illumos
Fork Yeah! The Rise and Development of illumosFork Yeah! The Rise and Development of illumos
Fork Yeah! The Rise and Development of illumos
bcantrill
 
Datetime - Julian Date
Datetime - Julian DateDatetime - Julian Date
Datetime - Julian DateShuo Chen
 
PThreads Vs Win32 Threads
PThreads  Vs  Win32 ThreadsPThreads  Vs  Win32 Threads
PThreads Vs Win32 Threads
Robert Sayegh
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsXiaozhe Wang
 

Viewers also liked (8)

Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ server
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
Where destructors meet threads
Where destructors meet threadsWhere destructors meet threads
Where destructors meet threads
 
Fork Yeah! The Rise and Development of illumos
Fork Yeah! The Rise and Development of illumosFork Yeah! The Rise and Development of illumos
Fork Yeah! The Rise and Development of illumos
 
Zurg part 1
Zurg part 1Zurg part 1
Zurg part 1
 
Datetime - Julian Date
Datetime - Julian DateDatetime - Julian Date
Datetime - Julian Date
 
PThreads Vs Win32 Threads
PThreads  Vs  Win32 ThreadsPThreads  Vs  Win32 Threads
PThreads Vs Win32 Threads
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
 

Similar to Muduo network library

node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
Aarti Parikh
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois
 
Netty @Apple: Large Scale Deployment/Connectivity
Netty @Apple: Large Scale Deployment/ConnectivityNetty @Apple: Large Scale Deployment/Connectivity
Netty @Apple: Large Scale Deployment/Connectivity
C4Media
 
Service Discovery Like a Pro
Service Discovery Like a ProService Discovery Like a Pro
Service Discovery Like a Pro
Eran Harel
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server InternalsPraveen Gollakota
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
Marcus Frödin
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Vikash Singh
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
Joseph de Castelnau
 
One-Man Ops
One-Man OpsOne-Man Ops
One-Man Ops
Jos Boumans
 
Sjug aug 2010_cloud
Sjug aug 2010_cloudSjug aug 2010_cloud
Sjug aug 2010_cloud
Michael Neale
 
SenchaLabs Connect & Express
SenchaLabs Connect & ExpressSenchaLabs Connect & Express
SenchaLabs Connect & Express
Tim Caswell
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
Jackson Tian
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.jsguileen
 
Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5
Web Directions
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
Apaichon Punopas
 
Speeding up I/O for Machine Learning ft Apple Case Study using TensorFlow, N...
Speeding up I/O for Machine Learning  ft Apple Case Study using TensorFlow, N...Speeding up I/O for Machine Learning  ft Apple Case Study using TensorFlow, N...
Speeding up I/O for Machine Learning ft Apple Case Study using TensorFlow, N...
Alluxio, Inc.
 
CoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love SystemdCoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love Systemd
Richard Lister
 

Similar to Muduo network library (20)

node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Netty @Apple: Large Scale Deployment/Connectivity
Netty @Apple: Large Scale Deployment/ConnectivityNetty @Apple: Large Scale Deployment/Connectivity
Netty @Apple: Large Scale Deployment/Connectivity
 
Proposal
ProposalProposal
Proposal
 
Service Discovery Like a Pro
Service Discovery Like a ProService Discovery Like a Pro
Service Discovery Like a Pro
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
 
netty_qcon_v4
netty_qcon_v4netty_qcon_v4
netty_qcon_v4
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
One-Man Ops
One-Man OpsOne-Man Ops
One-Man Ops
 
Sjug aug 2010_cloud
Sjug aug 2010_cloudSjug aug 2010_cloud
Sjug aug 2010_cloud
 
SenchaLabs Connect & Express
SenchaLabs Connect & ExpressSenchaLabs Connect & Express
SenchaLabs Connect & Express
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
 
Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5Dave Orchard - Offline Web Apps with HTML5
Dave Orchard - Offline Web Apps with HTML5
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Speeding up I/O for Machine Learning ft Apple Case Study using TensorFlow, N...
Speeding up I/O for Machine Learning  ft Apple Case Study using TensorFlow, N...Speeding up I/O for Machine Learning  ft Apple Case Study using TensorFlow, N...
Speeding up I/O for Machine Learning ft Apple Case Study using TensorFlow, N...
 
CoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love SystemdCoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love Systemd
 

Recently uploaded

FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 

Muduo network library

  • 1. 1 www.chenshuo.com __ __ _ | / | | | | / |_ _ __| |_ _ ___ | |/| | | | |/ _` | | | |/ _ | | | | |_| | (_| | |_| | (_) | |_| |_|__,_|__,_|__,_|___/ NETWORK PROGRAMMING IN C++ WITH MUDUO 2012/06 Shuo Chen
  • 2. What is Muduo? 2  non-blocking,  event-driven,  multi-core ready,  modern (NYF’s*)  C++ network library  for Linux  Buzz words!!!  BSD License of course 2012/06 * Not your father’s www.chenshuo.com
  • 3. Learn network programming 3 in an afternoon? Let’s build greeting server/client import socket, time import socket, os serversocket = socket.socket( sock = socket.socket( socket.AF_INET, socket.AF_INET, socket.SOCK_STREAM) socket.SOCK_STREAM) # set SO_REUSEADDR sock.connect((host, 8888)) serversocket.bind(('', 8888)) sock.send(os.getlogin() + 'n') serversocket.listen(5) message = sock.recv(4096) print message while True: sock.close() (clientsocket, address) = serversocket.accept() name = clientsocket.recv(4096) datetime = time.asctime() clientsocket.send('Hello ' + name) clientsocket.send('My time is ' + datetime + 'n') ~10 Sockets APIs clientsocket.close() Simple, huh ? 2012/06 www.chenshuo.com
  • 4. Sockets API might be harder than 4 you thought  Run on local host  Run on network $ ./hello-client.py localhost $ ./hello-client.py atom Hello schen Hello schen My time is Sun May 13 12:56:44 2012  Incomplete response!!! Why ?  Standard libraries (C/Java/Python) do not provide higher abstractions than Sockets API  Naive implementation is most-likely wrong  Sometimes hurt you after being deployed to prod env  That’s why we need good network library  2012/06 www.chenshuo.com
  • 5. Performance goals 5  High performance? Hard to define  Satisfactory (adequate) Performance  Not to be a/the bottleneck of the system  Saturate GbE bandwidth  Even Python can do this  50k concurrent connections  No special efforts needed on modern hardware  n0k+ messages per second  Distribute msg to 30k clients in 0.99s (EC2 small) 2012/06  40k clients in 0.75s (Atom D525 1.8GHz dualwww.chenshuo.com core HT)
  • 6. Caution: Unfair Comparison Nginx w/ echo module, not even static file http://weibo.com/1701018393/y8jw8AdUQ 6 2012/06 www.chenshuo.com
  • 7. Muduo vs. Boost Asio 7 http://www.cnblogs.com/Solstice/archive/2010/09/04/muduo_vs_asio.html Loopback device, because even Python can saturate 1GbE 2012/06 www.chenshuo.com
  • 8. Muduo vs. libevent 2.0.x 8 http://www.cnblogs.com/Solstice/archive/2010/09/05/muduo_vs_libevent.html Loopback device * Libevent 2.1.x should be better 2012/06 www.chenshuo.com
  • 10. ZeroMQ local_lat, remote_lat 10 2012/06 www.chenshuo.com
  • 11. Some performance metrics 11  Use their own benchmarks  Nginx 100k qps for in-memory reqs  Asio higher throughput, 800MiB+/s  Libevent ditto, same event handling speed  pub sub deliver msg to 40k clients in 1 sec  RPC 100k qps @ 100c,  260k~515k with batching/pipelining  At least proves “No obvious mistake made on critical path of Muduo” 2012/06 www.chenshuo.com
  • 12. Where does Muduo fit in the stack? 12  General-purpose (neutral carrier) network library  Let you focus on business logic  Wraps sockets API, take care of IO complexity  3.5 essential events (conn up/down, read, write complete)  Libraries that share similar features/purposes  C – libevent, C++ – ACE/ASIO, Java – Netty, Mina  Python – twisted, Perl – POE, Ruby – EventMachine  Not comparable to ‘frameworks’  ICE a RPC framework, see muduo-protorpc  Tomcat, Node.js built only/mainly for HTTP  ZeroMQ 2012/06 4 messaging patternswww.chenshuo.com
  • 13. Two major approaches to deal with 13 many concurrent connections  When ‘thread’ is cheap, 10k+ ‘thread’s in program  Createone or two threads per connection, blocking IO  Python gevent, Go goroutine/channel, Erlang actor  When thread is expensive, a handful of threads  Eachthread serves many connections  Non-blocking IO with IO multiplexing (select/epoll)  IO multiplexing is actually thread-reusing  Event notification using callbacks  Muduo, Netty, Python twisted, Node.js, libevent, etc.  Not all libraries can make good use of multi-cores.  But Muduo can  2012/06 www.chenshuo.com
  • 14. Blocking IO is not always bad 14  A socks proxy, TCP relay, port forwarding  client <-> proxy <-> server def forward(source, destination): while True: data = source.recv(4096) if data: destination.sendall(data) else: destination.shutdown(socket.SHUT_WR) break thread.start_new_thread(forward, (clientsocket, sock)) thread.start_new_thread(forward, (sock, clientsocket))  OK to use blocking IO when interaction is simple  Bandwidth/throttling is done by kernel 2012/06 www.chenshuo.com
  • 15. Non-blocking IO 15  Imagine writing a chat server with blocking IO  Message from one connection needs to be sent to many connections  Connections are up and down all the time  How to keep the integrity of a message being forwarded  How many threads do you need for N connections ?  Try non-blocking IO instead  Essential of event-driven network programming in 30 lines of code  Take a breath 2012/06 www.chenshuo.com
  • 16. # set up serversocket, socket()/bind()/listen(), as before poll = select.poll() # epoll() should work the same poll.register(serversocket.fileno(), select.POLLIN) connections = {} while True: # The event loop Demo only, not good quality events = poll.poll(10000) IO multiplexing only for fileno, event in events: if fileno == serversocket.fileno(): (clientsocket, address) = serversocket.accept() # clientsocket.setblocking(0) ?? poll.register(clientsocket.fileno(), select.POLLIN) connections[clientsocket.fileno()] = clientsocket elif event & select.POLLIN: clientsocket = connections[fileno] data = clientsocket.recv(4096) # incomplete msg ? if data: Business for (fd, othersocket) in connections.iteritems(): logic if othersocket != clientsocket: othersocket.send(data) # partial sent ?? else: chat server poll.unregister(fileno) clientsocket.close() 16 2012/06 del connections[fileno] www.chenshuo.com
  • 17. # set up serversocket, socket()/bind()/listen(), as before poll = select.poll() # epoll() should work the same poll.register(serversocket.fileno(), select.POLLIN) connections = {} while True: # The event loop Demo only, not good quality events = poll.poll(10000) IO multiplexing only for fileno, event in events: if fileno == serversocket.fileno(): (clientsocket, address) = serversocket.accept() # clientsocket.setblocking(0) ?? poll.register(clientsocket.fileno(), select.POLLIN) connections[clientsocket.fileno()] = clientsocket elif event & select.POLLIN: clientsocket = connections[fileno] data = clientsocket.recv(4096) if data: Business clientsocket.send(data) # partial sent ?? logic else: echo server poll.unregister(fileno) clientsocket.close() Most code are identical 17 2012/06 del connections[fileno] www.chenshuo.com Make them a library
  • 18. Pitfalls of non-blocking IO 18  Partial write, how to deal with remaining data?  You must use an output buffer per socket for next try, but when to watch POLLOUT event?  Incomplete read, what if data arrives byte-by-byte  TCP is a byte stream, use an input buffer to decode  Alternatively, use a state machine, which is more complex  Connection management, Socket lifetime mgmt  File descriptors are small integers, prone to cross talk  Muduo is aware of and well prepared for all above!  Focus on your business logic and let Muduo do the rest 2012/06 www.chenshuo.com
  • 19. Event loop (reactor), the heart of 19 non-blocking network programming  Dispatches IO event to callback functions  Events: socket is readable, writable, error, hang up  Message loop in Win32 programming While (GetMessage(&msg, NULL, 0, 0) > 0) // epoll_wait() { TranslateMessage(&msg); DispatchMessage(&msg); // here’s the beef }  Cooperative multitasking, blocking is unacceptable  Muduo unifies event loop wakeup, timer queue, signal handler all with file read/write  Also make it non-portable 2012/06 www.chenshuo.com
  • 20. 20 2012/06 IO responses are instant, one CPU used www.chenshuo.com Events happen in sequence
  • 21. One event loop with thread pool 21 2012/06 Computational task is heavy, IO is light www.chenshuo.com
  • 22. Any library function that accesses 22 file or network can be blocking  The whole C/Posix library is blocking/synchronous  Disk IO is blocking, use threads to make it cooperating  ‘harmless’ functions could block current thread  gethostbyname() could read /etc/hosts or query DNS  getpwuid() could read /etc/passwd or query NIS*  localtime()/ctime() could read /etc/localtime  Files could be on network mapped file system!  What if this happens in a busy network IO thread?  Server is responseless for seconds, may cause trashing 2012/06 * /var/db/passwd.db or LDAP www.chenshuo.com
  • 23. Non-blocking is a paradigm shift 23  Have to pay the cost if you want to write high performance network application in traditional languages like C/C++/Java  It’s a mature technique for nearly 20 years  Drivers/Adaptors needed for all operations  Non-blocking DNSresolving, UDNS or c-ares  Non-blocking HTTP client/server, curl and microhttpd  Examples provided in muduo and muduo-udns  Non-blocking database query, libpq or libdrizzle  Need drivers to make them work in muduo  Non-blocking logging, in muduo 0.5.0 2012/06 www.chenshuo.com
  • 24. Event loop in multi-core era 24  One loop per thread is usually a good model  Before you try any other fancy ‘pattern’  Muduo supports both single/multi-thread usage  Justassign TcpConnection to any EventLoop, all IO happens in that EventLoop thread  The thread is predictable, EventLoop::runInLoop()  Many other ‘event-driven’ libraries can’t make use of multi-cores, you have to run multiple processes 2012/06 www.chenshuo.com
  • 25. One event loop per thread 25 2012/06 Prioritize connections with threads www.chenshuo.com
  • 26. Hybrid solution, versatile 26 Decode/encode can be in IO thread 2012/06 www.chenshuo.com
  • 27. Object lifetime management 27  Muduo classes are concrete & non-copyable  And have no base class or virtual destructor  EventLoop, TcpServer, TcpClient are all long-live objects. Their ownership is clean, not shared.  TcpConnection is vague  TcpServer may hold all alive connection objects  You may also hold some/all of them for sending data  It’s the only class managed by std::shared_ptr  No ‘delete this’, it’s a joke  muduo will not pass raw pointer to/from www.chenshuo.com 2012/06 client code
  • 28. class EchoServer { // non-copyable public: EchoServer(EventLoop* loop, const InetAddress& listenAddr) : server_(loop, listenAddr, "EchoServer") { server_.setConnectionCallback( boost::bind(&EchoServer::onConnection, this, _1)); server_.setMessageCallback( boost::bind(&EchoServer::onMessage, this, _1, _2, _3)); server_.setThreadNum(numThreads); } private: void onConnection(const TcpConnectionPtr& conn) { // print, you may keep a copy of conn for further usage } void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time) { string data(buf->retrieveAsString()); conn->send(data); } But echo is too simple to be meaningful TcpServer server_; // a member, not base class. More is possible 28 }; 2012/06 www.chenshuo.com
  • 29. Muduo examples, all concurrent 29  Boost.asio chat  Codec , length prefix message encoder/decoder  Google Protocol Buffers codec  Filetransfer  Idle connection/max connection  Hub/Multiplexer  Pinpong/roundtrip  socks4a Business-oriented TCP network programming  Many more Efficient multithreaded network programming 2012/06 www.chenshuo.com
  • 30. Format-less protocol, pure data 30 2012/06 www.chenshuo.com
  • 31. Length header fmt, ‘messages’ 31 2012/06 www.chenshuo.com
  • 32. void onMessage(const muduo::net::TcpConnectionPtr& conn, muduo::net::Buffer* buf, muduo::Timestamp receiveTime) { while (buf->readableBytes() >= kHeaderLen) { // kHeaderLen == 4 const void* data = buf->peek(); int32_t be32 = *static_cast<const int32_t*>(data); // FIXME const int32_t len = muduo::net::sockets::networkToHost32(be32); if (len > 65536 || len < 0) { LOG_ERROR << "Invalid length " << len; conn->shutdown(); } else if (buf->readableBytes() >= len + kHeaderLen) { buf->retrieve(kHeaderLen); std::string message(buf->peek(), len); messageCallback_(conn, message, receiveTime); buf->retrieve(len); } else { break; Any grouping of input data should be decoded correctly } } 0x00, 0x00, 0x00, 0x05, ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, 0x00, } 0x00, 0x00, 0x08, ‘c’, ‘h’, ‘e’, ‘n’, ‘s’, ‘h’, ‘u’, ‘o’ 32 2012/06 www.chenshuo.com
  • 33. Protobuf format, message objects 33 2012/06 www.chenshuo.com http://www.cnblogs.com/Solstice/archive/2011/04/13/2014362.html
  • 34. 34 2012/06 www.chenshuo.com http://www.cnblogs.com/Solstice/archive/2011/04/03/2004458.html
  • 35. Design goals of Muduo 35  Intranet, not Internet.  Distributed system in a global company  Use HTTP on internet, it’s the universal protocol  Build network application with business logic, not writing well-known network server  Not for building high-performance httpd, ntpd, ftpd, webproxy, bind  Components in distributed system  master/chunk-server in GFS  TCP long connections  Muduo thread model is not optimized for short TCP connections, as accept(2) and IO in two loops 2012/06 www.chenshuo.com
  • 36. Muduo is NOT 36 2012/06 www.chenshuo.com
  • 37. Muduo doesn’t 37  Support transport protocols other than TCPv4  IPv6,UDP, Serial port, SNMP, ARP, RARP  Build your own with muduo::Channel class  Any thing that is ‘selectable’ can integrated into Muduo  May support SSL in future, but with low priority  Use https for internet service, use VPN for info security  Support platforms other than Linux 2.6/3.x  Never port to Windows  Unlikely port to FreeBSD, Solaris  However, it runs on ARM9 boards, with Linux 2.6.32 2012/06 www.chenshuo.com
  • 38. List of muduo libraries 38  Muduo The core library  baselibrary (threading, logging, datetime)  network library  Many examples  Muduo-udns Non-blocking DNS resolving  Muduo-protorpc  Asynchronous bidirectional RPC based on Muduo  Also has Java bindings with Netty  Examples: zurg – a master/slaves service mgmt sys  Paxos – a consensus algorithm* (to be written) 2012/06 www.chenshuo.com
  • 39. Check-ins per week 39  From 2010-03 to 2012-06 20 18 16 14 12 10 8 6 4 2 0 2012/06 www.chenshuo.com
  • 40. Q&A 40  Thank you!  www.chenshuo.com  github.com/chenshuo  weibo.com/giantchen  github.com/downloads/chenshuo/documents/ MuduoManual.pdf 2012/06 www.chenshuo.com
  • 41. Bonus Slides 41  Synchronous vs. asynchronous  Basic network performance metrics Simply wrong and misleading 2012/06 www.chenshuo.com
  • 42. Synchronous vs. asynchronous IO 42  Epoll is synchronous  Select/poll/epoll are O(N), but N stands differently  Anything but aio_* are synchronous  Non-blocking IO is synchronous  you call it, it returns. It never breaks/interrupt code flow  The only thing that can be blocking in event-driven program are epoll_wait and pthread_cond_wait  pthread_mutex_lock should almost not real block anything  Asynchronous IO is not practical in Linux  Either simulated with threads,  Or notify with signal, not good for multithreaded app 2012/06 www.chenshuo.com
  • 43. TCP/IP over 1Gb Ethernet 43  Ethernet frame  Raw b/w 125MB/s  Preamble 8B  Packet per second  MAC 12B  Max 1,488,000  Type 2B  Min 81,274 (no jumbo)  Payload 46~1500B  TCP/IP overhead  IPheader 20B  CRC 4B  TCP header 20B  Gap 12B  TCP option 12B (TSopt)  Total 84~1538B  Max TCP throughput 2012/06 112MB/s  81274*(1500-52) www.chenshuo.com
  • 44. PPS vs. throughput 44 PPS vs. MB/s 1400 120 1200 100 1000 80 800 60 600 40 400 20 200 0 0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 kPPS MiB/s 2012/06 www.chenshuo.com
  • 45. Back-of-the-envelope calculation 45  Read 1MB from net, ~10ms  Copy 1MB in memory, ~0.2ms on old E5150  Copying is not a sin, CPU and memory are so fast  Decode byte string to Message objects  500MB/sdecoding in IO thread, pass ptr to calc thr  50MB/s copy data to calc threads, decode there  Compress or not ? 200MB/s 2x ratio 10MB  10Mb ADSL 8s vs. 4.05s  1000Mb LAN 0.08s vs. 0.09s 2012/06 Redo for 10GbE, InfiniBand www.chenshuo.com
  • 46. High Performance ??? 46  Network application in user land  Network service in kernel  TCP/IP stack or network adaptor driver in kernel  Network device (switch/router)  Special purpose OS for network device (firmware)  Special purpose chips for network device (NP)  Control network adaptor with FPGAs  Coding in Verilog, hardwire logic 2012/06 www.chenshuo.com