Mandalika's scratchpad [ Work blog @Oracle | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17  12.17  01.18  02.18  03.18  04.18  05.18  06.18  07.18  08.18  09.18  11.18  12.18  01.19  02.19  05.19  06.19  08.19  10.19  11.19  05.20  10.20  11.20  12.20  09.21  11.21  12.22 


Saturday, January 31, 2009
 
PHP: Memory savings with mysqlnd

mysqlnd may save memory. In the best cases, it may consume only 50% memory as that of libmysql esp. when the client application does not modify the data in the result set after executing a query. Keep in mind that the client must use ext/mysqli and treat the data returned by the query as read-only in order to fully realize mysqlnd's memory gains. If the client application modifies any of the data, mysqlnd behaves just like libmysql.

Let's have a quick look at the memory consumption in both the cases (mysqlnd and libmysql) with an example before delving into the internals. For easy comparison, the sample PHP script does not modify any of the arrays returned from the fetch method. The following example uses DTrace on Sun Solaris to monitor the calls to malloc() and prints the requested bytes of memory on the standard output.

Source code for the script: PHPmysqliClient.php. MySQL table structure and the sample data are shown in the other blog post: Demonstrating the Features of MySQL Native Driver for PHP, mysqlnd.

bash# cat monitormalloc.d

#!/usr/sbin/dtrace -s

pid$1:libc:malloc:entry
{
printf("\t\tSize : %d Bytes", arg0);
ustack();
@malloc[probefunc] = quantize(arg0);
}

CASE 1: ext/mysqli with libmysql

In one terminal window:

bash# /opt/coolstack/php5/bin/php PHPmysqliClient.php
In another terminal window, run monitormalloc.d `pgrep php` and press any key to continue ..

Retrieved 3 row(s).

CityName
--------
Hyderabad, India
San Francisco, USA
Sydney, Australia

In another terminal window:

bash# ./monitormalloc.d `pgrep php`
dtrace: script './monitormalloc.d' matched 1 probe
CPU ID FUNCTION:NAME
0 80920 malloc:entry Size : 964 Bytes
libc.so.1`malloc
libmysqlclient.so.16.0.0`0xce2173c1
libmysqlclient.so.16.0.0`0xce23bb47
mysqli.so`0xce11d292

0 80920 malloc:entry Size : 20 Bytes
libc.so.1`malloc
libmysqlclient.so.16.0.0`0xce2173c1
libmysqlclient.so.16.0.0`0xce23da60
mysqli.so`0xce11dc72

0 80920 malloc:entry Size : 20 Bytes
libc.so.1`malloc
libmysqlclient.so.16.0.0`0xce2173c1
libmysqlclient.so.16.0.0`0xce21d991
libmysqlclient.so.16.0.0`0xce21d9ce
libmysqlclient.so.16.0.0`0xce23da72
mysqli.so`0xce11dc72

0 80920 malloc:entry Size : 17 Bytes
... elided stack traces for brevity ...
0 80920 malloc:entry Size : 152 Bytes
0 80920 malloc:entry Size : 16384 Bytes
0 80920 malloc:entry Size : 8199 Bytes
0 80920 malloc:entry Size : 7 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 18261 Bytes
0 80920 malloc:entry Size : 58 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 4088 Bytes
0 80920 malloc:entry Size : 120 Bytes
0 80920 malloc:entry Size : 5 Bytes
0 80920 malloc:entry Size : 6 Bytes
0 80920 malloc:entry Size : 5 Bytes
0 80920 malloc:entry Size : 56 Bytes
0 80920 malloc:entry Size : 8164 Bytes
0 80920 malloc:entry Size : 8164 Bytes
0 80920 malloc:entry Size : 92 Bytes
0 80920 malloc:entry Size : 56 Bytes
0 80920 malloc:entry Size : 8164 Bytes
libc.so.1`malloc
libmysqlclient.so.16.0.0`0xce2173c1
libmysqlclient.so.16.0.0`0xce21a27b
libmysqlclient.so.16.0.0`0xce23b8a4
libmysqlclient.so.16.0.0`0xce23d4fa
mysqli.so`0xce11fe56

0 80920 malloc:entry Size : 262144 Bytes
libc.so.1`malloc
php`0x856fb98

^C

malloc
value ------------- Distribution ------------- count
2 | 0
4 |@@@@@ 4
8 | 0
16 |@@@@ 3
32 |@@@@ 3
64 |@@@ 2
128 |@ 1
256 | 0
512 |@ 1
1024 | 0
2048 |@@@@@@@@@@@@@@ 11
4096 |@@@@ 3
8192 |@ 1
16384 |@@@ 2
32768 | 0
65536 | 0
131072 | 0
262144 |@ 1
524288 | 0

CASE 2: ext/mysqli with mysqlnd

In one terminal window:

bash# /export/home/php53/bin/php PHPmysqliClient.php
In another terminal window, run monitormalloc.d `pgrep php` and press any key to continue ..

Retrieved 3 row(s).

CityName
--------
Hyderabad, India
San Francisco, USA
Sydney, Australia

In another terminal window:

bash# ./monitormalloc.d `pgrep php`
dtrace: script './monitormalloc.d' matched 1 probe
CPU ID FUNCTION:NAME
0 80920 malloc:entry Size : 262144 Bytes
libc.so.1`malloc
php`0x82f702b
php`0x82f80ab
php`0x82f841f
php`0x82f98c4
php`0x82c7668
php`0x83c30ae
php`0x80c059c

^C

malloc
value ------------- Distribution ------------- count
131072 | 0
262144 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 1
524288 | 0


In the case of ext/mysqli with libmysql, there are more than 25 calls to malloc() accounting to a total size around 367KB, where as in the case of ext/mysqli with mysqlnd, there is only one call to malloc() with a size of 256KB. In other words, mysqlnd is consuming 30% less memory relative to libmysql to do similar database operations (in reality, it is incorrect to treat every byte allocated as the memory consumed unless there exists a corresponding memory mapping -- however for the sake of this discussion, let's just assume that all the allocated bytes are eventually consumed).

The memory savings in the above example are the result of mysqlnd's ability to hold the results only once in the memory. On the other hand, as libmysql is not a part of PHP, some of the results fetched by libmysql will be copied into memory twice. When libmysql fetches the data from the MySQL Server, it puts the data into its own buffers. Then the data gets copied from the libmysql buffers into respective ext/mysqli data structures (often referred as zvals) before it is made available to the PHP clients to consume. So with ext/mysqli and libmysql, there might be two copies of the data in the main memory - one copy inside the libmysql buffers and the other inside zvals. With mysqlnd, there might be only one copy of the data in the memory. mysqlnd also uses buffers but links the zval structures directly to the read buffers, wherever possible. Therefore in majority of the instances, mysqlnd consumes less memory relative to libmysql. In the worst case, it may consume as much memory as that of libmysql. The total memory savings depend on the size of the buffered result set.

Shown below is the simplified behind-the-scenes actions of ext/mysqli with libmysql and ext/mysqli with mysqlnd when mysqli sends a query:

ext/mysqli with libmysql
  1. mysqli sends a query
  2. result set gets fetched into libmysql buffers
  3. mysqli allocates zvals, then new buffers
  4. mysqli copies data from libmysql to its own buffers
  5. mysqli calls mysql_free_result() and deallocates libmysql buffers

ext/mysqli with mysqlnd
  1. mysqli sends a query
  2. result set gets fetched row by row -- every row is a different buffer
  3. mysqlnd creates a result set of zvals pointing to the buffers
  4. mysqli calls mysqlnd_free_result() and deallocates the row buffers

In short, ext/mysqli with libmysql does:
when compared to ext/mysqli with mysqlnd.

Related Blog Posts:
  1. MySQL Native Driver for PHP, mysqlnd
  2. Demonstrating the Features of MySQL Native Driver for PHP, mysqlnd

Acknowledgments
Andrey Hristov & Ulf Wendel, Sun-MySQL AB

(Originally posted on blogs.sun.com at:
http://blogs.sun.com/mandalika/entry/php_memory_savings_with_mysqlnd
)
_______________
Technorati Tags:
| |


Comments: Post a Comment



<< Home


2004-2019 

This page is powered by Blogger. Isn't yours?