aboutsummaryrefslogtreecommitdiff
path: root/pw_thread/py/thread_analyzer_test.py
blob: 4f380f300266e60111ba6bb53140e5cfd12b0d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# Copyright 2021 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Tests for the thread analyzer."""

import unittest
from pw_thread.thread_analyzer import ThreadInfo, ThreadSnapshotAnalyzer
from pw_thread_protos import thread_pb2


class ThreadInfoTest(unittest.TestCase):
    """Tests that the ThreadInfo class produces expected results."""

    def test_empty_thread(self):
        thread_info = ThreadInfo(thread_pb2.Thread())
        expected = '\n'.join(
            (
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x???????? - 0x???????? (size unknown)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x???????? - 0x???????? (size unknown)',
            )
        )
        self.assertFalse(thread_info.has_stack_size_limit())
        self.assertFalse(thread_info.has_stack_used())
        self.assertEqual(expected, str(thread_info))

    def test_thread_with_cpu_usage(self):
        thread = thread_pb2.Thread()
        thread.cpu_usage_hundredths = 1234
        thread_info = ThreadInfo(thread)

        expected = '\n'.join(
            (
                'Est CPU usage: 12.34%',
                'Stack info',
                '  Current usage:   0x???????? - 0x???????? (size unknown)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x???????? - 0x???????? (size unknown)',
            )
        )
        self.assertFalse(thread_info.has_stack_size_limit())
        self.assertFalse(thread_info.has_stack_used())
        self.assertEqual(expected, str(thread_info))

    def test_thread_with_stack_pointer(self):
        thread = thread_pb2.Thread()
        thread.stack_pointer = 0x5AC6A86C
        thread_info = ThreadInfo(thread)

        expected = '\n'.join(
            (
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x???????? - 0x5ac6a86c (size unknown)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x???????? - 0x???????? (size unknown)',
            )
        )
        self.assertFalse(thread_info.has_stack_size_limit())
        self.assertFalse(thread_info.has_stack_used())
        self.assertEqual(expected, str(thread_info))

    def test_thread_with_stack_usage(self):
        thread = thread_pb2.Thread()
        thread.stack_start_pointer = 0x5AC6B86C
        thread.stack_pointer = 0x5AC6A86C
        thread_info = ThreadInfo(thread)

        expected = '\n'.join(
            (
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x5ac6b86c - 0x5ac6a86c (4096 bytes)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x5ac6b86c - 0x???????? (size unknown)',
            )
        )
        self.assertFalse(thread_info.has_stack_size_limit())
        self.assertTrue(thread_info.has_stack_used())
        self.assertEqual(expected, str(thread_info))

    def test_thread_with_all_stack_info(self):
        thread = thread_pb2.Thread()
        thread.stack_start_pointer = 0x5AC6B86C
        thread.stack_end_pointer = 0x5AC6986C
        thread.stack_pointer = 0x5AC6A86C
        thread_info = ThreadInfo(thread)

        # pylint: disable=line-too-long
        expected = '\n'.join(
            (
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x5ac6b86c - 0x5ac6a86c (4096 bytes, 50.00%)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x5ac6b86c - 0x5ac6986c (8192 bytes)',
            )
        )
        # pylint: enable=line-too-long
        self.assertTrue(thread_info.has_stack_size_limit())
        self.assertTrue(thread_info.has_stack_used())
        self.assertEqual(expected, str(thread_info))


class ThreadSnapshotAnalyzerTest(unittest.TestCase):
    """Tests that the ThreadSnapshotAnalyzer class produces expected results."""

    def test_no_threads(self):
        analyzer = ThreadSnapshotAnalyzer(thread_pb2.SnapshotThreadInfo())
        self.assertEqual('', str(analyzer))

    def test_one_empty_thread(self):
        snapshot = thread_pb2.SnapshotThreadInfo()
        snapshot.threads.append(thread_pb2.Thread())
        expected = '\n'.join(
            (
                'Thread State',
                '  1 thread running.',
                '',
                'Thread (UNKNOWN): [unnamed thread]',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x???????? - 0x???????? (size unknown)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x???????? - 0x???????? (size unknown)',
                '',
            )
        )
        analyzer = ThreadSnapshotAnalyzer(snapshot)
        self.assertEqual(analyzer.active_thread(), None)
        self.assertEqual(str(ThreadSnapshotAnalyzer(snapshot)), expected)

    def test_two_threads(self):
        """Ensures multiple threads are printed correctly."""
        snapshot = thread_pb2.SnapshotThreadInfo()

        temp_thread = thread_pb2.Thread()
        temp_thread.name = 'Idle'.encode()
        temp_thread.state = thread_pb2.ThreadState.Enum.READY
        temp_thread.stack_start_pointer = 0x2001AC00
        temp_thread.stack_end_pointer = 0x2001AA00
        temp_thread.stack_pointer = 0x2001AB0C
        temp_thread.stack_pointer_est_peak = 0x2001AA00
        snapshot.threads.append(temp_thread)

        temp_thread = thread_pb2.Thread()
        temp_thread.name = 'Alice'.encode()
        temp_thread.stack_start_pointer = 0x2001B000
        temp_thread.stack_pointer = 0x2001AE20
        temp_thread.state = thread_pb2.ThreadState.Enum.BLOCKED
        snapshot.threads.append(temp_thread)

        # pylint: disable=line-too-long
        expected = '\n'.join(
            (
                'Thread State',
                '  2 threads running.',
                '',
                'Thread (READY): Idle',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x2001ac00 - 0x2001ab0c (244 bytes, 47.66%)',
                '  Est peak usage:  512 bytes, 100.00%',
                '  Stack limits:    0x2001ac00 - 0x2001aa00 (512 bytes)',
                '',
                'Thread (BLOCKED): Alice',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x2001b000 - 0x2001ae20 (480 bytes)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x2001b000 - 0x???????? (size unknown)',
                '',
            )
        )
        # pylint: enable=line-too-long
        analyzer = ThreadSnapshotAnalyzer(snapshot)
        self.assertEqual(analyzer.active_thread(), None)
        self.assertEqual(str(ThreadSnapshotAnalyzer(snapshot)), expected)

    def test_interrupts_with_thread(self):
        """Ensures interrupts are properly reported as active."""
        snapshot = thread_pb2.SnapshotThreadInfo()

        temp_thread = thread_pb2.Thread()
        temp_thread.name = 'Idle'.encode()
        temp_thread.state = thread_pb2.ThreadState.Enum.READY
        temp_thread.stack_start_pointer = 0x2001AC00
        temp_thread.stack_end_pointer = 0x2001AA00
        temp_thread.stack_pointer = 0x2001AB0C
        temp_thread.stack_pointer_est_peak = 0x2001AA00
        snapshot.threads.append(temp_thread)

        temp_thread = thread_pb2.Thread()
        temp_thread.name = 'Main/Handler'.encode()
        temp_thread.stack_start_pointer = 0x2001B000
        temp_thread.stack_pointer = 0x2001AE20
        temp_thread.state = thread_pb2.ThreadState.Enum.INTERRUPT_HANDLER
        snapshot.threads.append(temp_thread)

        # pylint: disable=line-too-long
        expected = '\n'.join(
            (
                'Thread State',
                '  2 threads running, Main/Handler active at the time of capture.',
                '                     ~~~~~~~~~~~~',
                '',
                # Ensure the active thread is moved to the top of the list.
                'Thread (INTERRUPT_HANDLER): Main/Handler <-- [ACTIVE]',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x2001b000 - 0x2001ae20 (480 bytes)',
                '  Est peak usage:  size unknown',
                '  Stack limits:    0x2001b000 - 0x???????? (size unknown)',
                '',
                'Thread (READY): Idle',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x2001ac00 - 0x2001ab0c (244 bytes, 47.66%)',
                '  Est peak usage:  512 bytes, 100.00%',
                '  Stack limits:    0x2001ac00 - 0x2001aa00 (512 bytes)',
                '',
            )
        )
        # pylint: enable=line-too-long
        analyzer = ThreadSnapshotAnalyzer(snapshot)
        self.assertEqual(analyzer.active_thread(), temp_thread)
        self.assertEqual(str(ThreadSnapshotAnalyzer(snapshot)), expected)

    def test_active_thread(self):
        """Ensures the 'active' thread is highlighted."""
        snapshot = thread_pb2.SnapshotThreadInfo()

        temp_thread = thread_pb2.Thread()
        temp_thread.name = 'Idle'.encode()
        temp_thread.state = thread_pb2.ThreadState.Enum.READY
        temp_thread.stack_start_pointer = 0x2001AC00
        temp_thread.stack_end_pointer = 0x2001AA00
        temp_thread.stack_pointer = 0x2001AB0C
        temp_thread.stack_pointer_est_peak = 0x2001AC00 + 0x100
        snapshot.threads.append(temp_thread)

        temp_thread = thread_pb2.Thread()
        temp_thread.name = 'Main/Handler'.encode()
        temp_thread.active = True
        temp_thread.stack_start_pointer = 0x2001B000
        temp_thread.stack_pointer = 0x2001AE20
        temp_thread.stack_pointer_est_peak = 0x2001B000 + 0x200
        temp_thread.state = thread_pb2.ThreadState.Enum.INTERRUPT_HANDLER
        snapshot.threads.append(temp_thread)

        # pylint: disable=line-too-long
        expected = '\n'.join(
            (
                'Thread State',
                '  2 threads running, Main/Handler active at the time of capture.',
                '                     ~~~~~~~~~~~~',
                '',
                # Ensure the active thread is moved to the top of the list.
                'Thread (INTERRUPT_HANDLER): Main/Handler <-- [ACTIVE]',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x2001b000 - 0x2001ae20 (480 bytes)',
                '  Est peak usage:  512 bytes',
                '  Stack limits:    0x2001b000 - 0x???????? (size unknown)',
                '',
                'Thread (READY): Idle',
                'Est CPU usage: unknown',
                'Stack info',
                '  Current usage:   0x2001ac00 - 0x2001ab0c (244 bytes, 47.66%)',
                '  Est peak usage:  256 bytes, 50.00%',
                '  Stack limits:    0x2001ac00 - 0x2001aa00 (512 bytes)',
                '',
            )
        )
        # pylint: enable=line-too-long
        analyzer = ThreadSnapshotAnalyzer(snapshot)

        # Ensure the active thread is found.
        self.assertEqual(analyzer.active_thread(), temp_thread)
        self.assertEqual(str(ThreadSnapshotAnalyzer(snapshot)), expected)


if __name__ == '__main__':
    unittest.main()