1
/* Copyright 2013-2014 IBM Corp.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
23
static bool zalloc_should_fail = false;
24
static int zalloc_should_fail_after = 0;
26
static void *zalloc(size_t size)
28
if (zalloc_should_fail && zalloc_should_fail_after == 0) {
32
if (zalloc_should_fail_after > 0)
33
zalloc_should_fail_after--;
35
return calloc(size, 1);
38
#include "../opal-msg.c"
41
void lock(struct lock *l)
47
void unlock(struct lock *l)
53
void opal_update_pending_evt(uint64_t evt_mask, uint64_t evt_values)
59
static long magic = 8097883813087437089UL;
60
static void callback(void *data)
62
assert(*(uint64_t *)data == magic);
65
static size_t list_count(struct list_head *list)
68
struct opal_msg_entry *dummy;
70
list_for_each(list, dummy, link)
77
struct opal_msg_entry* entry;
78
int free_size = OPAL_MAX_MSGS;
79
int nfree = free_size;
82
static struct opal_msg m;
83
uint64_t *m_ptr = (uint64_t *)&m;
85
zalloc_should_fail = true;
86
zalloc_should_fail_after = 3;
89
zalloc_should_fail = false;
92
assert(list_count(&msg_pending_list) == npending);
93
assert(list_count(&msg_free_list) == nfree);
96
r = opal_queue_msg(0, &magic, callback, (u64)0, (u64)1, (u64)2);
99
assert(list_count(&msg_pending_list) == ++npending);
100
assert(list_count(&msg_free_list) == --nfree);
102
r = opal_get_msg(m_ptr, sizeof(m));
105
assert(m.params[0] == 0);
106
assert(m.params[1] == 1);
107
assert(m.params[2] == 2);
109
assert(list_count(&msg_pending_list) == --npending);
110
assert(list_count(&msg_free_list) == ++nfree);
113
r = opal_queue_msg(0, NULL, NULL);
116
assert(list_count(&msg_pending_list) == ++npending);
117
assert(list_count(&msg_free_list) == --nfree);
119
r = opal_get_msg(m_ptr, sizeof(m));
122
assert(list_count(&msg_pending_list) == --npending);
123
assert(list_count(&msg_free_list) == ++nfree);
125
/* > 8 params (ARRAY_SIZE(entry->msg.params) */
126
r = opal_queue_msg(0, NULL, NULL, 0, 1, 2, 3, 4, 5, 6, 7, 0xBADDA7A);
129
assert(list_count(&msg_pending_list) == ++npending);
130
assert(list_count(&msg_free_list) == --nfree);
132
r = opal_get_msg(m_ptr, sizeof(m));
135
assert(list_count(&msg_pending_list) == --npending);
136
assert(list_count(&msg_free_list) == ++nfree);
138
assert(m.params[0] == 0);
139
assert(m.params[1] == 1);
140
assert(m.params[2] == 2);
141
assert(m.params[3] == 3);
142
assert(m.params[4] == 4);
143
assert(m.params[5] == 5);
144
assert(m.params[6] == 6);
145
assert(m.params[7] == 7);
147
/* 8 params (ARRAY_SIZE(entry->msg.params) */
148
r = opal_queue_msg(0, NULL, NULL, 0, 10, 20, 30, 40, 50, 60, 70);
151
assert(list_count(&msg_pending_list) == ++npending);
152
assert(list_count(&msg_free_list) == --nfree);
154
r = opal_get_msg(m_ptr, sizeof(m));
157
assert(list_count(&msg_pending_list) == --npending);
158
assert(list_count(&msg_free_list) == ++nfree);
160
assert(m.params[0] == 0);
161
assert(m.params[1] == 10);
162
assert(m.params[2] == 20);
163
assert(m.params[3] == 30);
164
assert(m.params[4] == 40);
165
assert(m.params[5] == 50);
166
assert(m.params[6] == 60);
167
assert(m.params[7] == 70);
169
/* Full list (no free nodes in pending). */
171
r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
173
assert(list_count(&msg_pending_list) == ++npending);
174
assert(list_count(&msg_free_list) == --nfree);
176
assert(list_count(&msg_free_list) == 0);
178
assert(npending == OPAL_MAX_MSGS);
180
r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
183
assert(list_count(&msg_pending_list) == OPAL_MAX_MSGS+1);
184
assert(list_count(&msg_pending_list) == ++npending);
185
assert(list_count(&msg_free_list) == nfree);
187
/* Make zalloc fail to test error handling. */
188
zalloc_should_fail = true;
189
r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
190
assert(r == OPAL_RESOURCE);
192
assert(list_count(&msg_pending_list) == OPAL_MAX_MSGS+1);
193
assert(list_count(&msg_pending_list) == npending);
194
assert(list_count(&msg_free_list) == nfree);
196
/* Empty list (no nodes). */
197
while(!list_empty(&msg_pending_list)) {
198
r = opal_get_msg(m_ptr, sizeof(m));
203
assert(list_count(&msg_pending_list) == npending);
204
assert(list_count(&msg_free_list) == nfree);
205
assert(npending == 0);
206
assert(nfree == OPAL_MAX_MSGS+1);
208
r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
211
assert(list_count(&msg_pending_list) == ++npending);
212
assert(list_count(&msg_free_list) == --nfree);
214
/* Request invalid size. */
215
r = opal_get_msg(m_ptr, sizeof(m) - 1);
216
assert(r == OPAL_PARAMETER);
218
/* Pass null buffer. */
219
r = opal_get_msg(NULL, sizeof(m));
220
assert(r == OPAL_PARAMETER);
222
/* Get msg when none are pending. */
223
r = opal_get_msg(m_ptr, sizeof(m));
226
r = opal_get_msg(m_ptr, sizeof(m));
227
assert(r == OPAL_RESOURCE);
229
#define test_queue_num(type, val) \
230
r = opal_queue_msg(0, NULL, NULL, \
231
(type)val, (type)val, (type)val, (type)val, \
232
(type)val, (type)val, (type)val, (type)val); \
234
opal_get_msg(m_ptr, sizeof(m)); \
235
assert(r == OPAL_SUCCESS); \
236
assert(m.params[0] == (type)val); \
237
assert(m.params[1] == (type)val); \
238
assert(m.params[2] == (type)val); \
239
assert(m.params[3] == (type)val); \
240
assert(m.params[4] == (type)val); \
241
assert(m.params[5] == (type)val); \
242
assert(m.params[6] == (type)val); \
243
assert(m.params[7] == (type)val)
245
/* Test types of various widths */
246
test_queue_num(u64, -1);
247
test_queue_num(s64, -1);
248
test_queue_num(u32, -1);
249
test_queue_num(s32, -1);
250
test_queue_num(u16, -1);
251
test_queue_num(s16, -1);
252
test_queue_num(u8, -1);
253
test_queue_num(s8, -1);
255
/* Clean up the list to keep valgrind happy. */
256
while(!list_empty(&msg_free_list)) {
257
entry = list_pop(&msg_free_list, struct opal_msg_entry, link);
262
while(!list_empty(&msg_pending_list)) {
263
entry = list_pop(&msg_pending_list, struct opal_msg_entry, link);