Bug #27587 Unnecessarily many read()s in vio subsystem
Submitted: 2 Apr 2007 12:45 Modified: 4 Apr 2007 9:49
Reporter: Joseph Benden Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S5 (Performance)
Version:all OS:Solaris (Solaris 10 and OpenSolaris)
Assigned to: CPU Architecture:Any
Tags: 4 byte, network, OpenSolaris, performance, protocol, read, solaris, vio

[2 Apr 2007 12:45] Joseph Benden
Description:
I'm trying to squeeze all the performance possible from every piece of my Solaris systems.  The last piece that is bugging me is the high number of system calls experienced on the machines.  In analyzing the web server, I found the problem was the read() system call.  Next, I wanted to know what sizes were being read and where from.  It turns out the read()s are from the mysqlclient library and are 4 byte reads.

          value  ------------- Distribution ------------- count
              0 |                                         0
              1 |@@@@@                                    3447
              2 |@                                        616
              4 |@@@@@@@@@@@@@@@@@@@                      12531
              8 |@                                        626
             16 |@@@@@@@@                                 4966
             32 |@@                                       1158
             64 |                                         153
            128 |                                         29
            256 |                                         15
            512 |                                         5
           1024 |                                         17
           2048 |                                         0
           4096 |                                         0
           8192 |@@@@                                     2667
          16384 |                                         0

Upon further research with dtrace, I've found these read()s to correspond with the MySQL protocol packet header being read().

How to repeat:
By simply using the client library under a load, the system will experience a high number of system calls.

Suggested fix:
A moving window buffered approach should be taken.  

ie: A general windowed buffered read() of everything possible from the wire and this code returns the number of bytes requested for the read operation. At a very minimum, this would save 1 system call.  The protocol's header and packet data would be read in a single read(), instead of across two system read() calls.
[2 Apr 2007 22:35] Joseph Benden
Well, that was easy! Someone has already done the work and it has been committed!

http://lists.mysql.com/internals/22698
[3 Apr 2007 6:05] Sveta Smirnova
Thank you for the report.

Does last comment mean bug has fixed?
[3 Apr 2007 13:59] Joseph Benden
Not yet.  I need to run some tests against the latest version, to make sure.
[3 Apr 2007 19:26] Joseph Benden
Ok, the tests are in!  This is an application of mine sampled over 1 minute, all sample data is the same.

Results prior to latest MySQL library version:

           value  ------------- Distribution ------------- count
               0 |                                         0
               1 |@@@@@@@@@@@                              169945
               2 |                                         2173
               4 |@@@@@@@@@@@@@@@@@@                       278894
               8 |                                         4512
              16 |@                                        21123
              32 |@@                                       35091
              64 |                                         0
             128 |                                         3213
             256 |                                         0
             512 |                                         0
            1024 |                                         3
            2048 |                                         14
            4096 |@@                                       27194
            8192 |@@@@@@                                   87948
           16384 |                                         4
           32768 |                                         2
           65536 |                                         28
          131072 |                                         0

Results with the latest MySQL library version:

           value  ------------- Distribution ------------- count
             128 |                                         0
             256 |                                         1
             512 |                                         0
            1024 |                                         1
            2048 |                                         40
            4096 |@@@@@@@@@                                26569
            8192 |                                         7
           16384 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@          92972
           32768 |                                         0
           65536 |                                         66
          131072 |                                         0

I can safely say that the latest codebase is fixed.  Excellent! The last results used 5.0.37.
[4 Apr 2007 9:49] Sveta Smirnova
Thank you for the testing.

I'll mark it as "Can't repeat" as it is not repeatable with current version.