1c6402783Sakolb# 2c6402783Sakolb# CDDL HEADER START 3c6402783Sakolb# 4c6402783Sakolb# The contents of this file are subject to the terms of the 5c6402783Sakolb# Common Development and Distribution License (the "License"). 6c6402783Sakolb# You may not use this file except in compliance with the License. 7c6402783Sakolb# 8c6402783Sakolb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9c6402783Sakolb# or http://www.opensolaris.org/os/licensing. 10c6402783Sakolb# See the License for the specific language governing permissions 11c6402783Sakolb# and limitations under the License. 12c6402783Sakolb# 13c6402783Sakolb# When distributing Covered Code, include this CDDL HEADER in each 14c6402783Sakolb# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15c6402783Sakolb# If applicable, add the following below this CDDL HEADER, with the 16c6402783Sakolb# fields enclosed by brackets "[]" replaced with your own identifying 17c6402783Sakolb# information: Portions Copyright [yyyy] [name of copyright owner] 18c6402783Sakolb# 19c6402783Sakolb# CDDL HEADER END 20c6402783Sakolb# 21c6402783Sakolb 22c6402783Sakolb# 23df0345f7SJohn Sonnenschein# Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24c6402783Sakolb# Use is subject to license terms. 25*c9f77c52SAndy Stormont# Copyright (c) 2014 Racktop Systems. 26c6402783Sakolb# 27c6402783Sakolb 28c6402783Sakolb# 29c6402783Sakolb# Lgrp.pm provides procedural and object-oriented interface to the Solaris 30c6402783Sakolb# liblgrp(3LIB) library. 31c6402783Sakolb# 32c6402783Sakolb 33c6402783Sakolb 34*c9f77c52SAndy Stormontrequire 5.0010; 35c6402783Sakolbuse strict; 36c6402783Sakolbuse warnings; 37c6402783Sakolbuse Carp; 38c6402783Sakolb 39c6402783Sakolbpackage Sun::Solaris::Lgrp; 40c6402783Sakolb 419a70fc3bSMark J. Nelsonour $VERSION = '1.1'; 42c6402783Sakolbuse XSLoader; 43c6402783SakolbXSLoader::load(__PACKAGE__, $VERSION); 44c6402783Sakolb 45c6402783Sakolbrequire Exporter; 46c6402783Sakolb 47c6402783Sakolbour @ISA = qw(Exporter); 48c6402783Sakolb 49c6402783Sakolbour (@EXPORT_OK, %EXPORT_TAGS); 50c6402783Sakolb 51c6402783Sakolb# Things to export 52c6402783Sakolbmy @lgrp_constants = qw(LGRP_AFF_NONE LGRP_AFF_STRONG LGRP_AFF_WEAK 53c6402783Sakolb LGRP_CONTENT_DIRECT LGRP_CONTENT_HIERARCHY 54c6402783Sakolb LGRP_MEM_SZ_FREE LGRP_MEM_SZ_INSTALLED LGRP_VER_CURRENT 55c6402783Sakolb LGRP_VER_NONE LGRP_VIEW_CALLER 56c6402783Sakolb LGRP_VIEW_OS LGRP_NONE 57c6402783Sakolb LGRP_RSRC_CPU LGRP_RSRC_MEM 58c6402783Sakolb LGRP_CONTENT_ALL LGRP_LAT_CPU_TO_MEM 59c6402783Sakolb); 60c6402783Sakolb 61c6402783Sakolbmy @proc_constants = qw(P_PID P_LWPID P_MYID); 62c6402783Sakolb 63c6402783Sakolbmy @constants = (@lgrp_constants, @proc_constants); 64c6402783Sakolb 65c6402783Sakolbmy @functions = qw(lgrp_affinity_get lgrp_affinity_set 66c6402783Sakolb lgrp_children lgrp_cookie_stale lgrp_cpus lgrp_fini 67c6402783Sakolb lgrp_home lgrp_init lgrp_latency lgrp_latency_cookie 68c6402783Sakolb lgrp_mem_size lgrp_nlgrps lgrp_parents 69c6402783Sakolb lgrp_root lgrp_version lgrp_view lgrp_resources 70c6402783Sakolb lgrp_isleaf lgrp_lgrps lgrp_leaves); 71c6402783Sakolb 72c6402783Sakolbmy @all = (@constants, @functions); 73c6402783Sakolb 74c6402783Sakolb# Define symbolic names for various subsets of export lists 75c6402783Sakolb%EXPORT_TAGS = ('CONSTANTS' => \@constants, 76c6402783Sakolb 'LGRP_CONSTANTS' => \@lgrp_constants, 77c6402783Sakolb 'PROC_CONSTANTS' => \@proc_constants, 78c6402783Sakolb 'FUNCTIONS' => \@functions, 79c6402783Sakolb 'ALL' => \@all); 80c6402783Sakolb 81c6402783Sakolb# Define things that are ok ot export. 82c6402783Sakolb@EXPORT_OK = ( @{ $EXPORT_TAGS{'ALL'} } ); 83c6402783Sakolb 84c6402783Sakolb# 85c6402783Sakolb# _usage(): print error message and terminate the program. 86c6402783Sakolb# 87c6402783Sakolbsub _usage 88c6402783Sakolb{ 89c6402783Sakolb my $msg = shift; 90c6402783Sakolb Carp::croak "Usage: Sun::Solaris::Lgrp::$msg"; 91c6402783Sakolb} 92c6402783Sakolb 93c6402783Sakolb# 94c6402783Sakolb# lgrp_isleaf($cookie, $lgrp) 95c6402783Sakolb# Returns T if lgrp is leaf, F otherwise. 96c6402783Sakolb# 97c6402783Sakolbsub lgrp_isleaf 98c6402783Sakolb{ 99c6402783Sakolb scalar @_ == 2 or _usage "lgrp_isleaf(cookie, lgrp)"; 100c6402783Sakolb return (!lgrp_children(shift, shift)); 101c6402783Sakolb} 102c6402783Sakolb 103c6402783Sakolb# 104c6402783Sakolb# lgrp_lgrps($cookie, [$lgrp]) 105c6402783Sakolb# Returns: list of lgrps in a subtree starting from $lgrp. 106c6402783Sakolb# If $root is not specified, use lgrp_root. 107c6402783Sakolb# undef on failure. 108c6402783Sakolbsub lgrp_lgrps 109c6402783Sakolb{ 110c6402783Sakolb scalar @_ > 0 or _usage("lgrp_lgrps(cookie, [lgrp])"); 111c6402783Sakolb my $cookie = shift; 112c6402783Sakolb my $root = shift; 113c6402783Sakolb $root = lgrp_root($cookie) unless defined $root; 114c6402783Sakolb return unless defined $root; 115c6402783Sakolb my @children = lgrp_children($cookie, $root); 116c6402783Sakolb my @result; 117c6402783Sakolb 118c6402783Sakolb # 119c6402783Sakolb # Concatenate root with subtrees for every children. Every subtree is 120c6402783Sakolb # obtained by calling lgrp_lgrps recursively with each of the children 121c6402783Sakolb # as the argument. 122c6402783Sakolb # 123c6402783Sakolb @result = @children ? 124c6402783Sakolb ($root, map {lgrp_lgrps($cookie, $_)} @children) : 125c6402783Sakolb ($root); 126c6402783Sakolb return (wantarray ? @result : scalar @result); 127c6402783Sakolb} 128c6402783Sakolb 129c6402783Sakolb# 130c6402783Sakolb# lgrp_leaves($cookie, [$lgrp]) 131c6402783Sakolb# Returns: list of leaves in the hierarchy starting from $lgrp. 132c6402783Sakolb# If $lgrp is not specified, use lgrp_root. 133c6402783Sakolb# undef on failure. 134c6402783Sakolb# 135c6402783Sakolbsub lgrp_leaves 136c6402783Sakolb{ 137c6402783Sakolb scalar @_ > 0 or _usage("lgrp_leaves(cookie, [lgrp])"); 138c6402783Sakolb my $cookie = shift; 139c6402783Sakolb my $root = shift; 140c6402783Sakolb $root = lgrp_root($cookie) unless defined $root; 141c6402783Sakolb return unless defined $root; 142c6402783Sakolb my @result = grep { 143c6402783Sakolb lgrp_isleaf($cookie, $_) 144c6402783Sakolb } lgrp_lgrps($cookie, $root); 145c6402783Sakolb return (wantarray ? @result : scalar @result); 146c6402783Sakolb} 147c6402783Sakolb 148c6402783Sakolb###################################################################### 149c6402783Sakolb# Object-Oriented interface. 150c6402783Sakolb###################################################################### 151c6402783Sakolb 152c6402783Sakolb# 153c6402783Sakolb# cookie: extract cookie from the argument. 154c6402783Sakolb# If the argument is scalar, it is the cookie itself, otherwise it is the 155c6402783Sakolb# reference to the object and the cookie value is in $self->{COOKIE}. 156c6402783Sakolb# 157c6402783Sakolbsub cookie 158c6402783Sakolb{ 159c6402783Sakolb my $self = shift; 160c6402783Sakolb return ((ref $self) ? $self->{COOKIE} : $self); 161c6402783Sakolb} 162c6402783Sakolb 163c6402783Sakolb# 164c6402783Sakolb# new: The object constructor 165c6402783Sakolb# 166c6402783Sakolbsub new 167c6402783Sakolb{ 168c6402783Sakolb my $class = shift; 169c6402783Sakolb my ($self, $view); 170c6402783Sakolb $view = shift; 171c6402783Sakolb $self->{COOKIE} = ($view ? lgrp_init($view) : lgrp_init()) or 172c6402783Sakolb croak("lgrp_init: $!\n"), return; 173c6402783Sakolb bless($self, $class) if defined($class); 174c6402783Sakolb bless($self) unless defined($class); 175c6402783Sakolb return ($self); 176c6402783Sakolb} 177c6402783Sakolb 178c6402783Sakolb# 179c6402783Sakolb# DESTROY: the object destructor. 180c6402783Sakolb# 181c6402783Sakolbsub DESTROY 182c6402783Sakolb{ 183c6402783Sakolb lgrp_fini(cookie(shift)); 184c6402783Sakolb} 185c6402783Sakolb 186c6402783Sakolb############################################################ 187c6402783Sakolb# Wrapper methods. 188c6402783Sakolb# 189c6402783Sakolbsub stale 190c6402783Sakolb{ 191c6402783Sakolb scalar @_ == 1 or _usage("stale(class)"); 192c6402783Sakolb return (lgrp_cookie_stale(cookie(shift))); 193c6402783Sakolb} 194c6402783Sakolb 195c6402783Sakolbsub view 196c6402783Sakolb{ 197c6402783Sakolb scalar @_ == 1 or _usage("view(class)"); 198c6402783Sakolb return (lgrp_view(cookie(shift))); 199c6402783Sakolb} 200c6402783Sakolb 201c6402783Sakolbsub root 202c6402783Sakolb{ 203c6402783Sakolb scalar @_ == 1 or _usage("root(class)"); 204c6402783Sakolb return (lgrp_root(cookie(shift))); 205c6402783Sakolb} 206c6402783Sakolb 207c6402783Sakolbsub nlgrps 208c6402783Sakolb{ 209c6402783Sakolb scalar @_ == 1 or _usage("nlgrps(class)"); 210c6402783Sakolb return (lgrp_nlgrps(cookie(shift))); 211c6402783Sakolb} 212c6402783Sakolb 213c6402783Sakolbsub lgrps 214c6402783Sakolb{ 215c6402783Sakolb scalar @_ > 0 or _usage("lgrps(class, [lgrp])"); 216c6402783Sakolb return (lgrp_lgrps(cookie(shift), shift)); 217c6402783Sakolb} 218c6402783Sakolb 219c6402783Sakolbsub leaves 220c6402783Sakolb{ 221c6402783Sakolb scalar @_ > 0 or _usage("leaves(class, [lgrp])"); 222c6402783Sakolb return (lgrp_leaves(cookie(shift), shift)); 223c6402783Sakolb} 224c6402783Sakolb 225c6402783Sakolbsub version 226c6402783Sakolb{ 227c6402783Sakolb scalar @_ > 0 or _usage("leaves(class, [version])"); 228c6402783Sakolb shift; 229c6402783Sakolb return (lgrp_version(shift || 0)); 230c6402783Sakolb} 231c6402783Sakolb 232c6402783Sakolbsub children 233c6402783Sakolb{ 234c6402783Sakolb scalar @_ == 2 or _usage("children(class, lgrp)"); 235c6402783Sakolb return (lgrp_children(cookie(shift), shift)); 236c6402783Sakolb} 237c6402783Sakolb 238c6402783Sakolbsub parents 239c6402783Sakolb{ 240c6402783Sakolb scalar @_ == 2 or _usage("parents(class, lgrp)"); 241c6402783Sakolb return (lgrp_parents(cookie(shift), shift)); 242c6402783Sakolb} 243c6402783Sakolb 244c6402783Sakolbsub mem_size 245c6402783Sakolb{ 246c6402783Sakolb scalar @_ == 4 or _usage("mem_size(class, lgrp, type, content)"); 247c6402783Sakolb return (lgrp_mem_size(cookie(shift), shift, shift, shift)); 248c6402783Sakolb} 249c6402783Sakolb 250c6402783Sakolbsub cpus 251c6402783Sakolb{ 252c6402783Sakolb scalar @_ == 3 or _usage("cpus(class, lgrp, content)"); 253c6402783Sakolb return (lgrp_cpus(cookie(shift), shift, shift)); 254c6402783Sakolb} 255c6402783Sakolb 256c6402783Sakolbsub isleaf 257c6402783Sakolb{ 258c6402783Sakolb scalar @_ == 2 or _usage("isleaf(class, lgrp)"); 259c6402783Sakolb lgrp_isleaf(cookie(shift), shift); 260c6402783Sakolb} 261c6402783Sakolb 262c6402783Sakolbsub resources 263c6402783Sakolb{ 264c6402783Sakolb scalar @_ == 3 or _usage("resources(class, lgrp, resource)"); 265c6402783Sakolb return (lgrp_resources(cookie(shift), shift, shift)); 266c6402783Sakolb} 267c6402783Sakolb 268c6402783Sakolbsub latency 269c6402783Sakolb{ 270c6402783Sakolb scalar @_ == 3 or _usage("latency(class, from, to)"); 271c6402783Sakolb return (lgrp_latency_cookie(cookie(shift), shift, shift)); 272c6402783Sakolb} 273c6402783Sakolb 274c6402783Sakolb# Methods that do not require cookie 275c6402783Sakolbsub home 276c6402783Sakolb{ 277c6402783Sakolb scalar @_ == 3 or _usage("home(class, idtype, id)"); 278c6402783Sakolb shift; 279c6402783Sakolb return (lgrp_home(shift, shift)); 280c6402783Sakolb} 281c6402783Sakolb 282c6402783Sakolbsub affinity_get 283c6402783Sakolb{ 284c6402783Sakolb scalar @_ == 4 or _usage("affinity_get(class, idtype, id, lgrp)"); 285c6402783Sakolb shift; 286c6402783Sakolb return (lgrp_affinity_get(shift, shift, shift)); 287c6402783Sakolb} 288c6402783Sakolb 289c6402783Sakolbsub affinity_set 290c6402783Sakolb{ 291c6402783Sakolb scalar @_ == 5 or 292c6402783Sakolb _usage("affinity_set(class, idtype, id, lgrp, affinity)"); 293c6402783Sakolb shift; 294c6402783Sakolb return (lgrp_affinity_set(shift, shift, shift, shift)); 295c6402783Sakolb} 296c6402783Sakolb 297c6402783Sakolb1; 298c6402783Sakolb 299c6402783Sakolb__END__ 300