| 1 | /*    Fonction d'impression de temps de calcul et de temps passe  */
 | 
|---|
| 2 | /*                               R.Ansari    Juin 93              */
 | 
|---|
| 3 | 
 | 
|---|
| 4 | /*
 | 
|---|
| 5 | ++
 | 
|---|
| 6 |   Module        Temps CPU, passé (C)
 | 
|---|
| 7 |   Lib   LibsUtil
 | 
|---|
| 8 |   include       timing.h
 | 
|---|
| 9 | 
 | 
|---|
| 10 |         Fonctions permettant d'imprimer le temps CPU consommé et le temps 
 | 
|---|
| 11 |         passé.
 | 
|---|
| 12 | --
 | 
|---|
| 13 | */ 
 | 
|---|
| 14 | 
 | 
|---|
| 15 | /*
 | 
|---|
| 16 | ++
 | 
|---|
| 17 |   void InitTim()
 | 
|---|
| 18 |         Initialisation des chronomètres
 | 
|---|
| 19 |   void PrtTim(char *comm)
 | 
|---|
| 20 |         Imprime le temps CPU, et le temps passé depuis 
 | 
|---|
| 21 |         le dernier appel à "PrtTim()" avec le commentaire
 | 
|---|
| 22 |         "comm". Imprime aussi le cumul du temps CPU et du 
 | 
|---|
| 23 |         temps passé depuis l'appel à "InitTim()".
 | 
|---|
| 24 | --
 | 
|---|
| 25 | */
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include <stdlib.h>
 | 
|---|
| 28 | #include <stdio.h>
 | 
|---|
| 29 | #include <string.h>
 | 
|---|
| 30 | #include <time.h>
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include "timing.h"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | static clock_t CPUT0=0, CPUT=0;
 | 
|---|
| 35 | static time_t ELT0=0, ELT=0;
 | 
|---|
| 36 | /*-------  modifs Christophe 30/09/04
 | 
|---|
| 37 | On somme nous memes les temps partiels pour avoir une autre
 | 
|---|
| 38 | mesure du temps CPU total. En effet, pour des jobs longs
 | 
|---|
| 39 | clock()-CPUT0 depasse la possibilite de stockage d'un entier
 | 
|---|
| 40 | 32 bits car clock() renvoit des microsecondes.
 | 
|---|
| 41 | Par exemple pour un job de 20h = 72e+9 micro-secondes > 2^32
 | 
|---|
| 42 | (Ca ne marche plus pour des jobs > 2146 sec ~= 2^32 microsec)
 | 
|---|
| 43 | Seul un entier 64 bits pourrait donner un resultat correct
 | 
|---|
| 44 | mais il n'existe pas sur toutes les plateformes.
 | 
|---|
| 45 | -------*/ 
 | 
|---|
| 46 | static double tcalt_sum = 0.; 
 | 
|---|
| 47 | /* Flag ajutes par Reza le 7 Fev 2005 */ 
 | 
|---|
| 48 | static int prttim_debug=0;  /* Pour controler l'impression de la somme des temps partiels */
 | 
|---|
| 49 | static int prttim_usesum=0;  /* >0 compteur de clock a fait un tour, on utilise tcalt_sum */
 | 
|---|
| 50 |  
 | 
|---|
| 51 | /* Nouvelle-Fonction */
 | 
|---|
| 52 | /*!
 | 
|---|
| 53 |    \ingroup SysTools
 | 
|---|
| 54 |    Initializes CPU and elapsed time timer (C function).
 | 
|---|
| 55 |    The values of the CPU and elapsed time can then be printed 
 | 
|---|
| 56 |    using \b PrtTim() 
 | 
|---|
| 57 | */
 | 
|---|
| 58 | void InitTim(void)
 | 
|---|
| 59 | {
 | 
|---|
| 60 | CPUT0 = CPUT = clock();
 | 
|---|
| 61 | ELT0 = ELT = time(NULL);
 | 
|---|
| 62 | tcalt_sum = 0.;
 | 
|---|
| 63 | prttim_usesum = 0;
 | 
|---|
| 64 | return;
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /* Nouvelle-Fonction */
 | 
|---|
| 68 | /*!
 | 
|---|
| 69 |    \ingroup SysTools
 | 
|---|
| 70 |    Actvates (dbg>0) or deactivates (dbg=0) printing of sum of partial elapsed times.
 | 
|---|
| 71 | */
 | 
|---|
| 72 | void PrtTimSetDebug(int dbg)
 | 
|---|
| 73 | {
 | 
|---|
| 74 |   prttim_debug = dbg;
 | 
|---|
| 75 | } 
 | 
|---|
| 76 | /* Nouvelle-Fonction */
 | 
|---|
| 77 | /*!
 | 
|---|
| 78 |    \ingroup SysTools
 | 
|---|
| 79 |    Prints the values of the CPU and elapsed time, since call to \b InitTim().
 | 
|---|
| 80 | */
 | 
|---|
| 81 | void PrtTim(const char * Comm)
 | 
|---|
| 82 | {
 | 
|---|
| 83 | double tcal,tcalt;
 | 
|---|
| 84 | clock_t cput;
 | 
|---|
| 85 | time_t elt;
 | 
|---|
| 86 | unsigned long etm,etmt;
 | 
|---|
| 87 | 
 | 
|---|
| 88 | cput = clock();
 | 
|---|
| 89 | tcalt = ( (double)(cput) - (double)(CPUT0) ) / (double)(CLOCKS_PER_SEC);
 | 
|---|
| 90 | tcal = ( (double)(cput) - (double)(CPUT) ) / (double)(CLOCKS_PER_SEC);
 | 
|---|
| 91 | 
 | 
|---|
| 92 | elt = time(NULL);
 | 
|---|
| 93 | etm = elt - ELT;
 | 
|---|
| 94 | etmt = elt - ELT0;
 | 
|---|
| 95 | /*-------  modifs Christophe 30/09/04
 | 
|---|
| 96 | - tcalt_sum en 1/100 ieme de seconde:
 | 
|---|
| 97 |   On imprime des Secondes que l'on somme N fois
 | 
|---|
| 98 |   -> on se donne une precision a 1/100 de Seconde
 | 
|---|
| 99 | - Au moment ou clock()>2^32 tcal devient negatif
 | 
|---|
| 100 |   ==> On ne somme donc pas tcal et on TRICHE en sommant "etm"
 | 
|---|
| 101 |       (pour etre vrai il faut que le process ait 100% du CPU !)
 | 
|---|
| 102 | -------*/
 | 
|---|
| 103 | if(tcal>0.) { 
 | 
|---|
| 104 |   tcalt_sum += tcal*100.; 
 | 
|---|
| 105 |   if (prttim_debug > 0) printf("PrtTim/Warning - tcalt_sum will now be used ...\n");
 | 
|---|
| 106 | }
 | 
|---|
| 107 | else  tcalt_sum += (double)etm*100.;
 | 
|---|
| 108 | 
 | 
|---|
| 109 | if (prttim_usesum)  tcalt = tcalt_sum/100.;
 | 
|---|
| 110 | if (prttim_debug > 0) 
 | 
|---|
| 111 |   printf("%s CPUTime: Total= %g  (Sum~= %.1f) (Partial= %g) Sec. \n",
 | 
|---|
| 112 |          Comm, tcalt, tcalt_sum/100., tcal);
 | 
|---|
| 113 | else 
 | 
|---|
| 114 |   printf("%s CPUTime: Total= %g   (Partial= %g) Sec. \n",
 | 
|---|
| 115 |           Comm, tcalt, tcal);
 | 
|---|
| 116 | 
 | 
|---|
| 117 | printf("ElapsedTime(hh:mm:ss): Total= %02d:%02d:%02d ",
 | 
|---|
| 118 |        etmt/3600, (etmt%3600)/60, etmt%60);
 | 
|---|
| 119 | printf(" (Partial= %02d:%02d:%02d)\n",
 | 
|---|
| 120 |        etm/3600, (etm%3600)/60, etm%60);
 | 
|---|
| 121 | 
 | 
|---|
| 122 | ELT = elt;
 | 
|---|
| 123 | CPUT = cput;
 | 
|---|
| 124 | return;
 | 
|---|
| 125 | }
 | 
|---|