jitprofiling.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* <copyright>
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright (c) 2005-2014 Intel Corporation. All rights reserved.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. The full GNU General Public License is included in this distribution
  17. in the file called LICENSE.GPL.
  18. Contact Information:
  19. http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/
  20. BSD LICENSE
  21. Copyright (c) 2005-2014 Intel Corporation. All rights reserved.
  22. All rights reserved.
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions
  25. are met:
  26. * Redistributions of source code must retain the above copyright
  27. notice, this list of conditions and the following disclaimer.
  28. * Redistributions in binary form must reproduce the above copyright
  29. notice, this list of conditions and the following disclaimer in
  30. the documentation and/or other materials provided with the
  31. distribution.
  32. * Neither the name of Intel Corporation nor the names of its
  33. contributors may be used to endorse or promote products derived
  34. from this software without specific prior written permission.
  35. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. </copyright> */
  47. #include "ittnotify_config.h"
  48. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  49. #include <windows.h>
  50. #pragma optimize("", off)
  51. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  52. #include <stdlib.h>
  53. #include "jitprofiling.h"
  54. static const char rcsid[] = "\n@(#) $Revision: 463960 $\n";
  55. #define DLL_ENVIRONMENT_VAR "VS_PROFILER"
  56. #ifndef NEW_DLL_ENVIRONMENT_VAR
  57. #if ITT_ARCH==ITT_ARCH_IA32
  58. #define NEW_DLL_ENVIRONMENT_VAR "INTEL_JIT_PROFILER32"
  59. #else
  60. #define NEW_DLL_ENVIRONMENT_VAR "INTEL_JIT_PROFILER64"
  61. #endif
  62. #endif /* NEW_DLL_ENVIRONMENT_VAR */
  63. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  64. #define DEFAULT_DLLNAME "JitPI.dll"
  65. HINSTANCE m_libHandle = NULL;
  66. #elif ITT_PLATFORM==ITT_PLATFORM_MAC
  67. #define DEFAULT_DLLNAME "libJitPI.dylib"
  68. void* m_libHandle = NULL;
  69. #else
  70. #define DEFAULT_DLLNAME "libJitPI.so"
  71. void* m_libHandle = NULL;
  72. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  73. /* default location of JIT profiling agent on Android */
  74. #define ANDROID_JIT_AGENT_PATH "/data/intel/libittnotify.so"
  75. /* the function pointers */
  76. typedef unsigned int(JITAPI *TPInitialize)(void);
  77. static TPInitialize FUNC_Initialize=NULL;
  78. typedef unsigned int(JITAPI *TPNotify)(unsigned int, void*);
  79. static TPNotify FUNC_NotifyEvent=NULL;
  80. static iJIT_IsProfilingActiveFlags executionMode = iJIT_NOTHING_RUNNING;
  81. /* end collector dll part. */
  82. /* loadiJIT_Funcs() : this function is called just in the beginning
  83. * and is responsible to load the functions from BistroJavaCollector.dll
  84. * result:
  85. * on success: the functions loads, iJIT_DLL_is_missing=0, return value = 1
  86. * on failure: the functions are NULL, iJIT_DLL_is_missing=1, return value = 0
  87. */
  88. static int loadiJIT_Funcs(void);
  89. /* global representing whether the collector can't be loaded */
  90. static int iJIT_DLL_is_missing = 0;
  91. ITT_EXTERN_C int JITAPI
  92. iJIT_NotifyEvent(iJIT_JVM_EVENT event_type, void *EventSpecificData)
  93. {
  94. int ReturnValue;
  95. /* initialization part - the collector has not been loaded yet. */
  96. if (!FUNC_NotifyEvent)
  97. {
  98. if (iJIT_DLL_is_missing)
  99. return 0;
  100. if (!loadiJIT_Funcs())
  101. return 0;
  102. }
  103. if (event_type == iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED ||
  104. event_type == iJVM_EVENT_TYPE_METHOD_UPDATE)
  105. {
  106. if (((piJIT_Method_Load)EventSpecificData)->method_id == 0)
  107. return 0;
  108. }
  109. else if (event_type == iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED_V2)
  110. {
  111. if (((piJIT_Method_Load_V2)EventSpecificData)->method_id == 0)
  112. return 0;
  113. }
  114. else if (event_type == iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED_V3)
  115. {
  116. if (((piJIT_Method_Load_V3)EventSpecificData)->method_id == 0)
  117. return 0;
  118. }
  119. else if (event_type == iJVM_EVENT_TYPE_METHOD_INLINE_LOAD_FINISHED)
  120. {
  121. if (((piJIT_Method_Inline_Load)EventSpecificData)->method_id == 0 ||
  122. ((piJIT_Method_Inline_Load)EventSpecificData)->parent_method_id == 0)
  123. return 0;
  124. }
  125. ReturnValue = (int)FUNC_NotifyEvent(event_type, EventSpecificData);
  126. return ReturnValue;
  127. }
  128. ITT_EXTERN_C iJIT_IsProfilingActiveFlags JITAPI iJIT_IsProfilingActive()
  129. {
  130. if (!iJIT_DLL_is_missing)
  131. {
  132. loadiJIT_Funcs();
  133. }
  134. return executionMode;
  135. }
  136. /* This function loads the collector dll and the relevant functions.
  137. * on success: all functions load, iJIT_DLL_is_missing = 0, return value = 1
  138. * on failure: all functions are NULL, iJIT_DLL_is_missing = 1, return value = 0
  139. */
  140. static int loadiJIT_Funcs()
  141. {
  142. static int bDllWasLoaded = 0;
  143. char *dllName = (char*)rcsid; /* !! Just to avoid unused code elimination */
  144. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  145. DWORD dNameLength = 0;
  146. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  147. if(bDllWasLoaded)
  148. {
  149. /* dll was already loaded, no need to do it for the second time */
  150. return 1;
  151. }
  152. /* Assumes that the DLL will not be found */
  153. iJIT_DLL_is_missing = 1;
  154. FUNC_NotifyEvent = NULL;
  155. if (m_libHandle)
  156. {
  157. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  158. FreeLibrary(m_libHandle);
  159. #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  160. dlclose(m_libHandle);
  161. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  162. m_libHandle = NULL;
  163. }
  164. /* Try to get the dll name from the environment */
  165. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  166. dNameLength = GetEnvironmentVariableA(NEW_DLL_ENVIRONMENT_VAR, NULL, 0);
  167. if (dNameLength)
  168. {
  169. DWORD envret = 0;
  170. dllName = (char*)malloc(sizeof(char) * (dNameLength + 1));
  171. if(dllName != NULL)
  172. {
  173. envret = GetEnvironmentVariableA(NEW_DLL_ENVIRONMENT_VAR,
  174. dllName, dNameLength);
  175. if (envret)
  176. {
  177. /* Try to load the dll from the PATH... */
  178. m_libHandle = LoadLibraryExA(dllName,
  179. NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  180. }
  181. free(dllName);
  182. }
  183. } else {
  184. /* Try to use old VS_PROFILER variable */
  185. dNameLength = GetEnvironmentVariableA(DLL_ENVIRONMENT_VAR, NULL, 0);
  186. if (dNameLength)
  187. {
  188. DWORD envret = 0;
  189. dllName = (char*)malloc(sizeof(char) * (dNameLength + 1));
  190. if(dllName != NULL)
  191. {
  192. envret = GetEnvironmentVariableA(DLL_ENVIRONMENT_VAR,
  193. dllName, dNameLength);
  194. if (envret)
  195. {
  196. /* Try to load the dll from the PATH... */
  197. m_libHandle = LoadLibraryA(dllName);
  198. }
  199. free(dllName);
  200. }
  201. }
  202. }
  203. #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  204. dllName = getenv(NEW_DLL_ENVIRONMENT_VAR);
  205. if (!dllName)
  206. dllName = getenv(DLL_ENVIRONMENT_VAR);
  207. #if defined(__ANDROID__) || defined(ANDROID)
  208. if (!dllName)
  209. dllName = ANDROID_JIT_AGENT_PATH;
  210. #endif
  211. if (dllName)
  212. {
  213. /* Try to load the dll from the PATH... */
  214. m_libHandle = dlopen(dllName, RTLD_LAZY);
  215. }
  216. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  217. if (!m_libHandle)
  218. {
  219. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  220. m_libHandle = LoadLibraryA(DEFAULT_DLLNAME);
  221. #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  222. m_libHandle = dlopen(DEFAULT_DLLNAME, RTLD_LAZY);
  223. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  224. }
  225. /* if the dll wasn't loaded - exit. */
  226. if (!m_libHandle)
  227. {
  228. iJIT_DLL_is_missing = 1; /* don't try to initialize
  229. * JIT agent the second time
  230. */
  231. return 0;
  232. }
  233. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  234. FUNC_NotifyEvent = (TPNotify)GetProcAddress(m_libHandle, "NotifyEvent");
  235. #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  236. FUNC_NotifyEvent = (TPNotify)dlsym(m_libHandle, "NotifyEvent");
  237. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  238. if (!FUNC_NotifyEvent)
  239. {
  240. FUNC_Initialize = NULL;
  241. return 0;
  242. }
  243. #if ITT_PLATFORM==ITT_PLATFORM_WIN
  244. FUNC_Initialize = (TPInitialize)GetProcAddress(m_libHandle, "Initialize");
  245. #else /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  246. FUNC_Initialize = (TPInitialize)dlsym(m_libHandle, "Initialize");
  247. #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
  248. if (!FUNC_Initialize)
  249. {
  250. FUNC_NotifyEvent = NULL;
  251. return 0;
  252. }
  253. executionMode = (iJIT_IsProfilingActiveFlags)FUNC_Initialize();
  254. bDllWasLoaded = 1;
  255. iJIT_DLL_is_missing = 0; /* DLL is ok. */
  256. return 1;
  257. }
  258. ITT_EXTERN_C unsigned int JITAPI iJIT_GetNewMethodID()
  259. {
  260. static unsigned int methodID = 1;
  261. if (methodID == 0)
  262. return 0; /* ERROR : this is not a valid value */
  263. return methodID++;
  264. }