Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2010.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #define _GNU_SOURCE 1
0034 #include <sys/resource.h>
0035 #undef _GNU_SOURCE
0036 
0037 #include <errno.h>
0038 
0039 #include <rtems.h>
0040 #include <rtems/seterr.h>
0041 
0042 #include <rtems/score/threadimpl.h>
0043 
0044 struct usage_stats {
0045   Timestamp_Control task;
0046   Timestamp_Control idle;
0047 };
0048 
0049 static bool usage_visitor( Thread_Control *the_thread, void *arg )
0050 {
0051   struct usage_stats *stats = (struct usage_stats *) arg;
0052   Timestamp_Control   usage;
0053   Timestamp_Control  *total;
0054   usage = _Thread_Get_CPU_time_used( the_thread );
0055   if ( the_thread->is_idle ) {
0056     total = &stats->idle;
0057   } else {
0058     total = &stats->task;
0059   }
0060   _Timestamp_Add_to( total, &usage );
0061   return false;
0062 }
0063 
0064 static int getrusage_RUSAGE_SELF(
0065   struct rusage *usage
0066 )
0067 {
0068   /*
0069    *  RTEMS only has a single process so there are no children. The
0070    *  single process has been running since the system was booted. We
0071    *  account for IDLE time as system time so user or task time is the
0072    *  uptime time.
0073    */
0074   struct usage_stats stats;
0075 
0076   _Timestamp_Set_to_zero( &stats.task );
0077   _Timestamp_Set_to_zero( &stats.idle );
0078 
0079   rtems_task_iterate( usage_visitor, &stats );
0080 
0081   _Timestamp_To_timeval( &stats.task, &usage->ru_utime );
0082   _Timestamp_To_timeval( &stats.idle, &usage->ru_stime );
0083 
0084   return 0;
0085 }
0086 
0087 static int getrusage_RUSAGE_THREAD(
0088   struct rusage *usage
0089 )
0090 {
0091   Thread_Control    *the_thread;
0092   ISR_lock_Context   lock_context;
0093   Timestamp_Control  used;
0094   the_thread = _Thread_Get( OBJECTS_ID_OF_SELF, &lock_context );
0095   used = _Thread_Get_CPU_time_used( the_thread );
0096   _ISR_lock_ISR_enable( &lock_context );
0097   _Timestamp_To_timeval( &used, &usage->ru_utime );
0098   _Timestamp_Set_to_zero( &used );
0099   _Timestamp_To_timeval( &used, &usage->ru_stime );
0100   return 0;
0101 }
0102 
0103 int getrusage(
0104   int who, struct rusage *usage
0105 )
0106 {
0107   if ( !usage )
0108     rtems_set_errno_and_return_minus_one( EFAULT );
0109 
0110   switch ( who ) {
0111   case RUSAGE_SELF:
0112     return getrusage_RUSAGE_SELF( usage );
0113   case RUSAGE_THREAD:
0114     return getrusage_RUSAGE_THREAD( usage );
0115   case RUSAGE_CHILDREN:
0116     rtems_set_errno_and_return_minus_one( ENOSYS );
0117   default:
0118     break;
0119   }
0120 
0121   rtems_set_errno_and_return_minus_one( EINVAL );
0122 }