2003-11-01 10:34:18 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-10-06 10:37:41 +04:00
|
|
|
// vim:cindent:sw=4:et:ts=8:
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-03-14 06:52:18 +03:00
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
// The linux glibc hides part of sigaction if _POSIX_SOURCE is defined
|
|
|
|
#if defined(linux)
|
|
|
|
#undef _POSIX_SOURCE
|
2003-11-01 10:34:18 +03:00
|
|
|
#undef _SVID_SOURCE
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2000-01-18 06:43:26 +03:00
|
|
|
#endif
|
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#if defined(linux)
|
|
|
|
#include <linux/rtc.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2000-01-18 06:43:26 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2003-11-01 10:34:18 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
2000-01-18 06:43:26 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2003-11-01 10:34:18 +03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
2011-05-15 13:47:48 +04:00
|
|
|
#include <sys/syscall.h>
|
2006-10-04 08:42:33 +04:00
|
|
|
#include <ucontext.h>
|
2011-05-15 13:47:48 +04:00
|
|
|
#include <execinfo.h>
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
#include "libmalloc.h"
|
2004-12-15 08:52:44 +03:00
|
|
|
#include "jprof.h"
|
2000-01-18 06:43:26 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2011-11-08 09:34:40 +04:00
|
|
|
// Must define before including jprof.h
|
|
|
|
void *moz_xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void moz_xfree(void *mem)
|
|
|
|
{
|
|
|
|
free(mem);
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
#ifdef NTO
|
|
|
|
#include <sys/link.h>
|
|
|
|
extern r_debug _r_debug;
|
|
|
|
#else
|
|
|
|
#include <link.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-15 13:47:48 +04:00
|
|
|
#define USE_GLIBC_BACKTRACE 1
|
|
|
|
// To debug, use #define JPROF_STATIC
|
|
|
|
#define JPROF_STATIC //static
|
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
static int gLogFD = -1;
|
2003-11-01 10:34:18 +03:00
|
|
|
static pthread_t main_thread;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2006-08-29 00:12:37 +04:00
|
|
|
static void startSignalCounter(unsigned long millisec);
|
2004-03-10 01:31:56 +03:00
|
|
|
static int enableRTCSignals(bool enable);
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2011-06-24 08:01:49 +04:00
|
|
|
// replace use of atexit()
|
|
|
|
|
|
|
|
static void DumpAddressMap();
|
|
|
|
|
|
|
|
struct JprofShutdown {
|
|
|
|
JprofShutdown() {}
|
|
|
|
~JprofShutdown() {
|
|
|
|
DumpAddressMap();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void RegisterJprofShutdown() {
|
|
|
|
// This instanciates the dummy class above, and will trigger the class
|
|
|
|
// destructor when libxul is unloaded. This is equivalent to atexit(),
|
|
|
|
// but gracefully handles dlclose().
|
|
|
|
static JprofShutdown t;
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2006-10-04 08:42:33 +04:00
|
|
|
#if defined(i386) || defined(_i386) || defined(__x86_64__)
|
2011-05-15 13:47:48 +04:00
|
|
|
JPROF_STATIC void CrawlStack(malloc_log_entry* me,
|
|
|
|
void* stack_top, void* top_instr_ptr)
|
2000-01-18 06:43:26 +03:00
|
|
|
{
|
2011-05-15 13:47:48 +04:00
|
|
|
#if USE_GLIBC_BACKTRACE
|
|
|
|
// This probably works on more than x86! But we need a way to get the
|
|
|
|
// top instruction pointer, which is kindof arch-specific
|
|
|
|
void *array[500];
|
|
|
|
int cnt, i;
|
|
|
|
u_long numpcs = 0;
|
|
|
|
bool tracing = false;
|
|
|
|
|
|
|
|
// This is from glibc. A more generic version might use
|
|
|
|
// libunwind and/or CaptureStackBackTrace() on Windows
|
|
|
|
cnt = backtrace(&array[0],sizeof(array)/sizeof(array[0]));
|
|
|
|
|
|
|
|
// StackHook->JprofLog->CrawlStack
|
|
|
|
// Then we have sigaction, which replaced top_instr_ptr
|
|
|
|
array[3] = top_instr_ptr;
|
|
|
|
for (i = 3; i < cnt; i++)
|
|
|
|
{
|
|
|
|
me->pcs[numpcs++] = (char *) array[i];
|
|
|
|
}
|
|
|
|
me->numpcs = numpcs;
|
|
|
|
|
|
|
|
#else
|
|
|
|
// original code - this breaks on many platforms
|
2006-10-04 08:42:33 +04:00
|
|
|
void **bp;
|
|
|
|
#if defined(__i386)
|
|
|
|
__asm__( "movl %%ebp, %0" : "=g"(bp));
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
__asm__( "movq %%rbp, %0" : "=g"(bp));
|
|
|
|
#else
|
|
|
|
// It would be nice if this worked uniformly, but at least on i386 and
|
|
|
|
// x86_64, it stopped working with gcc 4.1, because it points to the
|
|
|
|
// end of the saved registers instead of the start.
|
|
|
|
bp = __builtin_frame_address(0);
|
|
|
|
#endif
|
2000-01-18 06:43:26 +03:00
|
|
|
u_long numpcs = 0;
|
2011-05-15 13:47:48 +04:00
|
|
|
bool tracing = false;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2006-10-04 08:42:33 +04:00
|
|
|
me->pcs[numpcs++] = (char*) top_instr_ptr;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
while (numpcs < MAX_STACK_CRAWL) {
|
2006-03-24 02:21:27 +03:00
|
|
|
void** nextbp = (void**) *bp++;
|
|
|
|
void* pc = *bp;
|
2006-10-04 08:42:33 +04:00
|
|
|
if (nextbp < bp) {
|
2000-01-18 06:43:26 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-05-15 13:47:48 +04:00
|
|
|
if (tracing) {
|
2006-10-04 08:42:33 +04:00
|
|
|
// Skip the signal handling.
|
2000-01-18 06:43:26 +03:00
|
|
|
me->pcs[numpcs++] = (char*) pc;
|
|
|
|
}
|
2011-05-15 13:47:48 +04:00
|
|
|
else if (pc == top_instr_ptr) {
|
|
|
|
tracing = true;
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
bp = nextbp;
|
|
|
|
}
|
|
|
|
me->numpcs = numpcs;
|
2011-05-15 13:47:48 +04:00
|
|
|
#endif
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2004-03-10 01:31:56 +03:00
|
|
|
static int rtcHz;
|
|
|
|
static int rtcFD = -1;
|
2011-11-08 09:34:40 +04:00
|
|
|
static bool circular = false;
|
2004-03-10 01:31:56 +03:00
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
#if defined(linux) || defined(NTO)
|
|
|
|
static void DumpAddressMap()
|
|
|
|
{
|
2005-11-25 22:48:04 +03:00
|
|
|
// Turn off the timer so we don't get interrupts during shutdown
|
2004-03-10 01:31:56 +03:00
|
|
|
#if defined(linux)
|
|
|
|
if (rtcHz) {
|
|
|
|
enableRTCSignals(false);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
startSignalCounter(0);
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
int mfd = open(M_MAPFILE, O_CREAT|O_WRONLY|O_TRUNC, 0666);
|
|
|
|
if (mfd >= 0) {
|
|
|
|
malloc_map_entry mme;
|
|
|
|
link_map* map = _r_debug.r_map;
|
2014-01-06 19:07:38 +04:00
|
|
|
while (nullptr != map) {
|
2003-10-16 14:04:14 +04:00
|
|
|
if (map->l_name && *map->l_name) {
|
2000-01-18 06:43:26 +03:00
|
|
|
mme.nameLen = strlen(map->l_name);
|
|
|
|
mme.address = map->l_addr;
|
|
|
|
write(mfd, &mme, sizeof(mme));
|
|
|
|
write(mfd, map->l_name, mme.nameLen);
|
|
|
|
#if 0
|
|
|
|
write(1, map->l_name, mme.nameLen);
|
|
|
|
write(1, "\n", 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
map = map->l_next;
|
|
|
|
}
|
|
|
|
close(mfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-12 19:59:17 +04:00
|
|
|
static bool was_paused = true;
|
|
|
|
|
2011-11-08 09:34:40 +04:00
|
|
|
JPROF_STATIC void JprofBufferDump();
|
|
|
|
JPROF_STATIC void JprofBufferClear();
|
|
|
|
|
|
|
|
static void ClearProfilingHook(int signum)
|
|
|
|
{
|
|
|
|
if (circular) {
|
|
|
|
JprofBufferClear();
|
|
|
|
puts("Jprof: cleared circular buffer.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-03 03:35:53 +03:00
|
|
|
static void EndProfilingHook(int signum)
|
|
|
|
{
|
2011-11-08 09:34:40 +04:00
|
|
|
if (circular)
|
|
|
|
JprofBufferDump();
|
|
|
|
|
2000-02-03 03:35:53 +03:00
|
|
|
DumpAddressMap();
|
2011-08-12 19:59:17 +04:00
|
|
|
was_paused = true;
|
2003-11-01 10:34:18 +03:00
|
|
|
puts("Jprof: profiling paused.");
|
2000-02-03 03:35:53 +03:00
|
|
|
}
|
|
|
|
|
2011-11-08 09:34:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// proper usage would be a template, including the function to find the
|
|
|
|
// size of an entry, or include a size header explicitly to each entry.
|
|
|
|
#if defined(linux)
|
|
|
|
#define DUMB_LOCK() pthread_mutex_lock(&mutex);
|
|
|
|
#define DUMB_UNLOCK() pthread_mutex_unlock(&mutex);
|
|
|
|
#else
|
|
|
|
#define DUMB_LOCK() FIXME()
|
|
|
|
#define DUMB_UNLOCK() FIXME()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
class DumbCircularBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DumbCircularBuffer(size_t init_buffer_size) {
|
|
|
|
used = 0;
|
|
|
|
buffer_size = init_buffer_size;
|
|
|
|
buffer = (unsigned char *) malloc(buffer_size);
|
|
|
|
head = tail = buffer;
|
|
|
|
|
|
|
|
#if defined(linux)
|
|
|
|
pthread_mutexattr_t mAttr;
|
|
|
|
pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_RECURSIVE_NP);
|
|
|
|
pthread_mutex_init(&mutex, &mAttr);
|
|
|
|
pthread_mutexattr_destroy(&mAttr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
~DumbCircularBuffer() {
|
|
|
|
free(buffer);
|
|
|
|
#if defined(linux)
|
|
|
|
pthread_mutex_destroy (&mutex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
DUMB_LOCK();
|
|
|
|
head = tail;
|
|
|
|
used = 0;
|
|
|
|
DUMB_UNLOCK();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() {
|
|
|
|
return head == tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t space_available() {
|
|
|
|
size_t result;
|
|
|
|
DUMB_LOCK();
|
|
|
|
if (tail > head)
|
|
|
|
result = buffer_size - (tail-head) - 1;
|
|
|
|
else
|
|
|
|
result = head-tail - 1;
|
|
|
|
DUMB_UNLOCK();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drop(size_t size) {
|
|
|
|
// assumes correctness!
|
|
|
|
DUMB_LOCK();
|
|
|
|
head += size;
|
|
|
|
if (head >= &buffer[buffer_size])
|
|
|
|
head -= buffer_size;
|
|
|
|
used--;
|
|
|
|
DUMB_UNLOCK();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool insert(void *data, size_t size) {
|
|
|
|
// can fail if not enough space in the entire buffer
|
|
|
|
DUMB_LOCK();
|
|
|
|
if (space_available() < size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t max_without_wrap = &buffer[buffer_size] - tail;
|
|
|
|
size_t initial = size > max_without_wrap ? max_without_wrap : size;
|
|
|
|
#if DEBUG_CIRCULAR
|
|
|
|
fprintf(stderr,"insert(%d): max_without_wrap %d, size %d, initial %d\n",used,max_without_wrap,size,initial);
|
|
|
|
#endif
|
|
|
|
memcpy(tail,data,initial);
|
|
|
|
tail += initial;
|
|
|
|
data = ((char *)data)+initial;
|
|
|
|
size -= initial;
|
|
|
|
if (size != 0) {
|
|
|
|
#if DEBUG_CIRCULAR
|
|
|
|
fprintf(stderr,"wrapping by %d bytes\n",size);
|
|
|
|
#endif
|
|
|
|
memcpy(buffer,data,size);
|
|
|
|
tail = &(((unsigned char *)buffer)[size]);
|
|
|
|
}
|
|
|
|
|
|
|
|
used++;
|
|
|
|
DUMB_UNLOCK();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for external access to the buffer (saving)
|
|
|
|
void lock() {
|
|
|
|
DUMB_LOCK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() {
|
|
|
|
DUMB_UNLOCK();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX These really shouldn't be public...
|
|
|
|
unsigned char *head;
|
|
|
|
unsigned char *tail;
|
|
|
|
unsigned int used;
|
|
|
|
unsigned char *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DumbCircularBuffer *JprofBuffer;
|
|
|
|
|
|
|
|
JPROF_STATIC void
|
|
|
|
JprofBufferInit(size_t size)
|
|
|
|
{
|
|
|
|
JprofBuffer = new DumbCircularBuffer(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
JPROF_STATIC void
|
|
|
|
JprofBufferClear()
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Told to clear JPROF circular buffer\n");
|
|
|
|
JprofBuffer->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
JPROF_STATIC size_t
|
|
|
|
JprofEntrySizeof(malloc_log_entry *me)
|
|
|
|
{
|
|
|
|
return offsetof(malloc_log_entry, pcs) + me->numpcs*sizeof(char*);
|
|
|
|
}
|
|
|
|
|
|
|
|
JPROF_STATIC void
|
|
|
|
JprofBufferAppend(malloc_log_entry *me)
|
|
|
|
{
|
|
|
|
size_t size = JprofEntrySizeof(me);
|
|
|
|
|
|
|
|
do {
|
|
|
|
while (JprofBuffer->space_available() < size &&
|
|
|
|
JprofBuffer->used > 0) {
|
|
|
|
#if DEBUG_CIRCULAR
|
|
|
|
fprintf(stderr,"dropping entry: %d in use, %d free, need %d, size_to_free = %d\n",
|
|
|
|
JprofBuffer->used,JprofBuffer->space_available(),size,JprofEntrySizeof((malloc_log_entry *) JprofBuffer->head));
|
|
|
|
#endif
|
|
|
|
JprofBuffer->drop(JprofEntrySizeof((malloc_log_entry *) JprofBuffer->head));
|
|
|
|
}
|
|
|
|
if (JprofBuffer->space_available() < size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
} while (!JprofBuffer->insert(me,size));
|
|
|
|
}
|
|
|
|
|
|
|
|
JPROF_STATIC void
|
|
|
|
JprofBufferDump()
|
|
|
|
{
|
|
|
|
JprofBuffer->lock();
|
|
|
|
#if DEBUG_CIRCULAR
|
|
|
|
fprintf(stderr,"dumping JP_CIRCULAR buffer, %d of %d bytes\n",
|
|
|
|
JprofBuffer->tail > JprofBuffer->head ?
|
|
|
|
JprofBuffer->tail - JprofBuffer->head :
|
|
|
|
JprofBuffer->buffer_size + JprofBuffer->tail - JprofBuffer->head,
|
|
|
|
JprofBuffer->buffer_size);
|
|
|
|
#endif
|
|
|
|
if (JprofBuffer->tail >= JprofBuffer->head) {
|
|
|
|
write(gLogFD, JprofBuffer->head, JprofBuffer->tail - JprofBuffer->head);
|
|
|
|
} else {
|
|
|
|
write(gLogFD, JprofBuffer->head, &(JprofBuffer->buffer[JprofBuffer->buffer_size]) - JprofBuffer->head);
|
|
|
|
write(gLogFD, JprofBuffer->buffer, JprofBuffer->tail - JprofBuffer->buffer);
|
|
|
|
}
|
|
|
|
JprofBuffer->clear();
|
|
|
|
JprofBuffer->unlock();
|
|
|
|
}
|
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2011-05-15 13:47:48 +04:00
|
|
|
JPROF_STATIC void
|
|
|
|
JprofLog(u_long aTime, void* stack_top, void* top_instr_ptr)
|
2000-01-18 06:43:26 +03:00
|
|
|
{
|
2011-08-12 19:59:17 +04:00
|
|
|
// Static is simply to make debugging tolerable
|
2000-01-18 06:43:26 +03:00
|
|
|
static malloc_log_entry me;
|
|
|
|
|
|
|
|
me.delTime = aTime;
|
2011-05-15 13:47:48 +04:00
|
|
|
me.thread = syscall(SYS_gettid); //gettid();
|
2011-08-12 19:59:17 +04:00
|
|
|
if (was_paused) {
|
|
|
|
me.flags = JP_FIRST_AFTER_PAUSE;
|
|
|
|
was_paused = 0;
|
|
|
|
} else {
|
|
|
|
me.flags = 0;
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2006-10-04 08:42:33 +04:00
|
|
|
CrawlStack(&me, stack_top, top_instr_ptr);
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2000-02-09 17:25:18 +03:00
|
|
|
#ifndef NTO
|
2011-11-08 09:34:40 +04:00
|
|
|
if (circular) {
|
|
|
|
JprofBufferAppend(&me);
|
|
|
|
} else {
|
|
|
|
write(gLogFD, &me, JprofEntrySizeof(&me));
|
|
|
|
}
|
2000-02-09 17:25:18 +03:00
|
|
|
#else
|
2011-11-08 09:34:40 +04:00
|
|
|
printf("Neutrino is missing the pcs member of malloc_log_entry!! \n");
|
2000-02-09 17:25:18 +03:00
|
|
|
#endif
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
static int realTime;
|
2000-02-03 03:35:53 +03:00
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
/* Lets interrupt at 10 Hz. This is so my log files don't get too large.
|
|
|
|
* This can be changed to a faster value latter. This timer is not
|
|
|
|
* programmed to reset, even though it is capable of doing so. This is
|
|
|
|
* to keep from getting interrupts from inside of the handler.
|
|
|
|
*/
|
2006-08-29 00:12:37 +04:00
|
|
|
static void startSignalCounter(unsigned long millisec)
|
2000-01-18 06:43:26 +03:00
|
|
|
{
|
|
|
|
struct itimerval tvalue;
|
|
|
|
|
|
|
|
tvalue.it_interval.tv_sec = 0;
|
|
|
|
tvalue.it_interval.tv_usec = 0;
|
2006-08-29 00:12:37 +04:00
|
|
|
tvalue.it_value.tv_sec = millisec/1000;
|
|
|
|
tvalue.it_value.tv_usec = (millisec%1000)*1000;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2000-02-03 03:35:53 +03:00
|
|
|
if (realTime) {
|
2014-01-06 19:07:38 +04:00
|
|
|
setitimer(ITIMER_REAL, &tvalue, nullptr);
|
2000-02-03 03:35:53 +03:00
|
|
|
} else {
|
2014-01-06 19:07:38 +04:00
|
|
|
setitimer(ITIMER_PROF, &tvalue, nullptr);
|
2000-02-03 03:35:53 +03:00
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
|
2013-04-19 17:54:16 +04:00
|
|
|
static long timerMilliSec = 50;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
#if defined(linux)
|
|
|
|
static int setupRTCSignals(int hz, struct sigaction *sap)
|
|
|
|
{
|
|
|
|
/* global */ rtcFD = open("/dev/rtc", O_RDONLY);
|
|
|
|
if (rtcFD < 0) {
|
|
|
|
perror("JPROF_RTC setup: open(\"/dev/rtc\", O_RDONLY)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-06 19:07:38 +04:00
|
|
|
if (sigaction(SIGIO, sap, nullptr) == -1) {
|
2003-11-01 10:34:18 +03:00
|
|
|
perror("JPROF_RTC setup: sigaction(SIGIO)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(rtcFD, RTC_IRQP_SET, hz) == -1) {
|
|
|
|
perror("JPROF_RTC setup: ioctl(/dev/rtc, RTC_IRQP_SET, $JPROF_RTC_HZ)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(rtcFD, RTC_PIE_ON, 0) == -1) {
|
|
|
|
perror("JPROF_RTC setup: ioctl(/dev/rtc, RTC_PIE_ON)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(rtcFD, F_SETSIG, 0) == -1) {
|
|
|
|
perror("JPROF_RTC setup: fcntl(/dev/rtc, F_SETSIG, 0)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(rtcFD, F_SETOWN, getpid()) == -1) {
|
|
|
|
perror("JPROF_RTC setup: fcntl(/dev/rtc, F_SETOWN, getpid())");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-10 01:31:56 +03:00
|
|
|
static int enableRTCSignals(bool enable)
|
2003-11-01 10:34:18 +03:00
|
|
|
{
|
2006-08-01 19:50:16 +04:00
|
|
|
static bool enabled = false;
|
|
|
|
if (enabled == enable) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
enabled = enable;
|
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
int flags = fcntl(rtcFD, F_GETFL);
|
|
|
|
if (flags < 0) {
|
|
|
|
perror("JPROF_RTC setup: fcntl(/dev/rtc, F_GETFL)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-10 01:31:56 +03:00
|
|
|
if (enable) {
|
|
|
|
flags |= FASYNC;
|
|
|
|
} else {
|
|
|
|
flags &= ~FASYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(rtcFD, F_SETFL, flags) == -1) {
|
|
|
|
if (enable) {
|
|
|
|
perror("JPROF_RTC setup: fcntl(/dev/rtc, F_SETFL, flags | FASYNC)");
|
|
|
|
} else {
|
|
|
|
perror("JPROF_RTC setup: fcntl(/dev/rtc, F_SETFL, flags & ~FASYNC)");
|
|
|
|
}
|
2003-11-01 10:34:18 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-15 13:47:48 +04:00
|
|
|
JPROF_STATIC void StackHook(
|
2000-01-18 06:43:26 +03:00
|
|
|
int signum,
|
|
|
|
siginfo_t *info,
|
2006-10-04 08:42:33 +04:00
|
|
|
void *ucontext)
|
2000-01-18 06:43:26 +03:00
|
|
|
{
|
|
|
|
static struct timeval tFirst;
|
|
|
|
static int first=1;
|
2006-08-29 00:12:37 +04:00
|
|
|
size_t millisec = 0;
|
2003-11-01 10:34:18 +03:00
|
|
|
|
|
|
|
#if defined(linux)
|
|
|
|
if (rtcHz && pthread_self() != main_thread) {
|
|
|
|
// Only collect stack data on the main thread, for now.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
if(first && !(first=0)) {
|
2003-11-01 10:34:18 +03:00
|
|
|
puts("Jprof: received first signal");
|
|
|
|
#if defined(linux)
|
|
|
|
if (rtcHz) {
|
2004-03-10 01:31:56 +03:00
|
|
|
enableRTCSignals(true);
|
2003-11-01 10:34:18 +03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
gettimeofday(&tFirst, 0);
|
2006-08-29 00:12:37 +04:00
|
|
|
millisec = 0;
|
2003-11-01 10:34:18 +03:00
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
} else {
|
2006-08-01 19:50:16 +04:00
|
|
|
#if defined(linux)
|
|
|
|
if (rtcHz) {
|
|
|
|
enableRTCSignals(true);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct timeval tNow;
|
|
|
|
gettimeofday(&tNow, 0);
|
|
|
|
double usec = 1e6*(tNow.tv_sec - tFirst.tv_sec);
|
|
|
|
usec += (tNow.tv_usec - tFirst.tv_usec);
|
2006-08-29 00:17:01 +04:00
|
|
|
millisec = static_cast<size_t>(usec*1e-3);
|
2006-08-01 19:50:16 +04:00
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:42:33 +04:00
|
|
|
gregset_t &gregs = ((ucontext_t*)ucontext)->uc_mcontext.gregs;
|
|
|
|
#ifdef __x86_64__
|
2011-05-15 13:47:48 +04:00
|
|
|
JprofLog(millisec, (void*)gregs[REG_RSP], (void*)gregs[REG_RIP]);
|
2000-02-01 00:32:10 +03:00
|
|
|
#else
|
2011-05-15 13:47:48 +04:00
|
|
|
JprofLog(millisec, (void*)gregs[REG_ESP], (void*)gregs[REG_EIP]);
|
2000-02-01 00:32:10 +03:00
|
|
|
#endif
|
2006-10-04 08:42:33 +04:00
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
if (!rtcHz)
|
2013-04-19 17:54:16 +04:00
|
|
|
startSignalCounter(timerMilliSec);
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
|
2006-03-22 10:29:45 +03:00
|
|
|
NS_EXPORT_(void) setupProfilingStuff(void)
|
2000-01-18 06:43:26 +03:00
|
|
|
{
|
|
|
|
static int gFirstTime = 1;
|
2011-08-12 19:59:20 +04:00
|
|
|
char filename[2048]; // XXX fix
|
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
if(gFirstTime && !(gFirstTime=0)) {
|
|
|
|
int startTimer = 1;
|
|
|
|
int doNotStart = 1;
|
2000-01-25 05:46:30 +03:00
|
|
|
int firstDelay = 0;
|
2001-10-06 10:37:41 +04:00
|
|
|
int append = O_TRUNC;
|
2000-01-18 06:43:26 +03:00
|
|
|
char *tst = getenv("JPROF_FLAGS");
|
|
|
|
|
|
|
|
/* Options from JPROF_FLAGS environment variable:
|
2000-02-03 03:35:53 +03:00
|
|
|
* JP_DEFER -> Wait for a SIGPROF (or SIGALRM, if JP_REALTIME
|
|
|
|
* is set) from userland before starting
|
2000-01-18 06:43:26 +03:00
|
|
|
* to generate them internally
|
|
|
|
* JP_START -> Install the signal handler
|
2001-02-01 03:29:40 +03:00
|
|
|
* JP_PERIOD -> Time between profiler ticks
|
2000-01-25 05:46:30 +03:00
|
|
|
* JP_FIRST -> Extra delay before starting
|
2000-02-03 03:35:53 +03:00
|
|
|
* JP_REALTIME -> Take stack traces in intervals of real time
|
|
|
|
* rather than time used by the process (and the
|
|
|
|
* system for the process). This is useful for
|
|
|
|
* finding time spent by the X server.
|
2001-10-06 10:37:41 +04:00
|
|
|
* JP_APPEND -> Append to jprof-log rather than overwriting it.
|
|
|
|
* This is somewhat risky since it depends on the
|
|
|
|
* address map staying constant across multiple runs.
|
2011-08-12 19:59:20 +04:00
|
|
|
* JP_FILENAME -> base filename to use when saving logs. Note that
|
|
|
|
* this does not affect the mapfile.
|
2011-11-08 09:34:40 +04:00
|
|
|
* JP_CIRCULAR -> use a circular buffer of size N, write/clear on SIGUSR1
|
2011-08-12 19:59:20 +04:00
|
|
|
*
|
|
|
|
* JPROF_SLAVE is set if this is not the first process.
|
2000-01-18 06:43:26 +03:00
|
|
|
*/
|
2011-11-08 09:34:40 +04:00
|
|
|
|
|
|
|
circular = false;
|
|
|
|
|
2000-01-18 06:43:26 +03:00
|
|
|
if(tst) {
|
2000-02-03 17:41:39 +03:00
|
|
|
if(strstr(tst, "JP_DEFER"))
|
|
|
|
{
|
|
|
|
doNotStart = 0;
|
|
|
|
startTimer = 0;
|
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
if(strstr(tst, "JP_START")) doNotStart = 0;
|
2000-02-03 03:35:53 +03:00
|
|
|
if(strstr(tst, "JP_REALTIME")) realTime = 1;
|
2001-10-06 10:37:41 +04:00
|
|
|
if(strstr(tst, "JP_APPEND")) append = O_APPEND;
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
char *delay = strstr(tst,"JP_PERIOD=");
|
|
|
|
if(delay) {
|
2014-01-06 19:07:38 +04:00
|
|
|
double tmp = strtod(delay+strlen("JP_PERIOD="), nullptr);
|
2011-05-15 13:47:48 +04:00
|
|
|
if (tmp>=1e-3) {
|
2013-04-19 17:54:16 +04:00
|
|
|
timerMilliSec = static_cast<unsigned long>(1000 * tmp);
|
2011-05-15 13:47:48 +04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"JP_PERIOD of %g less than 0.001 (1ms), using 1ms\n",
|
|
|
|
tmp);
|
2013-04-19 17:54:16 +04:00
|
|
|
timerMilliSec = 1;
|
2011-05-15 13:47:48 +04:00
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
2000-01-25 05:46:30 +03:00
|
|
|
|
2011-11-08 09:34:40 +04:00
|
|
|
char *circular_op = strstr(tst,"JP_CIRCULAR=");
|
|
|
|
if(circular_op) {
|
|
|
|
size_t size = atol(circular_op+strlen("JP_CIRCULAR="));
|
|
|
|
if (size < 1000) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"JP_CIRCULAR of %d less than 1000, using 10000\n",
|
|
|
|
size);
|
|
|
|
size = 10000;
|
|
|
|
}
|
|
|
|
JprofBufferInit(size);
|
|
|
|
fprintf(stderr,"JP_CIRCULAR buffer of %d bytes\n",size);
|
|
|
|
circular = true;
|
|
|
|
}
|
|
|
|
|
2000-01-25 05:46:30 +03:00
|
|
|
char *first = strstr(tst, "JP_FIRST=");
|
|
|
|
if(first) {
|
2011-05-15 13:47:48 +04:00
|
|
|
firstDelay = atol(first+strlen("JP_FIRST="));
|
2000-01-25 05:46:30 +03:00
|
|
|
}
|
2003-11-01 10:34:18 +03:00
|
|
|
|
|
|
|
char *rtc = strstr(tst, "JP_RTC_HZ=");
|
|
|
|
if (rtc) {
|
|
|
|
#if defined(linux)
|
2011-05-15 13:47:48 +04:00
|
|
|
rtcHz = atol(rtc+strlen("JP_RTC_HZ="));
|
2013-04-19 17:54:16 +04:00
|
|
|
timerMilliSec = 0; /* This makes JP_FIRST work right. */
|
2007-01-26 07:31:57 +03:00
|
|
|
realTime = 1; /* It's the _R_TC and all. ;) */
|
2003-11-01 10:34:18 +03:00
|
|
|
|
|
|
|
#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
|
|
|
|
|
|
|
|
if (!IS_POWER_OF_TWO(rtcHz) || rtcHz < 2) {
|
|
|
|
fprintf(stderr, "JP_RTC_HZ must be power of two and >= 2, "
|
|
|
|
"but %d was provided; using default of 2048\n",
|
|
|
|
rtcHz);
|
|
|
|
rtcHz = 2048;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fputs("JP_RTC_HZ found, but RTC profiling only supported on "
|
|
|
|
"Linux!\n", stderr);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2011-08-12 19:59:20 +04:00
|
|
|
char *f = strstr(tst,"JP_FILENAME=");
|
|
|
|
if (f)
|
|
|
|
f = f + strlen("JP_FILENAME=");
|
|
|
|
else
|
|
|
|
f = M_LOGFILE;
|
|
|
|
|
|
|
|
char *is_slave = getenv("JPROF_SLAVE");
|
|
|
|
if (!is_slave)
|
|
|
|
setenv("JPROF_SLAVE","", 0);
|
|
|
|
|
|
|
|
int pid = syscall(SYS_gettid); //gettid();
|
|
|
|
if (is_slave)
|
|
|
|
snprintf(filename,sizeof(filename),"%s-%d",f,pid);
|
|
|
|
else
|
|
|
|
snprintf(filename,sizeof(filename),"%s",f);
|
|
|
|
|
|
|
|
// XXX FIX! inherit current capture state!
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!doNotStart) {
|
|
|
|
|
|
|
|
if(gLogFD<0) {
|
2011-08-12 19:59:20 +04:00
|
|
|
gLogFD = open(filename, O_CREAT | O_WRONLY | append, 0666);
|
2000-01-18 06:43:26 +03:00
|
|
|
if(gLogFD<0) {
|
|
|
|
fprintf(stderr, "Unable to create " M_LOGFILE);
|
|
|
|
perror(":");
|
|
|
|
} else {
|
|
|
|
struct sigaction action;
|
|
|
|
sigset_t mset;
|
|
|
|
|
|
|
|
// Dump out the address map when we terminate
|
2011-06-24 08:01:49 +04:00
|
|
|
RegisterJprofShutdown();
|
2000-01-18 06:43:26 +03:00
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
main_thread = pthread_self();
|
2011-05-15 13:47:48 +04:00
|
|
|
//fprintf(stderr,"jprof: main_thread = %u\n",
|
|
|
|
// (unsigned int)main_thread);
|
2003-11-01 10:34:18 +03:00
|
|
|
|
2011-11-08 09:34:40 +04:00
|
|
|
// FIX! probably should block these against each other
|
|
|
|
// Very unlikely.
|
2000-01-18 06:43:26 +03:00
|
|
|
sigemptyset(&mset);
|
2014-01-06 19:07:38 +04:00
|
|
|
action.sa_handler = nullptr;
|
2000-01-18 06:43:26 +03:00
|
|
|
action.sa_sigaction = StackHook;
|
|
|
|
action.sa_mask = mset;
|
|
|
|
action.sa_flags = SA_RESTART | SA_SIGINFO;
|
2003-11-01 10:34:18 +03:00
|
|
|
#if defined(linux)
|
|
|
|
if (rtcHz) {
|
|
|
|
if (!setupRTCSignals(rtcHz, &action)) {
|
|
|
|
fputs("jprof: Error initializing RTC, NOT "
|
|
|
|
"profiling\n", stderr);
|
|
|
|
return;
|
|
|
|
}
|
2007-01-26 07:31:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rtcHz || firstDelay != 0)
|
2003-11-01 10:34:18 +03:00
|
|
|
#endif
|
2011-08-12 19:59:17 +04:00
|
|
|
{
|
|
|
|
if (realTime) {
|
2014-01-06 19:07:38 +04:00
|
|
|
sigaction(SIGALRM, &action, nullptr);
|
2011-08-12 19:59:17 +04:00
|
|
|
}
|
2003-11-01 10:34:18 +03:00
|
|
|
}
|
2011-08-12 19:59:17 +04:00
|
|
|
// enable PROF in all cases to simplify JP_DEFER/pause/restart
|
2014-01-06 19:07:38 +04:00
|
|
|
sigaction(SIGPROF, &action, nullptr);
|
2000-02-03 03:35:53 +03:00
|
|
|
|
|
|
|
// make it so a SIGUSR1 will stop the profiling
|
|
|
|
// Note: It currently does not close the logfile.
|
|
|
|
// This could be configurable (so that it could
|
|
|
|
// later be reopened).
|
|
|
|
|
|
|
|
struct sigaction stop_action;
|
|
|
|
stop_action.sa_handler = EndProfilingHook;
|
|
|
|
stop_action.sa_mask = mset;
|
|
|
|
stop_action.sa_flags = SA_RESTART;
|
2014-01-06 19:07:38 +04:00
|
|
|
sigaction(SIGUSR1, &stop_action, nullptr);
|
2000-02-03 03:35:53 +03:00
|
|
|
|
2011-11-08 09:34:40 +04:00
|
|
|
// make it so a SIGUSR2 will clear the circular buffer
|
|
|
|
|
|
|
|
stop_action.sa_handler = ClearProfilingHook;
|
|
|
|
stop_action.sa_mask = mset;
|
|
|
|
stop_action.sa_flags = SA_RESTART;
|
2014-01-06 19:07:38 +04:00
|
|
|
sigaction(SIGUSR2, &stop_action, nullptr);
|
2011-11-08 09:34:40 +04:00
|
|
|
|
2003-11-01 10:34:18 +03:00
|
|
|
printf("Jprof: Initialized signal handler and set "
|
|
|
|
"timer for %lu %s, %d s "
|
|
|
|
"initial delay\n",
|
2013-04-19 17:54:16 +04:00
|
|
|
rtcHz ? rtcHz : timerMilliSec,
|
2003-11-01 10:34:18 +03:00
|
|
|
rtcHz ? "Hz" : "ms",
|
|
|
|
firstDelay);
|
2000-01-18 06:43:26 +03:00
|
|
|
|
|
|
|
if(startTimer) {
|
2003-11-01 10:34:18 +03:00
|
|
|
#if defined(linux)
|
2007-01-26 07:31:57 +03:00
|
|
|
/* If we have an initial delay we can just use
|
|
|
|
startSignalCounter to set up a timer to fire the
|
|
|
|
first stackHook after that delay. When that happens
|
|
|
|
we'll go and switch to RTC profiling. */
|
|
|
|
if (rtcHz && firstDelay == 0) {
|
2003-11-01 10:34:18 +03:00
|
|
|
puts("Jprof: enabled RTC signals");
|
2004-03-10 01:31:56 +03:00
|
|
|
enableRTCSignals(true);
|
2003-11-01 10:34:18 +03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
puts("Jprof: started timer");
|
2013-04-19 17:54:16 +04:00
|
|
|
startSignalCounter(firstDelay*1000 + timerMilliSec);
|
2003-11-01 10:34:18 +03:00
|
|
|
}
|
2000-01-18 06:43:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("setupProfilingStuff() called multiple times\n");
|
|
|
|
}
|
|
|
|
}
|