00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00091 BackgroundMonitor = CALL(RawMonitorCreate)("_background");
00092 CALL(RawMonitorEnter)(BackgroundMonitor);
00093
00094
00095 bool ok = InitializeCommunications(HostName, PortNumber);
00096 if (!ok || ! IsConnected())
00097 {
00098 debug("couldn't connect, disabling events");
00099
00100 ShutdownCommunications();
00101 DisableEvents();
00102 return;
00103 }
00104
00105
00106
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
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
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 }