Skip to contentSkip to conversation

Optimize screendump

PATCH Applied Avi Kivity 6 participants 7 messages Jun 20, 2011 – Jun 26, 2011

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH v2 1/1 Optimize screendump

When running kvm-autotest, fputc() is often the second highest (sometimes #1) function showing up in a profile. This is due to fputc() locking the file for every byte written.

Optimize by buffering a line's worth of pixels and writing that out in a single call.

---

v2: drop unportable fputc_unlocked

 hw/vga.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
hw/vga.c +10 -3
index d5bc582..97c96bf 100644
@@ -2349,15 +2349,19 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
     uint32_t v;
     int y, x;
     uint8_t r, g, b;
+    int ret;
+    char *linebuf, *pbuf;
 
     f = fopen(filename, "wb");
     if (!f)
         return -1;
     fprintf(f, "P6\n%d %d\n%d\n",
             ds->width, ds->height, 255);
+    linebuf = qemu_malloc(ds->width * 3);
     d1 = ds->data;
     for(y = 0; y < ds->height; y++) {
         d = d1;
+        pbuf = linebuf;
         for(x = 0; x < ds->width; x++) {
             if (ds->pf.bits_per_pixel == 32)
                 v = *(uint32_t *)d;
@@ -2369,13 +2373,16 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                 (ds->pf.gmax + 1);
             b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
                 (ds->pf.bmax + 1);
-            fputc(r, f);
-            fputc(g, f);
-            fputc(b, f);
+            *pbuf++ = r;
+            *pbuf++ = g;
+            *pbuf++ = b;
             d += ds->pf.bytes_per_pixel;
         }
         d1 += ds->linesize;
+        ret = fwrite(linebuf, 1, pbuf - linebuf, f);
+        (void)ret;
     }
+    qemu_free(linebuf);
     fclose(f);
     return 0;
 }
Signature · 1 line
1.7.5.3
#1 lore raw
Quoted 49 lines · Avi Kivity

When running kvm-autotest, fputc() is often the second highest (sometimes #1) function showing up in a profile. This is due to fputc() locking the file for every byte written.

Optimize by buffering a line's worth of pixels and writing that out in a single call.

---

v2: drop unportable fputc_unlocked

 hw/vga.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
hw/vga.c +7 -3
index d5bc582..97c96bf 100644
@@ -2349,15 +2349,19 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
     uint32_t v;
     int y, x;
     uint8_t r, g, b;
+    int ret;
+    char *linebuf, *pbuf;
 
     f = fopen(filename, "wb");
     if (!f)
         return -1;
     fprintf(f, "P6\n%d %d\n%d\n",
             ds->width, ds->height, 255);
+    linebuf = qemu_malloc(ds->width * 3);
     d1 = ds->data;
     for(y = 0; y < ds->height; y++) {
         d = d1;
+        pbuf = linebuf;
         for(x = 0; x < ds->width; x++) {
             if (ds->pf.bits_per_pixel == 32)
                 v = *(uint32_t *)d;
@@ -2369,13 +2373,16 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                 (ds->pf.gmax + 1);
             b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
                 (ds->pf.bmax + 1);
-            fputc(r, f);
-            fputc(g, f);
-            fputc(b, f);
+            *pbuf++ = r;
+            *pbuf++ = g;
+            *pbuf++ = b;

hw/vga.c int ppm_save(const char *filename, struct DisplaySurface *ds) · line 2388

Quoted 10 lines
hw/vga.c +3 -0
             d += ds->pf.bytes_per_pixel;
         }
         d1 += ds->linesize;
+        ret = fwrite(linebuf, 1, pbuf - linebuf, f);
+        (void)ret;
     }
+    qemu_free(linebuf);
     fclose(f);
     return 0;
 }

Unrelated to this patch, but why is this function located in vga.c and not in console.c?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#2 lore raw
Quoted 6 lines · Jan Kiszka
Quoted 3 lines

--- a/hw/vga.c
+++ b/hw/vga.c
@@ -2349,15 +2349,19 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)

Unrelated to this patch, but why is this function located in vga.c and not in console.c?

It's located in omap_lcdc.c as well. But it needs to be fully generalized to be moved out (handle all PixelFormats).

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Am 20.06.2011 um 15:11 schrieb Avi Kivity:

Quoted 10 lines · Jan Kiszka
Quoted 7 lines
Quoted 3 lines

--- a/hw/vga.c
+++ b/hw/vga.c
@@ -2349,15 +2349,19 @@ int ppm_save(const char *filename,

struct DisplaySurface *ds)

Unrelated to this patch, but why is this function located in vga.c and not in console.c?

It's located in omap_lcdc.c as well. But it needs to be fully generalized to be moved out (handle all PixelFormats).

For the record, there's a similar function in tcx.c as well, and I have one coming in ibm8514.c.

Andreas

#4 lore raw
Quoted 9 lines · Andreas Färber

Am 20.06.2011 um 15:11 schrieb Avi Kivity:

Quoted 7 lines · Jan Kiszka
Quoted 5 lines
Quoted 5 lines

 --- a/hw/vga.c
 +++ b/hw/vga.c
 @@ -2349,15 +2349,19 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Unrelated to this patch, but why is this function located in vga.c and not in console.c?

It's located in omap_lcdc.c  as well.  But it needs to be fully generalized to be moved out (handle all PixelFormats).

For the record, there's a similar function in tcx.c as well, and I have one coming in ibm8514.c.

The screen dumpers generate their output based on the current state of the graphics card and the VRAM, this is why they are device specific.

A generic screen dumper (if possible) would read the data from display surface. Maybe this should be done at the SDL/VNC/Spice/curses/dummy level, but the output shouldn't change depending on the back end in question.

Reviewed-by on v2 1/1
#5 lore raw
In reply to Avi Kivity
Quoted 7 lines · Avi Kivity

When running kvm-autotest, fputc() is often the second highest (sometimes #1) function showing up in a profile.  This is due to fputc() locking the file for every byte written.

Optimize by buffering a line's worth of pixels and writing that out in a single call.

Quoted 7 lines

---

v2: drop unportable fputc_unlocked

 hw/vga.c |   13 ++++++++++---

 1 files changed, 10 insertions(+), 3 deletions(-)
#6 lore raw
In reply to Avi Kivity
Quoted 8 lines · Avi Kivity

When running kvm-autotest, fputc() is often the second highest (sometimes #1) function showing up in a profile. This is due to fputc() locking the file for every byte written.

Optimize by buffering a line's worth of pixels and writing that out in a single call.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 51 lines

---

v2: drop unportable fputc_unlocked

  hw/vga.c |   13 ++++++++++---
  1 files changed, 10 insertions(+), 3 deletions(-)
hw/vga.c +10 -3
index d5bc582..97c96bf 100644
@@ -2349,15 +2349,19 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
      uint32_t v;
      int y, x;
      uint8_t r, g, b;
+    int ret;
+    char *linebuf, *pbuf;
 
      f = fopen(filename, "wb");
      if (!f)
          return -1;
      fprintf(f, "P6\n%d %d\n%d\n",
              ds->width, ds->height, 255);
+    linebuf = qemu_malloc(ds->width * 3);
      d1 = ds->data;
      for(y = 0; y<  ds->height; y++) {
          d = d1;
+        pbuf = linebuf;
          for(x = 0; x<  ds->width; x++) {
              if (ds->pf.bits_per_pixel == 32)
                  v = *(uint32_t *)d;
@@ -2369,13 +2373,16 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
                  (ds->pf.gmax + 1);
              b = ((v>>  ds->pf.bshift)&  ds->pf.bmax) * 256 /
                  (ds->pf.bmax + 1);
-            fputc(r, f);
-            fputc(g, f);
-            fputc(b, f);
+            *pbuf++ = r;
+            *pbuf++ = g;
+            *pbuf++ = b;
              d += ds->pf.bytes_per_pixel;
          }
          d1 += ds->linesize;
+        ret = fwrite(linebuf, 1, pbuf - linebuf, f);
+        (void)ret;
      }
+    qemu_free(linebuf);
      fclose(f);
      return 0;
  }