xref: /onnv-gate/usr/src/cmd/perl/contrib/Sun/Solaris/Lgrp/Lgrp.pm (revision 8287:771477e4b843)
12685Sakolb#
22685Sakolb# CDDL HEADER START
32685Sakolb#
42685Sakolb# The contents of this file are subject to the terms of the
52685Sakolb# Common Development and Distribution License (the "License").
62685Sakolb# You may not use this file except in compliance with the License.
72685Sakolb#
82685Sakolb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92685Sakolb# or http://www.opensolaris.org/os/licensing.
102685Sakolb# See the License for the specific language governing permissions
112685Sakolb# and limitations under the License.
122685Sakolb#
132685Sakolb# When distributing Covered Code, include this CDDL HEADER in each
142685Sakolb# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152685Sakolb# If applicable, add the following below this CDDL HEADER, with the
162685Sakolb# fields enclosed by brackets "[]" replaced with your own identifying
172685Sakolb# information: Portions Copyright [yyyy] [name of copyright owner]
182685Sakolb#
192685Sakolb# CDDL HEADER END
202685Sakolb#
212685Sakolb
222685Sakolb#
23*8287SJohn.Sonnenschein@Sun.COM# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
242685Sakolb# Use is subject to license terms.
252685Sakolb#
262685Sakolb
272685Sakolb#
282685Sakolb# Lgrp.pm provides procedural and object-oriented interface to the Solaris
292685Sakolb# liblgrp(3LIB) library.
302685Sakolb#
312685Sakolb
322685Sakolb
33*8287SJohn.Sonnenschein@Sun.COMrequire 5.8.4;
342685Sakolbuse strict;
352685Sakolbuse warnings;
362685Sakolbuse Carp;
372685Sakolb
382685Sakolbpackage Sun::Solaris::Lgrp;
392685Sakolb
407298SMark.J.Nelson@Sun.COMour $VERSION = '1.1';
412685Sakolbuse XSLoader;
422685SakolbXSLoader::load(__PACKAGE__, $VERSION);
432685Sakolb
442685Sakolbrequire Exporter;
452685Sakolb
462685Sakolbour @ISA = qw(Exporter);
472685Sakolb
482685Sakolbour (@EXPORT_OK, %EXPORT_TAGS);
492685Sakolb
502685Sakolb# Things to export
512685Sakolbmy @lgrp_constants = qw(LGRP_AFF_NONE LGRP_AFF_STRONG LGRP_AFF_WEAK
522685Sakolb			LGRP_CONTENT_DIRECT LGRP_CONTENT_HIERARCHY
532685Sakolb			LGRP_MEM_SZ_FREE LGRP_MEM_SZ_INSTALLED LGRP_VER_CURRENT
542685Sakolb			LGRP_VER_NONE LGRP_VIEW_CALLER
552685Sakolb			LGRP_VIEW_OS LGRP_NONE
562685Sakolb			LGRP_RSRC_CPU LGRP_RSRC_MEM
572685Sakolb			LGRP_CONTENT_ALL LGRP_LAT_CPU_TO_MEM
582685Sakolb);
592685Sakolb
602685Sakolbmy @proc_constants = qw(P_PID P_LWPID P_MYID);
612685Sakolb
622685Sakolbmy @constants = (@lgrp_constants, @proc_constants);
632685Sakolb
642685Sakolbmy @functions = qw(lgrp_affinity_get lgrp_affinity_set
652685Sakolb		   lgrp_children lgrp_cookie_stale lgrp_cpus lgrp_fini
662685Sakolb		   lgrp_home lgrp_init lgrp_latency lgrp_latency_cookie
672685Sakolb		   lgrp_mem_size lgrp_nlgrps lgrp_parents
682685Sakolb		   lgrp_root lgrp_version lgrp_view lgrp_resources
692685Sakolb		   lgrp_isleaf lgrp_lgrps lgrp_leaves);
702685Sakolb
712685Sakolbmy @all = (@constants, @functions);
722685Sakolb
732685Sakolb# Define symbolic names for various subsets of export lists
742685Sakolb%EXPORT_TAGS = ('CONSTANTS' => \@constants,
752685Sakolb		'LGRP_CONSTANTS' => \@lgrp_constants,
762685Sakolb		'PROC_CONSTANTS' => \@proc_constants,
772685Sakolb		'FUNCTIONS' => \@functions,
782685Sakolb		'ALL' => \@all);
792685Sakolb
802685Sakolb# Define things that are ok ot export.
812685Sakolb@EXPORT_OK = ( @{ $EXPORT_TAGS{'ALL'} } );
822685Sakolb
832685Sakolb#
842685Sakolb# _usage(): print error message and terminate the program.
852685Sakolb#
862685Sakolbsub _usage
872685Sakolb{
882685Sakolb	my $msg = shift;
892685Sakolb	Carp::croak "Usage: Sun::Solaris::Lgrp::$msg";
902685Sakolb}
912685Sakolb
922685Sakolb#
932685Sakolb# lgrp_isleaf($cookie, $lgrp)
942685Sakolb#   Returns T if lgrp is leaf, F otherwise.
952685Sakolb#
962685Sakolbsub lgrp_isleaf
972685Sakolb{
982685Sakolb	scalar @_ == 2 or _usage "lgrp_isleaf(cookie, lgrp)";
992685Sakolb	return (!lgrp_children(shift, shift));
1002685Sakolb}
1012685Sakolb
1022685Sakolb#
1032685Sakolb# lgrp_lgrps($cookie, [$lgrp])
1042685Sakolb#   Returns: list of lgrps in a subtree starting from $lgrp.
1052685Sakolb# 	     If $root is not specified, use lgrp_root.
1062685Sakolb# 	     undef on failure.
1072685Sakolbsub lgrp_lgrps
1082685Sakolb{
1092685Sakolb	scalar @_ > 0 or _usage("lgrp_lgrps(cookie, [lgrp])");
1102685Sakolb	my $cookie = shift;
1112685Sakolb	my $root = shift;
1122685Sakolb	$root = lgrp_root($cookie) unless defined $root;
1132685Sakolb	return unless defined $root;
1142685Sakolb	my @children = lgrp_children($cookie, $root);
1152685Sakolb	my @result;
1162685Sakolb
1172685Sakolb	#
1182685Sakolb	# Concatenate root with subtrees for every children. Every subtree is
1192685Sakolb	# obtained by calling lgrp_lgrps recursively with each of the children
1202685Sakolb	# as the argument.
1212685Sakolb	#
1222685Sakolb	@result = @children ?
1232685Sakolb	  ($root, map {lgrp_lgrps($cookie, $_)} @children) :
1242685Sakolb	    ($root);
1252685Sakolb	return (wantarray ? @result : scalar @result);
1262685Sakolb}
1272685Sakolb
1282685Sakolb#
1292685Sakolb# lgrp_leaves($cookie, [$lgrp])
1302685Sakolb#   Returns: list of leaves in the hierarchy starting from $lgrp.
1312685Sakolb# 	     If $lgrp is not specified, use lgrp_root.
1322685Sakolb# 	     undef on failure.
1332685Sakolb#
1342685Sakolbsub lgrp_leaves
1352685Sakolb{
1362685Sakolb	scalar @_ > 0 or _usage("lgrp_leaves(cookie, [lgrp])");
1372685Sakolb	my $cookie = shift;
1382685Sakolb	my $root = shift;
1392685Sakolb	$root = lgrp_root($cookie) unless defined $root;
1402685Sakolb	return unless defined $root;
1412685Sakolb	my @result = grep {
1422685Sakolb		lgrp_isleaf($cookie, $_)
1432685Sakolb	} lgrp_lgrps($cookie, $root);
1442685Sakolb	return (wantarray ? @result : scalar @result);
1452685Sakolb}
1462685Sakolb
1472685Sakolb######################################################################
1482685Sakolb# Object-Oriented interface.
1492685Sakolb######################################################################
1502685Sakolb
1512685Sakolb#
1522685Sakolb# cookie: extract cookie from the argument.
1532685Sakolb# If the argument is scalar, it is the cookie itself, otherwise it is the
1542685Sakolb# reference to the object and the cookie value is in $self->{COOKIE}.
1552685Sakolb#
1562685Sakolbsub cookie
1572685Sakolb{
1582685Sakolb	my $self = shift;
1592685Sakolb	return ((ref $self) ? $self->{COOKIE} : $self);
1602685Sakolb}
1612685Sakolb
1622685Sakolb#
1632685Sakolb# new: The object constructor
1642685Sakolb#
1652685Sakolbsub new
1662685Sakolb{
1672685Sakolb	my $class = shift;
1682685Sakolb	my ($self, $view);
1692685Sakolb	$view = shift;
1702685Sakolb	$self->{COOKIE} = ($view ? lgrp_init($view) : lgrp_init()) or
1712685Sakolb	  croak("lgrp_init: $!\n"), return;
1722685Sakolb	bless($self, $class) if defined($class);
1732685Sakolb	bless($self) unless defined($class);
1742685Sakolb	return ($self);
1752685Sakolb}
1762685Sakolb
1772685Sakolb#
1782685Sakolb# DESTROY: the object destructor.
1792685Sakolb#
1802685Sakolbsub DESTROY
1812685Sakolb{
1822685Sakolb	lgrp_fini(cookie(shift));
1832685Sakolb}
1842685Sakolb
1852685Sakolb############################################################
1862685Sakolb# Wrapper methods.
1872685Sakolb#
1882685Sakolbsub stale
1892685Sakolb{
1902685Sakolb	scalar @_ == 1 or _usage("stale(class)");
1912685Sakolb	return (lgrp_cookie_stale(cookie(shift)));
1922685Sakolb}
1932685Sakolb
1942685Sakolbsub view
1952685Sakolb{
1962685Sakolb	scalar @_ == 1 or _usage("view(class)");
1972685Sakolb	return (lgrp_view(cookie(shift)));
1982685Sakolb}
1992685Sakolb
2002685Sakolbsub root
2012685Sakolb{
2022685Sakolb	scalar @_ == 1 or _usage("root(class)");
2032685Sakolb	return (lgrp_root(cookie(shift)));
2042685Sakolb}
2052685Sakolb
2062685Sakolbsub nlgrps
2072685Sakolb{
2082685Sakolb	scalar @_ == 1 or _usage("nlgrps(class)");
2092685Sakolb	return (lgrp_nlgrps(cookie(shift)));
2102685Sakolb}
2112685Sakolb
2122685Sakolbsub lgrps
2132685Sakolb{
2142685Sakolb	scalar @_ > 0 or _usage("lgrps(class, [lgrp])");
2152685Sakolb	return (lgrp_lgrps(cookie(shift), shift));
2162685Sakolb}
2172685Sakolb
2182685Sakolbsub leaves
2192685Sakolb{
2202685Sakolb	scalar @_ > 0 or _usage("leaves(class, [lgrp])");
2212685Sakolb	return (lgrp_leaves(cookie(shift), shift));
2222685Sakolb}
2232685Sakolb
2242685Sakolbsub version
2252685Sakolb{
2262685Sakolb	scalar @_ > 0 or _usage("leaves(class, [version])");
2272685Sakolb	shift;
2282685Sakolb	return (lgrp_version(shift || 0));
2292685Sakolb}
2302685Sakolb
2312685Sakolbsub children
2322685Sakolb{
2332685Sakolb	scalar @_ == 2 or _usage("children(class, lgrp)");
2342685Sakolb	return (lgrp_children(cookie(shift), shift));
2352685Sakolb}
2362685Sakolb
2372685Sakolbsub parents
2382685Sakolb{
2392685Sakolb	scalar @_ == 2 or _usage("parents(class, lgrp)");
2402685Sakolb	return (lgrp_parents(cookie(shift), shift));
2412685Sakolb}
2422685Sakolb
2432685Sakolbsub mem_size
2442685Sakolb{
2452685Sakolb	scalar @_ == 4 or _usage("mem_size(class, lgrp, type, content)");
2462685Sakolb	return (lgrp_mem_size(cookie(shift), shift, shift, shift));
2472685Sakolb}
2482685Sakolb
2492685Sakolbsub cpus
2502685Sakolb{
2512685Sakolb	scalar @_ == 3 or _usage("cpus(class, lgrp, content)");
2522685Sakolb	return (lgrp_cpus(cookie(shift), shift, shift));
2532685Sakolb}
2542685Sakolb
2552685Sakolbsub isleaf
2562685Sakolb{
2572685Sakolb	scalar @_ == 2 or _usage("isleaf(class, lgrp)");
2582685Sakolb	lgrp_isleaf(cookie(shift), shift);
2592685Sakolb}
2602685Sakolb
2612685Sakolbsub resources
2622685Sakolb{
2632685Sakolb	scalar @_ == 3 or _usage("resources(class, lgrp, resource)");
2642685Sakolb	return (lgrp_resources(cookie(shift), shift, shift));
2652685Sakolb}
2662685Sakolb
2672685Sakolbsub latency
2682685Sakolb{
2692685Sakolb	scalar @_ == 3 or _usage("latency(class, from, to)");
2702685Sakolb	return (lgrp_latency_cookie(cookie(shift), shift, shift));
2712685Sakolb}
2722685Sakolb
2732685Sakolb# Methods that do not require cookie
2742685Sakolbsub home
2752685Sakolb{
2762685Sakolb	scalar @_ == 3 or _usage("home(class, idtype, id)");
2772685Sakolb	shift;
2782685Sakolb	return (lgrp_home(shift, shift));
2792685Sakolb}
2802685Sakolb
2812685Sakolbsub affinity_get
2822685Sakolb{
2832685Sakolb	scalar @_ == 4 or _usage("affinity_get(class, idtype, id, lgrp)");
2842685Sakolb	shift;
2852685Sakolb	return (lgrp_affinity_get(shift, shift, shift));
2862685Sakolb}
2872685Sakolb
2882685Sakolbsub affinity_set
2892685Sakolb{
2902685Sakolb	scalar @_ == 5 or
2912685Sakolb	  _usage("affinity_set(class, idtype, id, lgrp, affinity)");
2922685Sakolb	shift;
2932685Sakolb	return (lgrp_affinity_set(shift, shift, shift, shift));
2942685Sakolb}
2952685Sakolb
2962685Sakolb1;
2972685Sakolb
2982685Sakolb__END__
299