>From 3918fcb958e14e0d325901f90538f31c8cda940a Mon Sep 17 00:00:00 2001
From: "Moroz.Oleg" <oleg.mo...@mcc.vniiem.ru>
Date: Wed, 17 Dec 2014 17:26:19 +0300
Subject: [PATCH 2/2] Adding CPU usage statistic get for usage in user code

---
 cpukit/libmisc/cpuuse/cpuusagereport.c | 115 +++++++++++++++++++++++++++++++++
 cpukit/libmisc/cpuuse/cpuuse.h         |  16 +++++
 2 files changed, 131 insertions(+)

diff --git a/cpukit/libmisc/cpuuse/cpuusagereport.c b/cpukit/libmisc/cpuuse/cpuusagereport.c
index 296fa28..f0fb869 100644
--- a/cpukit/libmisc/cpuuse/cpuusagereport.c
+++ b/cpukit/libmisc/cpuuse/cpuusagereport.c
@@ -225,3 +225,118 @@ void rtems_cpu_usage_report( void )
 {
   rtems_cpu_usage_report_with_plugin( NULL, printk_plugin );
 }
+
+void rtems_cpu_usage_statistic_get(cpu_usage_t *usage_array, uint32_t count)
+{
+	uint32_t threads_found = 0;
+	uint32_t             i;
+	uint32_t             api_index;
+	Thread_Control      *the_thread;
+	Objects_Information *information;
+	char                 name[13];
+	uint32_t             ival, fval;
+#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
+	Timestamp_Control  uptime, total, ran, uptime_at_last_reset;
+	uint32_t seconds, nanoseconds;
+#else
+	uint32_t           total_units = 0;
+#endif
+
+	if (!usage_array || count == 0 ) return;
+for (threads_found=0;threads_found<count;threads_found++)
+{
+	usage_array[threads_found].task_id = 0;
+	memset(usage_array[threads_found].task_name,0,sizeof(usage_array[threads_found].task_name));
+	usage_array[threads_found].usage_in_percents = 0.0f;
+}
+threads_found = 0;
+
+#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
+	_Timestamp_Set_to_zero( &total );
+	uptime_at_last_reset = CPU_usage_Uptime_at_last_reset;
+#else
+	for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST  && threads_found<count; api_index++ ) {
+#if !defined(RTEMS_POSIX_API) || defined(RTEMS_DEBUG)
+		if ( !_Objects_Information_table[ api_index ] )
+			continue;
+#endif
+
+		information = _Objects_Information_table[ api_index ][ 1 ];
+		if ( information ) {
+			for ( i=1 ; i <= information->maximum ; i++ ) {
+				the_thread = (Thread_Control *)information->local_table[ i ];
+
+				if ( the_thread )
+					total_units += the_thread->cpu_time_used;
+			}
+		}
+	}
+#endif
+
+
+	for ( api_index = 1 ; api_index <= OBJECTS_APIS_LAST && threads_found<count; api_index++ ) {
+#if !defined(RTEMS_POSIX_API) || defined(RTEMS_DEBUG)
+		if ( !_Objects_Information_table[ api_index ] )
+			continue;
+#endif
+
+		information = _Objects_Information_table[ api_index ][ 1 ];
+		if ( information ) {
+			for ( i=1 ; i <= information->maximum ; i++ ) {
+				the_thread = (Thread_Control *)information->local_table[ i ];
+
+				if ( !the_thread )
+					continue;
+
+				rtems_object_get_name( the_thread->Object.id, sizeof(name), name );
+
+				usage_array[threads_found].task_id = the_thread->Object.id;
+				memcpy(usage_array[threads_found].task_name,name,sizeof(name));
+
+#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
+				{
+					Timestamp_Control last;
+
+					ran = the_thread->cpu_time_used;
+					if ( is_executing_on_a_core( the_thread, &last ) ) {
+						Timestamp_Control used;
+						_TOD_Get_uptime( &uptime );
+						_Timestamp_Subtract( &last, &uptime, &used );
+						_Timestamp_Add_to( &ran, &used );
+					} else {
+						_TOD_Get_uptime( &uptime );
+					}
+					_Timestamp_Subtract( &uptime_at_last_reset, &uptime, &total );
+					_Timestamp_Divide( &ran, &total, &ival, &fval );
+
+					seconds = _Timestamp_Get_seconds( &ran );
+					nanoseconds = _Timestamp_Get_nanoseconds( &ran ) /
+							TOD_NANOSECONDS_PER_MICROSECOND;
+
+					usage_array[threads_found].usage_in_percents = ival+(fval/1000.0);
+				}
+#else
+	if (total_units) {
+		uint64_t ival_64;
+
+		ival_64 = the_thread->cpu_time_used;
+		ival_64 *= 100000;
+		ival = ival_64 / total_units;
+	} else {
+		ival = 0;
+	}
+
+	fval = ival % 1000;
+	ival /= 1000;
+	usage_array[threads_found].usage_in_percents = ival+(fval/1000.0);
+#endif
+
+	threads_found++;
+			}
+
+
+		}
+
+	}
+
+}
diff --git a/cpukit/libmisc/cpuuse/cpuuse.h b/cpukit/libmisc/cpuuse/cpuuse.h
index 1aee275..9b7b371 100644
--- a/cpukit/libmisc/cpuuse/cpuuse.h
+++ b/cpukit/libmisc/cpuuse/cpuuse.h
@@ -39,6 +39,14 @@
 extern "C" {
 #endif
 
+typedef struct
+{
+  rtems_id task_id;
+  uint8_t task_name[13];
+  float usage_in_percents;
+}cpu_usage_t;
+
+
 #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
   extern Timestamp_Control  CPU_usage_Uptime_at_last_reset;
 #else
@@ -70,6 +78,14 @@ void rtems_cpu_usage_report( void );
 
 void rtems_cpu_usage_reset( void );
 
+/**
+ *  @brief Get CPU usage into array of cpu_usage_t
+ *
+ *  CPU Usage Reporter for telemetry
+ */
+
+void rtems_cpu_usage_statistic_get(cpu_usage_t *usage_array, uint32_t count);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.1.3

_______________________________________________
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Reply via email to