posix: Improve clock_gettime()

Return CPU usage values for CLOCK_PROCESS_CPUTIME_ID and
CLOCK_THREAD_CPUTIME_ID.
This commit is contained in:
Sebastian Huber
2025-01-03 04:36:15 +01:00
committed by Kinsey Moore
parent 5a9484e6cb
commit 294761ca8d
7 changed files with 40 additions and 22 deletions

View File

@@ -39,6 +39,6 @@
#include "config.h" #include "config.h"
#endif #endif
#include "cpuuseimpl.h" #include <rtems/cpuuseimpl.h>
Timestamp_Control CPU_usage_Uptime_at_last_reset; Timestamp_Control CPU_usage_Uptime_at_last_reset;

View File

@@ -46,12 +46,11 @@
#include <inttypes.h> #include <inttypes.h>
#include <rtems/cpuuse.h> #include <rtems/cpuuse.h>
#include <rtems/cpuuseimpl.h>
#include <rtems/printer.h> #include <rtems/printer.h>
#include <rtems/score/threadimpl.h> #include <rtems/score/threadimpl.h>
#include <rtems/score/todimpl.h> #include <rtems/score/todimpl.h>
#include "cpuuseimpl.h"
typedef struct { typedef struct {
const rtems_printer *printer; const rtems_printer *printer;
Timestamp_Control total; Timestamp_Control total;

View File

@@ -40,14 +40,13 @@
#endif #endif
#include <rtems/cpuuse.h> #include <rtems/cpuuse.h>
#include <rtems/cpuuseimpl.h>
#include <rtems/rtems/scheduler.h> #include <rtems/rtems/scheduler.h>
#include <rtems/score/percpu.h> #include <rtems/score/percpu.h>
#include <rtems/score/todimpl.h> #include <rtems/score/todimpl.h>
#include <rtems/score/schedulerimpl.h> #include <rtems/score/schedulerimpl.h>
#include <rtems/score/watchdogimpl.h> #include <rtems/score/watchdogimpl.h>
#include "cpuuseimpl.h"
static bool CPU_usage_Per_thread_handler( static bool CPU_usage_Per_thread_handler(
Thread_Control *the_thread, Thread_Control *the_thread,
void *arg void *arg

View File

@@ -53,6 +53,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <rtems/cpuuse.h> #include <rtems/cpuuse.h>
#include <rtems/cpuuseimpl.h>
#include <rtems/printer.h> #include <rtems/printer.h>
#include <rtems/malloc.h> #include <rtems/malloc.h>
#include <rtems/score/objectimpl.h> #include <rtems/score/objectimpl.h>
@@ -64,8 +65,6 @@
#include <rtems/score/wkspace.h> #include <rtems/score/wkspace.h>
#include <rtems/rtems/tasksimpl.h> #include <rtems/rtems/tasksimpl.h>
#include "cpuuseimpl.h"
/* /*
* Use a struct for all data to allow more than one top and to support the * Use a struct for all data to allow more than one top and to support the
* thread iterator. * thread iterator.

View File

@@ -5,10 +5,12 @@
* *
* @ingroup POSIXAPI * @ingroup POSIXAPI
* *
* @brief Retrieves the Specified Clock Time * @brief This source file contains the implementation of clock_gettime().
*/ */
/* /*
* Copyright (C) 2024 embedded brains GmbH & Co. KG
*
* COPYRIGHT (c) 1989-2007. * COPYRIGHT (c) 1989-2007.
* On-Line Applications Research Corporation (OAR). * On-Line Applications Research Corporation (OAR).
* *
@@ -41,6 +43,9 @@
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include <rtems/cpuuseimpl.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/timestampimpl.h>
#include <rtems/score/todimpl.h> #include <rtems/score/todimpl.h>
#include <rtems/seterr.h> #include <rtems/seterr.h>
@@ -70,17 +75,29 @@ int clock_gettime(
#ifdef _POSIX_CPUTIME #ifdef _POSIX_CPUTIME
if ( clock_id == CLOCK_PROCESS_CPUTIME_ID ) { if ( clock_id == CLOCK_PROCESS_CPUTIME_ID ) {
_Timecounter_Nanouptime( tp ); Timestamp_Control uptime;
Timestamp_Control last_reset;
Timestamp_Control process_time;
last_reset = CPU_usage_Uptime_at_last_reset;
_TOD_Get_uptime( &uptime );
_Timestamp_Subtract( &last_reset, &uptime, &process_time );
_Timestamp_To_timespec( &process_time, tp );
return 0; return 0;
} }
#endif #endif
#ifdef _POSIX_THREAD_CPUTIME #ifdef _POSIX_THREAD_CPUTIME
if ( clock_id == CLOCK_THREAD_CPUTIME_ID ) if ( clock_id == CLOCK_THREAD_CPUTIME_ID ) {
rtems_set_errno_and_return_minus_one( ENOSYS ); Timestamp_Control used;
used = _Thread_Get_CPU_time_used_after_last_reset(
_Thread_Get_executing()
);
_Timestamp_To_timespec( &used, tp );
return 0;
}
#endif #endif
rtems_set_errno_and_return_minus_one( EINVAL ); rtems_set_errno_and_return_minus_one( EINVAL );
return 0;
} }

View File

@@ -386,7 +386,19 @@ static rtems_task Init(
/* print new times to make sure it has changed and we can get the realtime */ /* print new times to make sure it has changed and we can get the realtime */
sc = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &tv ); sc = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &tv );
rtems_test_assert( !sc ); rtems_test_assert( !sc );
printf("Time since boot: (%" PRIdtime_t ", %ld)\n", tv.tv_sec,tv.tv_nsec ); printf(
"Time since last CPU usage reset: (%" PRIdtime_t ", %ld)\n",
tv.tv_sec,
tv.tv_nsec
);
sc = clock_gettime( CLOCK_THREAD_CPUTIME_ID, &tv );
rtems_test_assert( !sc );
printf(
"CPU time used since last CPU usage reset: (%" PRIdtime_t ", %ld)\n",
tv.tv_sec,
tv.tv_nsec
);
sc = clock_gettime( CLOCK_REALTIME, &tv ); sc = clock_gettime( CLOCK_REALTIME, &tv );
rtems_test_assert( !sc ); rtems_test_assert( !sc );
@@ -499,14 +511,6 @@ static rtems_task Init(
printf( ctime( &tv.tv_sec ) ); printf( ctime( &tv.tv_sec ) );
empty_line(); empty_line();
puts( "clock_gettime - CLOCK_THREAD_CPUTIME_ID -- ENOSYS" );
#if defined(_POSIX_THREAD_CPUTIME)
{
struct timespec tp;
sc = clock_gettime( CLOCK_THREAD_CPUTIME_ID, &tp );
check_enosys( sc );
}
#endif
puts( "clock_settime - CLOCK_PROCESS_CPUTIME_ID -- ENOSYS" ); puts( "clock_settime - CLOCK_PROCESS_CPUTIME_ID -- ENOSYS" );
#if defined(_POSIX_CPUTIME) #if defined(_POSIX_CPUTIME)