Blame resource/vtimes.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <sys/vtimes.h>
Packit 6c4009
#include <sys/resource.h>
Packit 6c4009
Packit 6c4009
/* Return the number of 1/VTIMES_UNITS_PER_SECOND-second
Packit 6c4009
   units in the `struct timeval' TV.  */
Packit 6c4009
#define TIMEVAL_TO_VTIMES(tv) \
Packit 6c4009
  ((tv.tv_sec * VTIMES_UNITS_PER_SECOND) + \
Packit 6c4009
   (tv.tv_usec * VTIMES_UNITS_PER_SECOND / 1000000))
Packit 6c4009
Packit 6c4009
/* If VT is not NULL, write statistics for WHO into *VT.
Packit 6c4009
   Return 0 for success, -1 for failure.  */
Packit 6c4009
static int
Packit 6c4009
vtimes_one (struct vtimes *vt, enum __rusage_who who)
Packit 6c4009
{
Packit 6c4009
  if (vt != NULL)
Packit 6c4009
    {
Packit 6c4009
      struct rusage usage;
Packit 6c4009
Packit 6c4009
      if (__getrusage (who, &usage) < 0)
Packit 6c4009
	return -1;
Packit 6c4009
Packit 6c4009
      vt->vm_utime = TIMEVAL_TO_VTIMES (usage.ru_utime);
Packit 6c4009
      vt->vm_stime = TIMEVAL_TO_VTIMES (usage.ru_stime);
Packit 6c4009
      vt->vm_idsrss = usage.ru_idrss + usage.ru_isrss;
Packit 6c4009
      vt->vm_majflt = usage.ru_majflt;
Packit 6c4009
      vt->vm_minflt = usage.ru_minflt;
Packit 6c4009
      vt->vm_nswap = usage.ru_nswap;
Packit 6c4009
      vt->vm_inblk = usage.ru_inblock;
Packit 6c4009
      vt->vm_oublk = usage.ru_oublock;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* If CURRENT is not NULL, write statistics for the current process into
Packit 6c4009
   *CURRENT.  If CHILD is not NULL, write statistics for all terminated child
Packit 6c4009
   processes into *CHILD.  Returns 0 for success, -1 for failure.  */
Packit 6c4009
int
Packit 6c4009
vtimes (struct vtimes *current, struct vtimes *child)
Packit 6c4009
{
Packit 6c4009
  if (vtimes_one (current, RUSAGE_SELF) < 0
Packit 6c4009
      || vtimes_one (child, RUSAGE_CHILDREN) < 0)
Packit 6c4009
    return -1;
Packit 6c4009
  return 0;
Packit 6c4009
}