cmtkTimers.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2152 $
00026 //
00027 //  $LastChangedDate: 2010-08-04 10:19:33 -0700 (Wed, 04 Aug 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkTimers.h"
00034 
00035 #include <string.h>
00036 #include <stdio.h>
00037 #include <errno.h>
00038 
00039 #ifdef HAVE_SYS_IOCTL_H
00040 #  include <sys/ioctl.h>
00041 #endif
00042 
00043 #ifdef HAVE_FCNTL_H
00044 #  include <fcntl.h>
00045 #endif
00046 
00047 #ifdef HAVE_SYS_PROCFS_H
00048 #  include <sys/procfs.h>
00049 #endif
00050 
00051 #ifdef CMTK_USE_THREADS
00052 #  include <pthread.h>
00053 #endif
00054 
00055 double
00056 cmtk::Timers::GetTimeProcess()
00057 {
00058 #ifdef _MSC_VER
00059   return (double) clock();
00060 #else
00061   struct tms t; 
00062   if ( times(&t) )
00063 #ifdef _SC_CLK_TCK
00064     return (double)( t.tms_utime + t.tms_cutime + t.tms_stime + t.tms_cstime )/sysconf(_SC_CLK_TCK);
00065 #else
00066     return (double)( t.tms_utime + t.tms_cutime + t.tms_stime + t.tms_cstime )/CLK_TCK;
00067 #endif
00068   else
00069     return 0;
00070 #endif // #ifdef _MSC_VER
00071 }
00072 
00073 double
00074 cmtk::Timers::GetWalltime()
00075 {
00076   return static_cast<double>( time( NULL ) );
00077 }
00078 
00079 double
00080 cmtk::Timers::GetTimeThread()
00081 {
00082 #ifndef _MSC_VER
00083 #ifdef HAVE_SYS_PROCFS_H
00084 
00085   char buffer[80];
00086 
00087   snprintf( buffer, sizeof( buffer ), "/proc/%ld/usage", (long)getpid() );
00088   FILE *fp = fopen( buffer, "r" );
00089   if ( fp ) {
00090 #ifdef PRUSAGE_T_IN_SYS_PROCFS_H
00091     prusage_t usage;
00092     if ( fread( &usage, sizeof( usage ), 1, fp ) ) {
00093       fclose( fp );
00094       //      return ((double) usage.pr_rtime.tv_sec) + 
00095       //        ((double) usage.pr_rtime.tv_nsec) / NANOSEC;
00096       return ((double) usage.pr_utime.tv_sec) + ((double) usage.pr_utime.tv_nsec) / NANOSEC +
00097         ((double) usage.pr_stime.tv_sec) + ((double) usage.pr_stime.tv_nsec) / NANOSEC;
00098     }
00099 #endif
00100     fclose( fp );
00101   }
00102   return 0;
00103 
00104 #else // #ifdef HAVE_SYS_PROCFS_H
00105   struct tms t; 
00106   if ( times(&t) )
00107 #ifdef _SC_CLK_TCK
00108     return (double)(t.tms_utime + t.tms_stime)/sysconf(_SC_CLK_TCK);
00109 #else
00110     return (double)(t.tms_utime + t.tms_stime)/CLK_TCK;
00111 #endif
00112   else
00113     return 0;
00114 #endif // #ifdef HAVE_SYS_PROCFS_H
00115 #else // ifndef _MSC_VER
00116   return (double) clock();
00117 #endif // ifndef _MSC_VER
00118 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines