diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index 625bcbf251d..22ca2646189 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -260,7 +260,7 @@ All rights reserved. xhprof XHProf A Hierarchical Profiler for PHP. - 2.3.5 + 2.3.9 Apache 2.0 https://github.com/longxinH/xhprof diff --git a/lib/xhprof/README.md b/lib/xhprof/README.md index 710e0ecbfe3..9a6f342bf5a 100644 --- a/lib/xhprof/README.md +++ b/lib/xhprof/README.md @@ -1,17 +1,17 @@ -# xhprof for PHP7 -[![Build Status](https://travis-ci.com/longxinH/xhprof.svg?branch=master)](https://travis-ci.com/longxinH/xhprof) [![Build status](https://ci.appveyor.com/api/projects/status/dornfeel5yutaxte/branch/master?svg=true)](https://ci.appveyor.com/project/longxinH/xhprof/branch/master) +# xhprof for PHP7 and PHP8 +[![Build Status](https://travis-ci.com/longxinH/xhprof.svg?branch=master)](https://app.travis-ci.com/github/longxinH/xhprof) [![Build status](https://ci.appveyor.com/api/projects/status/dornfeel5yutaxte/branch/master?svg=true)](https://ci.appveyor.com/project/longxinH/xhprof/branch/master) XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based navigational interface. The raw data collection component is implemented in C (as a PHP extension). The reporting/UI layer is all in PHP. It is capable of reporting function-level inclusive and exclusive wall times, memory usage, CPU times and number of calls for each function. Additionally, it supports ability to compare two runs (hierarchical DIFF reports), or aggregate results from multiple runs. -This version supports PHP7 +This version supports PHP7 and PHP8 # PHP Version -- 7.0 -- 7.1 - 7.2 - 7.3 - 7.4 - 8.0 +- 8.1 +- 8.2 # Installation ``` diff --git a/lib/xhprof/readme_moodle.txt b/lib/xhprof/readme_moodle.txt index 5d2fba90e5b..8d872b7459d 100644 --- a/lib/xhprof/readme_moodle.txt +++ b/lib/xhprof/readme_moodle.txt @@ -1,4 +1,4 @@ -Description of XHProf 2.3.5 library/viewer import into Moodle +Description of XHProf 2.3.9 library/viewer import into Moodle Removed (commit #1): * examples - examples dir removed completely @@ -41,3 +41,4 @@ TODO: 20201210 - MDL-70297 - Ilya Tregubov (ilyatregubov): Upgrade to 2.2.3 release; 20210209 - MDL-70525 - Tomo Tsuyuki (tomotsuyuki): Allow huge number of values 20211209 - MDL-71705 - Ilya Tregubov (ilyatregubov): Upgrade to 2.3.5 release; +20221214 - MDL-76397 - Stevani Andolo (stevandoMoodle): Upgrade to 2.3.9 release; diff --git a/lib/xhprof/xhprof_html/css/xhprof.css b/lib/xhprof/xhprof_html/css/xhprof.css index 250b9903fc3..6eb8eb05c75 100644 --- a/lib/xhprof/xhprof_html/css/xhprof.css +++ b/lib/xhprof/xhprof_html/css/xhprof.css @@ -15,7 +15,7 @@ /* Start moodle modification: add basic, smaller, font specs */ body, p, table, li { - font: normal normal normal 13px/1.231 arial, helvetica, clean, sans-serif; + font: normal normal normal 13px/1.231 arial, helvetica, clean, sans-serif; } /* End moodle modification */ diff --git a/lib/xhprof/xhprof_html/docs/index-fr.html b/lib/xhprof/xhprof_html/docs/index-fr.html new file mode 100644 index 00000000000..5c1ea2214a5 --- /dev/null +++ b/lib/xhprof/xhprof_html/docs/index-fr.html @@ -0,0 +1,738 @@ + + + + + +Documentation XHProf (Brouillon) + + +

Documentation XHProf (Brouillon)

+ +Sommaire +
    +
  1. Introduction
  2. + +
  3. Présentation
  4. + +
  5. Installer l’extension XHProf
  6. + +
  7. Profiler avec XHProf
  8. + +
  9. Définir un environnement graphique pour XHProf
  10. + +
  11. Notes sur l’utilisation d’XHProf en production
  12. + +
  13. Mode aléatoire et léger + +
  14. Fonctionnalités supplémentaires
  15. + +
  16. Dépendences
  17. + +
  18. Remerciements
  19. +
+ +
    +
  1. Introduction

    + +

    XHProf est un outil de profilage hiérarchique pour PHP. Il relève +les appels au niveau des fonctions et mesure inclusivement et +exclusivement des métriques telles que le temps écoulé +la charge CPU ou l’usage de la mémoire. Un profil de fonction peut être divisé selon ses appelants, ou ses appelés. Le composant qui extrait les données brutes est écrit en C +et implémenté telle une extension PHP Zend. +xhprof. XHProf a une interface utilisateur simple en HTML, (écrite en PHP). +L’interface permet de visualiser et de partager facilement le résultat des profilages dans un navigateur. +Un rendu sous forme de graphique est également disponible. + +

    Les rapports fournis par XHProf permettent souvent de mieux comprendre +la structure du code qui est éxécuté. +Le rendu hiérarchique des rapports permet par exemple de déterminer +quelle chaîne d’appels mène à une fonction particulière. + +

    XHProf propose également de comparer deux runs (résultat de profilage) +pour analyser les différences ou aggréger les résultat de multiples runs afin +d’analyser des données consolidées. +Les comparaisons et aggrégations de données permettent surtout de visualiser des données plates + +

    XHProf est un outil de profilage très léger. Pendant la phase de collecte +Il garde une trace du nombre d’appels et des métriques inclusives viualisables en courbes dans le graphe d’appels dynamique d’un programme. +Il calcule les métriques exclusives dans la phase de rapport. +XHProf supporte les fonctions recursives en détectant les cycles dans la pile d’appels dès la capture des données et en utilisant un nom unique pour l’invocation principale.

    + +

    La nature légère d’XHProf, ses performances et ses possibilités de consolidations de données +en font un outil taillé pour les environnements de production [Voir les notes sur l’usage en production.] + +

    + + +

    Développé à l’origine par Facebook, XHProf est maintenant open source depuis mars 2009.

    + + + + + +
  2. Présentation

    + +

    XHProf offre: + +

    + +

    Terminologie

    +
      + +
    1. Temps inclusive (ou temps du sous-ensemble): +Inclus le temps passé dans la fonction et celui passé dans les fonctions descendantes (filles). + +
    2. Temps exclusive (ou temps "propre"): Mesure le temps passé dans la fonction elle-même et n’inclus pas le temps passé dans les fonctions descendantes. + +
    3. Wall Time: Temps passé ou temps ressenti. + +
    4. CPU Time: Charge CPU sur les process utilisateur + charge CPU sur les process noyaux + +
    +

    Convention de nommage pour les fonctions spéciales

    + +
      +

    1. main(): Une fonction fictive qui est à la racine de la pile d’appel. + + +

    2. load::<filename> +et run_init::<filename>: + +

      XHProf trace les appels include/require comme des appels de fonction. + +

      Par exemple, une inclusion include "lib/common.php"; va donner deux entrées pour XHProf : + +

        + +
      • load::lib/common.php - Cela représente le travail fait par l’interpréteur pour charger et compiler le fichier. +[Note: Si vous utilisez un cache d’opcode PHP comme APC, alors la compilation intervient uniquement si le cahce est manquant dans APC.] + +
      • run_init::lib/common.php - Cela répresente le code exécuté au niveau du fichier, soit le résultat de l’inclusion. + +
      + +

    3. foo@<n>: Implique un appel récursif de foo(), ou <n> représente le niveau de récursion. +Cette récursion peut être directe comme foo() --> foo()), ou indirecte comme foo() --> goo() --> foo(). + +
    + + +

    Limitations

    + +

    Un vrai profileur hiérarchique trace toute la pile d’appel pour chaque donnée., et est capables de répondre aux questions comme : Quel était le coût du 3e appel de foo(), ou quel était le coût de bar() quand il était appelé par a()->b()->bar()? + +

    + +

    XHProf garde une trace d’un seul niveau dans le contexte de l’appel et est seulement capable de répondre aux questions à propos +d’une fonction qui regarde un niveau en dessus ou en dessous. +Il appraît que dans la majorité des cas c’est bien suffisant. +

    + +

    Pour mieux comprendre, regaredez l’exemple suivant : +

    + +
    +Vous avez:
    + 1 appel de a() --> c()
    + 1 appel de b() --> c()
    + 50 appels de c() --> d()
    +
    + +

    Quand XHProf peut vous dire que d() a été appelé par c() 50 fois, il ne peut pas vous dire +combien d’appels dont dus à a() ou b(). +[On peut imaginer que c’est peut être 25 pour a() et 25 pour b(), mais ce n’est pas nécéssairement vrai.] +

    + +

    De toutes façons en pratique ce n’est pas vraiment une limitation. +

    + +
  3. Installer l’extension XHProf

    + +

    L’extension se trouve dans le sous-répertoire "extension/". + +

    + +

    Les étapes suivantes sont prévues pour un environnement Linux/Unix. + + +

    +% cd <repertoire_source_xhprof>/extension/
    +% phpize
    +% ./configure --with-php-config=<chemin vers php-config>
    +% make
    +% make install
    +% make test
    +
    + + +

    php.ini file: Vous pouvez mettre à jour votre fichier +php.ini file afin qu’il charge automatiquement votre extension en ajoutant le code suivant : + +

    +[xhprof]
    +extension=xhprof.so
    +;
    +; répertoire utilisé par l’implémentation par défaut de l’interface iXHProfRuns
    +; (nommée, XHProfRuns_Default class) pour stocker les runs XHProf.
    +;
    +xhprof.output_dir=<repertoire_pour_stocker_les_runs_xhprof>
    +
    + + +
  4. Profiler avec XHProf

    + +

    Test de génération de donées brutes avec l’exemple simple d’un programme tel que : + +

    + +

    Lancez ce programme : + +

    +% php -dextension=xhprof.so foo.php
    +
    + +

    Vous devez avoir un résultat tel que : + +

    +Array
    +(
    +    [foo==>bar] => Array
    +        (
    +            [ct] => 2         # 2 appels de bar() depuis foo()
    +            [wt] => 27        # temps inclusif dans bar() quand il est appelé par foo()
    +        )
    +
    +    [foo==>strlen] => Array
    +        (
    +            [ct] => 2
    +            [wt] => 2
    +        )
    +
    +    [bar==>bar@1] => Array    # un appelrécursif à bar()
    +        (
    +            [ct] => 1
    +            [wt] => 2
    +        )
    +
    +    [main()==>foo] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 74
    +        )
    +
    +    [main()==>xhprof_disable] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 0
    +        )
    +
    +    [main()] => Array         # fausse fonction représentant la racine
    +        (
    +            [ct] => 1
    +            [wt] => 83
    +        )
    +
    +)
    +
    + +

    Note: Les données brutes contienent uniquement les métriques inclusives. +Par exemple les données brutes du tableau de données temporelles represente les temps inclusifs en microsecondes. +Les temps exclusifs sont calculés pour chaque fonction lors de la phase d’analyse et de rapport. + +

    Note: Par défault suelemnt le nombre d’appel & et le temps passé sont profilés. +Vous pouvez aussi profilerle temps CPU et/ou la charge mémoire. Remplacez, + +

    +dans le programme précédent avec, par exemple : + + +

    Vous aurez en sortie : + +

    +Array
    +(
    +    [foo==>bar] => Array
    +        (
    +            [ct] => 2        # nombre d’appel à bar() depuis foo()
    +            [wt] => 37       # tempas passé dans bar() quand appel de foo()
    +            [cpu] => 0       # temps cpu time dans bar() quand appel de foo()
    +            [mu] => 2208     # changement dans l’usage de la mémoire par PHP dans bar() quand appel de foo()
    +            [pmu] => 0       # changement dans l’usage de pic mémoire par PHP pour bar() quand appel de foo()
    +        )
    +
    +    [foo==>strlen] => Array
    +        (
    +            [ct] => 2
    +            [wt] => 3
    +            [cpu] => 0
    +            [mu] => 624
    +            [pmu] => 0
    +        )
    +
    +    [bar==>bar@1] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 2
    +            [cpu] => 0
    +            [mu] => 856
    +            [pmu] => 0
    +        )
    +
    +    [main()==>foo] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 104
    +            [cpu] => 0
    +            [mu] => 4168
    +            [pmu] => 0
    +        )
    +
    +    [main()==>xhprof_disable] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 1
    +            [cpu] => 0
    +            [mu] => 344
    +            [pmu] => 0
    +        )
    +
    +    [main()] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 139
    +            [cpu] => 0
    +            [mu] => 5936
    +            [pmu] => 0
    +        )
    +
    +)
    +
    + +

    Éviter les fonctions natives lors du profilage + +

    Par défault les fonctions natives de PHP (comme strlen) sont profilées. +Si vous ne voulez pas les profiler (pour simplifier le résultat et la taille des données brutes générées), +Vous pouvez utiliser le drapeau XHPROF_FLAGS_NO_BUILTINS comme dans l’exemple ci-dessous : + +

    + + +

    Ignorer des fonctions spécfiques lors du profilage (0.9.2 ou plus récent) + +

    À partir de la version 0.9.2 d’XHProf, vous pouvez spécifier une liste de +fonctions à ignorer pendant le profilage. Cela vous permet de ne pas prendre en compte par exemple +des fonctions utilisées pour des appels indirects comme call_user_func et call_user_func_array. +Ces fonctions intermédiaires compliquent inutilement la hirarchie des appels et rendent plus ardue l’interprétation des rapports en brouillant les relations parent/enfant. + +

    Pour spécifier cette liste de fonctions à ignorer durant le profilage, il suffit d’utiliser le second paramètre (optionnel) de xhprof_enable. +Par exemple, + +

    +
      +
      +// temps passé en profilage; ignore les appels de call_user_func* pendant le profilage
      +xhprof_enable(0,
      +             array('ignored_functions' =>  array('call_user_func',
      +                                                 'call_user_func_array')));
      +
      +or,
      +
      +// tempas pasé en profilage + profilage mémoire; ignore call_user_func* durant le profilage
      +xhprof_enable(XHPROF_FLAGS_MEMORY,
      +              array('ignored_functions' =>  array('call_user_func',
      +                                                  'call_user_func_array')));
      +
      +
    + + +
  5. + +
  6. Définir un environnement graphique pour XHProf

    + + +
      + +
    1. Structure de la source PHP +

      l’interface graphique d’XHProf est implémentée en PHP. Le code est divisé en deux sous-répertoires, +xhprof_html/ and xhprof_lib/. + +

      Le répertoire xhprof_html contient les 3 pages PHP principales. + +

        +
      • index.php: Pour visualiser un run ou un différentiel entre deux runs. +
      • callgraph.php: Pour visualiser sous la forme de graphique avec un rendu en image. +
      • typeahead.php: Utilisé implicitement pour les fonctions de gestion de pile sur un rapport XHProf. +
      + +

      Le répertoire xhprof_lib contient le code pour l’analyse et l’affichage. +(calcul sur les informations de profilage, calcul des différentiels, aggrégation de données, etc.). + +

    2. Configuration du server web : Vous devez vous assurer que le répertoire +xhprof_html/ est accessible depuis le serveur web, et qu’il est configuré pour éxécuter des scripts PHP. + +

    3. Gérer les runs XHProf + +

      Les clients web ont une certaine souplesse dans la manière de sauvegarder les données brutes fournies par XHProf. +XHProf expose une interface utilisateur nommée iXHProfRuns (voir xhprof_lib/utils/xhprof_runs.php) que les clients peuvent implémenter. +Cela permet aux clients de préciser comment afficher les donées des runs. + +

      L’interface utilisateur d’XHProf fournit une implementation par défaut nommée, +"XHProfRuns_Default" (aussi dans xhprof_lib/utils/xhprof_runs.php). +L’implementation par d"faut stocke les runs dans le répertoire définit par le paramètre INI : +xhprof.output_dir. + +

      Un run XHProf doit être définit de manière unique par un espace de nom et un identifiant de run. + +

      a) Sauver les données XHProf de façon persistente : + +

      Soit si vous utilisez l’interface par défaut, +XHProfRuns_Default qui implémente +iXHProfRuns, Un run XHProf sauvegardé ressemble au code suivant : + + +

      +// début du profilage
      +xhprof_enable();
      +
      +// lancement du programme
      +...
      +
      +// fin du profilage
      +$xhprof_data = xhprof_disable();
      +
      +//
      +// Sauvegarde du run XHProf
      +// en utilisant l’implementation par défaut de iXHProfRuns.
      +//
      +include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
      +include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
      +
      +$xhprof_runs = new XHProfRuns_Default();
      +
      +// sauvegarde du run avec l’espace de nom "xhprof_foo".
      +//
      +// **NOTE**:
      +// par défault save_run() va automatiquement générer un identifiant de run
      +// unique. [Vous pouvez surcharger cette donnée en passant l’identifiant en paramètre optionnel 
      +// à la méthode save_run().]
      +//
      +$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
      +
      +echo "---------------\n".
      +     "En partant du principe que vous avez parametré l’interface utilisateur http \n".
      +     "XHProf, vous pouvez visualiser les runs avec l’adresse : \n".
      +     "http://<adresse-interface-utilisateur-xhprof>/index.php?run=$run_id&source=xhprof_foo\n".
      +     "---------------\n";
      +
      +
      + +

      La suite permet de sauvegarder le run sous forme d’un fichier dans le répertoire spécifié +par le paramètre ini xhprof.output_dir. Le nom du fichier doit être de la forme +49bafaa3a3f66.xhprof_foo; Les deux parties du nom sont formées par l’identifiant du run +("49bafaa3a3f66") et l’espace de nom ("xhprof_foo"). [Si vous souhaitez créer un identifiant de run vous-même +(comme une sequence de base de données, ou un timestamp), vous pouvez explicitementpasser l’identifiant +du run à la méthode save_run. + +

      b) En utilisant votre propre implementation d’iXHProfRuns + +

      Si vous décidez de stocker différement les runs XHProf +(soit dans un format compressé, dans une base de données, +etc.), vous aurez besoin d’implémenter une classe qui implémente l’interface +iXHProfRuns(). + +

      Vous devrez également modifier les 3 pages PHP d’entrée (index.php, +callgraph.php, typeahead.php) dans le répertoire "xhprof_html/" pour utiliser la +nouvelle interface au lieu de celle par défaut (XHProfRuns_Default), +changez cette ligne dans les 3 fichier. + +

      +$xhprof_runs_impl = new XHProfRuns_Default();
      +
      + +

      Vous aurez aussi besoin d’inclure le fichier qui implémente votre classe dans les fichiers cités. + +

    4. Acceéder aux runs depuis l’interface utilisateur + +

      a) Voir un rapport simple + +

      Pour voir un rapport avec l’identifiant <run_id> et l’espace de nom +<namespace> utilisez une url de la forme : + +

      +http://<adresse-interface-utilisateur-xhprof>/index.php?run=<run_id>&source=<namespace> + + +

      Par example, +

      +http://<adresse-interface-utilisateur-xhprof>/index.php?run=49bafaa3a3f66&source=xhprof_foo + + +

      b) Voir un rapport différentiel + +

      Pour voir un rapport avec les identifiants <run_id1> et +<run_id2> et l’espace de nom <namespace> utilisez une url de la forme : + +

      +http://<adresse-interface-utilisateur-xhprof>/index.php?run1=<run_id1>&run2=<run_id2>&source=<namespace> + + +

      c) Voir un rapport d’aggrégation + +

      Vous pouvez aussi spécifier un ensemble de runspour lesquels vous souhaitez un rapport d’aggrégation. + +

      Si vous avez trois runs XHProf avec les identifiants 1, 2 & 3 pour l’espace de noms +"benchmark". Pour voir l’aggrégation de ces trois runs : + +

        +http://<adresse-interface-utilisateur-xhprof>/index.php?run=1,2,3&source=benchmark +

      + +

      Aggrégations pondérées: En supposant que les trois runs +correspondent à trois types de programmes p1.php, p2.php and p3.php +qui occupent chacun respectivement 20%, 30% et 50%. Pour voir un rapport d’aggrégation +pondéré par les poids des runs : + +

        +http://<adresse-interface-utilisateur-xhprof>/index.php?run=1,2,3&wts=20,30,50&source=benchmark +

      + +
    + +
  7. Notes sur l’utilisation d’XHProf en production

    + +

    Quelques observations qui peuvent faire varier votre expérience : + +

    + +
  8. Mode d’échantillonage léger

    + +

    L’extension XHProf propose aussi un mode très léger d’échantillonage. +L’intervalle est de 0,1 seconde. Les échantillons enregistrent l’ensemble des données. +Ce mode peut être très utile pour avoir un impact le plus négligeable possible, et permettre +Le mode sample peut être utile si vous désirez un moyen avec peu de dépassement de faire de la surveillance de performances et des diagnostics. + +

    Les très pertinentes fonctions utilisées par l’extension pour utiliser le mode +d’échantillonage sont xhprof_sample_enable() et xhprof_sample_disable(). + +

    [TBD: Documentation plus détaillée pour le mode d’échantillonage.] + +

  9. Fonctionnalités supplémentaires

  10. + +

    Le fichier XHProf_lib/utils/xhprof_lib.php contient +des librairies de fonctions additionellesqui peuvent être utilisées pour manipuler +et aggréger les runs XHProf. + +

    Par exemple: + +

    + +
      + +
    + +
  11. Dependances

  12. + + +
  13. Remerciements

    + +

    Le rendu HTML et l’interface de navigation pour consulter les résultat du profilage sont inspirés par un outil similaire +qui existe pour les procédures stockées PL/SQL d’Oracle. Mais c’est là que la comparaison s’arrête; +Le fonctionnement interne du profileur étant assez différent + +[NDT : Merci à Rudy Rigot (@rudyrigot) pour sa relecture attentive ] +

  14. + +
+ + + diff --git a/lib/xhprof/xhprof_html/docs/index.html b/lib/xhprof/xhprof_html/docs/index.html new file mode 100644 index 00000000000..327a1065e6d --- /dev/null +++ b/lib/xhprof/xhprof_html/docs/index.html @@ -0,0 +1,826 @@ + + +XHProf Documentation (Draft) + + +

XHProf Documentation (Draft)

+ +Contents +
    +
  1. Introduction
  2. + +
  3. XHProf Overview
  4. + +
  5. Installing the XHProf extension
  6. + +
  7. Profiling using XHProf
  8. + +
  9. Setting up the XHProf UI
  10. + +
  11. Notes on using XHProf in production
  12. + +
  13. Lightweight Sampling Mode + +
  14. Additional features
  15. + +
  16. Dependencies
  17. + +
  18. Acknowledgements
  19. +
+ +
    +
  1. Introduction

    + +

    XHProf is a hierarchical profiler for PHP. It reports +function-level call counts and inclusive and +exclusive metrics such as wall (elapsed) +time, CPU time and memory usage. A function's profile can be broken +down by callers or callees. The raw data collection component is +implemented in C as a PHP Zend extension called +xhprof. XHProf has a simple HTML based user +interface (written in PHP). The browser based UI for viewing profiler +results makes it easy to view results or to share results with peers. +A callgraph image view is also supported. + +

    XHProf reports can often be helpful in understanding the structure +of the code being executed. The hierarchical nature of the reports can +be used to determine, for example, what chain of calls led to a +particular function getting called. + +

    XHProf supports ability to compare two runs (a.k.a. "diff" reports) +or aggregate data from multiple runs. Diff and aggregate reports, much +like single run reports, offer "flat" as well as "hierarchical" views +of the profile. + +

    XHProf is a light-weight instrumentation based profiler. During the +data collection phase, it keeps track of call counts and inclusive +metrics for arcs in the dynamic callgraph of a program. It computes +exclusive metrics in the reporting/post processing phase. XHProf +handles recursive functions by detecting cycles in the callgraph at +data collection time itself and avoiding the cycles by giving unique +depth qualified names for the recursive invocations. +

    + +

    XHProf's light-weight nature and aggregation capabilities make it +well suited for collecting "function-level" performance statistics +from production environments. [See additional notes for use in production.] + +

    + + +

    Originally developed at Facebook, XHProf was open sourced in Mar, 2009.

    + + + + + +
  2. XHProf Overview

    + +

    XHProf provides: + +

    + +

    Terminology

    +
      + +
    1. Inclusive Time (or Subtree Time): +Includes time spent in the function as well as in descendant functions +called from a given function. + +
    2. Exclusive Time/Self Time: Measures +time spent in the function itself. Does not include time in descendant +functions. + +
    3. Wall Time: a.k.a. Elapsed time or wall clock time. + +
    4. CPU Time: CPU time in user space + CPU time in kernel space + +
    +

    Naming convention for special functions

    + +
      +

    1. main(): a fictitious function that is at the root of the call graph. + + +

    2. load::<filename> +and run_init::<filename>: + +

      XHProf tracks PHP include/require operations as +function calls. + +

      For example, an include "lib/common.php"; operation will +result in two XHProf function entries: + +

        + +
      • load::lib/common.php - This represents the work done by the +interpreter to compile/load the file. [Note: If you are using a PHP +opcode cache like APC, then the compile only happens on a cache miss +in APC.] + +
      • run_init::lib/common.php - This represents +initialization code executed at the file scope as a result of the +include operation. + +
      + +

    3. foo@<n>: Implies that this is a +recursive invocation of foo(), where <n> represents +the recursion depth. The recursion may be direct (such as due to +foo() --> foo()), or indirect (such as +due to foo() --> goo() --> foo()). + +
    + + +

    Limitations

    + +

    True hierarchical profilers keep track of a full call stack at +every data gathering point, and are later able to answer questions +like: what was the cost of the 3rd invokation of foo()? or what was +the cost of bar() when the call stack looked like +a()->b()->bar()? + +

    + +

    XHProf keeps track of only 1-level of calling context and is +therefore only able to answer questions about a function looking +either 1-level up or 1-level down. It turns out that in practice this +is sufficient for most use cases. +

    + +

    To make this more concrete, take for instance the following +example. +

    + +
    +Say you have:
    + 1 call from a() --> c()
    + 1 call from b() --> c()
    + 50 calls from c() --> d()
    +
    + +

    While XHProf can tell you that d() was called from c() 50 times, it +cannot tell you how many of those calls were triggered due to a() +vs. b(). [We could speculate that perhaps 25 were due to a() and 25 +due to b(), but that's not necessarily true.] +

    + +

    In practice however, this isn't a very big limitation. +

    + +
  3. Installing the XHProf Extension

    + +

    The extension lives in the "extension/" sub-directory. + +

    + +

    The steps +below should work for Linux/Unix environments. + + +

    +% cd <xhprof_source_directory>/extension/
    +% phpize
    +% ./configure --with-php-config=<path to php-config>
    +% make
    +% make install
    +% make test
    +
    + + +

    php.ini file: You can update your +php.ini file to automatically load your extension. Add the following +to your php.ini file. + +

    +[xhprof]
    +extension=xhprof.so
    +;
    +; directory used by default implementation of the iXHProfRuns
    +; interface (namely, the XHProfRuns_Default class) for storing
    +; XHProf runs.
    +;
    +xhprof.output_dir=<directory_for_storing_xhprof_runs>
    +
    + + +
  4. Profiling using XHProf

    + +

    Test generating raw profiler data using a sample test program like: + +

    + +

    Run the above test program: + +

    +% php -dextension=xhprof.so foo.php
    +
    + +

    You should get an output like: + +

    +Array
    +(
    +    [foo==>bar] => Array
    +        (
    +            [ct] => 2         # 2 calls to bar() from foo()
    +            [wt] => 27        # inclusive time in bar() when called from foo()
    +        )
    +
    +    [foo==>strlen] => Array
    +        (
    +            [ct] => 2
    +            [wt] => 2
    +        )
    +
    +    [bar==>bar@1] => Array    # a recursive call to bar()
    +        (
    +            [ct] => 1
    +            [wt] => 2
    +        )
    +
    +    [main()==>foo] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 74
    +        )
    +
    +    [main()==>xhprof_disable] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 0
    +        )
    +
    +    [main()] => Array         # fake symbol representing root
    +        (
    +            [ct] => 1
    +            [wt] => 83
    +        )
    +
    +)
    +
    + +

    Note: The raw data only contains "inclusive" metrics. For +example, the wall time metric in the raw data represents inclusive +time in microsecs. Exclusive times for any function are computed +during the analysis/reporting phase. + +

    Note: By default only call counts & elapsed time is profiled. +You can optionally also profile CPU time and/or memory usage. Replace, + +

    +in the above program with, for example: + + +

    You should now get an output like: + +

    +Array
    +(
    +    [foo==>bar] => Array
    +        (
    +            [ct] => 2        # number of calls to bar() from foo()
    +            [wt] => 37       # time in bar() when called from foo()
    +            [cpu] => 0       # cpu time in bar() when called from foo()
    +            [mu] => 2208     # change in PHP memory usage in bar() when called from foo()
    +            [pmu] => 0       # change in PHP peak memory usage in bar() when called from foo()
    +        )
    +
    +    [foo==>strlen] => Array
    +        (
    +            [ct] => 2
    +            [wt] => 3
    +            [cpu] => 0
    +            [mu] => 624
    +            [pmu] => 0
    +        )
    +
    +    [bar==>bar@1] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 2
    +            [cpu] => 0
    +            [mu] => 856
    +            [pmu] => 0
    +        )
    +
    +    [main()==>foo] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 104
    +            [cpu] => 0
    +            [mu] => 4168
    +            [pmu] => 0
    +        )
    +
    +    [main()==>xhprof_disable] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 1
    +            [cpu] => 0
    +            [mu] => 344
    +            [pmu] => 0
    +        )
    +
    +    [main()] => Array
    +        (
    +            [ct] => 1
    +            [wt] => 139
    +            [cpu] => 0
    +            [mu] => 5936
    +            [pmu] => 0
    +        )
    +
    +)
    +
    + +

    Skipping builtin functions during profiling + +

    By default PHP builtin functions (such as strlen) are +profiled. If you do not want to profile builtin functions (to either +reduce the overhead of profiling further or size of generated raw +data), you can use the XHPROF_FLAGS_NO_BUILTINS +flag as in for example: + +

    + + +

    Ignoring specific functions during profiling (0.9.2 or higher) + +

    Starting with release 0.9.2 of xhprof, you can tell XHProf to +ignore a specified list of functions during profiling. This allows you +to ignore, for example, functions used for indirect function calls +such as call_user_func and +call_user_func_array. These intermediate functions +unnecessarily complicate the call hierarchy and make the XHProf +reports harder to interpret since they muddle the parent-child +relationship for functions called indirectly. + +

    To specify the list of functions to be ignored during profiling +use the 2nd (optional) argument to xhprof_enable. +For example, + +

    +
      +
      +// elapsed time profiling; ignore call_user_func* during profiling
      +xhprof_enable(0,
      +             array('ignored_functions' =>  array('call_user_func',
      +                                                 'call_user_func_array')));
      +
      +or,
      +
      +// elapsed time + memory profiling; ignore call_user_func* during profiling
      +xhprof_enable(XHPROF_FLAGS_MEMORY,
      +              array('ignored_functions' =>  array('call_user_func',
      +                                                  'call_user_func_array')));
      +
      +
    + + +
  5. + +
  6. Setting up XHProf UI

    + + +
      + +
    1. PHP source structure +

      The XHProf UI is implemented in PHP. The code resides in two +subdirectories, xhprof_html/ and xhprof_lib/. + +

      The xhprof_html directory contains the 3 top-level PHP pages. + +

        +
      • index.php: For viewing a single run or diff report. +
      • callgraph.php: For viewing a callgraph of a XHProf run as an image. +
      • typeahead.php: Used implicitly for the function typeahead form +on a XHProf report. +
      + +

      The xhprof_lib directory contains supporting code for +display as well as analysis (computing flat profile info, computing +diffs, aggregating data from multiple runs, etc.). + +

    2. Web server config: You'll need to make sure that the +xhprof_html/ directory is accessible from your web server, and that +your web server is setup to serve PHP scripts. + +

    3. Managing XHProf Runs + +

      Clients have flexibility in how they save the XHProf raw data +obtained from an XHProf run. The XHProf UI layer exposes an interface +iXHProfRuns (see xhprof_lib/utils/xhprof_runs.php) that clients can +implement. This allows the clients to tell the UI layer how to fetch +the data corresponding to a XHProf run. + +

      The XHProf UI libaries come with a default file based +implementation of the iXHProfRuns interface, namely +"XHProfRuns_Default" (also in xhprof_lib/utils/xhprof_runs.php). +This default implementation stores runs in the directory specified by +xhprof.output_dir INI parameter. + +

      A XHProf run must be uniquely identified by a namespace and a run +id. + + + +

      a) Saving XHProf data persistently: + +

      Assuming you are using the default implementation +XHProfRuns_Default of the +iXHProfRuns interface, a typical XHProf run +followed by the save step might look something like: + + +

      +// start profiling
      +xhprof_enable();
      +
      +// run program
      +....
      +
      +// stop profiler
      +$xhprof_data = xhprof_disable();
      +
      +//
      +// Saving the XHProf run
      +// using the default implementation of iXHProfRuns.
      +//
      +include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
      +include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
      +
      +$xhprof_runs = new XHProfRuns_Default();
      +
      +// Save the run under a namespace "xhprof_foo".
      +//
      +// **NOTE**:
      +// By default save_run() will automatically generate a unique
      +// run id for you. [You can override that behavior by passing
      +// a run id (optional arg) to the save_run() method instead.]
      +//
      +$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
      +
      +echo "---------------\n".
      +     "Assuming you have set up the http based UI for \n".
      +     "XHProf at some address, you can view run at \n".
      +     "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
      +     "---------------\n";
      +
      +
      + +

      The above should save the run as a file in the directory specified +by the xhprof.output_dir INI parameter. The file's +name might be something like +49bafaa3a3f66.xhprof_foo; the two parts being the +run id ("49bafaa3a3f66") and the namespace ("xhprof_foo"). [If you +want to create/assign run ids yourself (such as a database sequence +number, or a timestamp), you can explicitly pass in the run id to the +save_run method. + + +

      b) Using your own implementation of iXHProfRuns + +

      If you decide you want your XHProf runs to be stored differently +(either in a compressed format, in an alternate place such as DB, +etc.) database, you'll need to implement a class that implements the +iXHProfRuns() interface. + +

      You'll also need to modify the 3 main PHP entry pages (index.php, +callgraph.php, typeahead.php) in the "xhprof_html/" directory to use +the new class instead of the default class XHProfRuns_Default. +Change this line in the 3 files. + +

      +$xhprof_runs_impl = new XHProfRuns_Default();
      +
      + +

      You'll also need to "include" the file that implements your class in +the above files. + + +

    4. Accessing runs from UI + +

      a) Viewing a Single Run Report + +

      To view the report for run id say <run_id> and namespace +<namespace> use a URL of the form: + +

      +http://<xhprof-ui-address>/index.php?run=<run_id>&source=<namespace> + + +

      For example, +

      +http://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo + + + +

      b) Viewing a Diff Report + +

      To view the report for run ids say <run_id1> and +<run_id2> in namespace <namespace> use a URL of the form: + +

      +http://<xhprof-ui-address>/index.php?run1=<run_id1>&run2=<run_id2>&source=<namespace> + + +

      c) Aggregate Report + +

      You can also specify a set of run ids for which you want an aggregated view/report. + +

      Say you have three XHProf runs with ids 1, 2 & 3 in namespace +"benchmark". To view an aggregate report of these runs: + +

        +http://<xhprof-ui-address>/index.php?run=1,2,3&source=benchmark +

      + +

      Weighted aggregations: Further suppose that the above three runs +correspond to three types of programs p1.php, p2.php and p3.php that +typically occur in a mix of 20%, 30%, 50% respectively. To view an +aggregate report that corresponds to a weighted average of these runs +using: + +

        +http://<xhprof-ui-address>/index.php?run=1,2,3&wts=20,30,50&source=benchmark +

      + + +
    + +
  7. Notes on using XHProf in production

    + +

    Some observations/guidelines. Your mileage may vary: + +

    + + +
  8. Lightweight Sampling Mode

    + +

    The xhprof extension also provides a very light weight sampling +mode. The sampling interval is 0.1 secs. Samples record the full +function call stack. The sampling mode can be useful if an extremely +low overhead means of doing performance monitoring and diagnostics is +desired. + +

    The relevant functions exposed by the extension for using the +sampling mode are xhprof_sample_enable() and +xhprof_sample_disable(). + + +

    [TBD: more detailed documentation on sampling mode.] + + +

  9. Additional Features

  10. + +

    The xhprof_lib/utils/xhprof_lib.php file contains +additional library functions that can be used for manipulating/ +aggregating XHProf runs. + +

    For example: + +

    + + +
      + +
    + +
  11. Dependencies

  12. + + +
  13. Acknowledgements

    + +

    The HTML-based navigational interface for browsing profiler results +is inspired by that of a similar tool that exists for Oracle's stored +procedure language, PL/SQL. But that's where the similarity ends; the +internals of the profiler itself are quite different. + +

  14. + +
+ + + diff --git a/lib/xhprof/xhprof_html/docs/sample-callgraph-image.jpg b/lib/xhprof/xhprof_html/docs/sample-callgraph-image.jpg new file mode 100644 index 00000000000..073d4eb5765 Binary files /dev/null and b/lib/xhprof/xhprof_html/docs/sample-callgraph-image.jpg differ diff --git a/lib/xhprof/xhprof_html/docs/sample-diff-report-flat-view.jpg b/lib/xhprof/xhprof_html/docs/sample-diff-report-flat-view.jpg new file mode 100644 index 00000000000..6b2cda079be Binary files /dev/null and b/lib/xhprof/xhprof_html/docs/sample-diff-report-flat-view.jpg differ diff --git a/lib/xhprof/xhprof_html/docs/sample-diff-report-parent-child-view.jpg b/lib/xhprof/xhprof_html/docs/sample-diff-report-parent-child-view.jpg new file mode 100644 index 00000000000..fc151df4fb0 Binary files /dev/null and b/lib/xhprof/xhprof_html/docs/sample-diff-report-parent-child-view.jpg differ diff --git a/lib/xhprof/xhprof_html/docs/sample-flat-view.jpg b/lib/xhprof/xhprof_html/docs/sample-flat-view.jpg new file mode 100644 index 00000000000..060dee64f30 Binary files /dev/null and b/lib/xhprof/xhprof_html/docs/sample-flat-view.jpg differ diff --git a/lib/xhprof/xhprof_html/docs/sample-parent-child-view.jpg b/lib/xhprof/xhprof_html/docs/sample-parent-child-view.jpg new file mode 100644 index 00000000000..e1107acb83b Binary files /dev/null and b/lib/xhprof/xhprof_html/docs/sample-parent-child-view.jpg differ