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

background.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/background.cpp 11    5/09/01 5:29a Admin $ 
00031  */
00035 #include "xdprof.h"
00036 
00037 
00038 
00040 int RefreshTime = 2000;
00041 
00043 static string SelfInfo;
00044 
00046 volatile JVMPI_RawMonitor BackgroundMonitor;
00047 
00049 static string HostName;
00050 
00052 static int PortNumber;
00053 
00055 volatile bool ShutdownRequested;
00056 
00057 static void DisableEvents()
00058 {
00059 CHECKEDCALL(DisableEvent(JVMPI_EVENT_JVM_INIT_DONE               , NULL));
00060 CHECKEDCALL(DisableEvent(JVMPI_EVENT_JVM_SHUT_DOWN              , NULL));
00061 CHECKEDCALL(DisableEvent(JVMPI_EVENT_THREAD_START, NULL));
00062 CHECKEDCALL(DisableEvent(JVMPI_EVENT_THREAD_END, NULL));
00063 CHECKEDCALL(DisableEvent(JVMPI_EVENT_CLASS_LOAD                  , NULL));
00064 CHECKEDCALL(DisableEvent(JVMPI_EVENT_CLASS_UNLOAD                , NULL));
00065 CHECKEDCALL(DisableEvent(JVMPI_EVENT_OBJECT_ALLOC                  , NULL));
00066 
00067 }
00068 
00076 void InitializeBackgroundThread(string host, int port, string selfinfo, int refreshTime)
00077 {
00078     HostName = host;
00079     PortNumber = port;
00080     SelfInfo = selfinfo;
00081     RefreshTime = refreshTime;
00082 }
00083 
00088 extern "C" void BackgroundThread(void *)
00089 {
00090     // must be created by this function
00091     BackgroundMonitor = CALL(RawMonitorCreate)("_background");
00092     CALL(RawMonitorEnter)(BackgroundMonitor);
00093 
00094     // now set up the communications
00095     bool ok = InitializeCommunications(HostName, PortNumber);
00096     if (!ok || ! IsConnected())
00097     {
00098         debug("couldn't connect, disabling events");
00099         // TODO: shut it down
00100         ShutdownCommunications();
00101         DisableEvents();
00102         return;
00103     }
00104     
00105     // Once and only once, we send the SelfInfo
00106     //Send(SelfInfo + "\n");
00107     ShutdownRequested = false;
00108 
00109     ThreadID me;
00110 
00111     (*GlobalJVM).GetEnv((void **)&me, JNI_VERSION_1_2);
00112     jclass c = me->FindClass("java/lang/System");
00113     jmethodID m = me->GetStaticMethodID(c, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;"); 
00114     vector<string> properties;
00115     properties.push_back("os.arch");
00116     properties.push_back("os.name");
00117     properties.push_back("os.version");
00118     properties.push_back("java.vm.name");
00119     properties.push_back("java.vm.info");
00120     properties.push_back("java.vm.version");
00121     properties.push_back("java.vm.vendor");
00122     properties.push_back("java.runtime.name");
00123     properties.push_back("java.runtime.version");
00124 
00125     ostringstream oss;
00126     for (vector<string>::size_type  i = 0; i < properties.size(); i++)
00127     {
00128         jstring str = (jstring)me->CallStaticObjectMethod(c, m, me->NewStringUTF(properties[i].c_str()));
00129         //cout << properties[i] << "\t" << me->GetStringUTFChars(str, 0) << endl;
00130         debug(i, properties[i], me->GetStringUTFChars(str, 0));
00131         oss << properties[i] << "=" << me->GetStringUTFChars(str, 0) << '!';
00132     }
00133     oss << ends;
00134     string ost(oss.str());
00135     SelfInfo += " !";
00136     SelfInfo += ost.substr(0, ost.size() - 2) ;
00137     SelfInfo += '\n';
00138     debug("selfinfo", SelfInfo);
00139     debug("background thread id", me);
00140 
00141     while(! ShutdownRequested)
00142     {
00143         CALL(RawMonitorWait)(BackgroundMonitor, (jlong)RefreshTime);
00144         if (ShutdownRequested)
00145             break;
00146         if (IsConnected())
00147             ThreadDump(SelfInfo, me);
00148         else
00149         {
00150             debug("lost connection, shutting down.");
00151             DisableEvents();
00152             ShutdownCommunications();
00153             return;
00154         }
00155     }
00156     //  debug("out of shutdown loop.");
00157     CALL(RawMonitorDestroy)(BackgroundMonitor);
00158 }
00159 
00163 void CreateBackgroundThread()
00164 {
00165     CALL(CreateSystemThread)("DPROF Background Thread", JVMPI_NORMAL_PRIORITY, BackgroundThread);
00166     debug("bg thread created");
00167 }

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