2

We have a fairly complicated PHP script that's generating a lot of minor and major page faults.

$>  ps -o min_flt,maj_flt,time,cmd,pid  4686 
 MINFL  MAJFL     TIME CMD                           PID
3074640 255514 00:06:51 php scripts/daemon/PostProc 4686

This script runs for 10 minutes and then kills itself. Cron starts it back up a minute later.

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                       
 4686 apache    20   0 2138m 1.6g 2012 T 48.5 86.9   6:33.30 php

I have no idea what's causing such a high number of page faults. I doubt you do either. But I'm hoping someone can point me at some Linux commands that I can use to investigate what might be causing these page faults.

Thanks!

Edit 1

I ran strace on the script, and here's what I got back.

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 20.15    9.297617          22    424130           gettimeofday
 13.49    6.223247          64     96600           read
 13.01    6.001955         158     37945           stat
  8.40    3.876297         306     12677           open
  7.43    3.425291          63     54691           write
  6.47    2.985425          41     72945           sendto
  6.00    2.765838          25    109401           recvmsg
  4.84    2.234694          26     85617           close
  4.79    2.210381          20    109401           fcntl
  3.54    1.635305          22     72934           socket
  3.01    1.387212          19     72906           poll
  1.96    0.901874          25     35712        70 lstat
  1.72    0.795357          22     36467           connect
  1.62    0.745462          20     36467           getsockname
  1.60    0.740286          20     36467           bind
  0.77    0.353189          28     12719           fstat
  0.67    0.309640          24     12664           lseek
  0.51    0.236759          71      3346           brk
  0.01    0.004424          36       123           munmap
  0.00    0.000524          25        21           mmap
  0.00    0.000000           0         2           mremap
  0.00    0.000000           0        11           recvfrom
  0.00    0.000000           0         1           shutdown
  0.00    0.000000           0         4           uname
  0.00    0.000000           0         1           flock
  0.00    0.000000           0         1           unlink
------ ----------- ----------- --------- --------- ----------------
100.00   46.130777               1323253        70 total
4
  • Can you post the script? Does the memory usage go up throughout the lifetime of the script? Guessing it's a nasty memory leak. – Bert Jul 25 '14 at 19:46
  • Unfortunately, I cannot post the script. It would end up being 1000+ lines of code, across dozens of classes. A nasty memory leak is my first guess too. I'm just trying to find it. – Alan C. Jul 25 '14 at 19:49
  • You may want to try StackOverflow since PHP code is the problem – Bert Jul 25 '14 at 19:57
  • That's an absurdly high number of calls to gettimeofday(). Why does your script need to check the clock that frequently? – Michael Hampton Jul 25 '14 at 20:11
2

A page fault occurs when a running program attempts to access a bit of it's memory that is not in RAM, most likely it is in the swap file on disk. (Obligatory Wikipedia article)

Your output of a running command shows that the PHP program is trying to allocate 2138MB (the "VIRT" column) and is only able to keep 1.6GB (the "RES" column) in RAM. (citation)

Each time the script accesses something that is not in RAM, a page fault is generated telling the OS that it needs to swap in a bit more of the processes memory off of the disk.

Back to your question:

But I'm hoping someone can point me at some Linux commands that I can use to investigate what might be causing these page faults.

You might want to see how big a simple PHP program is. It could be something like a variable that has data appended to it rather than erased and overwritten, or it could be you need to limit the size of the data set you're processing at once.

If you need more assistance, you might want to pose this question to a PHP specific forum.

1
  • Thanks Dan. The most helpful part of your answer was the second paragraph about VIRT and RES. With this information in hand, I was able to focus my search on memory leaks ... and ended up finding one that brought us down to about 300MB VIRT. – Alan C. Jul 25 '14 at 21:09
0

as you can see, the apache process is using 86% of memory:

  • either the script is buggy and using too much memory because of bad design,
  • or you should consider some hw upgrade (add memory).

if the memory usage is not increasing with time going, adding memory would fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.