/*
********************************************************************************
*                                                                              *  
*  2DHF version 1-2003                                                         *
*  Copyright (C) 1995 Leif Laaksonen                                           *
*                                                                              *
*  This software may be used and distributed according to the terms            *
*  of the GNU General Public License, see README and COPYING.                  *
*                                                                              *
********************************************************************************

  Two small c-routines to return the used cpu-time (both parent and child)
  and the time and date.

  This small FORTAN program:

c-------------------------------------
      character*80 text

      call getdatetime(text)

      do i = 1,1000000
      c = sqrt(c * 10000.)
      enddo

      call getusedcpu(a , b)

      write(6,*) a,b

      write(6,*) text

      end
c-------------------------------------

     returns the following:

azalea 121> time ./junk
   0.8200000      0.0000000E+00
 Tue Nov 21 21:41:03 1995

0.813u 0.012s 0:00.84 97.6% 0+0k 0+0io 0pf+0w

  (1) The first number 0.8200000 is the used cpu-time in seconds
  (2) The second number 0.0000000E+00 is the cpu-time used by sub-processes
      or children. In this case it is (of course zero)
  (3) The text 'Tue Nov 21 21:41:03 1995' is the date and time stamp
      returned. The FORTRAN string returning the time stamp has to be
      defined as 80 characters long!!!!!
  (4) The retuned cpu-time is the total used cpu-time (including system 
      services 0.813 + 0.012).

  The c-routine can be compiled as:

  cc -c util.c

  and the combined FORTRAN/C routines can be compiled/linked as:

  f77 -o junk test.f util.o

  and run the program:

  ./junk

*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>


#include <sys/types.h>
#include <sys/times.h>
#include <sys/param.h>

#if defined(dec)
#include <sys/acct.h>
#endif

#if defined(ymp)
#include <time.h>
#endif

/**************************************************************************/
#if defined(AIX)
void getusedcpu(float *Msecs , float *Csecs)  /* get used cpu seconds */
#endif
#if defined(ymp) 
void GETUSEDCPU(float *Msecs , float *Csecs)  /* get used cpu seconds */
#else
void getusedcpu_(float *Msecs , float *Csecs)  /* get used cpu seconds */
#endif
/**************************************************************************/
{
/* return used cpu seconds. This routine can to be called  
   with a negative value to set the cpu time = 0 */

   long ret_in;
   static float start = 0.0;
   static float start_child = 0.0;

   struct tms buffer;

   ret_in = times(&buffer);  /*start time */

   if(*Msecs < 0.0 ) {
#if defined(dec)
   start       = (float)(buffer.tms_utime  + buffer.tms_stime) / (float)(AHZ);
   start_child = (float)(buffer.tms_cutime + buffer.tms_cstime)/ (float)(AHZ);
#elif defined(ymp)
   start       = (float)(buffer.tms_utime  + buffer.tms_stime)  / (float)(CLK_TCK);
   start_child = (float)(buffer.tms_cutime + buffer.tms_cstime) / (float)(CLK_TCK);
#else
   start       = (float)(buffer.tms_utime  + buffer.tms_stime)  / (float)(HZ);
   start_child = (float)(buffer.tms_cutime + buffer.tms_cstime) / (float)(HZ);
#endif
   }
   else {
#if defined(dec)
   *Msecs = (float)(buffer.tms_utime  + buffer.tms_stime) / (float)(AHZ) - start_child;
   *Csecs = (float)(buffer.tms_cutime + buffer.tms_cstime)/ (float)(AHZ) - start_child;
#elif defined(ymp)
   *Msecs = (float)(buffer.tms_utime  + buffer.tms_stime)  / (float)(CLK_TCK) - start;
   *Csecs = (float)(buffer.tms_cutime + buffer.tms_cstime) / (float)(CLK_TCK) - start_child;
#else
   *Msecs = (float)(buffer.tms_utime  + buffer.tms_stime)  / (float)(HZ) - start;
   *Csecs = (float)(buffer.tms_cutime + buffer.tms_cstime) / (float)(HZ) - start_child;
#endif
 }
}




