Home | History | Annotate | Download | only in srm
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright (c) 2001 by Sun Microsystems, Inc.
     23  * All rights reserved.
     24  *
     25  * rmon.java
     26  */
     27 
     28 
     29 package com.sun.wbem.solarisprovider.srm;
     30 
     31 import java.util.Iterator;
     32 
     33 
     34 /**
     35  * Simple CLI to test ResourceMonitor class. Prints the current user, projects
     36  * and processor sets metrics on the stdout.
     37  * @author Sun Microsystems, Inc.
     38  */
     39 public class rmon {
     40 
     41     static String usage =
     42 "rmon [-p <pid> -u [<usr>] | -j [<prj>] | -s [<set>] -l [<cnt>] -i [<ms>] -d]";
     43 
     44     public  static void main(String []args) {
     45 
     46 	int pID = -1, uID = -1, jID = -1, sID = -1;
     47 	boolean pFlag = false, uFlag = false, jFlag = false, sFlag = false,
     48 	lFlag = false, selected = false, debF = false;
     49 	int loopCnt = 1, interval = 1000;
     50 	int argc;
     51 
     52 	ProcessDataModel pui;
     53 	ActiveUserModel aum;
     54 	ActiveProjectModel apm;
     55 	ProcessAggregateDataModel padm;
     56 	ResourceMonitor resourceMonitor = null;
     57 	Object	sync = new Object();
     58 
     59 	try {
     60 	    if ((argc = args.length) > 0) {
     61 		for (int argix = 0; argix < argc; argix++)  {
     62 		    if (args[argix].startsWith("-u")) {
     63 			uFlag = true; selected = true;
     64 			if (((argix + 1) < argc) &&
     65 				(!args[argix + 1].startsWith("-"))) {
     66 			    uID = Integer.parseInt(args[++argix]);
     67 			}
     68 		    } else if (args[argix].startsWith("-p")) {
     69 			pFlag = true; selected = true;
     70 			if (((argix + 1) < argc) &&
     71 				(!args[argix + 1].startsWith("-"))) {
     72 			    pID = Integer.parseInt(args[++argix]);
     73 			}
     74 		    } else if (args[argix].startsWith("-j")) {
     75 			jFlag = true; selected = true;
     76 			if (((argix + 1) < argc) &&
     77 				(!args[argix + 1].startsWith("-"))) {
     78 			    jID = Integer.parseInt(args[++argix]);
     79 			}
     80 		    } else if (args[argix].startsWith("-s")) {
     81 			sFlag = true; selected = true;
     82 			if (((argix + 1) < argc) &&
     83 				(!args[argix + 1].startsWith("-"))) {
     84 			    sID = Integer.parseInt(args[++argix]);
     85 			}
     86 		    } else if (args[argix].startsWith("-l")) {
     87 			lFlag = true;
     88 			if (((argix + 1) < argc) &&
     89 				(!args[argix + 1].startsWith("-"))) {
     90 			    loopCnt = Integer.parseInt(args[++argix]);
     91 			} else {
     92 			    loopCnt = 60;
     93 			}
     94 		    } else if (args[argix].startsWith("-i")) {
     95 			lFlag = true;
     96 			if (((argix + 1) < argc) &&
     97 				(!args[argix + 1].startsWith("-"))) {
     98 			    interval = Integer.parseInt(args[++argix]);
     99 			}
    100 		    } else if (args[argix].startsWith("-d")) {
    101 			debF = true;
    102 		    } else {
    103 			System.err.println(usage);
    104 			return;
    105 		    }
    106 		}
    107 	    }
    108 
    109 	    resourceMonitor = ResourceMonitor.getHandle();
    110 	    resourceMonitor.openDataModel(10000, 1000, 5000);
    111 	    int ret;
    112 	    Iterator iterator;
    113 	    DataModel dm = null;
    114 
    115 	    for (int l = 0; l < loopCnt; l++) {
    116 		System.out.println("\n------- Loop cnt = "+l+" -------------");
    117 		if (!selected || pFlag) {
    118 		    System.out.println("\n--------- PROCESSES ----------");
    119 		    dm = resourceMonitor.getDataModel(false);
    120 		    iterator = dm.getProcessIterator();
    121 		    while (iterator.hasNext()) {
    122 			pui = (ProcessDataModel) iterator.next();
    123 			if ((pID == -1) || ((ProcessDataModel)pui).pid == pID)
    124 			    System.out.println(pui);
    125 		    }
    126 		    resourceMonitor.releaseDataModel(dm);
    127 		}
    128 		if (!selected || uFlag) {
    129 		    System.out.println("\n--------- USERS ----------");
    130 		    dm = resourceMonitor.getDataModel(false);
    131 		    iterator = dm.getProcessIterator();
    132 		    while (iterator.hasNext()) {
    133 			aum = (ActiveUserModel) iterator.next();
    134 			System.out.println(aum);
    135 		    }
    136 		    resourceMonitor.releaseDataModel(dm);
    137 		}
    138 		if (!selected || uFlag) {
    139 		    System.out.println("\n--------- USERS PROCS ----------");
    140 		    dm = resourceMonitor.getDataModel(false);
    141 		    iterator = dm.getProcessIterator();
    142 		    while (iterator.hasNext()) {
    143 			padm = (ProcessAggregateDataModel) iterator.next();
    144 			System.out.println(padm);
    145 		    }
    146 		    resourceMonitor.releaseDataModel(dm);
    147 		}
    148 		if (!selected || jFlag) {
    149 		    System.out.println("\n--------- PROJECT ----------");
    150 		    dm = resourceMonitor.getDataModel(false);
    151 		    iterator = dm.getProcessIterator();
    152 		    while (iterator.hasNext()) {
    153 			apm = (ActiveProjectModel) iterator.next();
    154 			System.out.println(apm);
    155 		    }
    156 		    resourceMonitor.releaseDataModel(dm);
    157 		}
    158 		if (!selected || jFlag) {
    159 		    System.out.println("\n--------- USERS PROCS ----------");
    160 		    dm = resourceMonitor.getDataModel(false);
    161 		    iterator = dm.getProcessIterator();
    162 		    while (iterator.hasNext()) {
    163 			padm = (ProcessAggregateDataModel) iterator.next();
    164 			System.out.println(padm);
    165 		    }
    166 		    resourceMonitor.releaseDataModel(dm);
    167 		}
    168 		napms(6000);
    169 	    }
    170 
    171 	} catch (Exception e) {
    172 	    System.err.println(e);
    173 	    System.err.println(usage);
    174 	    resourceMonitor.closeDataModel();
    175 	    return;
    176 	}
    177 	resourceMonitor.closeDataModel();
    178 
    179     } // end main
    180 
    181     /**
    182     * Waits some milliseconds.
    183     *
    184     * @param ms time to wait in milliseconds
    185     */
    186     public static void napms(int ms) {
    187 	try {
    188 	    Thread.sleep(ms);
    189 	} catch (InterruptedException e) {}
    190     }
    191 
    192 } // end class rmon
    193