• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / JavaSpiNNaker / 6310285782

26 Sep 2023 08:47AM UTC coverage: 36.367% (-0.5%) from 36.866%
6310285782

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17 and JEE to 9

1675 of 1675 new or added lines in 266 files covered. (100.0%)

8368 of 23010 relevant lines covered (36.37%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/SpiNNaker-comms/src/main/java/uk/ac/manchester/spinnaker/transceiver/GetHeapProcess.java
1
/*
2
 * Copyright (c) 2018 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.transceiver;
17

18
import static java.util.Collections.unmodifiableList;
19
import static uk.ac.manchester.spinnaker.transceiver.CommonMemoryLocations.SYS_VARS;
20

21
import java.io.IOException;
22
import java.nio.IntBuffer;
23
import java.util.ArrayList;
24
import java.util.List;
25

26
import uk.ac.manchester.spinnaker.connections.ConnectionSelector;
27
import uk.ac.manchester.spinnaker.connections.SCPConnection;
28
import uk.ac.manchester.spinnaker.machine.HasChipLocation;
29
import uk.ac.manchester.spinnaker.machine.MemoryLocation;
30
import uk.ac.manchester.spinnaker.messages.model.HeapElement;
31
import uk.ac.manchester.spinnaker.messages.model.SARKField;
32
import uk.ac.manchester.spinnaker.messages.model.SARKStruct;
33
import uk.ac.manchester.spinnaker.messages.model.SystemVariableDefinition;
34
import uk.ac.manchester.spinnaker.messages.scp.ReadMemory;
35

36
/**
37
 * Get a description of the heap.
38
 */
39
final class GetHeapProcess extends TxrxProcess {
40
        private static final int HEAP_HEADER_SIZE = 16;
41

42
        private static final int HEAP_BLOCK_HEADER_SIZE = 8;
43

44
        /**
45
         * @param connectionSelector
46
         *            How to select how to communicate.
47
         * @param retryTracker
48
         *            Object used to track how many retries were used in an
49
         *            operation. May be {@code null} if no suck tracking is
50
         *            required.
51
         */
52
        GetHeapProcess(
53
                        ConnectionSelector<? extends SCPConnection> connectionSelector,
54
                        RetryTracker retryTracker) {
55
                super(connectionSelector, retryTracker);
×
56
        }
×
57

58
        /**
59
         * Get the heap block descriptors.
60
         *
61
         * @param chip
62
         *            The chip to ask.
63
         * @param heap
64
         *            The heap to ask about.
65
         * @return A list of block descriptors, in block chain order.
66
         * @throws IOException
67
         *             If anything goes wrong with networking.
68
         * @throws ProcessException
69
         *             If SpiNNaker rejects a message.
70
         * @throws InterruptedException
71
         *             If the communications were interrupted.
72
         */
73
        List<HeapElement> getBlocks(HasChipLocation chip,
74
                        SystemVariableDefinition heap)
75
                        throws IOException, ProcessException, InterruptedException {
76
                var header = getHeapHeader(chip, heap);
×
77
                var nextBlock = header.first;
×
78

79
                var blocks = new ArrayList<HeapElement>();
×
80

81
                while (!nextBlock.isNull()) {
×
82
                        var block = getBlockHeader(chip, nextBlock);
×
83
                        if (!block.next.isNull()) {
×
84
                                blocks.add(new HeapElement(nextBlock, block.next,
×
85
                                                block.free.address()));
×
86
                        }
87
                        nextBlock = block.next;
×
88
                }
×
89

90
                return unmodifiableList(blocks);
×
91
        }
92

93
        /**
94
         * Get the free heap block descriptors.
95
         * <p>
96
         * <em><strong>WARNING:</strong> This is untested code!</em>
97
         *
98
         * @param chip
99
         *            The chip to ask.
100
         * @param heap
101
         *            The heap to ask about.
102
         * @return A list of block descriptors, in block chain order.
103
         * @throws IOException
104
         *             If anything goes wrong with networking.
105
         * @throws ProcessException
106
         *             If SpiNNaker rejects a message.
107
         * @throws InterruptedException
108
         *             If the communications were interrupted.
109
         */
110
        List<HeapElement> getFreeBlocks(HasChipLocation chip,
111
                        SystemVariableDefinition heap)
112
                        throws IOException, ProcessException, InterruptedException {
113
                var header = getHeapHeader(chip, heap);
×
114
                var nextBlock = header.free;
×
115

116
                var blocks = new ArrayList<HeapElement>();
×
117

118
                while (!nextBlock.isNull() && !nextBlock.equals(header.last)) {
×
119
                        var block = getBlockHeader(chip, nextBlock);
×
120
                        if (!block.next.isNull()) {
×
121
                                blocks.add(new HeapElement(nextBlock, block.next,
×
122
                                                block.free.address()));
×
123
                        }
124
                        nextBlock = block.free;
×
125
                }
×
126

127
                return unmodifiableList(blocks);
×
128
        }
129

130
        /**
131
         * Get the space free in a heap.
132
         *
133
         * @param chip
134
         *            The chip to ask.
135
         * @param heap
136
         *            The heap to ask about.
137
         * @return The number of bytes free in the heap.
138
         * @throws IOException
139
         *             If anything goes wrong with networking.
140
         * @throws ProcessException
141
         *             If SpiNNaker rejects a message.
142
         * @throws InterruptedException
143
         *             If the communications were interrupted.
144
         */
145
        int getFreeSpace(HasChipLocation chip, SystemVariableDefinition heap)
146
                        throws IOException, ProcessException, InterruptedException {
147
                return getHeapHeader(chip, heap).freeBytes;
×
148
        }
149

150
        /**
151
         * Read a heap header.
152
         *
153
         * @param chip
154
         *            What chip to read from.
155
         * @param heap
156
         *            Which heap to get the header of.
157
         * @return The heap header.
158
         * @throws IOException
159
         *             If anything goes wrong with networking
160
         * @throws ProcessException
161
         *             If SpiNNaker rejects a message.
162
         * @throws InterruptedException
163
         *             If the communications were interrupted.
164
         */
165
        private HeapHeader getHeapHeader(HasChipLocation chip,
166
                        SystemVariableDefinition heap)
167
                        throws IOException, ProcessException, InterruptedException {
168
                var heapBase = new MemoryLocation(readFromAddress(chip,
×
169
                                SYS_VARS.add(heap.offset), heap.type.value).get());
×
170
                return new HeapHeader(
×
171
                                readFromAddress(chip, heapBase, HEAP_HEADER_SIZE));
×
172
        }
173

174
        /**
175
         * Read a memory block header.
176
         *
177
         * @param chip
178
         *            What chip to read from.
179
         * @param address
180
         *            What address to read from.
181
         * @return The memory block header.
182
         * @throws IOException
183
         *             If anything goes wrong with networking.
184
         * @throws ProcessException
185
         *             If SpiNNaker rejects a message.
186
         * @throws InterruptedException
187
         *             If the communications were interrupted.
188
         */
189
        private BlockHeader getBlockHeader(HasChipLocation chip,
190
                        MemoryLocation address)
191
                        throws IOException, ProcessException, InterruptedException {
192
                return new BlockHeader(
×
193
                                readFromAddress(chip, address, HEAP_BLOCK_HEADER_SIZE));
×
194
        }
195

196
        /**
197
         * Simplified read. <em>Assumes</em> that the amount of data being read can
198
         * fit in a single response message.
199
         *
200
         * @param chip
201
         *            What chip to read from.
202
         * @param address
203
         *            What address to read from.
204
         * @param size
205
         *            How much to read.
206
         * @return Data read, wrapped as little-endian integer buffer.
207
         * @throws IOException
208
         *             If anything goes wrong with networking.
209
         * @throws ProcessException
210
         *             If SpiNNaker rejects a message.
211
         * @throws InterruptedException
212
         *             If the communications were interrupted.
213
         */
214
        private IntBuffer readFromAddress(HasChipLocation chip,
215
                        MemoryLocation address, long size)
216
                        throws IOException, ProcessException, InterruptedException {
217
                return retrieve(new ReadMemory(chip, address, (int) size))
×
218
                                .asIntBuffer();
×
219
        }
220

221
        @SARKStruct("heap_t")
222
        private record HeapHeader(//
×
223
                        @SARKField("free") MemoryLocation free,
224
                        @SARKField("first") MemoryLocation first,
225
                        @SARKField("last") MemoryLocation last,
226
                        @SARKField("free_bytes") int freeBytes) {
227
                HeapHeader(IntBuffer data) {
228
                        this(//
×
229
                                        new MemoryLocation(data.get()),
×
230
                                        new MemoryLocation(data.get()),
×
231
                                        new MemoryLocation(data.get()), //
×
232
                                        data.get());
×
233
                        // Note that we don't read or look at the 'buffer' field
234
                }
×
235
        }
236

237
        @SARKStruct("block_t")
238
        private record BlockHeader(//
×
239
                        @SARKField("next") MemoryLocation next,
240
                        @SARKField("free") MemoryLocation free) {
241
                BlockHeader(IntBuffer data) {
242
                        this(//
×
243
                                        new MemoryLocation(data.get()),
×
244
                                        new MemoryLocation(data.get()));
×
245
                }
×
246
        }
247
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc