0

I have to measure the memory usage and the execution time of the process executed by proc_open(). Measuring the time is not that hard, but I can't find any method to get the memory usage. (the program can terminate very quickly)

Is there any method to get the memory usage of a process executed by proc_open()?

My OS is windows for some reason..

Love Paper
  • 471
  • 1
  • 4
  • 15

1 Answers1

1

@love-paper, make use of the descriptorspec parameters for proc_open to write the memory usage to a file, that you can then read in.

parent process

$child_id = uniqid();
$descriptorspec = array(
   2 => array("file", "/tmp/$child_id", "a") // stderr is a file to write to
);
proc_open($cmd, $descriptorspec);

$memory_used = file_get_contents("/tmp/$child_id");

child process

// at the end of the script
file_put_contents('php://stderr', memory_get_peak_usage(true));
toneplex
  • 673
  • 5
  • 6
  • Unfortunately, it's just a process, not made by php. I'm sorry that I didn't mention it. – Love Paper Dec 30 '12 at 15:40
  • Can you modify the child process to record its memory usage, or is it a black box? – toneplex Dec 30 '12 at 17:22
  • It's a black box, so I can't do anything with it. – Love Paper Dec 31 '12 at 03:30
  • If you are on a linux box, then use either `/usr/bin/time -v` or `memusg`. See this earlier answer for more details: http://stackoverflow.com/questions/774556/peak-memory-usage-of-a-linux-unix-process – toneplex Dec 31 '12 at 13:01
  • I already saw that, but my server is on windows. I tried to use 'tasklist', but the process terminates too fast. – Love Paper Dec 31 '12 at 14:05
  • Have you tried to use perfmon.exe [http://stackoverflow.com/questions/69332/tracking-cpu-and-memory-usage-per-process] – toneplex Dec 31 '12 at 14:53
  • I've tried it, but the processes are created and deleted by the script so many times, so I'm not sure it's okay to get the memory usage. – Love Paper Dec 31 '12 at 15:59
  • Why do you have to measure the memory usage of the process? Is it for scale purposes, so you can how the system resources are being used? – toneplex Dec 31 '12 at 16:04
  • I'm making a tester of my program (implemented by C++), which gives my program some inputs and gets outputs from the program. I used PHP because I wanted to test on the web. – Love Paper Dec 31 '12 at 16:23
  • In that case, I would use your C++ program to log its cpu and memory usage. Then parse the log in a separate step. – toneplex Dec 31 '12 at 18:52