Back to index

glibc  2.9
times.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,95,96,97,1998,2001 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stddef.h>
00021 #include <sys/resource.h>
00022 #include <sys/times.h>
00023 #include <sys/time.h>
00024 #include <time.h>
00025 
00026 
00027 /* Time the program started.  */
00028 extern time_t _posix_start_time;
00029 
00030 #ifdef __GNUC__
00031 __inline
00032 #endif
00033 static clock_t
00034 timeval_to_clock_t (const struct timeval *tv, clock_t clk_tck)
00035 {
00036   return (clock_t) ((tv->tv_sec * clk_tck) +
00037                   (tv->tv_usec * clk_tck / 1000000L));
00038 }
00039 
00040 /* Store the CPU time used by this process and all its
00041    dead children (and their dead children) in BUFFER.
00042    Return the elapsed real time, or (clock_t) -1 for errors.
00043    All times are in CLK_TCKths of a second.  */
00044 clock_t
00045 __times (buffer)
00046      struct tms *buffer;
00047 {
00048   struct rusage usage;
00049   clock_t clk_tck;
00050 
00051   if (buffer == NULL)
00052     {
00053       __set_errno (EINVAL);
00054       return (clock_t) -1;
00055     }
00056 
00057   clk_tck = __getclktck ();
00058   
00059   if (__getrusage (RUSAGE_SELF, &usage) < 0)
00060     return (clock_t) -1;
00061   buffer->tms_utime = (clock_t) timeval_to_clock_t (&usage.ru_utime, clk_tck);
00062   buffer->tms_stime = (clock_t) timeval_to_clock_t (&usage.ru_stime, clk_tck);
00063 
00064   if (__getrusage (RUSAGE_CHILDREN, &usage) < 0)
00065     return (clock_t) -1;
00066   buffer->tms_cutime = (clock_t) timeval_to_clock_t (&usage.ru_utime, clk_tck);
00067   buffer->tms_cstime = (clock_t) timeval_to_clock_t (&usage.ru_stime, clk_tck);
00068 
00069   return (time ((time_t *) NULL) - _posix_start_time) * clk_tck;
00070 }
00071 
00072 weak_alias (__times, times)