4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
25 # Copyright (c) 2014 Racktop Systems.
29 # Lgrp.pm provides procedural and object-oriented interface to the Solaris
30 # liblgrp(3LIB) library.
39 package Sun
::Solaris
::Lgrp
;
43 XSLoader
::load
(__PACKAGE__
, $VERSION);
47 our @ISA = qw(Exporter);
49 our (@EXPORT_OK, %EXPORT_TAGS);
52 my @lgrp_constants = qw(LGRP_AFF_NONE LGRP_AFF_STRONG LGRP_AFF_WEAK
53 LGRP_CONTENT_DIRECT LGRP_CONTENT_HIERARCHY
54 LGRP_MEM_SZ_FREE LGRP_MEM_SZ_INSTALLED LGRP_VER_CURRENT
55 LGRP_VER_NONE LGRP_VIEW_CALLER
56 LGRP_VIEW_OS LGRP_NONE
57 LGRP_RSRC_CPU LGRP_RSRC_MEM
58 LGRP_CONTENT_ALL LGRP_LAT_CPU_TO_MEM
61 my @proc_constants = qw(P_PID P_LWPID P_MYID);
63 my @constants = (@lgrp_constants, @proc_constants);
65 my @functions = qw(lgrp_affinity_get lgrp_affinity_set
66 lgrp_children lgrp_cookie_stale lgrp_cpus lgrp_fini
67 lgrp_home lgrp_init lgrp_latency lgrp_latency_cookie
68 lgrp_mem_size lgrp_nlgrps lgrp_parents
69 lgrp_root lgrp_version lgrp_view lgrp_resources
70 lgrp_isleaf lgrp_lgrps lgrp_leaves);
72 my @all = (@constants, @functions);
74 # Define symbolic names for various subsets of export lists
75 %EXPORT_TAGS = ('CONSTANTS' => \
@constants,
76 'LGRP_CONSTANTS' => \
@lgrp_constants,
77 'PROC_CONSTANTS' => \
@proc_constants,
78 'FUNCTIONS' => \
@functions,
81 # Define things that are ok ot export.
82 @EXPORT_OK = ( @
{ $EXPORT_TAGS{'ALL'} } );
85 # _usage(): print error message and terminate the program.
90 Carp
::croak
"Usage: Sun::Solaris::Lgrp::$msg";
94 # lgrp_isleaf($cookie, $lgrp)
95 # Returns T if lgrp is leaf, F otherwise.
99 scalar @_ == 2 or _usage
"lgrp_isleaf(cookie, lgrp)";
100 return (!lgrp_children
(shift, shift));
104 # lgrp_lgrps($cookie, [$lgrp])
105 # Returns: list of lgrps in a subtree starting from $lgrp.
106 # If $root is not specified, use lgrp_root.
110 scalar @_ > 0 or _usage
("lgrp_lgrps(cookie, [lgrp])");
113 $root = lgrp_root
($cookie) unless defined $root;
114 return unless defined $root;
115 my @children = lgrp_children
($cookie, $root);
119 # Concatenate root with subtrees for every children. Every subtree is
120 # obtained by calling lgrp_lgrps recursively with each of the children
123 @result = @children ?
124 ($root, map {lgrp_lgrps
($cookie, $_)} @children) :
126 return (wantarray ?
@result : scalar @result);
130 # lgrp_leaves($cookie, [$lgrp])
131 # Returns: list of leaves in the hierarchy starting from $lgrp.
132 # If $lgrp is not specified, use lgrp_root.
137 scalar @_ > 0 or _usage
("lgrp_leaves(cookie, [lgrp])");
140 $root = lgrp_root
($cookie) unless defined $root;
141 return unless defined $root;
143 lgrp_isleaf
($cookie, $_)
144 } lgrp_lgrps
($cookie, $root);
145 return (wantarray ?
@result : scalar @result);
148 ######################################################################
149 # Object-Oriented interface.
150 ######################################################################
153 # cookie: extract cookie from the argument.
154 # If the argument is scalar, it is the cookie itself, otherwise it is the
155 # reference to the object and the cookie value is in $self->{COOKIE}.
160 return ((ref $self) ?
$self->{COOKIE
} : $self);
164 # new: The object constructor
171 $self->{COOKIE
} = ($view ? lgrp_init
($view) : lgrp_init
()) or
172 croak
("lgrp_init: $!\n"), return;
173 bless($self, $class) if defined($class);
174 bless($self) unless defined($class);
179 # DESTROY: the object destructor.
183 lgrp_fini
(cookie
(shift));
186 ############################################################
191 scalar @_ == 1 or _usage
("stale(class)");
192 return (lgrp_cookie_stale
(cookie
(shift)));
197 scalar @_ == 1 or _usage
("view(class)");
198 return (lgrp_view
(cookie
(shift)));
203 scalar @_ == 1 or _usage
("root(class)");
204 return (lgrp_root
(cookie
(shift)));
209 scalar @_ == 1 or _usage
("nlgrps(class)");
210 return (lgrp_nlgrps
(cookie
(shift)));
215 scalar @_ > 0 or _usage
("lgrps(class, [lgrp])");
216 return (lgrp_lgrps
(cookie
(shift), shift));
221 scalar @_ > 0 or _usage
("leaves(class, [lgrp])");
222 return (lgrp_leaves
(cookie
(shift), shift));
227 scalar @_ > 0 or _usage
("leaves(class, [version])");
229 return (lgrp_version
(shift || 0));
234 scalar @_ == 2 or _usage
("children(class, lgrp)");
235 return (lgrp_children
(cookie
(shift), shift));
240 scalar @_ == 2 or _usage
("parents(class, lgrp)");
241 return (lgrp_parents
(cookie
(shift), shift));
246 scalar @_ == 4 or _usage
("mem_size(class, lgrp, type, content)");
247 return (lgrp_mem_size
(cookie
(shift), shift, shift, shift));
252 scalar @_ == 3 or _usage
("cpus(class, lgrp, content)");
253 return (lgrp_cpus
(cookie
(shift), shift, shift));
258 scalar @_ == 2 or _usage
("isleaf(class, lgrp)");
259 lgrp_isleaf
(cookie
(shift), shift);
264 scalar @_ == 3 or _usage
("resources(class, lgrp, resource)");
265 return (lgrp_resources
(cookie
(shift), shift, shift));
270 scalar @_ == 3 or _usage
("latency(class, from, to)");
271 return (lgrp_latency_cookie
(cookie
(shift), shift, shift));
274 # Methods that do not require cookie
277 scalar @_ == 3 or _usage
("home(class, idtype, id)");
279 return (lgrp_home
(shift, shift));
284 scalar @_ == 4 or _usage
("affinity_get(class, idtype, id, lgrp)");
286 return (lgrp_affinity_get
(shift, shift, shift));
292 _usage
("affinity_set(class, idtype, id, lgrp, affinity)");
294 return (lgrp_affinity_set
(shift, shift, shift, shift));