nfs-ganesha 1.4

MesureTemps.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  * 
00023  * ---------------------------------------
00024  * Outils de mesure du temps
00025  *
00026  * $Header: /cea/home/cvs/cvs/SHERPA/BaseCvs/GANESHA/src/test/MesureTemps.c,v 1.3 2005/11/28 17:03:05 deniel Exp $
00027  *
00028  * $Log: MesureTemps.c,v $
00029  *
00030  * Revision 1.2  2004/08/19 08:08:12  deniel
00031  * Mise au carre des tests sur les libs dynamiques et insertions des mesures
00032  * de temps dans les tests
00033  *
00034  *
00035  */
00036 
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 #include <sys/types.h>
00040 #include <sys/time.h>
00041 #include <string.h>
00042 #include "MesureTemps.h"
00043 
00044 void MesureTemps(struct Temps *resultatp, struct Temps *basep)
00045 {
00046   struct timeval t;
00047 
00048   gettimeofday(&t, NULL);
00049   if(basep == NULL)
00050     {
00051       resultatp->secondes = t.tv_sec;
00052       resultatp->micro_secondes = t.tv_usec;
00053     }
00054   else
00055     {
00056       long tempo;
00057 
00058       resultatp->secondes = t.tv_sec - basep->secondes;
00059       if((tempo = t.tv_usec - basep->micro_secondes) < 0)
00060         {
00061           resultatp->secondes--;
00062           resultatp->micro_secondes = tempo + 1000000;
00063         }
00064       else
00065         {
00066           resultatp->micro_secondes = tempo;
00067         }
00068     }
00069 }
00070 
00071 char *ConvertiTempsChaine(struct Temps temps, char *resultat)
00072 {
00073   static char chaine[100];
00074   char *ptr;
00075 
00076   if(resultat == NULL)
00077     {
00078       ptr = chaine;
00079     }
00080   else
00081     {
00082       ptr = resultat;
00083     }
00084   sprintf(ptr, "%u.%.6llu", (unsigned int)temps.secondes,
00085           (unsigned long long)temps.micro_secondes);
00086   return (ptr);
00087 }
00088 
00089 struct Temps *ConvertiChaineTemps(char *chaine, struct Temps *resultatp)
00090 {
00091   static struct Temps temps;
00092   struct Temps *tp;
00093   char *ptr;
00094 
00095   if(resultatp == NULL)
00096     {
00097       tp = &temps;
00098     }
00099   else
00100     {
00101       tp = resultatp;
00102     }
00103   ptr = strchr(chaine, '.');
00104   if(ptr == NULL)
00105     {
00106       tp->secondes = atoi(chaine);
00107       tp->micro_secondes = 0;
00108     }
00109   else
00110     {
00111       *ptr = '\0';
00112       tp->secondes = atoi(chaine);
00113       tp->micro_secondes = atoi(ptr + 1);
00114     }
00115   return (tp);
00116 }