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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Lgrp.xs contains XS wrappers for the system locality group library
* liblgrp(3LIB).
*/
#include <sys/errno.h>
#include <sys/lgrp_user.h>
/*
* On i386 Solaris defines SP, which conflicts with the perl definition of SP
* We don't need the Solaris one, so get rid of it to avoid warnings.
*/
#undef SP
/* Perl XS includes. */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
/* Return undef in scalar context and empty list in list context */
#define LGRP_BADVAL() { \
if (GIMME_V == G_ARRAY) \
XSRETURN_EMPTY; \
else \
XSRETURN_UNDEF; \
}
/*
* Push all values from input array onto the perl return stack.
*/
#define PUSHARRAY(array, nitems) \
{ \
int x; \
\
if (nitems < 0) { \
LGRP_BADVAL() \
} else if (nitems > 0) { \
EXTEND(SP, nitems); \
for (x = 0; x < nitems; x++) { \
PUSHs(sv_2mortal(newSVnv(array[x]))); \
} \
} \
}
/*
* Several constants are not present in the first version of the Lgrp API,
* we define them here.
*
* lgrp_resources() and lgrp_latency_cookie() only appear in API v2. If the
* module is linked with old version of liblgrp(3LIB) there is no lgrp_resources
* symbol in the library and perl wrapper returns empty list and sets errno to
* EINVAL.
*
* The lgrp_latency_cookie() is emulated using lgrp_latency().
*/
#if LGRP_VER_CURRENT == 1
#define LGRP_CONTENT_ALL LGRP_CONTENT_HIERARCHY
#define LGRP_LAT_CPU_TO_MEM 0
#define LGRP_RSRC_CPU 0 /* CPU resources */
#define LGRP_RSRC_MEM 1 /* memory resources */
#define LGRP_RESOURCES(c, lgrp, type) \
{ errno = EINVAL; LGRP_BADVAL(); }
/*
* Simulate lgrp_latency_cookie() which just fails. This macro is never called
* and we just define it so that the C compiler will not complain about the
* missing symbol.
*/
#define lgrp_latency_cookie(c, f, t, b) (errno = EINVAL, -1)
#else
#define LGRP_RESOURCES(c, lgrp, type) { \
int nr; \
lgrp_id_t *lgrps; \
\
errno = 0; \
nr = lgrp_resources(c, lgrp, NULL, 0, type); \
if (nr < 0) \
LGRP_BADVAL(); \
if (GIMME_V == G_SCALAR) \
XSRETURN_IV(nr); \
if (nr == 0) { \
XSRETURN_EMPTY; \
} else if (New(0, lgrps, nr, lgrp_id_t) == NULL) { \
errno = ENOMEM; \
LGRP_BADVAL(); \
} else { \
nr = lgrp_resources(c, lgrp, lgrps, nr, type); \
PUSHARRAY(lgrps, nr); \
Safefree(lgrps); \
} \
}
#endif
/*
* Special version of lgrp_latency_cookie(). Use lgrp_latency() for liblgrp V1
* and lgrp_latency_cookie for V2.
*/
static int
_lgrp_latency_cookie(lgrp_cookie_t cookie, lgrp_id_t from, lgrp_id_t to,
int between)
{
return (LGRP_VER_CURRENT < 2 ?
lgrp_latency(from, to) :
lgrp_latency_cookie(cookie, from, to, between));
}
/*
* Most functions in liblgrp return -1 on failure. The perl equivalent returns
* 'undef' instead. The macro should be call after the RETVAL is set to the
* return value of the function.
*/
#define RETURN_UNDEF_IF_FAIL { if (RETVAL < 0) XSRETURN_UNDEF; }
/*
* End of C part, start of XS part.
*
* The XS code exported to perl is below here. Note that the XS preprocessor
* has its own commenting syntax, so all comments from this point on are in
* that form.
*/
MODULE = Sun::Solaris::Lgrp PACKAGE = Sun::Solaris::Lgrp
PROTOTYPES: ENABLE
#
# Define any constants that need to be exported. By doing it this way we can
# avoid the overhead of using the DynaLoader package, and in addition constants
# defined using this mechanism are eligible for inlining by the perl
# interpreter at compile time.
#
BOOT:
{
HV *stash;
stash = gv_stashpv("Sun::Solaris::Lgrp", TRUE);
newCONSTSUB(stash, "LGRP_AFF_NONE", newSViv(LGRP_AFF_NONE));
newCONSTSUB(stash, "LGRP_AFF_STRONG", newSViv(LGRP_AFF_STRONG));
newCONSTSUB(stash, "LGRP_AFF_WEAK", newSViv(LGRP_AFF_WEAK));
newCONSTSUB(stash, "LGRP_VER_CURRENT", newSViv(LGRP_VER_CURRENT));
newCONSTSUB(stash, "LGRP_VER_NONE", newSViv(LGRP_VER_NONE));
newCONSTSUB(stash, "LGRP_NONE", newSViv(LGRP_NONE));
newCONSTSUB(stash, "LGRP_RSRC_CPU", newSViv(LGRP_RSRC_CPU));
newCONSTSUB(stash, "LGRP_RSRC_MEM", newSViv(LGRP_RSRC_MEM));
newCONSTSUB(stash, "LGRP_CONTENT_HIERARCHY",
newSViv(LGRP_CONTENT_HIERARCHY));
newCONSTSUB(stash, "LGRP_CONTENT_DIRECT", newSViv(LGRP_CONTENT_DIRECT));
newCONSTSUB(stash, "LGRP_VIEW_CALLER", newSViv(LGRP_VIEW_CALLER));
newCONSTSUB(stash, "LGRP_VIEW_OS", newSViv(LGRP_VIEW_OS));
newCONSTSUB(stash, "LGRP_MEM_SZ_FREE", newSViv(LGRP_MEM_SZ_FREE));
newCONSTSUB(stash, "LGRP_MEM_SZ_INSTALLED",
newSViv(LGRP_MEM_SZ_INSTALLED));
newCONSTSUB(stash, "LGRP_CONTENT_ALL", newSViv(LGRP_CONTENT_ALL));
newCONSTSUB(stash, "LGRP_LAT_CPU_TO_MEM", newSViv(LGRP_LAT_CPU_TO_MEM));
newCONSTSUB(stash, "P_PID", newSViv(P_PID));
newCONSTSUB(stash, "P_LWPID", newSViv(P_LWPID));
newCONSTSUB(stash, "P_MYID", newSViv(P_MYID));
}
#
# The code below uses POSTCALL directive which allows to return 'undef'
# whenever a C function returns a negative value.
#
#
# lgrp_init([view])
# Use LGRP_VIEW_OS as the default view.
#
lgrp_cookie_t
lgrp_init(lgrp_view_t view = LGRP_VIEW_OS)
POSTCALL:
RETURN_UNDEF_IF_FAIL;
lgrp_view_t
lgrp_view(cookie)
lgrp_cookie_t cookie
POSTCALL:
RETURN_UNDEF_IF_FAIL;
lgrp_affinity_t
lgrp_affinity_get(idtype, id, lgrp)
idtype_t idtype;
id_t id;
lgrp_id_t lgrp;
POSTCALL:
RETURN_UNDEF_IF_FAIL;
int
lgrp_affinity_set(idtype, id, lgrp, affinity)
idtype_t idtype;
id_t id;
lgrp_id_t lgrp;
lgrp_affinity_t affinity;
POSTCALL:
RETURN_UNDEF_IF_FAIL;
XSRETURN_YES;
int
lgrp_cookie_stale(cookie)
lgrp_cookie_t cookie;
POSTCALL:
RETURN_UNDEF_IF_FAIL;
int
lgrp_fini(cookie)
lgrp_cookie_t cookie;
POSTCALL:
RETURN_UNDEF_IF_FAIL;
XSRETURN_YES;
lgrp_id_t
lgrp_home(idtype, id)
idtype_t idtype;
id_t id;
POSTCALL:
RETURN_UNDEF_IF_FAIL;
int
lgrp_latency(lgrp_id_t from,lgrp_id_t to)
POSTCALL:
RETURN_UNDEF_IF_FAIL;
lgrp_mem_size_t
lgrp_mem_size(cookie, lgrp, type, content)
lgrp_cookie_t cookie
lgrp_id_t lgrp
int type
lgrp_content_t content
POSTCALL:
RETURN_UNDEF_IF_FAIL;
int
lgrp_nlgrps(cookie)
lgrp_cookie_t cookie;
POSTCALL:
RETURN_UNDEF_IF_FAIL;
lgrp_id_t
lgrp_root(cookie)
lgrp_cookie_t cookie
POSTCALL:
RETURN_UNDEF_IF_FAIL;
int
lgrp_version(int version = LGRP_VER_NONE)
#
# lgrp_latency_cookie calls our internal wrapper _lgrp_latency_cookie() which
# works for both old and new versions of liblgrp.
#
int
lgrp_latency_cookie(lgrp_cookie_t cookie, lgrp_id_t from, lgrp_id_t to, int between = 0)
CODE:
RETVAL = _lgrp_latency_cookie(cookie, from, to, between);
POSTCALL:
RETURN_UNDEF_IF_FAIL;
OUTPUT:
RETVAL
#
# Functions below convert C arrays into Perl lists. They use XS PPCODE
# directive to avoid implicit RETVAL assignments and manipulate perl
# stack directly.
#
# When called in scalar context functions return the number of elements
# in the list or undef on failure.
#
# The PUSHARRAY() macro defined above pushes all values from the C array to
# the perl stack.
#
#
# @children = lgrp_children($cookie, $parent).
#
void
lgrp_children(cookie, lgrp)
lgrp_cookie_t cookie;
lgrp_id_t lgrp;
PREINIT:
lgrp_id_t *lgrps;
int count;
PPCODE:
errno = 0;
if ((count = lgrp_children(cookie, lgrp, NULL, 0)) < 0)
LGRP_BADVAL();
if (GIMME_V == G_SCALAR)
XSRETURN_IV(count);
if (count > 0) {
if (New(0, lgrps, count, lgrp_id_t) == NULL) {
errno = ENOMEM;
LGRP_BADVAL();
} else {
count = lgrp_children(cookie, lgrp, lgrps, count);
PUSHARRAY(lgrps, count);
Safefree(lgrps);
}
}
#
# @parents = lgrp_parents($cookie, $lgrp).
#
void
lgrp_parents(cookie, lgrp)
lgrp_cookie_t cookie;
lgrp_id_t lgrp;
PREINIT:
lgrp_id_t *lgrps;
int count;
PPCODE:
errno = 0;
if ((count = lgrp_parents(cookie, lgrp, NULL, 0)) < 0)
LGRP_BADVAL();
if (GIMME_V == G_SCALAR)
XSRETURN_IV(count);
if (count > 0) {
if (New(0, lgrps, count, lgrp_id_t) == NULL) {
errno = ENOMEM;
LGRP_BADVAL();
} else {
count = lgrp_parents(cookie, lgrp, lgrps, count);
PUSHARRAY(lgrps, count);
Safefree(lgrps);
}
}
#
# @parents = lgrp_cpus($cookie, $lgrp, $content).
# Content should be LGRP_CONTENT_HIERARCHY or LGRP_CONTENT_ALL or
# LGRP_CONTENT_DIRECT
void
lgrp_cpus(cookie, lgrp, content)
lgrp_cookie_t cookie;
lgrp_id_t lgrp;
lgrp_content_t content;
PREINIT:
int ncpus;
processorid_t *cpus;
PPCODE:
errno = 0;
if ((ncpus = lgrp_cpus(cookie, lgrp, NULL, 0, content)) < 0)
LGRP_BADVAL();
if (GIMME_V == G_SCALAR)
XSRETURN_IV(ncpus);
if (ncpus > 0) {
if (New(0, cpus, ncpus, processorid_t) == NULL) {
errno = ENOMEM;
LGRP_BADVAL();
} else {
ncpus = lgrp_cpus(cookie, lgrp, cpus, ncpus, content);
PUSHARRAY(cpus, ncpus);
Safefree(cpus);
}
}
void
lgrp_resources(cookie, lgrp, type)
lgrp_cookie_t cookie;
lgrp_id_t lgrp;
int type;
PPCODE:
LGRP_RESOURCES(cookie, lgrp, type);
|