Back to index

glibc  2.9
getrusage.c
Go to the documentation of this file.
00001 /* getrusage -- Get resource usage information about processes.  Hurd version.
00002    Copyright (C) 1999,2001 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 <string.h>
00022 #include <sys/resource.h>
00023 #include <mach.h>
00024 #include <mach/task_info.h>
00025 #include <hurd.h>
00026 
00027 /* Return resource usage information on process indicated by WHO
00028    and put it in *USAGE.  Returns 0 for success, -1 for failure.  */
00029 int
00030 __getrusage (who, usage)
00031      enum __rusage_who who;
00032      struct rusage *usage;
00033 {
00034   struct task_basic_info bi;
00035   struct task_events_info ei;
00036   struct task_thread_times_info tti;
00037   mach_msg_type_number_t count;
00038   error_t err;
00039 
00040   switch (who)
00041     {
00042     case RUSAGE_SELF:
00043       count = TASK_BASIC_INFO_COUNT;
00044       err = __task_info (__mach_task_self (), TASK_BASIC_INFO,
00045                       (task_info_t) &bi, &count);
00046       if (err)
00047        return __hurd_fail (err);
00048 
00049       count = TASK_EVENTS_INFO_COUNT;
00050       err = __task_info (__mach_task_self (), TASK_EVENTS_INFO,
00051                       (task_info_t) &ei, &count);
00052       if (err == KERN_INVALID_ARGUMENT)   /* microkernel doesn't implement it */
00053        memset (&ei, 0, sizeof ei);
00054       else if (err)
00055        return __hurd_fail (err);
00056 
00057       count = TASK_THREAD_TIMES_INFO_COUNT;
00058       err = __task_info (__mach_task_self (), TASK_THREAD_TIMES_INFO,
00059                       (task_info_t) &tti, &count);
00060       if (err)
00061        return __hurd_fail (err);
00062 
00063       time_value_add (&bi.user_time, &tti.user_time);
00064       time_value_add (&bi.system_time, &tti.system_time);
00065 
00066       memset (usage, 0, sizeof (struct rusage));
00067 
00068       usage->ru_utime.tv_sec = bi.user_time.seconds;
00069       usage->ru_utime.tv_usec = bi.user_time.microseconds;
00070       usage->ru_stime.tv_sec = bi.system_time.seconds;
00071       usage->ru_stime.tv_usec = bi.system_time.microseconds;
00072 
00073       /* These statistics map only approximately.  */
00074       usage->ru_majflt = ei.pageins;
00075       usage->ru_minflt = ei.faults - ei.pageins;
00076       usage->ru_msgsnd = ei.messages_sent; /* Mach IPC, not SysV IPC */
00077       usage->ru_msgrcv = ei.messages_received; /* Mach IPC, not SysV IPC */
00078       break;
00079 
00080     case RUSAGE_CHILDREN:
00081       /* XXX Not implemented yet.  However, zero out USAGE to be
00082          consistent with the wait3 and wait4 functions.  */
00083       memset (usage, 0, sizeof (struct rusage));
00084 
00085       break;
00086 
00087     default:
00088       return EINVAL;
00089     }
00090 
00091   return 0;
00092 }
00093 
00094 weak_alias (__getrusage, getrusage)