Bug #64353 mysql_binlog_send calls malloc/free for every event sent to a slave
Submitted: 16 Feb 2012 15:45 Modified: 29 Dec 2012 21:56
Reporter: Mark Callaghan Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Replication Severity:S5 (Performance)
Version:5.1.52 OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution

[16 Feb 2012 15:45] Mark Callaghan
Description:
Apply this patch reduced CPU by ~5%. The host had 5 slave connections.

The patch comes from the big Google diff for MySQL 5.0.37

mysql_binlog_send calls malloc/free for every event sent to a slave. The buffer, "packet", is initialized with a buffer provided by the caller prior to reading an event:
    packet->set("\0", 1, &my_charset_bin);

That free's the old buffer it was using and then initializes it to use a
buffer that won't be freed. Any appends that follow will require it to
allocate/reallocate memory, thus more calls to malloc/free/realloc.

  /**
     Points the internal buffer to the supplied one. The old buffer is
freed.
     @param str Pointer to the new buffer.
     @param arg_length Length of the new buffer in characters, excluding
any
            null character.
     @param cs Character set to use for interpreting string data.
     @note The new buffer will not be null terminated.
  */
  inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs)
  {
    free();
    Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0;
    str_charset=cs;
  }

How to repeat:
Read the code, apply the patch and notice that malloc/free are called less frequently and CPU load should be reduced.

Suggested fix:
Allocate a buffer and reuse it in the while loop.
[16 Feb 2012 15:45] Mark Callaghan
patch to reduce malloc/free calls

Attachment: reb.patch (application/octet-stream, text), 9.73 KiB.

[23 Apr 2012 6:23] zhai weixiang
hi, Mark
i had tried to merge your patch into Percona Server5.5.18 , and after a simple test (use vmstat to watch CPU usage ), i found the CPU usage was not improved.

i don't know if i had test the patch  in a wrong way. so can you provide more information about your test?
[23 Apr 2012 14:18] Mark Callaghan
It depends on your workload. A busy server with large binlog events and many slave connections and a high rate of transactions and many CPU cores is the best way to reproduce the possible benefit.
[24 Apr 2012 1:51] zhai weixiang
thank you very much for your advise, Mark.
I will follow your suggestion to test. :)
[29 Dec 2012 21:56] Erlend Dahl
This is a duplicate of an internally filed feature request.