[70432fc] | 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
| 2 | |
---|
[c5d2760] | 3 | /* |
---|
[bcef89f2] | 4 | * Copyright (C) 2012, 2014 embedded brains GmbH & Co. KG |
---|
[c5d2760] | 5 | * |
---|
[70432fc] | 6 | * Redistribution and use in source and binary forms, with or without |
---|
| 7 | * modification, are permitted provided that the following conditions |
---|
| 8 | * are met: |
---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
---|
| 10 | * notice, this list of conditions and the following disclaimer. |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
---|
| 13 | * documentation and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
[c5d2760] | 26 | */ |
---|
| 27 | |
---|
[80cf60e] | 28 | #ifdef HAVE_CONFIG_H |
---|
| 29 | #include "config.h" |
---|
[c5d2760] | 30 | #endif |
---|
| 31 | |
---|
| 32 | #include <rtems/libcsupport.h> |
---|
| 33 | |
---|
| 34 | #include <string.h> |
---|
| 35 | |
---|
| 36 | #include <rtems/libio_.h> |
---|
| 37 | #include <rtems/malloc.h> |
---|
[7bdb765] | 38 | #include <rtems/score/rbtreeimpl.h> |
---|
[c5d2760] | 39 | #include <rtems/score/protectedheap.h> |
---|
[1b1be254] | 40 | #include <rtems/score/threadimpl.h> |
---|
| 41 | #include <rtems/score/wkspace.h> |
---|
[7bdb765] | 42 | #include <rtems/posix/keyimpl.h> |
---|
[ac252bdc] | 43 | |
---|
[9597c4ed] | 44 | static const struct { |
---|
| 45 | Objects_APIs api; |
---|
| 46 | uint16_t cls; |
---|
| 47 | } objects_info_table[] = { |
---|
| 48 | { OBJECTS_POSIX_API, OBJECTS_POSIX_KEYS }, |
---|
| 49 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_BARRIERS }, |
---|
| 50 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_EXTENSIONS }, |
---|
| 51 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_MESSAGE_QUEUES }, |
---|
| 52 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_PARTITIONS }, |
---|
| 53 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_PERIODS }, |
---|
| 54 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_PORTS }, |
---|
| 55 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_REGIONS }, |
---|
| 56 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_SEMAPHORES }, |
---|
| 57 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TASKS }, |
---|
[54f35888] | 58 | { OBJECTS_CLASSIC_API, OBJECTS_RTEMS_TIMERS }, |
---|
| 59 | { OBJECTS_POSIX_API, OBJECTS_POSIX_MESSAGE_QUEUES }, |
---|
| 60 | { OBJECTS_POSIX_API, OBJECTS_POSIX_SEMAPHORES }, |
---|
| 61 | { OBJECTS_POSIX_API, OBJECTS_POSIX_THREADS } |
---|
[c5d2760] | 62 | #ifdef RTEMS_POSIX_API |
---|
| 63 | , |
---|
[9597c4ed] | 64 | { OBJECTS_POSIX_API, OBJECTS_POSIX_TIMERS } |
---|
[c5d2760] | 65 | #endif |
---|
| 66 | }; |
---|
| 67 | |
---|
| 68 | static int open_files(void) |
---|
| 69 | { |
---|
| 70 | int free_count = 0; |
---|
| 71 | rtems_libio_t *iop; |
---|
| 72 | |
---|
| 73 | rtems_libio_lock(); |
---|
| 74 | |
---|
[ac74162] | 75 | iop = rtems_libio_iop_free_head; |
---|
[c5d2760] | 76 | while (iop != NULL) { |
---|
| 77 | ++free_count; |
---|
| 78 | |
---|
| 79 | iop = iop->data1; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | rtems_libio_unlock(); |
---|
| 83 | |
---|
| 84 | return (int) rtems_libio_number_iops - free_count; |
---|
| 85 | } |
---|
| 86 | |
---|
[2c3c657] | 87 | static void get_heap_info(Heap_Control *heap, Heap_Information_block *info) |
---|
| 88 | { |
---|
| 89 | _Heap_Get_information(heap, info); |
---|
| 90 | memset(&info->Stats, 0, sizeof(info->Stats)); |
---|
| 91 | } |
---|
| 92 | |
---|
[5eaf0e7] | 93 | static POSIX_Keys_Control *get_next_key(Objects_Id *id) |
---|
[7bdb765] | 94 | { |
---|
[5eaf0e7] | 95 | return (POSIX_Keys_Control *) |
---|
[36cd27c] | 96 | _Objects_Get_next(*id, &_POSIX_Keys_Information, id); |
---|
[7bdb765] | 97 | } |
---|
| 98 | |
---|
| 99 | static uint32_t get_active_posix_key_value_pairs(void) |
---|
| 100 | { |
---|
| 101 | uint32_t count = 0; |
---|
[5eaf0e7] | 102 | Objects_Id id = OBJECTS_ID_INITIAL_INDEX; |
---|
| 103 | POSIX_Keys_Control *the_key; |
---|
[7bdb765] | 104 | |
---|
[5eaf0e7] | 105 | while ((the_key = get_next_key(&id)) != NULL ) { |
---|
| 106 | count += _Chain_Node_count_unprotected(&the_key->Key_value_pairs); |
---|
| 107 | _Objects_Allocator_unlock(); |
---|
| 108 | } |
---|
[7bdb765] | 109 | |
---|
| 110 | return count; |
---|
| 111 | } |
---|
| 112 | |
---|
[c5d2760] | 113 | void rtems_resource_snapshot_take(rtems_resource_snapshot *snapshot) |
---|
| 114 | { |
---|
[9597c4ed] | 115 | uint32_t *active; |
---|
[c5d2760] | 116 | size_t i; |
---|
| 117 | |
---|
[7bdb765] | 118 | memset(snapshot, 0, sizeof(*snapshot)); |
---|
| 119 | |
---|
[23fec9f0] | 120 | _RTEMS_Lock_allocator(); |
---|
[4f1da72] | 121 | |
---|
[1b1be254] | 122 | _Thread_Kill_zombies(); |
---|
| 123 | |
---|
[2c3c657] | 124 | get_heap_info(RTEMS_Malloc_Heap, &snapshot->heap_info); |
---|
| 125 | get_heap_info(&_Workspace_Area, &snapshot->workspace_info); |
---|
[c5d2760] | 126 | |
---|
[9597c4ed] | 127 | active = &snapshot->active_posix_keys; |
---|
| 128 | |
---|
[c5d2760] | 129 | for (i = 0; i < RTEMS_ARRAY_SIZE(objects_info_table); ++i) { |
---|
[9597c4ed] | 130 | const Objects_Information *information; |
---|
| 131 | |
---|
| 132 | information = _Objects_Get_information( |
---|
| 133 | objects_info_table[i].api, |
---|
| 134 | objects_info_table[i].cls |
---|
| 135 | ); |
---|
| 136 | |
---|
| 137 | if (information != NULL) { |
---|
| 138 | active[i] = _Objects_Active_count(information); |
---|
| 139 | } |
---|
[c5d2760] | 140 | } |
---|
| 141 | |
---|
[23fec9f0] | 142 | _RTEMS_Unlock_allocator(); |
---|
[c5d2760] | 143 | |
---|
[7bdb765] | 144 | snapshot->active_posix_key_value_pairs = get_active_posix_key_value_pairs(); |
---|
[c5d2760] | 145 | snapshot->open_files = open_files(); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | bool rtems_resource_snapshot_equal( |
---|
| 149 | const rtems_resource_snapshot *a, |
---|
| 150 | const rtems_resource_snapshot *b |
---|
| 151 | ) |
---|
| 152 | { |
---|
| 153 | return memcmp(a, b, sizeof(*a)) == 0; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | bool rtems_resource_snapshot_check(const rtems_resource_snapshot *snapshot) |
---|
| 157 | { |
---|
| 158 | rtems_resource_snapshot now; |
---|
| 159 | |
---|
| 160 | rtems_resource_snapshot_take(&now); |
---|
| 161 | |
---|
| 162 | return rtems_resource_snapshot_equal(&now, snapshot); |
---|
| 163 | } |
---|