2011-08-27 00:05:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Benoit Girard <bgirard@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-12-04 19:09:00 +00:00
|
|
|
#include <stdlib.h>
|
2011-12-15 20:44:13 +00:00
|
|
|
#include <signal.h>
|
2011-12-04 19:09:00 +00:00
|
|
|
#include "thread_helper.h"
|
2011-11-04 21:07:50 +00:00
|
|
|
#include "nscore.h"
|
2012-02-02 21:57:20 +00:00
|
|
|
#include "jsapi.h"
|
2011-12-07 19:48:15 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
|
|
|
|
using mozilla::TimeStamp;
|
|
|
|
using mozilla::TimeDuration;
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2011-12-04 19:09:00 +00:00
|
|
|
extern mozilla::tls::key pkey_stack;
|
|
|
|
extern mozilla::tls::key pkey_ticker;
|
2011-12-08 15:46:02 +00:00
|
|
|
extern bool stack_key_initialized;
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-01-17 20:33:04 +00:00
|
|
|
#ifndef SAMPLE_FUNCTION_NAME
|
|
|
|
# ifdef __GNUC__
|
|
|
|
# define SAMPLE_FUNCTION_NAME __FUNCTION__
|
|
|
|
# elif defined(_MSC_VER)
|
|
|
|
# define SAMPLE_FUNCTION_NAME __FUNCTION__
|
|
|
|
# else
|
|
|
|
# define SAMPLE_FUNCTION_NAME __func__ // defined in C99, supported in various C++ compilers. Just raw function name.
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2011-12-05 01:53:17 +00:00
|
|
|
#define SAMPLER_INIT() mozilla_sampler_init()
|
|
|
|
#define SAMPLER_DEINIT() mozilla_sampler_deinit()
|
2012-01-12 18:33:32 +00:00
|
|
|
#define SAMPLER_START(entries, interval, features, featureCount) mozilla_sampler_start(entries, interval, features, featureCount)
|
2011-12-05 01:53:17 +00:00
|
|
|
#define SAMPLER_STOP() mozilla_sampler_stop()
|
|
|
|
#define SAMPLER_IS_ACTIVE() mozilla_sampler_is_active()
|
2011-12-07 19:48:15 +00:00
|
|
|
#define SAMPLER_RESPONSIVENESS(time) mozilla_sampler_responsiveness(time)
|
|
|
|
#define SAMPLER_GET_RESPONSIVENESS() mozilla_sampler_get_responsiveness()
|
2011-12-05 01:53:17 +00:00
|
|
|
#define SAMPLER_SAVE() mozilla_sampler_save()
|
|
|
|
#define SAMPLER_GET_PROFILE() mozilla_sampler_get_profile()
|
2012-02-02 21:57:20 +00:00
|
|
|
#define SAMPLER_GET_PROFILE_DATA(ctx) mozilla_sampler_get_profile_data(ctx)
|
2012-01-12 18:33:32 +00:00
|
|
|
#define SAMPLER_GET_FEATURES() mozilla_sampler_get_features()
|
2012-01-17 20:33:04 +00:00
|
|
|
// we want the class and function name but can't easily get that using preprocessor macros
|
|
|
|
// __func__ doesn't have the class name and __PRETTY_FUNCTION__ has the parameters
|
|
|
|
#define SAMPLE_LABEL(name_space, info) mozilla::SamplerStackFrameRAII only_one_sampleraii_per_scope(name_space "::" info)
|
2011-12-05 01:53:17 +00:00
|
|
|
#define SAMPLE_MARKER(info) mozilla_sampler_add_marker(info)
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2011-12-02 22:05:33 +00:00
|
|
|
/* we duplicate this code here to avoid header dependencies
|
|
|
|
* which make it more difficult to include in other places */
|
|
|
|
#if defined(_M_X64) || defined(__x86_64__)
|
|
|
|
#define V8_HOST_ARCH_X64 1
|
|
|
|
#elif defined(_M_IX86) || defined(__i386__) || defined(__i386)
|
|
|
|
#define V8_HOST_ARCH_IA32 1
|
|
|
|
#elif defined(__ARMEL__)
|
|
|
|
#define V8_HOST_ARCH_ARM 1
|
|
|
|
#else
|
|
|
|
#warning Please add support for your architecture in chromium_types.h
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
// STORE_SEQUENCER: Because signals can interrupt our profile modification
|
|
|
|
// we need to make stores are not re-ordered by the compiler
|
|
|
|
// or hardware to make sure the profile is consistent at
|
|
|
|
// every point the signal can fire.
|
2011-12-02 22:05:33 +00:00
|
|
|
#ifdef V8_HOST_ARCH_ARM
|
2011-08-27 00:05:37 +00:00
|
|
|
// TODO Is there something cheaper that will prevent
|
|
|
|
// memory stores from being reordered
|
2011-12-02 22:05:33 +00:00
|
|
|
|
|
|
|
typedef void (*LinuxKernelMemoryBarrierFunc)(void);
|
|
|
|
LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) =
|
|
|
|
(LinuxKernelMemoryBarrierFunc) 0xffff0fa0;
|
|
|
|
|
|
|
|
# define STORE_SEQUENCER() pLinuxKernelMemoryBarrier()
|
|
|
|
#elif defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_X64)
|
2011-12-04 19:09:00 +00:00
|
|
|
# if defined(_MSC_VER)
|
2012-01-19 00:53:35 +00:00
|
|
|
// MSVC2005 has a name collision bug caused when both <intrin.h> and <winnt.h> are included together.
|
|
|
|
#ifdef _WINNT_
|
2011-12-04 19:09:00 +00:00
|
|
|
# define _interlockedbittestandreset _interlockedbittestandreset_NAME_CHANGED_TO_AVOID_MSVS2005_ERROR
|
|
|
|
# define _interlockedbittestandset _interlockedbittestandset_NAME_CHANGED_TO_AVOID_MSVS2005_ERROR
|
|
|
|
# include <intrin.h>
|
2012-01-17 20:33:04 +00:00
|
|
|
#else
|
|
|
|
# include <intrin.h>
|
|
|
|
# define _interlockedbittestandreset _interlockedbittestandreset_NAME_CHANGED_TO_AVOID_MSVS2005_ERROR
|
|
|
|
# define _interlockedbittestandset _interlockedbittestandset_NAME_CHANGED_TO_AVOID_MSVS2005_ERROR
|
|
|
|
#endif
|
2011-12-04 19:09:00 +00:00
|
|
|
// Even though MSVC2005 has the intrinsic _ReadWriteBarrier, it fails to link to it when it's
|
|
|
|
// not explicitly declared.
|
|
|
|
# pragma intrinsic(_ReadWriteBarrier)
|
|
|
|
# define STORE_SEQUENCER() _ReadWriteBarrier();
|
|
|
|
# elif defined(__INTEL_COMPILER)
|
|
|
|
# define STORE_SEQUENCER() __memory_barrier();
|
|
|
|
# elif __GNUC__
|
|
|
|
# define STORE_SEQUENCER() asm volatile("" ::: "memory");
|
|
|
|
# else
|
|
|
|
# error "Memory clobber not supported for your compiler."
|
|
|
|
# endif
|
2011-08-27 00:05:37 +00:00
|
|
|
#else
|
|
|
|
# error "Memory clobber not supported for your platform."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Returns a handdle to pass on exit. This can check that we are popping the
|
|
|
|
// correct callstack.
|
|
|
|
inline void* mozilla_sampler_call_enter(const char *aInfo);
|
|
|
|
inline void mozilla_sampler_call_exit(void* handle);
|
|
|
|
inline void mozilla_sampler_add_marker(const char *aInfo);
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
void mozilla_sampler_start(int aEntries, int aInterval, const char** aFeatures, uint32_t aFeatureCount);
|
2011-12-07 19:48:15 +00:00
|
|
|
void mozilla_sampler_stop();
|
|
|
|
bool mozilla_sampler_is_active();
|
|
|
|
void mozilla_sampler_responsiveness(TimeStamp time);
|
2012-01-12 16:50:43 +00:00
|
|
|
const double* mozilla_sampler_get_responsiveness();
|
2011-12-07 19:48:15 +00:00
|
|
|
void mozilla_sampler_save();
|
|
|
|
char* mozilla_sampler_get_profile();
|
2012-02-02 21:57:20 +00:00
|
|
|
JSObject *mozilla_sampler_get_profile_data(JSContext *aCx);
|
2012-01-12 18:33:32 +00:00
|
|
|
const char** mozilla_sampler_get_features();
|
2011-08-27 00:05:37 +00:00
|
|
|
void mozilla_sampler_init();
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class NS_STACK_CLASS SamplerStackFrameRAII {
|
|
|
|
public:
|
2012-01-17 20:33:04 +00:00
|
|
|
// we only copy the strings at save time, so to take multiple parameters we'd need to copy them then.
|
|
|
|
SamplerStackFrameRAII(const char *aInfo) {
|
2011-08-27 00:05:37 +00:00
|
|
|
mHandle = mozilla_sampler_call_enter(aInfo);
|
|
|
|
}
|
|
|
|
~SamplerStackFrameRAII() {
|
|
|
|
mozilla_sampler_call_exit(mHandle);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void* mHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
} //mozilla
|
|
|
|
|
|
|
|
// the SamplerStack members are read by signal
|
|
|
|
// handlers, so the mutation of them needs to be signal-safe.
|
2012-03-12 14:58:33 +00:00
|
|
|
struct ProfileStack
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-03-12 14:58:33 +00:00
|
|
|
ProfileStack()
|
2011-08-27 00:05:37 +00:00
|
|
|
: mStackPointer(0)
|
|
|
|
, mMarkerPointer(0)
|
|
|
|
, mDroppedStackEntries(0)
|
|
|
|
, mQueueClearMarker(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void addMarker(const char *aMarker)
|
|
|
|
{
|
|
|
|
if (mQueueClearMarker) {
|
|
|
|
clearMarkers();
|
|
|
|
}
|
|
|
|
if (!aMarker) {
|
|
|
|
return; //discard
|
|
|
|
}
|
|
|
|
if (mMarkerPointer == 1024) {
|
|
|
|
return; //array full, silently drop
|
|
|
|
}
|
|
|
|
mMarkers[mMarkerPointer] = aMarker;
|
|
|
|
STORE_SEQUENCER();
|
|
|
|
mMarkerPointer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called within signal. Function must be reentrant
|
|
|
|
const char* getMarker(int aMarkerId)
|
|
|
|
{
|
|
|
|
if (mQueueClearMarker) {
|
|
|
|
clearMarkers();
|
|
|
|
}
|
|
|
|
if (aMarkerId >= mMarkerPointer) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mMarkers[aMarkerId];
|
|
|
|
}
|
|
|
|
|
|
|
|
// called within signal. Function must be reentrant
|
|
|
|
void clearMarkers()
|
|
|
|
{
|
|
|
|
mMarkerPointer = 0;
|
|
|
|
mQueueClearMarker = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(const char *aName)
|
|
|
|
{
|
|
|
|
if (mStackPointer >= 1024) {
|
|
|
|
mDroppedStackEntries++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we increment the pointer after the name has
|
|
|
|
// been written such that mStack is always consistent.
|
|
|
|
mStack[mStackPointer] = aName;
|
|
|
|
// Prevent the optimizer from re-ordering these instructions
|
2011-12-04 19:09:00 +00:00
|
|
|
STORE_SEQUENCER();
|
2011-08-27 00:05:37 +00:00
|
|
|
mStackPointer++;
|
|
|
|
}
|
|
|
|
void pop()
|
|
|
|
{
|
|
|
|
if (mDroppedStackEntries > 0) {
|
|
|
|
mDroppedStackEntries--;
|
|
|
|
} else {
|
|
|
|
mStackPointer--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool isEmpty()
|
|
|
|
{
|
|
|
|
return mStackPointer == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep a list of active checkpoints
|
2011-12-04 19:09:00 +00:00
|
|
|
char const * volatile mStack[1024];
|
2011-08-27 00:05:37 +00:00
|
|
|
// Keep a list of active markers to be applied to the next sample taken
|
2011-12-04 19:09:00 +00:00
|
|
|
char const * volatile mMarkers[1024];
|
|
|
|
volatile mozilla::sig_safe_t mStackPointer;
|
|
|
|
volatile mozilla::sig_safe_t mMarkerPointer;
|
|
|
|
volatile mozilla::sig_safe_t mDroppedStackEntries;
|
2011-08-27 00:05:37 +00:00
|
|
|
// We don't want to modify _markers from within the signal so we allow
|
|
|
|
// it to queue a clear operation.
|
2011-12-04 19:09:00 +00:00
|
|
|
volatile mozilla::sig_safe_t mQueueClearMarker;
|
2011-08-27 00:05:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline void* mozilla_sampler_call_enter(const char *aInfo)
|
|
|
|
{
|
2011-12-08 15:46:02 +00:00
|
|
|
// check if we've been initialized to avoid calling pthread_getspecific
|
|
|
|
// with a null pkey_stack which will return undefined results.
|
|
|
|
if (!stack_key_initialized)
|
|
|
|
return NULL;
|
|
|
|
|
2012-03-12 14:58:33 +00:00
|
|
|
ProfileStack *stack = mozilla::tls::get<ProfileStack>(pkey_stack);
|
2011-12-08 15:46:02 +00:00
|
|
|
// we can't infer whether 'stack' has been initialized
|
|
|
|
// based on the value of stack_key_intiailized because
|
|
|
|
// 'stack' is only intialized when a thread is being
|
|
|
|
// profiled.
|
2011-08-27 00:05:37 +00:00
|
|
|
if (!stack) {
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
stack->push(aInfo);
|
|
|
|
|
|
|
|
// The handle is meant to support future changes
|
|
|
|
// but for now it is simply use to save a call to
|
|
|
|
// pthread_getspecific on exit. It also supports the
|
|
|
|
// case where the sampler is initialized between
|
|
|
|
// enter and exit.
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mozilla_sampler_call_exit(void *aHandle)
|
|
|
|
{
|
|
|
|
if (!aHandle)
|
|
|
|
return;
|
|
|
|
|
2012-03-12 14:58:33 +00:00
|
|
|
ProfileStack *stack = (ProfileStack*)aHandle;
|
2011-08-27 00:05:37 +00:00
|
|
|
stack->pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void mozilla_sampler_add_marker(const char *aMarker)
|
|
|
|
{
|
2012-03-12 14:58:33 +00:00
|
|
|
ProfileStack *stack = mozilla::tls::get<ProfileStack>(pkey_stack);
|
2011-08-27 00:05:37 +00:00
|
|
|
if (!stack) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stack->addMarker(aMarker);
|
|
|
|
}
|
|
|
|
|