Back to index

glibc  2.9
times.c
Go to the documentation of this file.
00001 /* Return CPU and real time used by process and its children.  Hurd version.
00002    Copyright (C) 2001,2002,2003,2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stddef.h>
00022 #include <sys/resource.h>
00023 #include <sys/times.h>
00024 #include <sys/time.h>
00025 #include <time.h>
00026 #include <mach.h>
00027 #include <mach/task_info.h>
00028 #include <hurd.h>
00029 
00030 static inline clock_t
00031 clock_from_time_value (const time_value_t *t)
00032 {
00033   return t->seconds * 1000000 + t->microseconds;
00034 }
00035 
00036 /* Store the CPU time used by this process and all its
00037    dead children (and their dead children) in BUFFER.
00038    Return the elapsed real time, or (clock_t) -1 for errors.
00039    All times are in CLK_TCKths of a second.  */
00040 clock_t
00041 __times (struct tms *tms)
00042 {
00043   struct task_basic_info bi;
00044   struct task_thread_times_info tti;
00045   mach_msg_type_number_t count;
00046   union { time_value_t tvt; struct timeval tv; } now;
00047   error_t err;
00048 
00049   count = TASK_BASIC_INFO_COUNT;
00050   err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
00051                    (task_info_t) &bi, &count);
00052   if (err)
00053     return __hurd_fail (err);
00054 
00055   count = TASK_THREAD_TIMES_INFO_COUNT;
00056   err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
00057                    (task_info_t) &tti, &count);
00058   if (err)
00059     return __hurd_fail (err);
00060 
00061   tms->tms_utime = (clock_from_time_value (&bi.user_time)
00062                   + clock_from_time_value (&tti.user_time));
00063   tms->tms_stime = (clock_from_time_value (&bi.system_time)
00064                   + clock_from_time_value (&tti.system_time));
00065 
00066   /* XXX This can't be implemented until getrusage(RUSAGE_CHILDREN) can be.  */
00067   tms->tms_cutime = tms->tms_cstime = 0;
00068 
00069   if (__gettimeofday (&now.tv, NULL) < 0)
00070     return -1;
00071 
00072   return (clock_from_time_value (&now.tvt)
00073          - clock_from_time_value (&bi.creation_time));
00074 }
00075 weak_alias (__times, times)