libc/winsup/cygwin/profil.c

256 lines
6.2 KiB
C
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* profil.c -- win32 profil.c equivalent
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
/*
* This file is taken from Cygwin distribution. Please keep it in sync.
* The differences should be within __MINGW32__ guard.
*/
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <stdio.h>
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
#include <sys/cygwin.h>
2000-02-17 20:38:33 +01:00
#include <sys/types.h>
#include <errno.h>
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
#include <pthread.h>
#include "profil.h"
2000-02-17 20:38:33 +01:00
#define SLEEPTIME (1000 / PROF_HZ)
/* global profinfo for profil() call */
static struct profinfo prof;
/* Get the pc for thread THR */
static size_t
2000-02-17 20:38:33 +01:00
get_thrpc (HANDLE thr)
{
CONTEXT ctx;
size_t pc;
2000-02-17 20:38:33 +01:00
int res;
res = SuspendThread (thr);
if (res == -1)
return (size_t) - 1;
2000-02-17 20:38:33 +01:00
ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
pc = (size_t) - 1;
if (GetThreadContext (thr, &ctx)) {
#ifndef _WIN64
2000-02-17 20:38:33 +01:00
pc = ctx.Eip;
#else
pc = ctx.Rip;
2013-04-23 11:44:36 +02:00
#endif
}
2000-02-17 20:38:33 +01:00
ResumeThread (thr);
return pc;
}
/* Display cell of profile buffer */
#if 0
static void
print_prof (struct profinfo *p)
{
printf ("profthr %x\ttarget thr %x\n", p->profthr, p->targthr);
printf ("pc: %x - %x\n", p->lowpc, p->highpc);
printf ("scale: %x\n", p->scale);
return;
}
#endif
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
/* Every time we wake up sample the main thread's pc to hash into the cell
in the profile buffer ARG. Then all other pthreads' pc's are sampled. */
2000-02-17 20:38:33 +01:00
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
static void
profthr_byhandle (HANDLE thr)
{
size_t pc;
/* Sample the pc of the thread indicated by thr; bail if anything amiss. */
if (thr == INVALID_HANDLE_VALUE)
return;
pc = get_thrpc (thr);
if (pc == -1)
return;
/* Code assumes there's only one profinfo in play: the static prof above. */
if (pc >= prof.lowpc && pc < prof.highpc)
{
size_t idx = PROFIDX (pc, prof.lowpc, prof.scale);
prof.counter[idx]++;
}
}
static void CALLBACK
2000-02-17 20:38:33 +01:00
profthr_func (LPVOID arg)
{
struct profinfo *p = (struct profinfo *) arg;
2000-02-17 20:38:33 +01:00
for (;;)
{
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
/* Record profiling sample for main thread. */
profthr_byhandle (p->targthr);
/* Record profiling samples for other pthreads, if any. */
cygwin_internal (CW_CYGHEAP_PROFTHR_ALL, profthr_byhandle);
2000-02-17 20:38:33 +01:00
#if 0
print_prof (p);
#endif
/* Check quit condition, WAIT_OBJECT_0 or WAIT_TIMEOUT */
if (WaitForSingleObject (p->quitevt, SLEEPTIME) == WAIT_OBJECT_0)
return;
2000-02-17 20:38:33 +01:00
}
}
/* Stop profiling to the profiling buffer pointed to by P. */
static int
profile_off (struct profinfo *p)
{
if (p->profthr)
{
SignalObjectAndWait (p->quitevt, p->profthr, INFINITE, FALSE);
CloseHandle (p->quitevt);
2000-02-17 20:38:33 +01:00
CloseHandle (p->profthr);
}
if (p->targthr)
CloseHandle (p->targthr);
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
p->targthr = 0;
2000-02-17 20:38:33 +01:00
return 0;
}
/* Create a timer thread and pass it a pointer P to the profiling buffer. */
static int
profile_on (struct profinfo *p)
{
DWORD thrid;
2000-02-17 20:38:33 +01:00
/* get handle for this thread */
if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
GetCurrentProcess (), &p->targthr, 0, FALSE,
DUPLICATE_SAME_ACCESS))
{
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
p->targthr = 0;
2000-02-17 20:38:33 +01:00
errno = ESRCH;
return -1;
}
p->quitevt = CreateEvent (NULL, TRUE, FALSE, NULL);
if (!p->quitevt)
{
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
CloseHandle (p->targthr);
p->targthr = 0;
errno = EAGAIN;
return -1;
}
p->profthr = CreateThread (0, 0, (DWORD (WINAPI *)(LPVOID)) profthr_func,
(void *) p, 0, &thrid);
2000-02-17 20:38:33 +01:00
if (!p->profthr)
{
CloseHandle (p->targthr);
CloseHandle (p->quitevt);
2000-02-17 20:38:33 +01:00
p->targthr = 0;
errno = EAGAIN;
return -1;
}
/* Set profiler thread priority to highest to be sure that it gets the
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
processor as soon it requests it (i.e. when the Sleep terminates) to get
the next data sample as quickly as possible. */
SetThreadPriority (p->profthr, THREAD_PRIORITY_TIME_CRITICAL);
2000-02-17 20:38:33 +01:00
return 0;
}
/*
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
* Restart profiling in child after fork.
2000-02-17 20:38:33 +01:00
*
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
* The profiling control info in prof needs to be selectively updated.
* Items counter, lowpc, highpc, and scale are correct as-is. But items
* targthr, profthr, and quitevt need updating because these copied HANDLE
* values are only valid in parent process. We also zero out the sample
* buffer so that samples aren't double-counted if multiple gmon.out files
* are aggregated. We calculate buffer's size from other data in prof.
*/
static void
profile_child (void)
{
/* Bail if this was a fork after profiling turned off or was never on. */
if (!prof.targthr)
return;
/* Figure out how big the sample buffer is and zero it out. */
size_t size = PROFIDX (prof.highpc, prof.lowpc, prof.scale) << 1;
memset ((char *) prof.counter, 0, size);
/* Replace stale HANDLEs in prof and create profiling thread. */
profile_on (&prof);
}
/*
* Start or stop profiling.
*
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
* Profiling data goes into the SAMPLES buffer of size SIZE (which is treated
* as an array of uint16_t of size SIZE/2).
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
*
* Each bin represents a range of pc addresses from OFFSET. The number
2000-02-17 20:38:33 +01:00
* of pc addresses in a bin depends on SCALE. (A scale of 65536 maps
* each bin to two addresses, A scale of 32768 maps each bin to 4 addresses,
* a scale of 1 maps each bin to 128k addreses). Scale may be 1 - 65536,
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
* or zero to turn off profiling.
2000-02-17 20:38:33 +01:00
*/
int
profile_ctl (struct profinfo * p, char *samples, size_t size,
size_t offset, uint32_t scale)
2000-02-17 20:38:33 +01:00
{
2013-04-23 11:44:36 +02:00
size_t maxbin;
2000-02-17 20:38:33 +01:00
if (scale > 65536)
{
errno = EINVAL;
return -1;
}
profile_off (p);
if (scale)
{
memset (samples, 0, size);
memset (p, 0, sizeof *p);
maxbin = size >> 1;
prof.counter = (uint16_t *) samples;
2000-02-17 20:38:33 +01:00
prof.lowpc = offset;
prof.highpc = PROFADDR (maxbin, offset, scale);
prof.scale = scale;
Support profiling of multi-threaded apps. This patch set modifies Cygwin's profiling support to sample PC values of all an app's threads, not just the main thread. There is no change to how profiling is requested: just compile and link the app with "-pg" as usual. The profiling info is dumped into file gmon.out as always. A new facility enabled via the environment variable GMON_OUT_PREFIX. This facility is intended to match an undocumented Linux glibc feature. Exporting the variable with a non-empty value such as "foo" causes the profiling info to go to a file named foo.$pid instead of the default. With that, both resulting processes of a fork() can have their profiling data captured in separate files. gprof already knows how to accumulate data from multiple files if they all pertain to the same app. There is no change to the normal Cygwin execution paths if profiling is not enabled. And when it is enabled, only the one profiling thread per profiled app is doing more work than it used to. * include/sys/cygwin.h: Add CW_CYGHEAP_PROFTHR_ALL. * cygheap.cc (cygheap_profthr_all): New C-callable function that runs cygheap's threadlist handing each pthread's thread handle in turn to profthr_byhandle(). * external.cc (cygwin_internal): Add case CW_CYGHEAP_PROFTHR_ALL. * gmon.c (_mcleanup): Add support for multiple simultaneous gmon.out* files named via environment variable GMON_OUT_PREFIX. * gmon.h (struct gmonparam): Make state decl volatile. * mcount.c (_MCOUNT_DECL): Change stores into gmonparam.state to use Interlocked operations. Add #include "winsup.h", update commentary. * profil.c (profthr_byhandle): New function abstracting out the updating of profile counters based on a thread handle. (profthr_func): Update to call profthr_byhandle() to sample the main thread then call cygheap_profthr_all() indirectly through cygwin_internal(CW_CYGHEAP_PROFTHR_ALL) to sample all other threads. (profile_off): Zero targthr to indicate profiling was turned off. (profile_on): Fix handle leak on failure path. (profile_child): New callback func to restart profiling in child process after a fork if the parent was being profiled. (profile_ctl): Call pthread_atfork() to set profile_child callback. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-02-20 07:58:31 +01:00
/* Set up callback to restart profiling in child after fork. */
pthread_atfork (NULL, NULL, profile_child);
2000-02-17 20:38:33 +01:00
return profile_on (p);
}
return 0;
}
/* Equivalent to unix profil()
Every SLEEPTIME interval, the user's program counter (PC) is examined:
offset is subtracted and the result is multiplied by scale.
2000-02-17 20:38:33 +01:00
The word pointed to by this address is incremented. Buf is unused. */
int
profil (char *samples, size_t size, size_t offset, uint32_t scale)
2000-02-17 20:38:33 +01:00
{
return profile_ctl (&prof, samples, size, offset, scale);
}