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) 2012, 2014 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include <rtems/libcsupport.h>
0033 
0034 #include <string.h>
0035 
0036 #include <rtems/libio_.h>
0037 #include <rtems/malloc.h>
0038 #include <rtems/score/rbtreeimpl.h>
0039 #include <rtems/score/protectedheap.h>
0040 #include <rtems/score/threadimpl.h>
0041 #include <rtems/score/wkspace.h>
0042 #include <rtems/posix/keyimpl.h>
0043 
0044 static const struct {
0045   Objects_APIs api;
0046   uint16_t cls;
0047 } objects_info_table[] = {
0048   { OBJECTS_POSIX_API, OBJECTS_POSIX_KEYS },
0049   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_BARRIERS },
0050   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_EXTENSIONS },
0051   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_MESSAGE_QUEUES },
0052   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_PARTITIONS },
0053   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_PERIODS },
0054   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_PORTS },
0055   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_REGIONS },
0056   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_SEMAPHORES },
0057   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TASKS },
0058   { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TIMERS },
0059   { OBJECTS_POSIX_API, OBJECTS_POSIX_MESSAGE_QUEUES },
0060   { OBJECTS_POSIX_API, OBJECTS_POSIX_SEMAPHORES },
0061   { OBJECTS_POSIX_API, OBJECTS_POSIX_THREADS }
0062   #ifdef RTEMS_POSIX_API
0063     ,
0064     { OBJECTS_POSIX_API, OBJECTS_POSIX_TIMERS }
0065   #endif
0066 };
0067 
0068 static int open_files(void)
0069 {
0070   int free_count = 0;
0071   rtems_libio_t *iop;
0072 
0073   rtems_libio_lock();
0074 
0075   iop = rtems_libio_iop_free_head;
0076   while (iop != NULL) {
0077     ++free_count;
0078 
0079     iop = iop->data1;
0080   }
0081 
0082   rtems_libio_unlock();
0083 
0084   return (int) rtems_libio_number_iops - free_count;
0085 }
0086 
0087 static void get_heap_info(Heap_Control *heap, Heap_Information_block *info)
0088 {
0089   _Heap_Get_information(heap, info);
0090   memset(&info->Stats, 0, sizeof(info->Stats));
0091 }
0092 
0093 static POSIX_Keys_Control *get_next_key(Objects_Id *id)
0094 {
0095   return (POSIX_Keys_Control *)
0096     _Objects_Get_next(*id, &_POSIX_Keys_Information, id);
0097 }
0098 
0099 static uint32_t get_active_posix_key_value_pairs(void)
0100 {
0101   uint32_t count = 0;
0102   Objects_Id id = OBJECTS_ID_INITIAL_INDEX;
0103   POSIX_Keys_Control *the_key;
0104 
0105   while ((the_key = get_next_key(&id)) != NULL ) {
0106     count += _Chain_Node_count_unprotected(&the_key->Key_value_pairs);
0107     _Objects_Allocator_unlock();
0108   }
0109 
0110   return count;
0111 }
0112 
0113 void rtems_resource_snapshot_take(rtems_resource_snapshot *snapshot)
0114 {
0115   uint32_t *active;
0116   size_t i;
0117 
0118   memset(snapshot, 0, sizeof(*snapshot));
0119 
0120   _RTEMS_Lock_allocator();
0121 
0122   _Thread_Kill_zombies();
0123 
0124   get_heap_info(RTEMS_Malloc_Heap, &snapshot->heap_info);
0125   get_heap_info(&_Workspace_Area, &snapshot->workspace_info);
0126 
0127   active = &snapshot->active_posix_keys;
0128 
0129   for (i = 0; i < RTEMS_ARRAY_SIZE(objects_info_table); ++i) {
0130     const Objects_Information *information;
0131 
0132     information = _Objects_Get_information(
0133       objects_info_table[i].api,
0134       objects_info_table[i].cls
0135     );
0136 
0137     if (information != NULL) {
0138       active[i] = _Objects_Active_count(information);
0139     }
0140   }
0141 
0142   _RTEMS_Unlock_allocator();
0143 
0144   snapshot->active_posix_key_value_pairs = get_active_posix_key_value_pairs();
0145   snapshot->open_files = open_files();
0146 }
0147 
0148 bool rtems_resource_snapshot_equal(
0149   const rtems_resource_snapshot *a,
0150   const rtems_resource_snapshot *b
0151 )
0152 {
0153   return memcmp(a, b, sizeof(*a)) == 0;
0154 }
0155 
0156 bool rtems_resource_snapshot_check(const rtems_resource_snapshot *snapshot)
0157 {
0158   rtems_resource_snapshot now;
0159 
0160   rtems_resource_snapshot_take(&now);
0161 
0162   return rtems_resource_snapshot_equal(&now, snapshot);
0163 }