Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
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       
00095       
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 }