Skip to contentSkip to conversation

Linux SIGIO handling changes

Qemu-devel New Anders Melchiorsen 4 participants 9 messages Apr 20, 2008 – Apr 22, 2008

lore
Share & tools
lore
#0 lore raw

I am resending this patch, split into two parts now.

This cleans up SIGIO handling to improve latency:
- SIGALRM for alarm timers
- enable SIGIO on qemu_set_fd_handler2()

The issue was found in KVM, where it is much more visible, because there is no periodic timer. However, it has been confirmed (by Aurelien Jarno) that even for qemu, this approach "improves network transfers in a huge way".

Please apply, or give a firm rejection so I can stop resending.

Cheers,
Anders.

#1 lore raw

PATCH 1/1 Use SIGALRM even for timers delivered over a fd

This ensures that all timers use the same signal, no matter how the underlying timer is implemented. It also frees up SIGIO for real I/O events.

The patch is only relevant for Linux, hpet and rtc timers.

vl.c +5 -4
index 78486cf..ad4f6ef 100644
@@ -1241,7 +1241,7 @@ static uint64_t qemu_next_deadline(void)
 
 #define RTC_FREQ 1024
 
-static void enable_sigio_timer(int fd)
+static void enable_sigalrm(int fd)
 {
     struct sigaction act;
 
@@ -1250,8 +1250,9 @@ static void enable_sigio_timer(int fd)
     act.sa_flags = 0;
     act.sa_handler = host_alarm_handler;
 
-    sigaction(SIGIO, &act, NULL);
+    sigaction(SIGALRM, &act, NULL);
     fcntl(fd, F_SETFL, O_ASYNC);
+    fcntl(fd, F_SETSIG, SIGALRM);
     fcntl(fd, F_SETOWN, getpid());
 }
 
@@ -1288,7 +1289,7 @@ static int hpet_start_timer(struct qemu_alarm_timer *t)
     if (r < 0)
         goto fail;
 
-    enable_sigio_timer(fd);
+    enable_sigalrm(fd);
     t->priv = (void *)(long)fd;
 
     return 0;
@@ -1326,7 +1327,7 @@ static int rtc_start_timer(struct qemu_alarm_timer *t)
         return -1;
     }
 
-    enable_sigio_timer(rtc_fd);
+    enable_sigalrm(rtc_fd);
 
     t->priv = (void *)(long)rtc_fd;
 
Signature · 1 line
1.5.2.5
#2 lore raw

PATCH 1/1 Use SIGIO in Linux host

Network packets coming over TAP have a latency that is dictated by the periodic timer. That can hurt performance.

This patch activates signals for fds that are being used with select(), giving predictable latency.

vl.c +36 -0
index ad4f6ef..eb2a75e 100644
@@ -1149,6 +1149,25 @@ static int timer_load(QEMUFile *f, void *opaque, int version_id)
     return 0;
 }
 
+#if defined(__linux__)
+static void host_io_handler(int host_signum)
+{
+    CPUState *env = next_cpu;
+
+    if (env) {
+        /* stop the currently executing cpu because io occured */
+        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
+#ifdef USE_KQEMU
+        if (env->kqemu_enabled) {
+            kqemu_cpu_interrupt(env);
+        }
+#endif
+    }
+
+    event_pending = 1;
+}
+#endif
+
 #ifdef _WIN32
 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
                                  DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
@@ -1241,6 +1260,19 @@ static uint64_t qemu_next_deadline(void)
 
 #define RTC_FREQ 1024
 
+static void enable_sigio(int fd)
+{
+    struct sigaction act;
+
+    sigfillset(&act.sa_mask);
+    act.sa_flags = 0;
+    act.sa_handler = host_io_handler;
+
+    sigaction(SIGIO, &act, NULL);
+    fcntl(fd, F_SETFL, O_ASYNC|O_NONBLOCK);
+    fcntl(fd, F_SETOWN, getpid());
+}
+
 static void enable_sigalrm(int fd)
 {
     struct sigaction act;
@@ -5530,6 +5562,10 @@ int qemu_set_fd_handler2(int fd,
             return -1;
         ioh->next = first_io_handler;
         first_io_handler = ioh;
+#if defined(__linux__)
+        enable_sigio(fd);
+#endif
+
     found:
         ioh->fd = fd;
         ioh->fd_read_poll = fd_read_poll;
Signature · 1 line
1.5.2.5
#3 lore raw
In reply to Anders Melchiorsen
Quoted 6 lines · Anders Melchiorsen

I am resending this patch, split into two parts now.

This cleans up SIGIO handling to improve latency:
- SIGALRM for alarm timers
- enable SIGIO on qemu_set_fd_handler2()

Quoted 7 lines

The issue was found in KVM, where it is much more visible, because there is no periodic timer. However, it has been confirmed (by Aurelien Jarno) that even for qemu, this approach "improves network transfers in a huge way".

Please apply, or give a firm rejection so I can stop resending.

Probably the right thing to do is the direction KVM is moving toward, i.e. have a separate IO thread.

Setting SIGIO on every file descriptor is really just a hack to break out of the cpu exec loop. It's unclear to me whether it's really always the right thing to do for every file descriptor.

Regards,

Anthony Liguori

Quoted 6 lines

Cheers,
Anders.

#4 lore raw
Quoted 12 lines · Anthony Liguori
Quoted 11 lines · Anders Melchiorsen

I am resending this patch, split into two parts now.

This cleans up SIGIO handling to improve latency:
- SIGALRM for alarm timers
- enable SIGIO on qemu_set_fd_handler2()

The issue was found in KVM, where it is much more visible, because there is no periodic timer. However, it has been confirmed (by Aurelien Jarno) that even for qemu, this approach "improves network transfers in a huge way".

Quoted 7 lines
Quoted 2 lines

Please apply, or give a firm rejection so I can stop resending.

Probably the right thing to do is the direction KVM is moving toward, i.e. have a separate IO thread.

Setting SIGIO on every file descriptor is really just a hack to break out of the cpu exec loop. It's unclear to me whether it's really always the right thing to do for every file descriptor.

Even with a separate iothread one needs the signals, as there is no other race-free way to poll for both aio completions and fd readiness.

[well, pselect works, but I dislike it and it isn't present on all kernels]

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Anthony Liguori
Quoted 13 lines · Anders Melchiorsen

I am resending this patch, split into two parts now.

This cleans up SIGIO handling to improve latency:
- SIGALRM for alarm timers
- enable SIGIO on qemu_set_fd_handler2()

The issue was found in KVM, where it is much more visible, because there is no periodic timer. However, it has been confirmed (by Aurelien Jarno) that even for qemu, this approach "improves network transfers in a huge way".

Please apply, or give a firm rejection so I can stop resending.

Quoted 6 lines
Quoted 4 lines

Probably the right thing to do is the direction KVM is moving toward, i.e. have a separate IO thread.

Setting SIGIO on every file descriptor is really just a hack to break out of the cpu exec loop. It's unclear to me whether it's really always the right thing to do for every file descriptor.

Even with a separate iothread one needs the signals, as there is no other race-free way to poll for both aio completions and fd readiness.

Unless you emulate signalfd() using a thread. FWIW, I've been thinking of implementing something similar to posix-aio (using a thread-pool for AIO) for QEMU to get a bit more control for this sort of thing.

posix-aio is a pretty unfortunate interface as it doesn't provide a mechanism to do asynchronous fdatasync() nor individual vector requests.

Regards,

Anthony Liguori

Regards,

Anthony Liguori

Quoted 2 lines

[well, pselect works, but I dislike it and it isn't present on all kernels]

#6 lore raw
Quoted 2 lines

posix-aio is a pretty unfortunate interface as it doesn't provide a mechanism to do asynchronous fdatasync()

Isn't that what aio_fsync(O_DSYNC) does?

Paul

#7 lore raw
Quoted 6 lines · Paul Brook
Quoted 3 lines

posix-aio is a pretty unfortunate interface as it doesn't provide a mechanism to do asynchronous fdatasync()

Isn't that what aio_fsync(O_DSYNC) does?

Indeed. So it's really just the lack of vectored operations and the fact that we limit it to a single thread.

Regards,

Anthony Liguori

Quoted 2 lines

Paul

#8 lore raw
In reply to Anthony Liguori
Quoted 5 lines · Anthony Liguori
Quoted 2 lines

Even with a separate iothread one needs the signals, as there is no other race-free way to poll for both aio completions and fd readiness.

Unless you emulate signalfd() using a thread. FWIW, I've been thinking of implementing something similar to posix-aio (using a thread-pool for AIO) for QEMU to get a bit more control for this sort of thing.

Yes, of course. It's still a separate thread, but as it's hidden from the main logic, that's no problem.

Signature · 1 line
error compiling committee.c: too many arguments to function