File: eventloop.html

package info (click to toggle)
libvirt 3.0.0-4%2Bdeb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,536 kB
  • sloc: ansic: 536,027; xml: 118,597; sh: 9,608; makefile: 5,399; perl: 3,888; python: 3,838; ml: 468; sed: 16
file content (149 lines) | stat: -rw-r--r-- 7,332 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
        This file is autogenerated from internals/eventloop.html.in
        Do not edit this file. Changes will be lost.
      -->
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="../main.css" />
    <link rel="SHORTCUT ICON" href="../32favicon.png" />
    <title>libvirt: Libvirt's event loop</title>
    <meta name="description" content="libvirt, virtualization, virtualization API" />
  </head>
  <body>
    <div id="body">
      <div id="content">
        <h1>Libvirt's event loop</h1>
        <ul><li>
            <a href="#event_loop">Event driven programming</a>
          </li><li>
            <a href="#api">The event loop API</a>
          </li><li>
            <a href="#worker_pool">Worker pool</a>
          </li></ul>
        <p>
      This page describes the event loop approach used in
      libvirt. Both server and client.
    </p>
        <h2>
          <a name="event_loop" shape="rect" id="event_loop">Event driven programming</a>
          <a class="headerlink" href="#event_loop" title="Permalink to this headline">¶</a>
        </h2>
        <p>Traditionally, a program simply ran once, then terminated.
    This type of program was very common in the early days of
    computing, and lacked any form of user interactivity. This is
    still used frequently, particularly in small one purpose
    programs.</p>
        <p>However, that approach is not suitable for all the types
    of applications. For instance graphical applications spend
    most of their run time waiting for an input from user. Only
    after it happened (in our example a button was clicked, a key
    pressed, etc.) an event is generated to which they respond
    by executing desired function. If generalized, this is how
    many long running programs (daemons) work. Even those who are
    not waiting for direct user input and have no graphical
    interface. Such as Libvirt.</p>
        <img alt="event loop" src="http://libvirt.org/git/?p=libvirt-media.git;a=blob_plain;f=png/event_loop_simple.png;hb=HEAD" />
        <p>In Libvirt this approach is used in combination with
    <code>poll(2)</code> as all the communication with its
    clients (and domains it manages too) happens through sockets.
    Therefore whenever new client connects, it is given exclusive
    file descriptor which is then watched for incoming events,
    e.g. messages. </p>
        <h2>
          <a name="api" shape="rect" id="api">The event loop API</a>
          <a class="headerlink" href="#api" title="Permalink to this headline">¶</a>
        </h2>
        <p>To work with event loop from our code we have plenty of
    APIs.</p>
        <ul><li><code>virEventAddHandle</code>: Registers a
        callback for monitoring file handle events.</li><li><code>virEventUpdateHandle</code>: Change set of events
        monitored file handle is being watched for.</li><li><code>virEventRemoveHandle</code>: Unregisters
        previously registered file handle so that it is no
        longer monitored for any events.</li><li><code>virEventAddTimeout</code>: Registers a
        callback for timer event.</li><li><code>virEventUpdateTimeout</code>: Changes frequency
        for a timer.</li><li><code>virEventRemoveTimeout</code>: Unregisters
        a timer.</li></ul>
        <p>For more information on these APIs continue reading <a href="../html/libvirt-libvirt-event.html" shape="rect">here</a>.</p>
        <h2>
          <a name="worker_pool" shape="rect" id="worker_pool">Worker pool</a>
          <a class="headerlink" href="#worker_pool" title="Permalink to this headline">¶</a>
        </h2>
        <p>Looking back at the image above we can see one big
    limitation. While processing a message event loop is blocked
    and for an outside observer unresponsive. This is not
    acceptable for Libvirt. Therefore we have came up with the
    following solution.</p>
        <img alt="event loop" src="http://libvirt.org/git/?p=libvirt-media.git;a=blob_plain;f=png/event_loop_worker.png;hb=HEAD" />
        <p>The event loop does only necessary minimum and hand over
    message processing to another thread. In fact, there can be
    as many processing threads as configured increasing
    processing power.</p>
        <p>To break this high level description into smaller pieces,
    here is what happens when user calls an API:</p>
        <ol><li>User (or management application) calls a Libvirt API.
      Depending on the connection URI, this may or may not
      involve server. Well, for the sake of our
      demonstration we assume the former.</li><li>Remote driver encodes the API among it's arguments
      into an <a href="rpc.html" shape="rect">RPC message</a> and sends
      it to the server.</li><li>Here, server is waiting in <code>poll(2)</code> for
      an event, like incoming message.</li><li>As soon as the first bytes of message are received,
      even loop wakes up and server starts reading the
      whole message.</li><li>Once fully read, the event loop notifies threads
      known as worker threads from which one picks the incoming
      message, decodes and process it.</li><li>As soon as API execution is finished, a reply is sent
      to the client.</li></ol>
        <p>In case that there's no free worker to process an incoming
    message in step 5, message is placed at the end of a message
    queue and is processed in next iteration.</p>
      </div>
    </div>
    <div id="nav">
      <div id="home">
        <a href="../index.html">Home</a>
      </div>
      <div id="jumplinks">
        <ul><li>
            <a href="../downloads.html">Download</a>
          </li><li>
            <a href="../contribute.html">Contribute</a>
          </li><li>
            <a href="../docs.html">Learn</a>
          </li></ul>
      </div>
      <div id="search">
        <form action="../search.php" enctype="application/x-www-form-urlencoded" method="get"><div>
            <input name="query" type="text" size="12" value="" />
            <input name="submit" type="submit" value="Go" />
          </div></form>
      </div>
    </div>
    <div id="footer">
      <div id="contact">
        <h3>Contact</h3>
        <ul><li>
            <a href="../contact.html#email">email</a>
          </li><li>
            <a href="../contact.html#irc">irc</a>
          </li></ul>
      </div>
      <div id="community">
        <h3>Community</h3>
        <ul><li>
            <a href="https://twitter.com/hashtag/libvirt">twitter</a>
          </li><li>
            <a href="https://plus.google.com/communities/109522598353007505282">google+</a>
          </li><li>
            <a href="http://stackoverflow.com/questions/tagged/libvirt">stackoverflow</a>
          </li><li>
            <a href="http://serverfault.com/questions/tagged/libvirt">serverfault</a>
          </li></ul>
      </div>
      <div id="conduct">
            Participants in the libvirt project agree to abide by <a href="../governance.html#codeofconduct">the project code of conduct</a></div>
      <br class="clear" />
    </div>
  </body>
</html>