# One suggestion to PHP-FPM

- Thread: https://externals.io/message/47797
- Original archive: https://news-web.php.net/php.internals/47797
- Started by tly.phpfans@gmail.com on 2010-04-06T07:11:32+00:00
- Messages: 44
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/47797.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #47797 — tly.phpfans@gmail.com — 2010-04-06T07:11:32+00:00

URL: https://externals.io/message/47797#47797

hi,

We use PHP's Imagemagick extension to process images, and this would
consume a lot of memory under some circumstances.
However, the mainstream PHP's memory management is really weak, so a
huge amount of memory would be consumed under high server load. What's
worse, this even cause our server to crash frequently.

So I suggest that if we could add a config option to PHP-FPM, so that
PHP-FPM could just kill a PHP process if it consumes too much memory
(say more than 256M).
This would prevent PHP from consuming a lot of memory which couldn't
be freed later.

For example, assuming that this option is "exit_on_memory_exceeds",
and I set it to 256M. Also, I set PHP's "memory_limit" option to
1024M.
I would expect PHP-FPM to kill the PHP process which consumes more
than 256M memory, even though PHP could use up to 1024M memory.
We have discussed this idea with the author of PHP-FPM,url is
http://groups.google.com/group/highload-php-en/browse_thread/thread/1882a3b2257dcc5c/ea73892cea011541?lnk=gst&q=gaochunhui#ea73892cea011541
, and now I have reach my goal by patching the source code of PHP-FPM. So
we can edit the php-fpm.conf and add one item such as "<value
name="term_on_memory_limit">3145728</value>" in the "global_options" section,
when PHP's memory beyond this limit,child process will exit.

mypatch.diff http://docs.google.com/leaf?id=0B-ig5wUi7GTMZTUxYjg1ZTQtOGQ5Zi00N2Q1LTkxODYtYWQ4Y2FiMzhlOWM5&hl=en<%20http://docs.google.com/leaf?id=0B-ig5wUi7GTMZTUxYjg1ZTQtOGQ5Zi00N2Q1LTkxODYtYWQ4Y2FiMzhlOWM5&hl=en>
 This patch is for
http://svn.php.net/repository/php/php-src/branches/PHP_5_3_FPM version:
294187.

---

## #47932 — jerome@loyet.net — 2010-04-14T07:29:56+00:00

URL: https://externals.io/message/47797#47932 · In reply to: #47797

2010/4/6 滕路遥 <tly.phpfans@gmail.com>:
> hi,
>
> We use PHP's Imagemagick extension to process images, and this would
> consume a lot of memory under some circumstances.
> However, the mainstream PHP's memory management is really weak, so a
> huge amount of memory would be consumed under high server load. What's
> worse, this even cause our server to crash frequently.
>
> So I suggest that if we could add a config option to PHP-FPM, so that
> PHP-FPM could just kill a PHP process if it consumes too much memory
> (say more than 256M).
> This would prevent PHP from consuming a lot of memory which couldn't
> be freed later.
>
> For example, assuming that this option is "exit_on_memory_exceeds",
> and I set it to 256M. Also, I set PHP's "memory_limit" option to
> 1024M.
> I would expect PHP-FPM to kill the PHP process which consumes more
> than 256M memory, even though PHP could use up to 1024M memory.
> We have discussed this idea with the author of PHP-FPM,url is
> http://groups.google.com/group/highload-php-en/browse_thread/thread/1882a3b2257dcc5c/ea73892cea011541?lnk=gst&q=gaochunhui#ea73892cea011541
> , and now I have reach my goal by patching the source code of PHP-FPM. So
> we can edit the php-fpm.conf and add one item such as "<value
> name="term_on_memory_limit">3145728</value>" in the "global_options" section,
> when PHP's memory beyond this limit,child process will exit.
>
> mypatch.diff http://docs.google.com/leaf?id=0B-ig5wUi7GTMZTUxYjg1ZTQtOGQ5Zi00N2Q1LTkxODYtYWQ4Y2FiMzhlOWM5&hl=en<%20http://docs.google.com/leaf?id=0B-ig5wUi7GTMZTUxYjg1ZTQtOGQ5Zi00N2Q1LTkxODYtYWQ4Y2FiMzhlOWM5&hl=en>
>  This patch is for
> http://svn.php.net/repository/php/php-src/branches/PHP_5_3_FPM version:
> 294187.
>

thanks, I'll review the patch. But I don't have time right now. It's
in my todo list.

++ Jerome

---

## #47938 — jerome@loyet.net — 2010-04-14T09:01:24+00:00

URL: https://externals.io/message/47797#47938 · In reply to: #47932

Le 14 avril 2010 09:29, Jérôme Loyet <jerome@loyet.net> a écrit :
> 2010/4/6 滕路遥 <tly.phpfans@gmail.com>:
> > […]
>
> thanks, I'll review the patch. But I don't have time right now. It's
> in my todo list.

The patch seems good (I didn't test it yet) but I'm curious about the
way you check the memory real size:

+				if(zend_memory_usage(1 TSRMLS_CC) >
fpm_global_config.term_on_memory_limit){

I don't know much about the zend_memory_usage() function. But does it
return the real memory size of the process ?

ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC)
{
  if (real_usage) {
    return AG(mm_heap)->real_size;
  } else {
    size_t usage = AG(mm_heap)->size;
#if ZEND_MM_CACHE
    usage -= AG(mm_heap)->cached;
#endif
    return usage;
  }
}

It seems it return the zend heap size. I don't know if it's the same
as the process heap size.

++ Jerome

---

## #47941 — Rasmus Lerdorf — 2010-04-14T09:38:24+00:00

URL: https://externals.io/message/47797#47941 · In reply to: #47938

> The patch seems good (I didn't test it yet) but I'm curious about the
> way you check the memory real size:
> 
> +				if(zend_memory_usage(1 TSRMLS_CC) >
> fpm_global_config.term_on_memory_limit){
> 
> I don't know much about the zend_memory_usage() function. But does it
> return the real memory size of the process ?

Right, that will only give you the memory allocated by the memory
manager, obviously.  But, pecl/imagick does seem to use emalloc to
allocate the imagick objects.  However, if the underlying magickwand
library mallocs a bunch of temporary space on its own, this will not get
picked up.

-Rasmus

---

## #47944 — tly.phpfans@gmail.com — 2010-04-15T01:58:57+00:00

URL: https://externals.io/message/47797#47944 · In reply to: #47941

We check error log after our server crashed, and we found that php heap
memory is out of limit,
so I think imagick use emalloc to allocate php heap memory, to check the
zend_memory_usage is
not a method for imagick, other circumstances which occupy huge memory can
use this method to
ensure the system have enough free memory.

thanks,
    looyao

2010/4/14 Rasmus Lerdorf <rasmus@lerdorf.com>

> 
> > […]
>
> Right, that will only give you the memory allocated by the memory
> manager, obviously.  But, pecl/imagick does seem to use emalloc to
> allocate the imagick objects.  However, if the underlying magickwand
> library mallocs a bunch of temporary space on its own, this will not get
> picked up.
>
> -Rasmus

---

## #47945 — jerome@loyet.net — 2010-04-15T06:03:27+00:00

URL: https://externals.io/message/47797#47945 · In reply to: #47944

2010/4/15 滕路遥 <tly.phpfans@gmail.com>:
> We check error log after our server crashed, and we found that php heap
> memory is out of limit,
> so I think imagick use emalloc to allocate php heap memory, to check the
> zend_memory_usage is
> not a method for imagick, other circumstances which occupy huge memory can
> use this method to
> ensure the system have enough free memory.

OK,

one thing I didn't understand is "why should you want check de zend
memory usage when there is a memory_limit parameter in PHP.ini " ?
does'nt it do the same at PHP layer when allocating ?

>
> thanks,
>     looyao
>
> 2010/4/14 Rasmus Lerdorf <rasmus@lerdorf.com>
> > […]

---

## #47946 — Ferenc Kovacs — 2010-04-15T07:09:14+00:00

URL: https://externals.io/message/47797#47946 · In reply to: #47945

2010/4/15 Jérôme Loyet <jerome@loyet.net>

> 2010/4/15 滕路遥 <tly.phpfans@gmail.com>:
> > […]
> can
> > […]
>
> OK,
>
> one thing I didn't understand is "why should you want check de zend
> memory usage when there is a memory_limit parameter in PHP.ini " ?
> does'nt it do the same at PHP layer when allocating ?
>
>
Obviously you didn't read the original mail, and the links inside:
http://groups.google.com/group/highload-php-en/browse_thread/thread/1882a3b2257dcc5c/ea73892cea011541?lnk=gst&q=gaochunhui#ea73892cea011541

The problem here is that PHP wouldn't free any allocated memory untill
the process exits, so there's the risk of memory exhaustion if too
many PHP processes were running, even though each process wouldn't
exceed PHP's memory_limit.

My suggestion is more about releasing the allocated memory as soon as
possible. That is, this option is similar to "max_requests".
PHP-FPM would kill the PHP process if the requests a process handled
exceed max_requests, and similarly, PHP-FPM should kill the PHP
process whose memory usage exceeds "exit_on_memory_exceeds".

So one of your lib (for example imagick) leaks memory, on the long run, it
will exhaust the memory limit, and will kill a totaly request.
You can set that how many request should be served with one worker, but you
can't soft limit it's memory consumption.
This is what the patch does:
if you set the hard limit: (memory_limit) you can guarante that no process
will use more memory, because if it tries, it will fail.
and you can set soft limit, if that reached, the process will die and
respawn after finishing the current request.

Tyrael

> > […]

---

## #47947 — Ferenc Kovacs — 2010-04-15T07:14:16+00:00

URL: https://externals.io/message/47797#47947 · In reply to: #47946

>
>
> 2010/4/15 Jérôme Loyet <jerome@loyet.net>
>
> 2010/4/15 滕路遥 <tly.phpfans@gmail.com>:
> > […]
> Obviously you didn't read the original mail, and the links inside:
>
>
> http://groups.google.com/group/highload-php-en/browse_thread/thread/1882a3b2257dcc5c/ea73892cea011541?lnk=gst&q=gaochunhui#ea73892cea011541
>
> The problem here is that PHP wouldn't free any allocated memory untill
> the process exits, so there's the risk of memory exhaustion if too
> many PHP processes were running, even though each process wouldn't
> exceed PHP's memory_limit.
>
> My suggestion is more about releasing the allocated memory as soon as
> possible. That is, this option is similar to "max_requests".
> PHP-FPM would kill the PHP process if the requests a process handled
> exceed max_requests, and similarly, PHP-FPM should kill the PHP
> process whose memory usage exceeds "exit_on_memory_exceeds".
>
> So one of your lib (for example imagick) leaks memory, on the long run, it
> will exhaust the memory limit, and will kill a totaly request.
> You can set that how many request should be served with one worker, but you
> can't soft limit it's memory consumption.
> This is what the patch does:
> if you set the hard limit: (memory_limit) you can guarante that no process
> will use more memory, because if it tries, it will fail.
> and you can set soft limit, if that reached, the process will die and
> respawn after finishing the current request.
>
> Tyrael
>

Btw: did you know about
http://php.net/manual/en/function.apache-child-terminate.php I just noticed
a few days ago, obviously, you can't use with cgi/fastcgi/php-fpm but for
apache, it does exactly what you need, except that you have to execute from
userland.
So I think it would be a good idea, to generally implement a soft/hard limit
for memory allocation: if soft fails, the process terminates after the
request, if the hard limit fails, it is terminated instantly.

Tyrael

---

## #47948 — looyao teng — 2010-04-15T07:15:14+00:00

URL: https://externals.io/message/47797#47948 · In reply to: #47945

When allocate memory from PHP memory heap, PHP runtime check the
memory_limit which
in  php.ini, but this can only give a error message and  not to continue to
excute php code, it can't
control the PHP-FPM child process, my idea is that when the  memory out of
limit we set,
kill this child process, we know, though PHP runtime can free its heap
memory, but  it can't give
it back to the system, so we can kill this child process and ensure the
system have enough free
memory. We do check the memory limit is to control the process.

2010/4/15 Jérôme Loyet <jerome@loyet.net>

> 2010/4/15 滕路遥 <tly.phpfans@gmail.com>:
> > […]
> can
> > […]
>
> OK,
>
> one thing I didn't understand is "why should you want check de zend
> memory usage when there is a memory_limit parameter in PHP.ini " ?
> does'nt it do the same at PHP layer when allocating ?
>
> > […]

---

## #47949 — jerome@loyet.net — 2010-04-15T07:25:36+00:00

URL: https://externals.io/message/47797#47949 · In reply to: #47947

2010/4/15 Ferenc Kovacs <tyra3l@gmail.com>:
>
>
> 
> > […]
>
> Btw: did you know about
> http://php.net/manual/en/function.apache-child-terminate.php I just noticed
> a few days ago, obviously, you can't use with cgi/fastcgi/php-fpm but for
> apache, it does exactly what you need, except that you have to execute from
> userland.

This function is interesting but it's called from php scripts. When
you are a sysadmin who host hundreds of different customers, you can't
garanty each customer will call this function each time he uses
consomption scripts.

---

## #47950 — Michael Shadle — 2010-04-15T07:28:18+00:00

URL: https://externals.io/message/47797#47950 · In reply to: #47946

> My suggestion is more about releasing the allocated memory as soon as
> possible. That is, this option is similar to "max_requests".
> PHP-FPM would kill the PHP process if the requests a process handled
> exceed max_requests, and similarly, PHP-FPM should kill the PHP
> process whose memory usage exceeds "exit_on_memory_exceeds".
>
> So one of your lib (for example imagick) leaks memory, on the long run, it
> will exhaust the memory limit, and will kill a totaly request.
> You can set that how many request should be served with one worker, but you
> can't soft limit it's memory consumption.
> This is what the patch does:
> if you set the hard limit: (memory_limit) you can guarante that no process
> will use more memory, because if it tries, it will fail.
> and you can set soft limit, if that reached, the process will die and
> respawn after finishing the current request.

Sounds like you more or less want a "request_terminate_timeout" type
of functionality but based on memory. Since `set_time_limit()` and other
things in PHP don't seem to force kill the process. So PHP-FPM
forcefully terminates the process based on the actual wall clock
seconds.

I'm thinking you're hoping for the same thing to be possible but for
memory limits per-process? I would say that could be cool, either per
pool or per child somehow. No clue if it is possible, but that would
be a great way to limit the usage *forcefully* - helpful on lower
resource machines (like a vps...)

---

## #47951 — Ferenc Kovacs — 2010-04-15T07:39:15+00:00

URL: https://externals.io/message/47797#47951 · In reply to: #47949

> 2010/4/15 Ferenc Kovacs <tyra3l@gmail.com>:
> > […]
> heap
> > […]
> memory
> > […]
> http://groups.google.com/group/highload-php-en/browse_thread/thread/1882a3b2257dcc5c/ea73892cea011541?lnk=gst&q=gaochunhui#ea73892cea011541
> > […]
> it
> > […]
> process
> > […]
> noticed
> > […]
> from
> > […]
>
> This function is interesting but it's called from php scripts. When
> you are a sysadmin who host hundreds of different customers, you can't
> garanty each customer will call this function each time he uses
> consomption scripts.
>
Yeah, I know, and I think I did mention that, but this sounds like a good
feature in the language, but sadly it has too many limitation in it's
current form. (no windows support, only works with apache)

Tyrael

---

## #47952 — jerome@loyet.net — 2010-04-15T07:41:52+00:00

URL: https://externals.io/message/47797#47952 · In reply to: #47951

2010/4/15 Ferenc Kovacs <tyra3l@gmail.com>:
>
>
> 
> > […]
>
> Yeah, I know, and I think I did mention that, but this sounds like a good
> feature in the language, but sadly it has too many limitation in it's
> current form. (no windows support, only works with apache)

I see how soft_limit could work (after processing a page, memory is
checked and if it's more than the soft limit, the process is killed).
But how can you make the hard_limit to work ? You'll have to setup
memory check for each process by a master process, because the child
won't be able to check it's memory realtime (or I missed something)

++ Jerome

---

## #47953 — Ferenc Kovacs — 2010-04-15T07:45:39+00:00

URL: https://externals.io/message/47797#47953 · In reply to: #47950

> 
>
> > […]
> it
> > […]
> you
> > […]
> process
> > […]
>
> Sounds like you more or less want a "request_terminate_timeout" type
> of functionality but based on memory. Since `set_time_limit()` and other
> things in PHP don't seem to force kill the process. So PHP-FPM
> forcefully terminates the process based on the actual wall clock
> seconds.
>
> Unfortunately I don't know how PHP-FPM handles it's worker pool, but as far
as with goes with apache php_prefork and fastcgi you can limit how many
request should one worker process, but it will be a good option to limit the
process lifetime based on the memory consumption.

> I'm thinking you're hoping for the same thing to be possible but for
> memory limits per-process? I would say that could be cool, either per
> pool or per child somehow.

Yeah.

> No clue if it is possible, but that would
> be a great way to limit the usage *forcefully* - helpful on lower
> resource machines (like a vps...)
>
Its possible, the patch adds this functionality to the php-fpm.
but I started thinking about, that the possibility to terminate (I mean
really terminate, not just abort the execution of the current request) the
worker would be a good feature in the php language itself.

Tyrael

---

## #47954 — Ferenc Kovacs — 2010-04-15T07:56:11+00:00

URL: https://externals.io/message/47797#47954 · In reply to: #47952

> 2010/4/15 Ferenc Kovacs <tyra3l@gmail.com>:
> > […]
> wrote:
> > […]
> check
> > […]
> http://groups.google.com/group/highload-php-en/browse_thread/thread/1882a3b2257dcc5c/ea73892cea011541?lnk=gst&q=gaochunhui#ea73892cea011541
> > […]
> untill
> > […]
> run,
> > […]
> but
> > […]
>
> I see how soft_limit could work (after processing a page, memory is
> checked and if it's more than the soft limit, the process is killed).
> But how can you make the hard_limit to work ? You'll have to setup
> memory check for each process by a master process

This is what the proposed php-fpm patch does, or am I missing something?

> , because the child
> won't be able to check it's memory realtime (or I missed something)
>
> ++ Jerome
>
If we talking about my proposal(to kill/set to kill the worker from inside
the request), then maybe you are right, I don't know much about the internal
memory management system, but you are saying, that there is no way to tell
the worker's memory consumption from inside the php userland?
there are functions like `memory_get_usage()`, `memory_get_peak_usage()`, and
there are ways in every OS to tell the ~ memory consumption of a given
process.
Am I missing something?

Tyrael

---

## #47955 — Antony Dovgal — 2010-04-15T08:14:30+00:00

URL: https://externals.io/message/47797#47955 · In reply to: #47797

> So I suggest that if we could add a config option to PHP-FPM, so that
> PHP-FPM could just kill a PHP process if it consumes too much memory
> (say more than 256M).

I personally don't think this belongs to any SAPI.
How about adding another ini option to PECL/memtrack to not just throw a warning, 
but terminate the process when it hits some limit?

-- 
Wbr,
Antony Dovgal
---
http://pinba.org - realtime statistics for PHP

---

## #47956 — jerome@loyet.net — 2010-04-15T08:23:39+00:00

URL: https://externals.io/message/47797#47956 · In reply to: #47955

2010/4/15 Antony Dovgal <tony@daylessday.org>:
> 
> > […]
>
> I personally don't think this belongs to any SAPI.
> How about adding another ini option to PECL/memtrack to not just throw a warning,
> but terminate the process when it hits some limit?

In fork processes it could be great but when dealing with threads it won't work

---

## #47957 — Antony Dovgal — 2010-04-15T08:36:44+00:00

URL: https://externals.io/message/47797#47957 · In reply to: #47956

> 2010/4/15 Antony Dovgal <tony@daylessday.org>:
> > […]
> 
> In fork processes it could be great but when dealing with threads it won't work

Adding this to FPM won't help anyway - FPM doesn't use threads.

-- 
Wbr,
Antony Dovgal
---
http://pinba.org - realtime statistics for PHP

---

## #47958 — Ferenc Kovacs — 2010-04-15T08:54:05+00:00

URL: https://externals.io/message/47797#47958 · In reply to: #47957

2010/4/15 Antony Dovgal <tony@daylessday.org>

> 
> > […]
> warning,
> > […]
> won't work
>
> Adding this to FPM won't help anyway - FPM doesn't use threads.
>
> You meant won't hurt?

Tyrael

---

## #47959 — Antony Dovgal — 2010-04-15T08:57:59+00:00

URL: https://externals.io/message/47797#47959 · In reply to: #47958

>     Adding this to FPM won't help anyway - FPM doesn't use threads.
> 
> You meant won't hurt?

No, I mean implementing it in PECL/memtrack would be better since this way you add it to all non-threaded SAPIs.
While adding it to FPM means that other non-threaded SAPIs will have to do it in an other way.

-- 
Wbr,
Antony Dovgal
---
http://pinba.org - realtime statistics for PHP

---

## #47966 — Ferenc Kovacs — 2010-04-15T11:03:33+00:00

URL: https://externals.io/message/47797#47966 · In reply to: #47959

> 
> > […]
>
> No, I mean implementing it in PECL/memtrack would be better since this way
> you add it to all non-threaded SAPIs.
> While adding it to FPM means that other non-threaded SAPIs will have to do
> it in an other way.
>
> Yeah, this could be implemented there, but it seems that its not actively
developed, last release 0.1 beta more than a year ago.
But as you say, it would be a huge benefit, to add this feature for all
non-threaded SAPI.

Tyrael

---

## #47967 — Antony Dovgal — 2010-04-15T11:08:38+00:00

URL: https://externals.io/message/47797#47967 · In reply to: #47966

> Yeah, this could be implemented there, but it seems that its not
> actively developed, last release 0.1 beta more than a year ago.

In this case 'not actively developed' means 'it works and no bugs were found/reported',
so I don't see it as a disadvantage.

-- 
Wbr,
Antony Dovgal
---
http://pinba.org - realtime statistics for PHP

---

## #47971 — Ferenc Kovacs — 2010-04-15T11:17:02+00:00

URL: https://externals.io/message/47797#47971 · In reply to: #47967

> 
> > […]
>
> In this case 'not actively developed' means 'it works and no bugs were
> found/reported',
> so I don't see it as a disadvantage.
>
> If this is the case, then it should be stable for now, isn't it?

Tyrael

---

## #47975 — Antony Dovgal — 2010-04-15T11:36:20+00:00

URL: https://externals.io/message/47797#47975 · In reply to: #47971

> If this is the case, then it should be stable for now, isn't it?

I guess you can call it that, yes.

-- 
Wbr,
Antony Dovgal
---
http://pinba.org - realtime statistics for PHP

---

## #47981 — Ferenc Kovacs — 2010-04-15T13:49:52+00:00

URL: https://externals.io/message/47797#47981 · In reply to: #47975

> 
> > […]
>
> I guess you can call it that, yes.
>
> I mean this should be reflected in the pecl package status field.

Tyrael

---

Next page: https://externals.io/message/47797.md?page=2
