js/src/vtune/jitprofiling.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     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.
     5   GPL LICENSE SUMMARY
     7   Copyright (c) 2005-2012 Intel Corporation. All rights reserved.
     9   This program is free software; you can redistribute it and/or modify
    10   it under the terms of version 2 of the GNU General Public License as
    11   published by the Free Software Foundation.
    13   This program is distributed in the hope that it will be useful, but
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    16   General Public License for more details.
    18   You should have received a copy of the GNU General Public License
    19   along with this program; if not, write to the Free Software
    20   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
    21   The full GNU General Public License is included in this distribution
    22   in the file called LICENSE.GPL.
    24   Contact Information:
    25   http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/
    27   BSD LICENSE
    29   Copyright (c) 2005-2012 Intel Corporation. All rights reserved.
    30   All rights reserved.
    32   Redistribution and use in source and binary forms, with or without
    33   modification, are permitted provided that the following conditions
    34   are met:
    36     * Redistributions of source code must retain the above copyright
    37       notice, this list of conditions and the following disclaimer.
    38     * Redistributions in binary form must reproduce the above copyright
    39       notice, this list of conditions and the following disclaimer in
    40       the documentation and/or other materials provided with the
    41       distribution.
    42     * Neither the name of Intel Corporation nor the names of its
    43       contributors may be used to endorse or promote products derived
    44       from this software without specific prior written permission.
    46   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    47   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    48   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    49   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    50   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    51   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    52   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    53   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    54   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    55   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    56   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    57 */
    58 #include "vtune/ittnotify_config.h"
    60 #if ITT_PLATFORM==ITT_PLATFORM_WIN
    61 #include <windows.h>
    62 #pragma optimize("", off)
    63 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
    64 #include <pthread.h>
    65 #include <dlfcn.h>
    66 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
    67 #include <malloc.h>
    68 #include <stdlib.h>
    70 #include "vtune/jitprofiling.h"
    72 static const char rcsid[] = "\n@(#) $Revision: 294150 $\n";
    74 #define DLL_ENVIRONMENT_VAR             "VS_PROFILER"
    76 #ifndef NEW_DLL_ENVIRONMENT_VAR
    77 #if ITT_ARCH==ITT_ARCH_IA32
    78 #define NEW_DLL_ENVIRONMENT_VAR	        "INTEL_JIT_PROFILER32"
    79 #else
    80 #define NEW_DLL_ENVIRONMENT_VAR	        "INTEL_JIT_PROFILER64"
    81 #endif
    82 #endif /* NEW_DLL_ENVIRONMENT_VAR */
    84 #if ITT_PLATFORM==ITT_PLATFORM_WIN
    85 #define DEFAULT_DLLNAME                 "JitPI.dll"
    86 HINSTANCE m_libHandle = NULL;
    87 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
    88 #define DEFAULT_DLLNAME                 "libJitPI.so"
    89 void* m_libHandle = NULL;
    90 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
    92 /* default location of JIT profiling agent on Android */
    93 #define ANDROID_JIT_AGENT_PATH  "/data/intel/libittnotify.so"
    95 /* the function pointers */
    96 typedef unsigned int(*TPInitialize)(void);
    97 static TPInitialize FUNC_Initialize=NULL;
    99 typedef unsigned int(*TPNotify)(unsigned int, void*);
   100 static TPNotify FUNC_NotifyEvent=NULL;
   102 static iJIT_IsProfilingActiveFlags executionMode = iJIT_NOTHING_RUNNING;
   104 /* end collector dll part. */
   106 /* loadiJIT_Funcs() : this function is called just in the beginning 
   107  *  and is responsible to load the functions from BistroJavaCollector.dll
   108  * result:
   109  *  on success: the functions loads, iJIT_DLL_is_missing=0, return value = 1
   110  *  on failure: the functions are NULL, iJIT_DLL_is_missing=1, return value = 0
   111  */ 
   112 static int loadiJIT_Funcs(void);
   114 /* global representing whether the BistroJavaCollector can't be loaded */
   115 static int iJIT_DLL_is_missing = 0;
   117 /* Virtual stack - the struct is used as a virtual stack for each thread.
   118  * Every thread initializes with a stack of size INIT_TOP_STACK.
   119  * Every method entry decreases from the current stack point,
   120  * and when a thread stack reaches its top of stack (return from the global 
   121  * function), the top of stack and the current stack increase. Notice that 
   122  * when returning from a function the stack pointer is the address of 
   123  * the function return.
   124 */
   125 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   126 static DWORD threadLocalStorageHandle = 0;
   127 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   128 static pthread_key_t threadLocalStorageHandle = (pthread_key_t)0;
   129 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   131 #define INIT_TOP_Stack 10000
   133 typedef struct 
   134 {
   135     unsigned int TopStack;
   136     unsigned int CurrentStack;
   137 } ThreadStack, *pThreadStack;
   139 /* end of virtual stack. */
   141 /*
   142  * The function for reporting virtual-machine related events to VTune.
   143  * Note: when reporting iJVM_EVENT_TYPE_ENTER_NIDS, there is no need to fill 
   144  * in the stack_id field in the iJIT_Method_NIDS structure, as VTune fills it.
   145  * The return value in iJVM_EVENT_TYPE_ENTER_NIDS && 
   146  * iJVM_EVENT_TYPE_LEAVE_NIDS events will be 0 in case of failure.
   147  * in iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED event 
   148  * it will be -1 if EventSpecificData == 0 otherwise it will be 0.
   149 */
   151 ITT_EXTERN_C int JITAPI 
   152 iJIT_NotifyEvent(iJIT_JVM_EVENT event_type, void *EventSpecificData)
   153 {
   154     int ReturnValue;
   156     /*
   157      * This section is for debugging outside of VTune. 
   158      * It creates the environment variables that indicates call graph mode.
   159      * If running outside of VTune remove the remark.
   160      *
   161      *
   162      * static int firstTime = 1;
   163      * char DoCallGraph[12] = "DoCallGraph";
   164      * if (firstTime)
   165      * {
   166      * firstTime = 0;
   167      * SetEnvironmentVariable( "BISTRO_COLLECTORS_DO_CALLGRAPH", DoCallGraph);
   168      * }
   169      *
   170      * end of section.
   171     */
   173     /* initialization part - the functions have not been loaded yet. This part
   174      *        will load the functions, and check if we are in Call Graph mode. 
   175      *        (for special treatment).
   176      */
   177     if (!FUNC_NotifyEvent) 
   178     {
   179         if (iJIT_DLL_is_missing) 
   180             return 0;
   182         /* load the Function from the DLL */
   183         if (!loadiJIT_Funcs()) 
   184             return 0;
   186         /* Call Graph initialization. */
   187     }
   189     /* If the event is method entry/exit, check that in the current mode 
   190      * VTune is allowed to receive it
   191      */
   192     if ((event_type == iJVM_EVENT_TYPE_ENTER_NIDS || 
   193          event_type == iJVM_EVENT_TYPE_LEAVE_NIDS) &&
   194         (executionMode != iJIT_CALLGRAPH_ON))
   195     {
   196         return 0;
   197     }
   198     /* This section is performed when method enter event occurs.
   199      * It updates the virtual stack, or creates it if this is the first 
   200      * method entry in the thread. The stack pointer is decreased.
   201      */
   202     if (event_type == iJVM_EVENT_TYPE_ENTER_NIDS)
   203     {
   204 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   205         pThreadStack threadStack = 
   206             (pThreadStack)TlsGetValue (threadLocalStorageHandle);
   207 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   208         pThreadStack threadStack = 
   209             (pThreadStack)pthread_getspecific(threadLocalStorageHandle);
   210 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   212         /* check for use of reserved method IDs */
   213         if ( ((piJIT_Method_NIDS) EventSpecificData)->method_id <= 999 )
   214             return 0;
   216         if (!threadStack)
   217         {
   218             /* initialize the stack. */
   219             threadStack = (pThreadStack) calloc (sizeof(ThreadStack), 1);
   220             if (!threadStack)
   221                 return 0;
   222             threadStack->TopStack = INIT_TOP_Stack;
   223             threadStack->CurrentStack = INIT_TOP_Stack;
   224 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   225             TlsSetValue(threadLocalStorageHandle,(void*)threadStack);
   226 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   227             pthread_setspecific(threadLocalStorageHandle,(void*)threadStack);
   228 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   229         }
   231         /* decrease the stack. */
   232         ((piJIT_Method_NIDS) EventSpecificData)->stack_id = 
   233             (threadStack->CurrentStack)--;
   234     }
   236     /* This section is performed when method leave event occurs
   237      * It updates the virtual stack.
   238      *    Increases the stack pointer.
   239      *    If the stack pointer reached the top (left the global function)
   240      *        increase the pointer and the top pointer.
   241      */
   242     if (event_type == iJVM_EVENT_TYPE_LEAVE_NIDS)
   243     {
   244 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   245         pThreadStack threadStack = 
   246            (pThreadStack)TlsGetValue (threadLocalStorageHandle);
   247 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   248         pThreadStack threadStack = 
   249             (pThreadStack)pthread_getspecific(threadLocalStorageHandle);
   250 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   252         /* check for use of reserved method IDs */
   253         if ( ((piJIT_Method_NIDS) EventSpecificData)->method_id <= 999 )
   254             return 0;
   256         if (!threadStack)
   257         {
   258             /* Error: first report in this thread is method exit */
   259             exit (1);
   260         }
   262         ((piJIT_Method_NIDS) EventSpecificData)->stack_id = 
   263             ++(threadStack->CurrentStack) + 1;
   265         if (((piJIT_Method_NIDS) EventSpecificData)->stack_id 
   266                > threadStack->TopStack)
   267             ((piJIT_Method_NIDS) EventSpecificData)->stack_id = 
   268                 (unsigned int)-1;
   269     }
   271     if (event_type == iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED)
   272     {
   273         /* check for use of reserved method IDs */
   274         if ( ((piJIT_Method_Load) EventSpecificData)->method_id <= 999 )
   275             return 0;
   276     }
   277     else if (event_type == iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED_V2)
   278     {
   279         /* check for use of reserved method IDs */
   280         if ( ((piJIT_Method_Load_V2) EventSpecificData)->method_id <= 999 )
   281             return 0;
   282     }
   284     ReturnValue = (int)FUNC_NotifyEvent(event_type, EventSpecificData);
   286     return ReturnValue;
   287 }
   289 /* The new mode call back routine */
   290 ITT_EXTERN_C void JITAPI 
   291 iJIT_RegisterCallbackEx(void *userdata, iJIT_ModeChangedEx 
   292                         NewModeCallBackFuncEx) 
   293 {
   294     /* is it already missing... or the load of functions from the DLL failed */
   295     if (iJIT_DLL_is_missing || !loadiJIT_Funcs())
   296     {
   297         /* then do not bother with notifications */
   298         NewModeCallBackFuncEx(userdata, iJIT_NO_NOTIFICATIONS);  
   299         /* Error: could not load JIT functions. */
   300         return;
   301     }
   302     /* nothing to do with the callback */
   303 }
   305 /*
   306  * This function allows the user to query in which mode, if at all, 
   307  *VTune is running
   308  */
   309 ITT_EXTERN_C iJIT_IsProfilingActiveFlags JITAPI iJIT_IsProfilingActive()
   310 {
   311     if (!iJIT_DLL_is_missing)
   312     {
   313         loadiJIT_Funcs();
   314     }
   316     return executionMode;
   317 }
   319 /* this function loads the collector dll (BistroJavaCollector) 
   320  * and the relevant functions.
   321  * on success: all functions load,     iJIT_DLL_is_missing = 0, return value = 1
   322  * on failure: all functions are NULL, iJIT_DLL_is_missing = 1, return value = 0
   323  */ 
   324 static int loadiJIT_Funcs()
   325 {
   326     static int bDllWasLoaded = 0;
   327     char *dllName = (char*)rcsid; /* !! Just to avoid unused code elimination */
   328 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   329     DWORD dNameLength = 0;
   330 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   332     if(bDllWasLoaded)
   333     {
   334         /* dll was already loaded, no need to do it for the second time */
   335         return 1;
   336     }
   338     /* Assumes that the DLL will not be found */
   339     iJIT_DLL_is_missing = 1;
   340     FUNC_NotifyEvent = NULL;
   342     if (m_libHandle) 
   343     {
   344 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   345         FreeLibrary(m_libHandle);
   346 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   347         dlclose(m_libHandle);
   348 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   349         m_libHandle = NULL;
   350     }
   352     /* Try to get the dll name from the environment */
   353 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   354     dNameLength = GetEnvironmentVariableA(NEW_DLL_ENVIRONMENT_VAR, NULL, 0);
   355     if (dNameLength)
   356     {
   357         DWORD envret = 0;
   358         dllName = (char*)malloc(sizeof(char) * (dNameLength + 1));
   359         envret = GetEnvironmentVariableA(NEW_DLL_ENVIRONMENT_VAR, 
   360                                          dllName, dNameLength);
   361         if (envret)
   362         {
   363             /* Try to load the dll from the PATH... */
   364             m_libHandle = LoadLibraryExA(dllName, 
   365                                          NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
   366         }
   367         free(dllName);
   368     } else {
   369         /* Try to use old VS_PROFILER variable */
   370         dNameLength = GetEnvironmentVariableA(DLL_ENVIRONMENT_VAR, NULL, 0);
   371         if (dNameLength)
   372         {
   373             DWORD envret = 0;
   374             dllName = (char*)malloc(sizeof(char) * (dNameLength + 1));
   375             envret = GetEnvironmentVariableA(DLL_ENVIRONMENT_VAR, 
   376                                              dllName, dNameLength);
   377             if (envret)
   378             {
   379                 /* Try to load the dll from the PATH... */
   380                 m_libHandle = LoadLibraryA(dllName);
   381             }
   382             free(dllName);
   383         }
   384     }
   385 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   386     dllName = getenv(NEW_DLL_ENVIRONMENT_VAR);
   387     if (!dllName)
   388         dllName = getenv(DLL_ENVIRONMENT_VAR);
   389 #ifdef ANDROID
   390     if (!dllName)
   391         dllName = ANDROID_JIT_AGENT_PATH;
   392 #endif
   393     if (dllName)
   394     {
   395         /* Try to load the dll from the PATH... */
   396         m_libHandle = dlopen(dllName, RTLD_LAZY);
   397     }
   398 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   400     if (!m_libHandle)
   401     {
   402 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   403         m_libHandle = LoadLibraryA(DEFAULT_DLLNAME);
   404 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   405         m_libHandle = dlopen(DEFAULT_DLLNAME, RTLD_LAZY);
   406 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   407     }
   409     /* if the dll wasn't loaded - exit. */
   410     if (!m_libHandle)
   411     {
   412         iJIT_DLL_is_missing = 1; /* don't try to initialize 
   413                                   * JIT agent the second time 
   414                                   */
   415         return 0;
   416     }
   418 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   419     FUNC_NotifyEvent = (TPNotify)GetProcAddress(m_libHandle, "NotifyEvent");
   420 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   421     FUNC_NotifyEvent = (TPNotify)dlsym(m_libHandle, "NotifyEvent");
   422 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   423     if (!FUNC_NotifyEvent) 
   424     {
   425         FUNC_Initialize = NULL;
   426         return 0;
   427     }
   429 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   430     FUNC_Initialize = (TPInitialize)GetProcAddress(m_libHandle, "Initialize");
   431 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   432     FUNC_Initialize = (TPInitialize)dlsym(m_libHandle, "Initialize");
   433 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   434     if (!FUNC_Initialize) 
   435     {
   436         FUNC_NotifyEvent = NULL;
   437         return 0;
   438     }
   440     executionMode = (iJIT_IsProfilingActiveFlags)FUNC_Initialize();
   442     bDllWasLoaded = 1;
   443     iJIT_DLL_is_missing = 0; /* DLL is ok. */
   445     /*
   446      * Call Graph mode: init the thread local storage
   447      * (need to store the virtual stack there).
   448      */
   449     if ( executionMode == iJIT_CALLGRAPH_ON )
   450     {
   451         /* Allocate a thread local storage slot for the thread "stack" */
   452         if (!threadLocalStorageHandle)
   453 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   454             threadLocalStorageHandle = TlsAlloc();
   455 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   456         pthread_key_create(&threadLocalStorageHandle, NULL);
   457 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   458     }
   460     return 1;
   461 }
   463 /*
   464  * This function should be called by the user whenever a thread ends, 
   465  * to free the thread "virtual stack" storage
   466  */
   467 ITT_EXTERN_C void JITAPI FinalizeThread()
   468 {
   469     if (threadLocalStorageHandle)
   470     {
   471 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   472         pThreadStack threadStack = 
   473             (pThreadStack)TlsGetValue (threadLocalStorageHandle);
   474 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   475         pThreadStack threadStack = 
   476             (pThreadStack)pthread_getspecific(threadLocalStorageHandle);
   477 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   478         if (threadStack)
   479         {
   480             free (threadStack);
   481             threadStack = NULL;
   482 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   483             TlsSetValue (threadLocalStorageHandle, threadStack);
   484 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   485             pthread_setspecific(threadLocalStorageHandle, threadStack);
   486 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   487         }
   488     }
   489 }
   491 /*
   492  * This function should be called by the user when the process ends, 
   493  * to free the local storage index
   494 */
   495 ITT_EXTERN_C void JITAPI FinalizeProcess()
   496 {
   497     if (m_libHandle) 
   498     {
   499 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   500         FreeLibrary(m_libHandle);
   501 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   502         dlclose(m_libHandle);
   503 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   504         m_libHandle = NULL;
   505     }
   507     if (threadLocalStorageHandle)
   508 #if ITT_PLATFORM==ITT_PLATFORM_WIN
   509         TlsFree (threadLocalStorageHandle);
   510 #else  /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   511     pthread_key_delete(threadLocalStorageHandle);
   512 #endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */
   513 }
   515 /*
   516  * This function should be called by the user for any method once.
   517  * The function will return a unique method ID, the user should maintain 
   518  * the ID for each method
   519  */
   520 ITT_EXTERN_C unsigned int JITAPI iJIT_GetNewMethodID()
   521 {
   522     static unsigned int methodID = 0x100000;
   524     if (methodID == 0)
   525         return 0;  /* ERROR : this is not a valid value */
   527     return methodID++;
   528 }

mercurial