1*2685Sakolb#! /usr/perl5/bin/perl 2*2685Sakolb# 3*2685Sakolb# CDDL HEADER START 4*2685Sakolb# 5*2685Sakolb# The contents of this file are subject to the terms of the 6*2685Sakolb# Common Development and Distribution License (the "License"). 7*2685Sakolb# You may not use this file except in compliance with the License. 8*2685Sakolb# 9*2685Sakolb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*2685Sakolb# or http://www.opensolaris.org/os/licensing. 11*2685Sakolb# See the License for the specific language governing permissions 12*2685Sakolb# and limitations under the License. 13*2685Sakolb# 14*2685Sakolb# When distributing Covered Code, include this CDDL HEADER in each 15*2685Sakolb# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*2685Sakolb# If applicable, add the following below this CDDL HEADER, with the 17*2685Sakolb# fields enclosed by brackets "[]" replaced with your own identifying 18*2685Sakolb# information: Portions Copyright [yyyy] [name of copyright owner] 19*2685Sakolb# 20*2685Sakolb# CDDL HEADER END 21*2685Sakolb# 22*2685Sakolb 23*2685Sakolb# 24*2685Sakolb# Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25*2685Sakolb# Use is subject to license terms. 26*2685Sakolb# 27*2685Sakolb# ident "%Z%%M% %I% %E% SMI" 28*2685Sakolb# 29*2685Sakolb 30*2685Sakolb# 31*2685Sakolb# Tests for Sun::Solaris::Lgrp API. 32*2685Sakolb# 33*2685Sakolb# Before `make install' is performed this script should be runnable with 34*2685Sakolb# `make test'. After `make install' it should work as `perl Lgrp.t' 35*2685Sakolb# 36*2685Sakolb# The test uses Test module which is available on Perl 5.6 and later. 37*2685Sakolb# 38*2685Sakolb 39*2685Sakolb 40*2685Sakolbuse strict; 41*2685Sakolbuse warnings; 42*2685Sakolbuse Test; 43*2685Sakolb 44*2685Sakolb# Tests to run 45*2685SakolbBEGIN { plan tests => 63 } 46*2685Sakolb 47*2685Sakolbuse Sun::Solaris::Lgrp ':ALL'; 48*2685Sakolb 49*2685Sakolb# 50*2685Sakolb###################################################################### 51*2685Sakolb 52*2685Sakolbmy ($home, $fail); 53*2685Sakolb 54*2685Sakolb###################################################################### 55*2685Sakolb# Check that all exported constants can be accessed. 56*2685Sakolb$fail = 0; 57*2685Sakolbforeach my $constname (qw( 58*2685Sakolb LGRP_AFF_NONE LGRP_AFF_STRONG LGRP_AFF_WEAK LGRP_CONTENT_DIRECT 59*2685Sakolb LGRP_CONTENT_HIERARCHY LGRP_MEM_SZ_FREE 60*2685Sakolb LGRP_MEM_SZ_INSTALLED LGRP_VER_CURRENT LGRP_VER_NONE 61*2685Sakolb LGRP_VIEW_CALLER LGRP_VIEW_OS LGRP_RSRC_CPU LGRP_RSRC_MEM 62*2685Sakolb LGRP_CONTENT_ALL LGRP_LAT_CPU_TO_MEM)) { 63*2685Sakolb next if (eval "my \$a = $constname; 1"); 64*2685Sakolb $fail++; 65*2685Sakolb} 66*2685Sakolb 67*2685Sakolbok($fail, 0, 'All Constants defined' ); 68*2685Sakolb 69*2685Sakolb######################### 70*2685Sakolb 71*2685Sakolb###################################################################### 72*2685Sakolb# Verify lgrp_version 73*2685Sakolb## 74*2685Sakolbmy $version = lgrp_version(-1); 75*2685Sakolbok($version, LGRP_VER_NONE, 'incorrect lgrp version unsupported'); 76*2685Sakolb 77*2685Sakolb$version = lgrp_version(LGRP_VER_NONE); 78*2685Sakolbok($version, LGRP_VER_CURRENT, 'lgrp version is current'); 79*2685Sakolb 80*2685Sakolb$version = lgrp_version(LGRP_VER_CURRENT); 81*2685Sakolbok($version, LGRP_VER_CURRENT, 'support LGRP_VER_CURRENT version'); 82*2685Sakolb# 83*2685Sakolb####################################################################### 84*2685Sakolb 85*2685Sakolb###################################################################### 86*2685Sakolb# Verify that lgrp_init()/lgrp_fini work. 87*2685Sakolb## 88*2685Sakolbmy $c = lgrp_init(LGRP_VIEW_CALLER); 89*2685Sakolbok($c) or 90*2685Sakolb die("lgrp_init: $!"); 91*2685Sakolb 92*2685Sakolbmy $view = lgrp_view($c); 93*2685Sakolb 94*2685Sakolbok($view, LGRP_VIEW_CALLER, 'View is LGRP_VIEW_CALLER'); 95*2685Sakolb 96*2685Sakolbmy $fin = lgrp_fini($c); 97*2685Sakolbok($fin); 98*2685Sakolb 99*2685Sakolb# Try to free it again, it should fail. 100*2685Sakolb$fin = lgrp_fini($c); 101*2685Sakolbok($fin, undef, 'lgrp_fini second time should fail'); 102*2685Sakolb 103*2685Sakolb$c = lgrp_init(LGRP_VIEW_OS); 104*2685Sakolbok($c) or 105*2685Sakolb die("lgrp_init: $!"); 106*2685Sakolb 107*2685Sakolb$view = lgrp_view($c); 108*2685Sakolb 109*2685Sakolbok($view, LGRP_VIEW_OS, 'View is LGRP_VIEW_OS'); 110*2685Sakolb# 111*2685Sakolb###################################################################### 112*2685Sakolb 113*2685Sakolb###################################################################### 114*2685Sakolb# root should have ID 0. 115*2685Sakolb## 116*2685Sakolbmy $root = lgrp_root($c); 117*2685Sakolbok($root, 0, 'root should have id zero'); 118*2685Sakolb# 119*2685Sakolb###################################################################### 120*2685Sakolb# Verify lgrp_nlgrps() 121*2685Sakolb## 122*2685Sakolbmy $nlgrps = lgrp_nlgrps($c); 123*2685Sakolbok($nlgrps); 124*2685Sakolb 125*2685Sakolbmy @lgrps = lgrp_lgrps($c); 126*2685Sakolbok(@lgrps); 127*2685Sakolbok(scalar @lgrps, $nlgrps, 'lgrp_nlgrps() should match number of lgrps'); 128*2685Sakolbok($nlgrps, lgrp_lgrps($c), 'lgrp_lgrps() in scalar context is sane'); 129*2685Sakolb 130*2685Sakolb###################################################################### 131*2685Sakolb# All root children should have root as their one and only one parent 132*2685Sakolb## 133*2685Sakolb$fail = 0; 134*2685Sakolbmy @children = lgrp_children($c, $root); 135*2685Sakolbok(scalar @children, lgrp_children($c, $root), 'lgrp_children as scalar'); 136*2685Sakolbmy @leaves = lgrp_leaves $c; 137*2685Sakolbok(scalar @leaves); 138*2685Sakolbok(scalar @leaves, lgrp_leaves $c); 139*2685Sakolbok(scalar @children <= scalar @leaves); 140*2685Sakolb 141*2685Sakolbmy @parents; 142*2685Sakolb 143*2685Sakolbmy $fail_lgrp_parents = 0; 144*2685Sakolb 145*2685Sakolbforeach my $l (@children) { 146*2685Sakolb @parents = lgrp_parents($c, $l) or 147*2685Sakolb (print STDERR "# lgrp_parents: $!\n"), $fail++, last; 148*2685Sakolb my $nparents = @parents; 149*2685Sakolb my ($parent, @rest) = @parents; 150*2685Sakolb $fail++ if $parent != $root; 151*2685Sakolb $fail++ unless $nparents == 1; 152*2685Sakolb $fail_lgrp_parents++ if $nparents != lgrp_parents($c, $l); 153*2685Sakolb} 154*2685Sakolbok($fail, 0, 'correct parents for children'); 155*2685Sakolbok($fail_lgrp_parents, 0, 'correct lgrp_parents() as scalar'); 156*2685Sakolb 157*2685Sakolb###################################################################### 158*2685Sakolb# Illegal parents have no children 159*2685Sakolb## 160*2685Sakolb@children = lgrp_children($c, -1); 161*2685Sakolbmy $nchildren = lgrp_children($c, -1); 162*2685Sakolbok(scalar @children, 0, 'Illegal parents have no children'); 163*2685Sakolb# Same in scalar context 164*2685Sakolbok($nchildren, undef, 'No children means undef as scalar'); 165*2685Sakolb 166*2685Sakolb###################################################################### 167*2685Sakolb# root should have no parents. 168*2685Sakolb## 169*2685Sakolb@parents = lgrp_parents($c, $root); 170*2685Sakolbok(scalar @parents, 0, 'root should have no parents'); 171*2685Sakolb# Same in scalar context 172*2685Sakolbok(lgrp_parents($c, $root), 0); 173*2685Sakolb# 174*2685Sakolb###################################################################### 175*2685Sakolb# Illegal children have no paremts 176*2685Sakolb## 177*2685Sakolb@parents = lgrp_parents($c, -1); 178*2685Sakolbmy $nparents = lgrp_parents($c, -1); 179*2685Sakolbok(scalar @parents, 0, 'Illegal children have no paremts'); 180*2685Sakolb# Same in scalar context 181*2685Sakolbok($nparents, undef, 'No parents means undef as scalar'); 182*2685Sakolb# 183*2685Sakolb###################################################################### 184*2685Sakolb# Root should have non-zero CPUs and memory size 185*2685Sakolb## 186*2685Sakolbmy @cpus = lgrp_cpus($c, $root, LGRP_CONTENT_HIERARCHY); 187*2685Sakolbmy $ncpus = lgrp_cpus($c, $root, LGRP_CONTENT_HIERARCHY); 188*2685Sakolbok(scalar @cpus, $ncpus); 189*2685Sakolbok($ncpus); 190*2685Sakolbok(lgrp_mem_size($c, $root, LGRP_MEM_SZ_INSTALLED, LGRP_CONTENT_HIERARCHY)); 191*2685Sakolbmy @ncpus_bad = lgrp_cpus($c, $root, -1); 192*2685Sakolbok(scalar @ncpus_bad, 0, 'Bad argument to lgrp_cpus should return empty'); 193*2685Sakolbmy $ncpus_bad = lgrp_cpus($c, $root, -1); 194*2685Sakolbok($ncpus_bad, undef, 'Bad argument to lgrp_cpus should return undef'); 195*2685Sakolb# 196*2685Sakolb###################################################################### 197*2685Sakolb 198*2685Sakolb###################################################################### 199*2685Sakolb# The cookie should not be stale 200*2685Sakolb# 201*2685Sakolbok(! lgrp_cookie_stale($c)); 202*2685Sakolb# 203*2685Sakolb###################################################################### 204*2685Sakolb 205*2685Sakolb###################################################################### 206*2685Sakolb# Can we call lgrp_latency? 207*2685Sakolb# The latencies from lgrp_latency and lgrp_latency_cookie should match. 208*2685Sakolb## 209*2685Sakolbmy $latency = lgrp_latency($root, $root); 210*2685Sakolbok(defined $latency); 211*2685Sakolb 212*2685Sakolbmy $latency1 = lgrp_latency_cookie($c, $root, $root); 213*2685Sakolbok(defined $latency1); 214*2685Sakolbok($latency, $latency1, 'Latencies should match'); 215*2685Sakolb# 216*2685Sakolb###################################################################### 217*2685Sakolb# Can we call lgrp_resources? 218*2685Sakolb## 219*2685Sakolbmy @lgrps_c = lgrp_resources($c, $root, LGRP_RSRC_CPU); 220*2685Sakolbmy $nresources = lgrp_resources($c, $root, LGRP_RSRC_CPU); 221*2685Sakolbok(!defined $nresources) if $version < 2; 222*2685Sakolbok(scalar @lgrps_c, 0) if $version < 2; 223*2685Sakolbok($nresources) if $version >= 2; 224*2685Sakolbok(@lgrps_c) if $version >= 2; 225*2685Sakolb 226*2685Sakolb## 227*2685Sakolb# lgrp_fini should always succeed. 228*2685Sakolbok(lgrp_fini($c)); 229*2685Sakolb 230*2685Sakolb 231*2685Sakolb###################################################################### 232*2685Sakolb# Now test Object-Oriented interface. 233*2685Sakolb## 234*2685Sakolb$c = Sun::Solaris::Lgrp->new or 235*2685Sakolb die "Lgrp->new(LGRP_VIEW_OS): $!"; 236*2685Sakolb 237*2685Sakolbok($c->view, LGRP_VIEW_OS); 238*2685Sakolbok($c->stale, 0, 'cookie is not stale'); 239*2685Sakolbok($nlgrps, $c->nlgrps, 'nlgrps'); 240*2685Sakolbmy @lg1 = $c->lgrps; 241*2685Sakolbok(@lgrps, @lg1); 242*2685Sakolbmy@leaves1 = $c->leaves; 243*2685Sakolbok(@leaves, @leaves1) or 244*2685Sakolb print STDERR "# \@leaves: @leaves, \@leaves1: @leaves\n"; 245*2685Sakolbok($root, $c->root); 246*2685Sakolb@cpus = lgrp_cpus($c->cookie, $root, LGRP_CONTENT_HIERARCHY); 247*2685Sakolbmy @cpus1 = $c->cpus($root, LGRP_CONTENT_HIERARCHY); 248*2685Sakolbok(@cpus, @cpus1) or 249*2685Sakolb print STDERR "# \@cpus: @cpus, \@cpus1: @cpus1\n"; 250*2685Sakolbok(lgrp_latency($root, $root), $c->latency($root, $root)); 251*2685Sakolbmy @lgrps_c1 = $c->resources($root, LGRP_RSRC_CPU); 252*2685Sakolbok(@lgrps_c, @lgrps_c1); 253*2685Sakolbok(lgrp_version(LGRP_VER_NONE), $c->version); 254*2685Sakolb 255*2685Sakolb# 256*2685Sakolb###################################################################### 257*2685Sakolb# Can we call lgrp_home? 258*2685Sakolb## 259*2685Sakolb$home = lgrp_home(P_PID, P_MYID); 260*2685Sakolbok(defined($home)); 261*2685Sakolbmy $home1 = $c->home(P_PID, P_MYID); 262*2685Sakolbok($home1 == $home); 263*2685Sakolb$home1 = lgrp_home(P_LWPID, 1); 264*2685Sakolbok($home1 == $home); 265*2685Sakolb$home1 = $c->home(P_LWPID, 1); 266*2685Sakolbok($home1 == $home); 267*2685Sakolb 268*2685Sakolb# 269*2685Sakolb###################################################################### 270*2685Sakolb# Can we call lgrp_affinity_set? 271*2685Sakolb## 272*2685Sakolbmy $affinity; 273*2685Sakolb 274*2685Sakolbok(LGRP_AFF_WEAK); 275*2685Sakolbok(P_LWPID); 276*2685Sakolb 277*2685Sakolb$affinity = $c->affinity_set(P_PID, P_MYID, $home, LGRP_AFF_WEAK); 278*2685Sakolbok($affinity); 279*2685Sakolb 280*2685Sakolb$affinity = $c->affinity_set(P_LWPID, 1, $home, LGRP_AFF_WEAK); 281*2685Sakolbok($affinity); 282*2685Sakolb 283*2685Sakolb$affinity = lgrp_affinity_set(P_PID, P_MYID, $home, LGRP_AFF_WEAK); 284*2685Sakolbok($affinity); 285*2685Sakolb 286*2685Sakolb$affinity = lgrp_affinity_set(P_LWPID, 1, $home, LGRP_AFF_WEAK); 287*2685Sakolbok($affinity); 288*2685Sakolb 289*2685Sakolb# 290*2685Sakolb###################################################################### 291*2685Sakolb# Can we call lgrp_affinity_get? 292*2685Sakolb## 293*2685Sakolb$affinity = lgrp_affinity_get(P_PID, P_MYID, $home); 294*2685Sakolbok($affinity = LGRP_AFF_WEAK); 295*2685Sakolb 296*2685Sakolb$affinity = lgrp_affinity_get(P_LWPID, 1, $home); 297*2685Sakolbok($affinity == LGRP_AFF_WEAK); 298*2685Sakolb 299*2685Sakolb$affinity = $c->affinity_get(P_PID, P_MYID, $home); 300*2685Sakolbok($affinity == LGRP_AFF_WEAK); 301*2685Sakolb 302*2685Sakolb$affinity = $c->affinity_get(P_LWPID, 1, $home); 303*2685Sakolbok($affinity == LGRP_AFF_WEAK); 304*2685Sakolb 305*2685Sakolb# 306*2685Sakolb###################################################################### 307*2685Sakolb# THE END! 308*2685Sakolb######### 309