Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

setup.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2001, John Lambert jlambert@jlambert.com
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are 
00006 permitted provided that the following conditions are met:
00007 
00008 Redistributions of source code must retain the above copyright notice, this list of 
00009 conditions and the following disclaimer.
00010 
00011 Redistributions in binary form must reproduce the above copyright notice, this list
00012 of conditions and the following disclaimer in the documentation and/or other
00013 materials provided with the distribution. 
00014 
00015 Neither the name of the xdProf project nor the names of its contributors may be used
00016 to endorse or promote products derived from this software without specific prior 
00017 written permission. 
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00020 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00022 SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00023 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
00025 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
00027 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 /*
00030 $Header: /xdprof-dll/setup.cpp 7     5/07/01 4:54a Admin $ 
00031  */
00032 #include "xdprof.h"
00033 #include <jni.h>
00034 // these must be extern-ed in dprof.h
00035 JVMPI_Interface *jvmpi_interface;
00036 JavaVM* GlobalJVM;
00037 
00038 inline static string GetSelfInfo()
00039 {
00040     ostringstream oss;  
00041     /*
00042     char compname[MAX_COMPUTERNAME_LENGTH + 1] = {0};
00043     DWORD size = MAX_COMPUTERNAME_LENGTH;
00044     GetComputerName(compname, &size);
00045     */
00046     //oss << "pid = " << 
00047     oss << _getpid();
00048     oss << ends;
00049     string s = oss.str();
00050     // trim null
00051     return s.substr(0, s.length() - 1);
00052 }
00053 
00054 inline void Leave(string s)
00055 {
00056     cout << flush << s << endl;
00057     CALL(ProfilerExit)((jlong)0);
00058 }
00059 
00061 static string GetUsage()
00062 {
00063     string usage = "\nXDPROF Usage:\n\n-Xrunxdprof:server=HOSTNAME,port=PORT,refresh=REFRESH[,description=OPTIONAL_DESCRIPTION,hotspot=true]";
00064     usage = usage + "\n" + "where ";
00065     usage = usage + "\n" + "\t" + "server=HOSTNAME is the machine running the DPROF server";
00066     usage = usage + "\n" + "\t" + "port=PORT is the port number of the DPROF server";
00067     usage = usage + "\n" + "\t" + "refresh=REFRESH is the time to refresh in milliseconds (1000 = 1 sec)";
00068     usage = usage + "\n" + "" + "Optional arguments: ";
00069     usage = usage + "\n" + "\t" + "description=OPTIONAL_DESCRIPTION is a string (without spaces) describing this VM";
00070     usage = usage + "\n" + "\t" + "hotspot=true is set when the HotSpot (non-classic) VM is used (this is not guaranteed to work)";
00071     usage = usage + "\n" + "\nOther runtime flags that should be passed to the VM:";
00072     usage = usage + "\n" + "\t" + "-classic -Xdebug -Xnoagent ";
00073     usage = usage + "\n";
00074     usage = usage + "\nThe final command line will then look like: \n\n" + 
00075             "java -classic -Xdebug -Xnoagent -Xrunxdprof:server=localhost,port=1337,refresh=199,description=MyDescription c\n\n";
00076     return usage;
00077 }
00078 
00079 
00083 static string getToken(const string &  entire, const string & token )
00084 {
00085     if (entire.size() == 0 || token.size() == 0)
00086         return "";
00087     const string foo = "," + entire + ",";
00088     string::size_type i = foo.find("," + token + "=");
00089     if (i == string::npos)
00090         return "";
00091     
00092     int j = i + token.size() + 2;
00093     int k = foo.find(",", j);
00094     return foo.substr(j, k - j);
00095 }
00096 
00097 
00098 // profiler agent entry point
00099 extern "C"
00100 {
00101 #pragma warning(disable:4100)
00102     JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *jvm, char *options, void *reserved)
00103     {
00104         if ((jvm->GetEnv((void **)&jvmpi_interface, JVMPI_VERSION_1)) < 0)
00105         {
00106             fprintf(stderr, "error in obtaining jvmpi interface pointer");
00107             fflush(stderr);
00108             return JNI_ERR;
00109         } 
00110 
00111         GlobalJVM = jvm;
00112         
00113         // initialize jvmpi interface
00114         jvmpi_interface->NotifyEvent = NotifyEvent;
00115 
00116         // initialize monitors
00117         InitializeMonitors();
00118         // enable notifications
00119         CHECKEDCALL(EnableEvent(JVMPI_EVENT_JVM_INIT_DONE               , NULL));
00120         CHECKEDCALL(EnableEvent(JVMPI_EVENT_JVM_SHUT_DOWN               , NULL));
00121 
00122         CHECKEDCALL(EnableEvent(JVMPI_EVENT_THREAD_START, NULL));
00123         CHECKEDCALL(EnableEvent(JVMPI_EVENT_THREAD_END, NULL));
00124         
00125         CHECKEDCALL(EnableEvent(JVMPI_EVENT_CLASS_LOAD                  , NULL));
00126         CHECKEDCALL(EnableEvent(JVMPI_EVENT_CLASS_UNLOAD                , NULL));
00127 
00128         // This is enabled only temporarily to catch the VM initialization thread
00129         CHECKEDCALL(EnableEvent(JVMPI_EVENT_OBJECT_ALLOC                  , NULL));
00130 
00131         /*
00132         CHECKEDCALL(EnableEvent(JVMPI_EVENT_MONITOR_CONTENDED_ENTER     , NULL));
00133         CHECKEDCALL(EnableEvent(JVMPI_EVENT_MONITOR_CONTENDED_ENTERED   , NULL));
00134         CHECKEDCALL(EnableEvent(JVMPI_EVENT_MONITOR_CONTENDED_EXIT      , NULL));
00135         CHECKEDCALL(EnableEvent(JVMPI_EVENT_MONITOR_WAIT                , NULL));
00136         CHECKEDCALL(EnableEvent(JVMPI_EVENT_MONITOR_WAITED              , NULL));
00137 
00138         CHECKEDCALL(EnableEvent(JVMPI_EVENT_RAW_MONITOR_CONTENDED_ENTER , NULL));
00139         CHECKEDCALL(EnableEvent(JVMPI_EVENT_RAW_MONITOR_CONTENDED_ENTERED, NULL));
00140         CHECKEDCALL(EnableEvent(JVMPI_EVENT_RAW_MONITOR_CONTENDED_EXIT  , NULL));
00141         CHECKEDCALL(EnableEvent(JVMPI_EVENT_DATA_DUMP_REQUEST           , NULL));
00142         */
00143 
00144 
00145         
00146         string selfinfo = GetSelfInfo();
00147         string hostname = "localhost";
00148         long port = 1337;
00149         long refresh = 2000;
00150         HotSpot = false; // assume we have no HotSpot
00151         
00152         if (options != NULL)
00153         {
00154             string s(options);
00155             if (s == "help")
00156             {
00157                 Leave(GetUsage());
00158             }
00159             hostname = getToken(s, "server");
00160             if (hostname == "")
00161                 Leave("Invalid hostname.");
00162 
00163             port = atoi(getToken(s, "port").c_str());
00164             if (port <= 0 || port > 65535)
00165                 Leave("Bad port number " + getToken(s, "port"));
00166 
00167             refresh = atoi(getToken(s, "refresh").c_str());
00168             if (refresh <= 0)
00169                 Leave("Bad refresh number " + getToken(s, "refresh"));
00170 
00171             // add it on
00172             if (getToken(s, "description") != "")
00173             {
00174                 selfinfo += ' ';
00175                 selfinfo += getToken(s, "description");
00176             }
00177             else
00178                 selfinfo += " none";
00179             
00180             if (getToken(s, "hotspot") == "true")
00181             {
00182                 HotSpot = true;
00183             }
00184 
00185             debug("All options", s);
00186         }
00187         else
00188         {
00189             debug("Using defaults...");
00190         }
00191 
00192         debug("Hostname: ", hostname);
00193         debug("Port: ", port);
00194         debug("Refresh: ", refresh);
00195         debug("SelfInfo: ", selfinfo);
00196         debug("HotSpot: ", HotSpot ? "true" : "false");
00197 
00198         InitializeBackgroundThread(hostname, port, selfinfo, refresh);
00199 
00200         return JNI_OK;
00201     }
00202 #pragma warning(default:4100)
00203 }

Generated at Sun Jun 24 20:57:15 2001 for xdprof by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001