Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/emory/mathcs/util/natives/PvmArch.java


1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is the Emory Utilities.
15   *
16   * The Initial Developer of the Original Code is
17   * The Distributed Computing Laboratory, Emory University.
18   * Portions created by the Initial Developer are Copyright (C) 2002
19   * the Initial Developer. All Rights Reserved.
20   *
21   * Alternatively, the contents of this file may be used under the terms of
22   * either the GNU General Public License Version 2 or later (the "GPL"), or
23   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24   * in which case the provisions of the GPL or the LGPL are applicable instead
25   * of those above. If you wish to allow use of your version of this file only
26   * under the terms of either the GPL or the LGPL, and not to allow others to
27   * use your version of this file under the terms of the MPL, indicate your
28   * decision by deleting the provisions above and replace them with the notice
29   * and other provisions required by the GPL or the LGPL. If you do not delete
30   * the provisions above, a recipient may use your version of this file under
31   * the terms of any one of the MPL, the GPL or the LGPL.
32   *
33   * ***** END LICENSE BLOCK ***** */
34  
35  package edu.emory.mathcs.util.natives;
36  
37  import java.io.*;
38  import java.security.*;
39  
40  /**
41   * Allows to detect local system architecture as defined by the
42   * <a href="http://www.csm.ornl.gov/pvm/pvmArch.html">PVM project</a>.
43   *
44   * @author Dawid Kurzyniec
45   * @version 1.0
46   */
47  public class PvmArch {
48  
49      private PvmArch() {}
50  
51      private static String arch;
52  
53      private static String[][] stdOsArchVerMappings = new String[][] {
54          // based on http://www.tolstoy.com/samizdat/sysprops.html
55          { "solaris,sparc,[25]\\..*", "SUN4SOL2" },
56  
57          // observed on compute.mathcs.emory.edu
58          { "sunos,sparc,5\\..*", "SUN4SOL2" },
59      };
60  
61      private static String[][] stdOsArchMappings = new String[][] {
62          // based on http://www.tolstoy.com/samizdat/sysprops.html
63          { "linux,x86",             "LINUX" },
64          { "linux,i[3456]86",       "LINUX" },
65          { "mac os.*,powerpc.*",    "MACOS" },
66          { "macos.*,powerpc.*",     "MACOS", },
67          { "os/2,x86",              "OS2" },
68          { "hp.*,pa.risc",          "HP3" },
69          { "aix.*",                 "RS6K" },
70          { "freebsd,x86",           "FREEBSD" },
71          { "irix,.*",               "SGI" },
72          { "irix64,.*",             "SGI64" },
73          { "digital unix,alpha",    "DGAV" },
74  
75          // based on pvmgetarch
76          { "sunos,sun3.*",          "SUN3" },
77          { "sunos,sun4.*",          "SUN4" },
78          { "sunos,i86pc",           "X86SOL2" },
79          { "ultrix,risc",           "PMAX" },
80          { "ultrix,vax",            "UVAX" },
81          { ".*hp.*,9000/[2345].*",  "HP300" },
82          { ".*hp.*,9000/[78].*",    "HPPA" },
83          { ".*osf.*,alpha",         "ALPHA" },
84          { "crsos,smp",             "CRAYSMP" },
85          { ".*,paragon",            "PGON" },
86          { "dgux,aviion",           "DGAV" },
87          { ".*,88k",                "MIPS" },
88          { ".*,cray-2",             "CRAY2" },
89          { "linux,i[3456]86",       "LINUX" },
90          { "linux,alpha",           "LINUXALPHA" },
91          { "linux,arm",             "LINUXARM" },
92          { "linux,sparc*",          "LINUXSPARC" },
93          { "linux,hp_pa",           "LINUXHPPA" },
94          { "linux,ppc",             "LINUXPPC" },
95          { "linux,ia64",            "LINUXIA64" },
96          { "linux,x86_64",          "LINUXAMD64" },
97          { "bsd/os,i[3456]86",      "BSD386" },
98          { "freebsd,i386",          "FREEBSD" },
99          { "super-ux,sx-3",         "SX3" },
100         { "uts,.*",                "UTS2" },
101         { "realix,m88",            "M88K" },
102         { "domainos,dn"  ,         "APOLLO" },
103         { "os/2,i[3456]86",        "OS2" },
104         { "cygwin.*,i[3456]86",    "CYGWIN" },
105 
106         // default Windows mappings
107         { "windows.*,x86",         "WIN32" },
108         { "windows.*,pentium",     "WIN32" },
109     };
110 
111     private static String getPvmArchFromStdMappings(String os, String ht, String ver) {
112         String key;
113         if (ver != null) {
114             key = os + "," + ht + "," + ver;
115             for (int i=0; i<stdOsArchVerMappings.length; i++) {
116                 String pattern = stdOsArchVerMappings[i][0];
117                 if (key.matches(pattern)) {
118                     return stdOsArchVerMappings[i][1];
119                 }
120             }
121         }
122         key = os + "," + ht;
123         for (int i=0; i<stdOsArchMappings.length; i++) {
124             String pattern = stdOsArchMappings[i][0];
125             if (key.matches(pattern)) {
126                 return stdOsArchMappings[i][1];
127             }
128         }
129         return "UNKNOWN";
130     }
131 
132     private static final String[] unameExecs = {
133         "/bin/uname", "/usr/bin/uname", "/bin/uname.exe", "/usr/bin/uname.exe"
134     };
135 
136     private synchronized static String getUnameCmd() {
137         for (int i=0; i<unameExecs.length; i++) {
138             if (isF(unameExecs[i])) {
139                 return unameExecs[i];
140             }
141         }
142         return null;
143     }
144 
145     private static boolean isF(String path) {
146         return new File(path).isFile();
147     }
148 
149     private static boolean isD(String path) {
150         return new File(path).isDirectory();
151     }
152 
153     /**
154      * Returns the identifier representing current system architecture,
155      * as defined by the
156      * <a href="http://www.csm.ornl.gov/pvm/pvmArch.html">PVM project</a>.
157      *
158      * @return PVM architecture
159      */
160     public static String getArch() {
161 
162         // make sure the client is authorized to access this information
163 
164         SecurityManager security = System.getSecurityManager();
165         if (security != null) {
166             security.checkPropertyAccess("os.name");
167             security.checkPropertyAccess("os.arch");
168             security.checkPropertyAccess("os.version");
169         }
170 
171         synchronized (PvmArch.class) {
172             if (PvmArch.arch != null) return PvmArch.arch;
173         }
174         String pvmArch = (String)AccessController.doPrivileged(new PrivilegedAction() {
175             public Object run() {
176                 try {
177                     return getArchPrivileged();
178                 }
179                 catch (Exception e) {
180                     return "UNKNOWN";
181                 }
182             }
183         });
184         synchronized (PvmArch.class) {
185             PvmArch.arch = pvmArch;
186         }
187         return pvmArch;
188     }
189 
190     private static String getArchPrivileged() throws IOException, InterruptedException {
191 
192         // try guessing from standard system properties
193         String osName = System.getProperty("os.name").toLowerCase();
194         String osArch = System.getProperty("os.arch").toLowerCase();
195         String osVer  = System.getProperty("os.version").toLowerCase();
196 
197         String pvmArch = getPvmArchFromStdMappings(osName, osArch, osVer);
198 
199         String os = null;
200         String ht = null;
201         String ov = null;
202 
203 
204         if ("UNKNOWN".equals(pvmArch)) {
205             String unameCmd = getUnameCmd();
206             if (unameCmd != null) {
207                 os = ExecUtils.execCommand(unameCmd + " -s").getOut().trim().toLowerCase();
208                 ht = ExecUtils.execCommand(unameCmd + " -m").getOut().trim().toLowerCase();
209                 ov = ExecUtils.execCommand(unameCmd + " -v").getOut().trim().toLowerCase();
210 
211                 pvmArch = getPvmArchFromStdMappings(os, ht, ov);
212             }
213         }
214 
215         if ("DGAV".equals(pvmArch)) {
216             // correction (from pvmgetarch) for DG/Intel
217             String unamea = ExecUtils.execCommand(getUnameCmd() + " -a").getOut();
218             if (unamea.matches("PentiumPro$")) {
219                 pvmArch = "DGIX";
220             }
221         }
222 
223         if ("UNKNOWN".equals(pvmArch)) {
224             if (isF("/bin/arch")) {
225                 String binarch = ExecUtils.execCommand("/bin/arch").getOut().trim();
226                 if      ("ksr1".equals(binarch)) pvmArch = "KSR1";
227                 else if ("sun2".equals(binarch)) pvmArch = "SUN2";
228                 else if ("sun3".equals(binarch)) pvmArch = "SUN3";
229                 else if ("sun4".equals(binarch)) pvmArch = "SUN4";
230             }
231         }
232 
233         if ("UNKNOWN".equals(pvmArch)) {
234             if (isF("/usr/etc/RELDEF")) { pvmArch = "ATT"; }
235             else if (isF("/ultrixboot")) {
236                 if (isF("/pcs750.bin")) {
237                     pvmArch = "UVAX";
238                 }
239                 else {
240                     pvmArch = "PMAX";
241                 }
242             }
243             else if (isF("/pcs750.bin"))       { pvmArch = "VAX"; }
244             else if (isD("/usr/bin/alliant"))  { pvmArch = "AFX8"; }
245             else if (isF("/usr/bin/cluster"))  { pvmArch = "BFLY"; }
246             else if (isD("/usr/convex"))       { pvmArch = "CNVX"; }
247             else if (isF("/unicos"))           { pvmArch = "CRAY"; }
248             else if (isF("/hp-ux"))            { pvmArch = "HP300"; }
249             else if (isF("/usr/bin/getcube"))  { pvmArch = "I860"; }
250             else if (isF("/usr/bin/asm56000")) { pvmArch = "NEXT"; }
251             else if (isF("/etc/vg"))           { pvmArch = "RS6K"; }
252             else if (isD("/usr/include/caif")) { pvmArch = "RT"; }
253             else if (isF("/bin/4d"))           { pvmArch = "SGI"; }
254             else if (isF("/dynix"))            { pvmArch = "SYMM"; }
255             else if (isF("/bin/titan"))        { pvmArch = "TITN"; }
256             else if (isF("/netbsd")) {
257                 String unamep = ExecUtils.execCommand("/usr/bin/uname -p").getOut().trim();
258                 if      ("alpha"  .equals(unamep)) { pvmArch = "NETBSDALPHA"; }
259                 else if ("arm32"  .equals(unamep)) { pvmArch = "NETBSDARM32"; }
260                 else if ("i386"   .equals(unamep)) { pvmArch = "NETBSDI386"; }
261                 else if ("m68k"   .equals(unamep)) { pvmArch = "NETBSDM68K"; }
262                 else if ("mipseb" .equals(unamep)) { pvmArch = "NETBSDMIPSEB"; }
263                 else if ("mipsel" .equals(unamep)) { pvmArch = "NETBSDMIPSEL"; }
264                 else if ("ns32k"  .equals(unamep)) { pvmArch = "NETBSDNS32K"; }
265                 else if ("powerpc".equals(unamep)) { pvmArch = "NETBSDPOWERPC"; }
266                 else if ("sh3"    .equals(unamep)) { pvmArch = "NETBSDSH3"; }
267                 else if ("sparc"  .equals(unamep)) { pvmArch = "NETBSDSPARC"; }
268                 else if ("sparc64".equals(unamep)) { pvmArch = "NETBSDSPARC64"; }
269                 else if ("vax"    .equals(unamep)) { pvmArch = "NETBSDVAX"; }
270             }
271             else if (isF("/usr/bin/machine")) {
272                 String machine = ExecUtils.execCommand("/usr/bin/machine").getOut().trim();
273                 if ("i386".equals(machine))   { pvmArch = "BSD386"; }
274             }
275             else if (isF("/usr/bin/uxpm")) {
276                 if (ExecUtils.execCommand("/usr/bin/uxpm").getExitValue() == 0) {
277                     pvmArch = "UXPM";
278                 }
279             }
280             else if (isF("/usr/bin/uxpv")) {
281                 if (ExecUtils.execCommand("/usr/bin/uxpv").getExitValue() == 0) {
282                     pvmArch = "UXPV";
283                 }
284             }
285         }
286 
287         if ("UNKNOWN".equals(pvmArch)) {
288             String unameCmd = getUnameCmd();
289             if (unameCmd != null) {
290                 String rv = ExecUtils.execCommand(unameCmd + " -r").getOut().trim();
291                 if ((os + "," + ht).matches(".*,i[3456]86")) {
292                     if (rv.matches("4\\..*")) {
293                         pvmArch = "UWARE";
294                     }
295                     else {
296                         pvmArch = "SCO";
297                     }
298                 }
299             }
300         }
301 
302         // update the machine type to derive subclasses
303         int nproc = Runtime.getRuntime().availableProcessors();
304         boolean pvmShmem =
305             Boolean.valueOf(System.getProperty("pvm.shmem", "false")).booleanValue();
306 
307         if ("SUN4".equals(pvmArch)) {
308             String rel = ExecUtils.execCommand(getUnameCmd() + " -r").getOut().trim();
309             if (rel.matches("5\\..*")) pvmArch = "SUN4SOL2";
310         }
311 
312         if ("SGI".equals(pvmArch)) {
313             String rel = ExecUtils.execCommand("/bin/uname -r").getOut().trim();
314             if      (rel.matches("5\\..*")) { pvmArch = "SGI5"; }
315             else if (rel.matches("6\\..*")) { pvmArch = "SGI6"; }
316         }
317 
318         if      ("SUN4".equals(pvmArch) && isF("/dev/cm"))     { pvmArch = "CM2"; }
319         else if ("SUN4".equals(pvmArch) && isF("/dev/cmni"))   { pvmArch = "CM5"; }
320         else if ("CNVX".equals(pvmArch)) {
321             if (ExecUtils.execCommand("/usr/convex/getsysinfo -f native_default").
322                     getExitValue() == 0) {
323                 pvmArch = "CNVXN";
324             }
325         }
326         else if ("PMAX".equals(pvmArch) && isD("/usr/maspar")) { pvmArch = "MASPAR"; }
327         else if ("RS6K".equals(pvmArch) && (os + "," + ov).matches("AIX.*,4")) {
328             pvmArch = "AIX46K";
329         }
330         else if ("HPPA".equals(pvmArch) && isF("/bin/sysinfo")) { pvmArch = "CSPP"; }
331 
332         if (nproc >= 2 && pvmShmem) {
333             if ("SUN4SOL2".equals(pvmArch))     { pvmArch = "SUNMP"; }
334             else if ("ALPHA".equals(pvmArch))   { pvmArch = "ALPHAMP"; }
335             else if ("SGI64".equals(pvmArch))   { pvmArch = "SGIMP64"; }
336             else if ("SGI5".equals(pvmArch))    { pvmArch = "SGIMP5"; }
337             else if ("SGI6".equals(pvmArch))    { pvmArch = "SGIMP6"; }
338             else if ("AIX46K".equals(pvmArch))  { pvmArch = "AIX4MP"; }
339             else if ("HPPA".equals(pvmArch))    { pvmArch = "HPPAMP"; }
340         }
341 
342         return pvmArch;
343     }
344 }