Skip to contentSkip to conversation

Optimize screendump

PATCH Superseded Avi Kivity 5 participants 8 messages Jun 19, 2011 – Jun 20, 2011

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 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 using fputc_unlocked(). Since the file is local to the caller, clearly no locking is needed. According to the manual, _GNU_SOURCE is all that's needed for the function to be present.

---
 hw/vga.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/vga.c +3 -3
index d5bc582..8b63358 100644
@@ -2369,9 +2369,9 @@ 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);
+            fputc_unlocked(r, f);
+            fputc_unlocked(g, f);
+            fputc_unlocked(b, f);
             d += ds->pf.bytes_per_pixel;
         }
         d1 += ds->linesize;
Signature · 1 line
1.7.5.3
#1 lore raw

On Sun, 19 Jun 2011, Avi Kivity wrote:

CONFORMING TO
The four functions getc_unlocked(), getchar_unlocked(),
putc_unlocked(), putchar_unlocked() are in POSIX.1-2001. The non-standard *_unlocked() variants occur on a few Unix systems, and are available in recent glibc. They should probably not be used.

Quoted 31 lines

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 using fputc_unlocked(). Since the file is local to the caller, clearly no locking is needed. According to the manual, _GNU_SOURCE is all that's needed for the function to be present.

---
 hw/vga.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/vga.c +3 -3
index d5bc582..8b63358 100644
@@ -2369,9 +2369,9 @@ 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);
+            fputc_unlocked(r, f);
+            fputc_unlocked(g, f);
+            fputc_unlocked(b, f);
             d += ds->pf.bytes_per_pixel;
         }
         d1 += ds->linesize;
Signature · 1 line
mailto:[email protected]
#2 lore raw
In reply to Avi Kivity
Quoted 4 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.

Quoted 8 lines

Optimize by using fputc_unlocked().  Since the file is local to the caller, clearly no locking is needed.  According to the manual, _GNU_SOURCE is all that's needed for the function to be present.

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

 1 files changed, 3 insertions(+), 3 deletions(-)

"The nonstandard *_unlocked() variants occur on a few Unix systems, and are available in recent glibc. They should probably not be used."

I wonder if this will break non-Linux platforms. Perhaps buffer an entire row of pixels instead and only fwrite(3) at the end of the outer loop.

Stefan

author
#3 lore raw
Quoted 3 lines · Stefan Hajnoczi

I wonder if this will break non-Linux platforms. Perhaps buffer an entire row of pixels instead and only fwrite(3) at the end of the outer loop.

That's how I wrote this in the first place. Since the consensus is against these functions, I'll submit that version instead.

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

Am 19.06.2011 um 17:46 schrieb Avi Kivity:

Quoted 6 lines · Stefan Hajnoczi
Quoted 3 lines

I wonder if this will break non-Linux platforms. Perhaps buffer an entire row of pixels instead and only fwrite(3) at the end of the outer loop.

That's how I wrote this in the first place. Since the consensus is against these functions, I'll submit that version instead.

Maybe add a qemu_fputc_unlocked() and do a configure check for it?

Andreas

author
#5 lore raw
Quoted 10 lines · Andreas Färber

Am 19.06.2011 um 17:46 schrieb Avi Kivity:

Quoted 6 lines · Stefan Hajnoczi
Quoted 3 lines

I wonder if this will break non-Linux platforms. Perhaps buffer an entire row of pixels instead and only fwrite(3) at the end of the outer loop.

That's how I wrote this in the first place. Since the consensus is against these functions, I'll submit that version instead.

Maybe add a qemu_fputc_unlocked() and do a configure check for it?

Good idea. I'll try that, unless people disagree.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Andreas Färber

Am 19.06.2011 um 17:46 schrieb Avi Kivity:

Quoted 5 lines · Stefan Hajnoczi
Quoted 3 lines

I wonder if this will break non-Linux platforms. Perhaps buffer an entire row of pixels instead and only fwrite(3) at the end of the outer loop.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That's how I wrote this in the first place. Since the consensus is against these functions, I'll submit that version instead.

Maybe add a qemu_fputc_unlocked() and do a configure check for it?

Good idea. I'll try that, unless people disagree.

Writing by row should be faster and pretty straight forward, no?

Alex

author
#7 lore raw
Quoted 10 lines · Alexander Graf
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Andreas Färber

Am 19.06.2011 um 17:46 schrieb Avi Kivity:

Quoted 5 lines · Stefan Hajnoczi
Quoted 3 lines

I wonder if this will break non-Linux platforms. Perhaps buffer an entire row of pixels instead and only fwrite(3) at the end of the outer loop.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That's how I wrote this in the first place. Since the consensus is against these functions, I'll submit that version instead.

Maybe add a qemu_fputc_unlocked() and do a configure check for it?

Good idea. I'll try that, unless people disagree.

Writing by row should be faster and pretty straight forward, no?

I don't see how it's faster, but I guess I'll do that, it's a local issue and is best addressed locally.

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