phpday 2027 - Call for Papers

getrusage

(PHP 4, PHP 5, PHP 7, PHP 8)

getrusage — Ottiene l'utilizzo corrente delle risorse

Descrizione

function getrusage(int $mode = 0): array|false

Questa è un'interfaccia a getrusage(2). Ottiene i dati restituiti dalla chiamata di sistema.

Elenco dei parametri

mode

Se mode è 1, getrusage verrà chiamata con RUSAGE_CHILDREN.

Valori restituiti

Restituisce un array associativo contenente i dati restituiti dalla chiamata di sistema. Tutte le voci sono accessibili usando i nomi dei campi documentati. Restituisce false in caso di errore.

Esempi

Example #1 Esempio di getrusage()

<?php
$dat = getrusage();
echo $dat["ru_oublock"];       // numero di operazioni di output a blocchi
echo $dat["ru_inblock"];       // numero di operazioni di input a blocchi
echo $dat["ru_msgsnd"];        // numero di messaggi IPC inviati
echo $dat["ru_msgrcv"];        // numero di messaggi IPC ricevuti
echo $dat["ru_maxrss"];        // dimensione massima del resident set
echo $dat["ru_ixrss"];         // dimensione integrale della memoria condivisa
echo $dat["ru_idrss"];         // dimensione integrale dei dati non condivisi
echo $dat["ru_minflt"];        // numero di recuperi di pagina (soft page fault)
echo $dat["ru_majflt"];        // numero di page fault (hard page fault)
echo $dat["ru_nsignals"];      // numero di segnali ricevuti
echo $dat["ru_nvcsw"];         // numero di cambi di contesto volontari
echo $dat["ru_nivcsw"];        // numero di cambi di contesto involontari
echo $dat["ru_nswap"];         // numero di swap
echo $dat["ru_utime.tv_usec"]; // tempo utente utilizzato (microsecondi)
echo $dat["ru_utime.tv_sec"];  // tempo utente utilizzato (secondi)
echo $dat["ru_stime.tv_usec"]; // tempo di sistema utilizzato (microsecondi)
echo $dat["ru_stime.tv_sec"];  // tempo di sistema utilizzato (secondi)
?>

Note

Nota:

Su Windows getrusage() restituirà solo i seguenti membri:

  • "ru_stime.tv_sec"
  • "ru_stime.tv_usec"
  • "ru_utime.tv_sec"
  • "ru_utime.tv_usec"
  • "ru_majflt" (solo se mode è RUSAGE_SELF)
  • "ru_maxrss" (solo se mode è RUSAGE_SELF)

Se getrusage() viene chiamata con mode impostato a 1 (RUSAGE_CHILDREN), allora viene raccolto l'utilizzo delle risorse dei processi figli.

Nota:

Su BeOS 2000, vengono restituiti solo i seguenti membri:

  • "ru_stime.tv_sec"
  • "ru_stime.tv_usec"
  • "ru_utime.tv_sec"
  • "ru_utime.tv_usec"

Vedere anche:

  • La pagina man del proprio sistema su getrusage(2)

+add a note

User Contributed Notes 3 notes

up
11
jlh at gmx dot ch
9 years ago
Note that this function returns rusage of the current process. In a web environment where you have long running apache processes that serve several requests with PHP executions, this will return cumulative timings and is therefore not suitable for telling how much user time your used. The best you could do is to call getrusage() once at the beginning and once at the end and calculate the difference.
up
2
Anonymous
8 years ago
up
3
Domas Mituzas
18 years ago
getrusage() reports kernel counters that are updated only once application loses context and a switch to kernel space happens. For example on modern Linux server kernels that would mean that getrusage() calls would return information rounded at 10ms, desktop kernels - at 1ms.

getrusage() isn't usable for micro-measurements at all - and getmicrotime(true) might be much more valuable resource.
To Top