public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>, kvm@vger.kernel.org
Subject: Re: [PATCH kvm-unit-tests 2/4] Introduce a C++ wrapper for the kvm APIs
Date: Sun, 28 Nov 2010 15:14:17 +0200	[thread overview]
Message-ID: <4CF255A9.6030502@redhat.com> (raw)
In-Reply-To: <20101128114424.GB4499@redhat.com>

On 11/28/2010 01:44 PM, Michael S. Tsirkin wrote:
> On Sun, Nov 28, 2010 at 11:54:26AM +0200, Avi Kivity wrote:
> >  On 11/28/2010 11:50 AM, Michael S. Tsirkin wrote:
> >  >>   >
> >  >>   >Another problem is that there seem to be two memory allocations and a
> >  >>   >copy here, apparently just to simplify error handling.  It might be fine
> >  >>   >for this test but won't scale for when performance matters.
> >  >>
> >  >>   When it matters, we can fix it.  I don't see msr read/write becoming
> >  >>   a hot path.
> >  >
> >  >It will be very painful to fix it.
> >
> >  Why?
>
> Because the API returns a vector.

Returning an object does not involve a copy (return value optimization).

> >  >>
> >  >>   The compiler should optimize it away completely.
> >  >
> >  >Should as opposed to does.  Want me to try a simple test?
> >
> >  Please.
>
> Just for fun: optimize for size, and compare code sizes.
> The C++ code is yours but I have removed all use of STL to make
> it more of an even fight.  I checked by object and executable size.
> Note that this is shared library build: a C++ executable
> will pull in a large C++ library, a C executable won't.
> If you are interested in an STL based example let me know.
> You can take it from here and make it more real if you like,
> or look at the assembler output.
>
> ------------------------------
> [mst@tuck ~]$ cat test.c
> #include<sys/ioctl.h>
> #include<sys/types.h>
> #include<sys/stat.h>
> #include<fcntl.h>
> #include<unistd.h>
> #include<errno.h>
>
> int main(int argc, char **argv)
> {
>          int fd = open("/dev/kvm", O_RDWR);
>          int r;
>          if (fd<  0)
>                  goto open_err;
>          r = ioctl(fd, 0, 0);
>          if (r<  0)
>                  goto ioctl_err;
>          return 0;
> ioctl_err:
>          close(fd);
> open_err:
>          return -1;
> }


This code is not reusable.  Everywhere you use an fd, you have to repeat 
this code.

> [mst@tuck ~]$ gcc -c -Os test.c
> [mst@tuck ~]$ size test.o
>     text    data     bss     dec     hex filename
>       97       0       0      97      61 test.o
> [mst@tuck ~]$ gcc -Os test.c
> [mst@tuck ~]$ size a.out
>     text    data     bss     dec     hex filename
>     1192     260       8    1460     5b4 a.out
> [mst@tuck ~]$ wc -l test.c
> 22 test.c
> ------------------------------
> [mst@tuck ~]$ cat kvmxx.cpp
> extern "C" {
> #include<sys/ioctl.h>
> #include<sys/types.h>
> #include<sys/stat.h>
> #include<fcntl.h>
> #include<unistd.h>
> #include<errno.h>
> }
>
> namespace kvm {
>
>          class fd {
>                  public:
>                          explicit fd(const char *path, int flags);
>                          ~fd() { ::close(_fd); }
>                          long ioctl(unsigned nr, long arg);
>                  private:
>                          int _fd;
>          };
>
> };
>
> namespace kvm {
>
> static long check_error(long r)
> {
>          if (r == -1) {
>                  throw errno;
>          }
>          return r;
> }
>
> fd::fd(const char *device_node, int flags)
>      : _fd(::open(device_node, flags))
> {
>          check_error(_fd);
> }
>
>
> long fd::ioctl(unsigned nr, long arg)
> {
>          return check_error(::ioctl(_fd, nr, arg));
> }
>
> }
>
> int main(int ac, char **av)
> {
>          try {
>                  kvm::fd fd("/dev/kvm", O_RDWR);
>                  fd.ioctl(0, 0);
>          } catch (...) {
>                  return -1;
>          }
>          return 0;
> }

class kvm::fd is reusable, if you embed it in another object you don't 
have to worry about errors any more (as long as the object's methods are 
exception safe).

> [mst@tuck ~]$ g++ -c -Os kvmxx.cpp
> [mst@tuck ~]$ size kvmxx.o
>     text    data     bss     dec     hex filename
>      529       0       0     529     211 kvmxx.o
> [mst@tuck ~]$ g++ -Os kvmxx.cpp
> [mst@tuck ~]$ size a.out
>     text    data     bss     dec     hex filename
>     2254     308      16    2578     a12 a.out
> [mst@tuck ~]$ wc kvmxx.cpp
> 56 kvmxx.cpp
> ------------------------------
>
>
> One interesting thing is that the object size grew
> faster than linked executable size.
> This might mean that the compiler generated some
> dead code that the linker then threw out.
> It's also interesting that C++ managed to use up
> more data/bss storage.

C++ will have much larger data and code sizes because it uses DWARF 
tables for unwinding and generates stack unwinding code.  These are all 
out of the hot path.

> >  >>    There's been a lot
> >  >>   of work in gcc on that.
> >  >>
> >  >>   About compile times, I don't care much.
> >  >
> >  >I do. You will too when we have codebase that can be built as fast as
> >  >we commit things, so buildbot breaks.
> >  >This is common in C++ based projects.
> >
> >  If kvm-unit-tests.git takes to long to compile, I'll be very happy.
>
> If the claim is "it's so small it does not matter what it's written in"
> then I guess don't mind. But then - why do we care about
> error handling so much? For the test, it's probably ok to just assert
> after each ioctl/malloc and be done with it.

Yes, all the correctness is more or less pointless here.  Like I said, 
this is an experiment to see what things look like.  I guess each side 
will it as proving its claims.

-- 
error compiling committee.c: too many arguments to function


  reply	other threads:[~2010-11-28 13:14 UTC|newest]

Thread overview: 130+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-24 10:52 [PATCH kvm-unit-tests 0/4] API test framework Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 1/4] Makefile: add support for C++ Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 2/4] Introduce a C++ wrapper for the kvm APIs Avi Kivity
2010-11-24 12:59   ` Alexander Graf
2010-11-24 13:33     ` Gleb Natapov
2010-11-24 14:18     ` Anthony Liguori
2010-11-24 14:24       ` Anthony Liguori
2010-11-24 14:32       ` Avi Kivity
2010-11-24 14:31     ` Avi Kivity
2010-11-24 14:41     ` Anthony Liguori
2010-11-24 15:40       ` Gleb Natapov
2010-11-24 15:50         ` Anthony Liguori
2010-11-24 16:10           ` Avi Kivity
2010-12-02 13:52             ` Mike Day
2010-11-24 16:12           ` [PATCH kvm-unit-tests 2/4] " Gleb Natapov
2010-11-24 16:14             ` Avi Kivity
2010-11-24 16:21               ` Gleb Natapov
2010-11-24 16:25                 ` Avi Kivity
2010-11-24 16:29                   ` Gleb Natapov
2010-11-24 16:33                     ` Avi Kivity
2010-11-24 16:52                       ` Gleb Natapov
2010-11-24 16:56                         ` Avi Kivity
2010-11-24 17:02                           ` Gleb Natapov
2010-11-24 17:07                             ` Avi Kivity
2010-11-24 17:10                               ` Jes Sorensen
2010-11-24 17:12                                 ` Avi Kivity
2010-11-24 17:14                               ` Anthony Liguori
2010-11-24 16:43                 ` Anthony Liguori
2010-11-24 16:48                   ` Gleb Natapov
2010-11-24 16:56                     ` Anthony Liguori
2010-11-24 17:04                       ` Gleb Natapov
2010-11-24 16:40             ` Anthony Liguori
2010-11-24 17:33               ` Gleb Natapov
2010-11-24 17:39                 ` Avi Kivity
2010-11-24 17:41                   ` Gleb Natapov
2010-11-24 17:50                     ` Avi Kivity
2010-11-24 18:23                       ` Gleb Natapov
2010-11-24 18:50                         ` Avi Kivity
2010-11-24 18:17                 ` Anthony Liguori
2010-11-24 18:34                   ` Gleb Natapov
2010-11-24 18:53                     ` Anthony Liguori
2010-11-25  8:35                       ` Gleb Natapov
2010-11-24 16:40           ` Jes Sorensen
2010-11-24 16:47             ` Avi Kivity
2010-11-24 16:51               ` Jes Sorensen
2010-11-24 16:57                 ` Anthony Liguori
2010-11-24 17:29                   ` Avi Kivity
2010-11-24 16:59                 ` Avi Kivity
2010-11-24 17:06                   ` Jes Sorensen
2010-11-24 17:11                     ` Avi Kivity
2010-11-24 17:17                       ` Jes Sorensen
2010-11-24 17:25                         ` Avi Kivity
2010-11-24 17:28                           ` Jes Sorensen
2010-11-24 17:31                             ` Avi Kivity
2010-11-24 17:36                           ` Gleb Natapov
2010-11-24 17:41                             ` Avi Kivity
2010-11-24 17:27                         ` Anthony Liguori
2010-11-24 17:35                           ` Avi Kivity
2010-11-24 17:36                           ` Jes Sorensen
2010-11-24 17:41                             ` Avi Kivity
2010-11-24 17:43                               ` Gleb Natapov
2010-11-24 17:50                                 ` Avi Kivity
2010-11-24 18:10                                   ` Gleb Natapov
2010-11-24 18:55                                     ` Avi Kivity
2010-11-24 19:29                                       ` Jes Sorensen
2010-11-24 19:33                                         ` Avi Kivity
2010-11-24 17:43                               ` Jes Sorensen
2010-11-24 17:51                                 ` Avi Kivity
2010-11-24 17:43                             ` Anthony Liguori
2010-11-24 17:45                               ` Jes Sorensen
2010-11-24 17:51                                 ` Avi Kivity
2010-11-24 18:01                                 ` Anthony Liguori
2010-11-24 18:56                                   ` Avi Kivity
2010-11-24 16:55               ` Gleb Natapov
2010-11-24 17:01                 ` Avi Kivity
2010-11-24 17:16                   ` Gleb Natapov
2010-11-24 17:26                     ` Avi Kivity
2010-11-24 16:53             ` Anthony Liguori
2010-11-24 17:03               ` Jes Sorensen
2010-11-28 12:27       ` Michael S. Tsirkin
2010-11-28 22:04         ` Anthony Liguori
2010-11-28 22:28           ` Michael S. Tsirkin
2010-11-28 23:13             ` Anthony Liguori
2010-11-29  8:04               ` Michael S. Tsirkin
2010-11-29 13:44                 ` Anthony Liguori
2010-11-29 13:48                   ` Avi Kivity
2010-11-24 16:29     ` Jes Sorensen
2010-11-24 16:34       ` Avi Kivity
2010-11-24 16:44         ` Jes Sorensen
2010-11-24 16:49           ` Avi Kivity
2010-11-28 11:59     ` Michael S. Tsirkin
2010-11-28 13:02       ` Avi Kivity
2010-11-28 13:57         ` Michael S. Tsirkin
2010-11-28 14:34           ` Avi Kivity
2010-11-28 16:57             ` Michael S. Tsirkin
2010-11-29  9:22               ` Avi Kivity
2010-11-29 10:47                 ` Michael S. Tsirkin
2010-11-29 10:52                   ` Avi Kivity
2010-11-29 11:26                     ` Michael S. Tsirkin
2010-11-29 13:38                       ` Anthony Liguori
2010-11-24 14:10   ` Anthony Liguori
2010-11-24 14:29     ` Avi Kivity
2010-11-24 14:45       ` Anthony Liguori
2010-11-24 14:53         ` Avi Kivity
2010-11-24 14:55           ` Anthony Liguori
2010-11-25 16:32       ` Avi Kivity
2010-11-26 10:16   ` Michael S. Tsirkin
     [not found]     ` <4CF0CB9A.5060403@redhat.com>
2010-11-28  8:58       ` Michael S. Tsirkin
2010-11-28  9:31         ` Avi Kivity
2010-11-28  9:50           ` Michael S. Tsirkin
2010-11-28  9:54             ` Avi Kivity
2010-11-28 11:44               ` Michael S. Tsirkin
2010-11-28 13:14                 ` Avi Kivity [this message]
2010-11-28 14:40                   ` Michael S. Tsirkin
2010-11-28 22:12                     ` Anthony Liguori
2010-11-29  9:30                     ` Avi Kivity
2010-11-28 11:49   ` Michael S. Tsirkin
2010-11-28 13:15     ` Avi Kivity
2010-11-28 14:49       ` Michael S. Tsirkin
2010-11-29  9:30         ` Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 3/4] Add support for calling a function in guest mode Avi Kivity
2010-11-26 14:17   ` Michael S. Tsirkin
     [not found]     ` <4CF0CC26.8030407@redhat.com>
2010-11-28  8:59       ` Michael S. Tsirkin
2010-11-28  9:22         ` Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 4/4] Add sample test using the api test harness Avi Kivity
2010-11-26 14:17   ` Michael S. Tsirkin
     [not found]     ` <4CF0CC4A.8070100@redhat.com>
2010-11-28  9:04       ` Michael S. Tsirkin
2010-11-28  9:21         ` Avi Kivity
2010-11-29 16:09 ` [PATCH kvm-unit-tests 0/4] API test framework Marcelo Tosatti
2010-12-01 10:38   ` Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4CF255A9.6030502@redhat.com \
    --to=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox