Back to index

glibc  2.9
vtimes.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1997, 1998 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 <stddef.h>
00020 #include <sys/vtimes.h>
00021 #include <sys/resource.h>
00022 
00023 /* Return the number of 1/VTIMES_UNITS_PER_SECOND-second
00024    units in the `struct timeval' TV.  */
00025 #define TIMEVAL_TO_VTIMES(tv) \
00026   ((tv.tv_sec * VTIMES_UNITS_PER_SECOND) + \
00027    (tv.tv_usec * VTIMES_UNITS_PER_SECOND / 1000000))
00028 
00029 /* If VT is not NULL, write statistics for WHO into *VT.
00030    Return 0 for success, -1 for failure.  */
00031 static int
00032 vtimes_one (struct vtimes *vt, enum __rusage_who who)
00033 {
00034   if (vt != NULL)
00035     {
00036       struct rusage usage;
00037 
00038       if (__getrusage (who, &usage) < 0)
00039        return -1;
00040 
00041       vt->vm_utime = TIMEVAL_TO_VTIMES (usage.ru_utime);
00042       vt->vm_stime = TIMEVAL_TO_VTIMES (usage.ru_stime);
00043       vt->vm_idsrss = usage.ru_idrss + usage.ru_isrss;
00044       vt->vm_majflt = usage.ru_majflt;
00045       vt->vm_minflt = usage.ru_minflt;
00046       vt->vm_nswap = usage.ru_nswap;
00047       vt->vm_inblk = usage.ru_inblock;
00048       vt->vm_oublk = usage.ru_oublock;
00049     }
00050   return 0;
00051 }
00052 
00053 /* If CURRENT is not NULL, write statistics for the current process into
00054    *CURRENT.  If CHILD is not NULL, write statistics for all terminated child
00055    processes into *CHILD.  Returns 0 for success, -1 for failure.  */
00056 int
00057 vtimes (current, child)
00058      struct vtimes *current;
00059      struct vtimes *child;
00060 {
00061   if (vtimes_one (current, RUSAGE_SELF) < 0
00062       || vtimes_one (child, RUSAGE_CHILDREN) < 0)
00063     return -1;
00064   return 0;
00065 }