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.

Reviewed-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Avi Kivity 2011-06-20 11:12:47 +03:00 committed by Anthony Liguori
parent 9319a6d3a6
commit f8e378f27f
1 changed files with 10 additions and 3 deletions

View File

@ -2346,15 +2346,19 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
uint32_t v; uint32_t v;
int y, x; int y, x;
uint8_t r, g, b; uint8_t r, g, b;
int ret;
char *linebuf, *pbuf;
f = fopen(filename, "wb"); f = fopen(filename, "wb");
if (!f) if (!f)
return -1; return -1;
fprintf(f, "P6\n%d %d\n%d\n", fprintf(f, "P6\n%d %d\n%d\n",
ds->width, ds->height, 255); ds->width, ds->height, 255);
linebuf = qemu_malloc(ds->width * 3);
d1 = ds->data; d1 = ds->data;
for(y = 0; y < ds->height; y++) { for(y = 0; y < ds->height; y++) {
d = d1; d = d1;
pbuf = linebuf;
for(x = 0; x < ds->width; x++) { for(x = 0; x < ds->width; x++) {
if (ds->pf.bits_per_pixel == 32) if (ds->pf.bits_per_pixel == 32)
v = *(uint32_t *)d; v = *(uint32_t *)d;
@ -2366,13 +2370,16 @@ int ppm_save(const char *filename, struct DisplaySurface *ds)
(ds->pf.gmax + 1); (ds->pf.gmax + 1);
b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 / b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
(ds->pf.bmax + 1); (ds->pf.bmax + 1);
fputc(r, f); *pbuf++ = r;
fputc(g, f); *pbuf++ = g;
fputc(b, f); *pbuf++ = b;
d += ds->pf.bytes_per_pixel; d += ds->pf.bytes_per_pixel;
} }
d1 += ds->linesize; d1 += ds->linesize;
ret = fwrite(linebuf, 1, pbuf - linebuf, f);
(void)ret;
} }
qemu_free(linebuf);
fclose(f); fclose(f);
return 0; return 0;
} }