17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate * 267c478bd9Sstevel@tonic-gate * ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate * 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate package com.sun.solaris.service.locality; 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate import java.util.*; 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate import com.sun.solaris.service.pools.*; 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /** 377c478bd9Sstevel@tonic-gate * A representation of the Locality Groups for a single Solaris 387c478bd9Sstevel@tonic-gate * instance. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate public class LocalityDomain 417c478bd9Sstevel@tonic-gate { 427c478bd9Sstevel@tonic-gate /** 437c478bd9Sstevel@tonic-gate * Obtain a Locality Group snapshot based on the view 447c478bd9Sstevel@tonic-gate * available to the caller. 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate public static final int LGRP_VIEW_CALLER = 0; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /** 497c478bd9Sstevel@tonic-gate * Obtain a Locality Group snapshot based on the view 507c478bd9Sstevel@tonic-gate * of the Operating System. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate public static final int LGRP_VIEW_OS = 1; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate static 557c478bd9Sstevel@tonic-gate { 567c478bd9Sstevel@tonic-gate System.loadLibrary("jlgrp"); 577c478bd9Sstevel@tonic-gate } 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /** 607c478bd9Sstevel@tonic-gate * The view used to create this LocalityDomain. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate private int view; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /** 657c478bd9Sstevel@tonic-gate * The cookie which represents the snapshot of locality 667c478bd9Sstevel@tonic-gate * information held by the lgrp library. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate private long cookie; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /** 717c478bd9Sstevel@tonic-gate * The root LocalityGroup for the LocalityDomain 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate private LocalityGroup root; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /** 767c478bd9Sstevel@tonic-gate * Cached value of maxLatency. 777c478bd9Sstevel@tonic-gate */ 787c478bd9Sstevel@tonic-gate private final int maxLatency; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /** 817c478bd9Sstevel@tonic-gate * String representation of often used property. 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate private final static String CPU_SYS_ID = "cpu.sys_id"; 84*55fea89dSDan Cross 857c478bd9Sstevel@tonic-gate /** 867c478bd9Sstevel@tonic-gate * Constructor. 877c478bd9Sstevel@tonic-gate * 887c478bd9Sstevel@tonic-gate * @param view to use when creating the LocalityDomain. 897c478bd9Sstevel@tonic-gate * @throws Exception if there is a problem initializing the 907c478bd9Sstevel@tonic-gate * lgrp snapshot. 917c478bd9Sstevel@tonic-gate */ LocalityDomain(int view)927c478bd9Sstevel@tonic-gate public LocalityDomain(int view) throws Exception 937c478bd9Sstevel@tonic-gate { 947c478bd9Sstevel@tonic-gate this.view = view; 957c478bd9Sstevel@tonic-gate cookie = jl_init(view); 967c478bd9Sstevel@tonic-gate root = jl_root(); 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * The maxLatency calculation is expensive and is used 997c478bd9Sstevel@tonic-gate * every time a locality objective is examined. Since 1007c478bd9Sstevel@tonic-gate * it will never change over the lifetime of a 1017c478bd9Sstevel@tonic-gate * LocalityDomain, we calculate it once in the 1027c478bd9Sstevel@tonic-gate * constructor and cache for future use. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate maxLatency = calcMaxLatency(); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /** 1087c478bd9Sstevel@tonic-gate * Reclaim the resource allocated by the C proxy. 1097c478bd9Sstevel@tonic-gate * 1107c478bd9Sstevel@tonic-gate * @throws Throwable if there is a problem reclaiming the reosurces. 1117c478bd9Sstevel@tonic-gate */ finalize()1127c478bd9Sstevel@tonic-gate protected void finalize() throws Throwable 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate try 1157c478bd9Sstevel@tonic-gate { 1167c478bd9Sstevel@tonic-gate close(); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate finally 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate super.finalize(); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /** 1257c478bd9Sstevel@tonic-gate * Return the "root" LocalityGroup. 1267c478bd9Sstevel@tonic-gate */ getRoot()1277c478bd9Sstevel@tonic-gate public LocalityGroup getRoot() 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate return (root); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /** 1337c478bd9Sstevel@tonic-gate * Close this LocalityDomain. Resources are reclaimed in the C 1347c478bd9Sstevel@tonic-gate * proxy and this LocalityDomain should never be used 1357c478bd9Sstevel@tonic-gate * again. None of the LocalityGroups which are referenced from 1367c478bd9Sstevel@tonic-gate * this LocalityDomain should be used after this method is 1377c478bd9Sstevel@tonic-gate * invoked. NB: jl_fini returns a success indicator which is 1387c478bd9Sstevel@tonic-gate * ignored as we are closing this domain. 1397c478bd9Sstevel@tonic-gate */ close()1407c478bd9Sstevel@tonic-gate public void close() 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate if (cookie != 0) { 1437c478bd9Sstevel@tonic-gate jl_fini(); 1447c478bd9Sstevel@tonic-gate cookie = 0; 1457c478bd9Sstevel@tonic-gate root = null; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /** 1507c478bd9Sstevel@tonic-gate * Return a string representation of this instance. 1517c478bd9Sstevel@tonic-gate */ toString()1527c478bd9Sstevel@tonic-gate public String toString() 1537c478bd9Sstevel@tonic-gate { 1547c478bd9Sstevel@tonic-gate return (root.toString()); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /** 1587c478bd9Sstevel@tonic-gate * Return the groups in this domain to which the supplied cpus 1597c478bd9Sstevel@tonic-gate * belong, excluding the supplied set of groups. 1607c478bd9Sstevel@tonic-gate * 1617c478bd9Sstevel@tonic-gate * @param exclude Set of groups to be excluded. 1627c478bd9Sstevel@tonic-gate * @param cpus List of cpus 1637c478bd9Sstevel@tonic-gate * 1647c478bd9Sstevel@tonic-gate * @throws PoolsException if there is an error accessing the 1657c478bd9Sstevel@tonic-gate * cpu details. 1667c478bd9Sstevel@tonic-gate */ foreignGroups(Set exclude, List cpus)1677c478bd9Sstevel@tonic-gate public Set foreignGroups(Set exclude, List cpus) throws PoolsException 1687c478bd9Sstevel@tonic-gate { 1697c478bd9Sstevel@tonic-gate Iterator cpuIt = cpus.iterator(); 1707c478bd9Sstevel@tonic-gate Set result = new HashSet(); 1717c478bd9Sstevel@tonic-gate while (cpuIt.hasNext()) { 1727c478bd9Sstevel@tonic-gate Component comp = (Component) cpuIt.next(); 1737c478bd9Sstevel@tonic-gate int id = (int) comp.getLongProperty(CPU_SYS_ID); 1747c478bd9Sstevel@tonic-gate LocalityGroup group = getGroup(id); 175*55fea89dSDan Cross if (group != null && exclude.contains(group) == false) 1767c478bd9Sstevel@tonic-gate result.add(group); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate return (result); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /** 1827c478bd9Sstevel@tonic-gate * Return the locality group which contains the majority of 1837c478bd9Sstevel@tonic-gate * the cpus in the supplied list. If more than one group 1847c478bd9Sstevel@tonic-gate * satisfies this criteria, then the choice of group is 1857c478bd9Sstevel@tonic-gate * deterministic but unspecified. 1867c478bd9Sstevel@tonic-gate * 1877c478bd9Sstevel@tonic-gate * @param cpus List of cpus to be examined. 1887c478bd9Sstevel@tonic-gate * 1897c478bd9Sstevel@tonic-gate * @throws PoolsException if there is an error accessing the 1907c478bd9Sstevel@tonic-gate * cpu details. 1917c478bd9Sstevel@tonic-gate */ getRepresentativeGroup(List cpus)1927c478bd9Sstevel@tonic-gate public LocalityGroup getRepresentativeGroup(List cpus) 1937c478bd9Sstevel@tonic-gate throws PoolsException 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate Iterator cpuIt = cpus.iterator(); 1967c478bd9Sstevel@tonic-gate Map grps = new HashMap(); 1977c478bd9Sstevel@tonic-gate while (cpuIt.hasNext()) { 1987c478bd9Sstevel@tonic-gate Component comp = (Component) cpuIt.next(); 1997c478bd9Sstevel@tonic-gate int id = (int) comp.getLongProperty(CPU_SYS_ID); 2007c478bd9Sstevel@tonic-gate LocalityGroup group = getGroup(id); 2017c478bd9Sstevel@tonic-gate Integer score = (Integer) grps.get(group); 2027c478bd9Sstevel@tonic-gate if (score != null) { 2037c478bd9Sstevel@tonic-gate int iscore = score.intValue() + 1; 2047c478bd9Sstevel@tonic-gate grps.put(group, new Integer(iscore)); 2057c478bd9Sstevel@tonic-gate } else { 2067c478bd9Sstevel@tonic-gate grps.put(group, new Integer(1)); 207*55fea89dSDan Cross } 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate Iterator groupIt = grps.keySet().iterator(); 2107c478bd9Sstevel@tonic-gate LocalityGroup centre = null; 2117c478bd9Sstevel@tonic-gate Integer highest = new Integer(0); 2127c478bd9Sstevel@tonic-gate while (groupIt.hasNext()) { 2137c478bd9Sstevel@tonic-gate LocalityGroup cand = (LocalityGroup) groupIt.next(); 2147c478bd9Sstevel@tonic-gate Integer value = (Integer) grps.get(cand); 2157c478bd9Sstevel@tonic-gate if (value.intValue() > highest.intValue()) { 2167c478bd9Sstevel@tonic-gate highest = value; 2177c478bd9Sstevel@tonic-gate centre = cand; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate return (centre); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /** 2247c478bd9Sstevel@tonic-gate * Return the maximum latency between the groups in this 2257c478bd9Sstevel@tonic-gate * domain. 2267c478bd9Sstevel@tonic-gate * 2277c478bd9Sstevel@tonic-gate */ calcMaxLatency()2287c478bd9Sstevel@tonic-gate private int calcMaxLatency() 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate int max = 0; 231*55fea89dSDan Cross 2327c478bd9Sstevel@tonic-gate Set groups = getGroups(); 2337c478bd9Sstevel@tonic-gate Iterator outer = groups.iterator(); 2347c478bd9Sstevel@tonic-gate while (outer.hasNext()) { 2357c478bd9Sstevel@tonic-gate Iterator inner = groups.iterator(); 2367c478bd9Sstevel@tonic-gate LocalityGroup g1 = (LocalityGroup) outer.next(); 2377c478bd9Sstevel@tonic-gate while (inner.hasNext()) { 2387c478bd9Sstevel@tonic-gate LocalityGroup g2 = (LocalityGroup) inner.next(); 2397c478bd9Sstevel@tonic-gate int latency = g1.getLatency(g2); 2407c478bd9Sstevel@tonic-gate if (latency > max) 2417c478bd9Sstevel@tonic-gate max = latency; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate return (max); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate /** 2487c478bd9Sstevel@tonic-gate * Return the maximum possible latency between all locality 2497c478bd9Sstevel@tonic-gate * groups in this domain. 2507c478bd9Sstevel@tonic-gate */ getMaxLatency()2517c478bd9Sstevel@tonic-gate public int getMaxLatency() 2527c478bd9Sstevel@tonic-gate { 2537c478bd9Sstevel@tonic-gate return (maxLatency); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /** 2577c478bd9Sstevel@tonic-gate * Return the set of all LocalityGroups for this LocalityDomain. 2587c478bd9Sstevel@tonic-gate */ getGroups()2597c478bd9Sstevel@tonic-gate public Set getGroups() 2607c478bd9Sstevel@tonic-gate { 2617c478bd9Sstevel@tonic-gate Set groups = new HashSet(); 2627c478bd9Sstevel@tonic-gate groups.add(root); 2637c478bd9Sstevel@tonic-gate getGroups(root, groups); 2647c478bd9Sstevel@tonic-gate return (groups); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate /** 2687c478bd9Sstevel@tonic-gate * Add all the descendent LocalityGroups for the supplied 2697c478bd9Sstevel@tonic-gate * group into the supplied set. 2707c478bd9Sstevel@tonic-gate * 2717c478bd9Sstevel@tonic-gate * @param group is the group whose descendents are processed. 2727c478bd9Sstevel@tonic-gate * @param descendents the set to add descendents of group. 2737c478bd9Sstevel@tonic-gate */ getGroups(LocalityGroup group, Set descendents)2747c478bd9Sstevel@tonic-gate private void getGroups(LocalityGroup group, Set descendents) 2757c478bd9Sstevel@tonic-gate { 2767c478bd9Sstevel@tonic-gate Set children = group.getChildren(); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate if (! children.isEmpty()) { 2797c478bd9Sstevel@tonic-gate Iterator itChild = children.iterator(); 2807c478bd9Sstevel@tonic-gate while (itChild.hasNext()) { 2817c478bd9Sstevel@tonic-gate LocalityGroup child = (LocalityGroup) itChild. 2827c478bd9Sstevel@tonic-gate next(); 2837c478bd9Sstevel@tonic-gate getGroups(child, descendents); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate descendents.addAll(children); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /** 2907c478bd9Sstevel@tonic-gate * Return the LocalityGroup containing the supplied CPU 2917c478bd9Sstevel@tonic-gate * id. Search all LocalityGroups starting at the root group. 2927c478bd9Sstevel@tonic-gate * 2937c478bd9Sstevel@tonic-gate * @param cpuid is the sys-id of the CPU to search for. 2947c478bd9Sstevel@tonic-gate */ getGroup(int cpuid)2957c478bd9Sstevel@tonic-gate public LocalityGroup getGroup(int cpuid) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate LocalityGroup answer = getGroup(root, cpuid); 2987c478bd9Sstevel@tonic-gate return (getGroup(root, cpuid)); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /** 3027c478bd9Sstevel@tonic-gate * Return the LocalityGroup containing the supplied CPU 3037c478bd9Sstevel@tonic-gate * id. Search LocalityGroups starting at the supplied group. 3047c478bd9Sstevel@tonic-gate * 3057c478bd9Sstevel@tonic-gate * @param group is the group to start searching from. 3067c478bd9Sstevel@tonic-gate * @param cpuid is the sys-id of the CPU to search for. 3077c478bd9Sstevel@tonic-gate */ getGroup(LocalityGroup group, int cpuid)3087c478bd9Sstevel@tonic-gate private LocalityGroup getGroup(LocalityGroup group, int cpuid) 3097c478bd9Sstevel@tonic-gate { 3107c478bd9Sstevel@tonic-gate Set children = group.getChildren(); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (children.isEmpty()) { 3137c478bd9Sstevel@tonic-gate int cpus[] = group.getCPUIDs(); 3147c478bd9Sstevel@tonic-gate 315*55fea89dSDan Cross 3167c478bd9Sstevel@tonic-gate for (int i = 0; i < cpus.length; i++) 3177c478bd9Sstevel@tonic-gate if (cpus[i] == cpuid) { 3187c478bd9Sstevel@tonic-gate return (group); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate } else { 3217c478bd9Sstevel@tonic-gate Iterator itGroup = children.iterator(); 3227c478bd9Sstevel@tonic-gate while (itGroup.hasNext()) { 3237c478bd9Sstevel@tonic-gate LocalityGroup owner; 3247c478bd9Sstevel@tonic-gate LocalityGroup child = (LocalityGroup) itGroup. 3257c478bd9Sstevel@tonic-gate next(); 3267c478bd9Sstevel@tonic-gate if ((owner = getGroup(child, cpuid)) != null) 3277c478bd9Sstevel@tonic-gate return (owner); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate return (null); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /** 3347c478bd9Sstevel@tonic-gate * Initialise the LocalityDomain with an lgrp snapshot. 3357c478bd9Sstevel@tonic-gate * 3367c478bd9Sstevel@tonic-gate * @param view is the type of snapshot to obtain. 3377c478bd9Sstevel@tonic-gate */ jl_init(int view)3387c478bd9Sstevel@tonic-gate private native long jl_init(int view) throws Exception; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /** 3417c478bd9Sstevel@tonic-gate * Release the lgrp snapshot. 3427c478bd9Sstevel@tonic-gate */ jl_fini()3437c478bd9Sstevel@tonic-gate private native int jl_fini(); 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate /** 3467c478bd9Sstevel@tonic-gate * Find the root LocalityGroup. 3477c478bd9Sstevel@tonic-gate */ jl_root()3487c478bd9Sstevel@tonic-gate private native LocalityGroup jl_root(); 3497c478bd9Sstevel@tonic-gate 350*55fea89dSDan Cross } 351