Current Location: Home> Latest Articles> getrusage() as a Replacement for External Monitoring Tools Like top or htop? How to Implement It in PHP

getrusage() as a Replacement for External Monitoring Tools Like top or htop? How to Implement It in PHP

M66 2025-06-27

In Linux systems, top and htop are commonly used external tools that help users monitor system performance in real time, showing detailed information about CPU, memory, processes, and more. The getrusage() function provided by PHP can retrieve information about process resource usage. So, can getrusage() replace external monitoring tools like top or htop? In this article, we will explore this question and discuss how to implement similar features in PHP.

1. Introduction to the getrusage() Function

getrusage() is a built-in function in PHP that returns detailed information about the resource usage of a process. It is typically used to obtain CPU time, memory usage, and other metrics for the running process. However, it does not provide system-level real-time monitoring data like top or htop. It only reports resource usage for the PHP process calling the function, not the overall system resource status.

Return Values

The getrusage() function returns an array containing various statistics about the resource usage of the process. The array usually includes:

  • ru_maxrss: Maximum resident set size of the process (in bytes)

  • ru_ixrss: Shared memory size used by the process (in bytes)

  • ru_idrss: Unshared memory size used by the process (in bytes)

  • ru_isrss: Memory used by shared libraries (in bytes)

  • ru_minflt: Number of page faults not requiring I/O (user mode)

  • ru_majflt: Number of page faults requiring I/O (kernel mode)

  • ru_nswap: Number of swaps

  • ru_inblock: Number of input block operations

  • ru_oublock: Number of output block operations

  • ru_msgsnd: Number of messages sent

  • ru_msgrcv: Number of messages received

  • ru_nsignals: Number of signals received

  • ru_nvcsw: Number of voluntary context switches (user mode)

  • ru_nivcsw: Number of involuntary context switches (kernel mode)

2. Differences Between getrusage() and top / htop

Although getrusage() provides some process-level resource usage data, it differs from external tools like top and htop in several key ways:

  • Monitoring Scope: getrusage() can only obtain resource usage information for the current PHP process, while top and htop display system-wide resource usage, including all running processes.

  • Real-time Updates: top and htop provide continuously updated, dynamic monitoring data. In contrast, getrusage() returns resource data at the moment it is called and cannot update dynamically.

  • Granularity of Monitoring: top and htop offer more detailed process information, such as process status, CPU percentage, and memory usage, whereas getrusage() provides simpler, less detailed data.

  • System Information: top and htop also display system-level hardware resource usage, such as total memory, swap space, and CPU load. getrusage() only provides resource usage related to the individual process.

3. Implementing Process Monitoring in PHP

Although getrusage() cannot fully replace top or htop, it can still offer useful information for developers, especially when performing performance analysis on specific PHP scripts.

Example: Getting Resource Usage of the Current PHP Script

<?php
// Get resource usage of the current PHP process
$usage = getrusage();
<p>// Display resource usage<br>
echo "Maximum resident set size: " . $usage["ru_maxrss"] . " bytes\n";<br>
echo "User CPU time: " . $usage["ru_utime.tv_sec"] . " seconds\n";<br>
echo "System CPU time: " . $usage["ru_stime.tv_sec"] . " seconds\n";<br>
?><br>
</span>

In the example above, we call getrusage() to get the current PHP process's resource usage and print out the maximum resident set size and CPU time information. These data help developers understand the resource consumption of the script during execution.

Example: Real-Time Monitoring of Memory and CPU Usage in PHP Scripts

If you need to monitor resource usage during the execution of a PHP script, you can periodically call getrusage() and calculate the difference in resource usage. For example, you could get resource data inside a loop at intervals to create a simple monitoring system:

<?php
function get_usage() {
    return getrusage();
}
<p>$startUsage = get_usage();</p>
<p>// Simulate PHP script execution<br>
sleep(2);  // Assume script runs for 2 seconds</p>
<p>$endUsage = get_usage();</p>
<p>// Calculate differences<br>
echo "Memory usage change: " . ($endUsage['ru_maxrss'] - $startUsage['ru_maxrss']) . " bytes\n";<br>
echo "CPU time change (user mode): " . ($endUsage['ru_utime.tv_sec'] - $startUsage['ru_utime.tv_sec']) . " seconds\n";<br>
echo "CPU time change (system mode): " . ($endUsage['ru_stime.tv_sec'] - $startUsage['ru_stime.tv_sec']) . " seconds\n";<br>
?><br>

This approach allows you to roughly track resource changes during the execution of a PHP script.

4. Limitations of Using getrusage() as a Replacement for top or htop

While getrusage() can provide some process monitoring features in PHP, it cannot replace the full functionality of tools like top or htop. For developers, getrusage() serves more as a performance tuning aid rather than a complete system monitoring tool. If you need comprehensive, real-time system monitoring, top and htop are definitely more suitable.

However, if you want to perform process-level resource monitoring within PHP, getrusage() remains a very useful tool, especially when executing critical operations where it helps developers gather and analyze performance bottlenecks in real time.

Conclusion

getrusage() can provide certain process resource usage information, but it cannot fully replace system-level monitoring tools like top or htop. If your goal is to monitor the performance of specific PHP scripts, it is an effective tool. However, for comprehensive and real-time system monitoring, top and htop remain the better options.