Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix profiling when IO_Fork from the IO package is used #2908

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ dnl Define kernel version
dnl


gap_kernel_major_version=5
gap_kernel_major_version=6
gap_kernel_minor_version=0
AC_SUBST([gap_kernel_major_version])
AC_SUBST([gap_kernel_minor_version])
Expand Down
6 changes: 6 additions & 0 deletions doc/ref/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,12 @@ limitations which users should be aware of:
is most obvious when looking at code coverage examples, which will appear
to miss lines of code in files not in a function.
</Item>
<Item>
If the current GAP is forked, using the <C>IO_fork</C> function in the
<Package>IO</Package> package,
a new profile output file will be created for the new child process, with
the process ID of the child attached to the end of the filename.
</Item>
</List>

Profiles are transformed into a human-readable form with
Expand Down
35 changes: 35 additions & 0 deletions src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
#include "modules.h"
#include "plist.h"
#include "stringobj.h"
#include "sysfiles.h"
#include "vars.h"

#include "hpc/thread.h"

#include <sys/time.h> // for gettimeofday
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h> // definition of 'struct rusage'
#endif
Expand Down Expand Up @@ -114,6 +117,8 @@ struct ProfileState
{
// C steam we are writing to
FILE* Stream;
// Filename we are writing to
char filename[GAP_PATH_MAX];
// Did we use 'popen' to open the stream (matters when closing)
int StreamWasPopened;
// Are we currently outputting repeats (false=code coverage)
Expand Down Expand Up @@ -337,6 +342,32 @@ static void fcloseMaybeCompressed(struct ProfileState* ps)
ps->Stream = 0;
}

// When a child is forked off, we force profile information to be stored
// in a new file for the child, to avoid corruption
void InformProfilingThatThisIsAForkedGAP(void)
{
HashLock(&profileState);
if (profileState_Active) {
char filenamecpy[GAP_PATH_MAX];
// Allow 20 chracters to allow space for .%d.gz
const int SUPPORTED_PATH_LEN = GAP_PATH_MAX - 20;
if(strlen(profileState.filename) > SUPPORTED_PATH_LEN) {
Pr("Filename can be at most %d character when forking", SUPPORTED_PATH_LEN, 0L);
SyExit(1);
}
if (endsWithgz(profileState.filename)) {
snprintf(filenamecpy, sizeof(filenamecpy), "%.*s.%d.gz",
SUPPORTED_PATH_LEN, profileState.filename, getpid());
}
else {
snprintf(filenamecpy, sizeof(filenamecpy), "%.*s.%d",
SUPPORTED_PATH_LEN, (char*)profileState.filename, getpid());
}
fcloseMaybeCompressed(&profileState);
fopenMaybeCompressed(filenamecpy, &profileState);
}
}

static inline Int8 CPUmicroseconds(void)
{
#ifdef HAVE_GETRUSAGE
Expand Down Expand Up @@ -576,6 +607,8 @@ void enableAtStartup(char * filename, Int repeats, TickMethod tickMethod)
exit(1);
}

strlcpy(profileState.filename, filename, GAP_PATH_MAX);

ActivateHooks(&profileHooks);

profileState_Active = 1;
Expand Down Expand Up @@ -706,6 +739,8 @@ Obj FuncACTIVATE_PROFILING(Obj self,

fopenMaybeCompressed(CONST_CSTR_STRING(filename), &profileState);

strlcpy(profileState.filename, CONST_CSTR_STRING(filename), GAP_PATH_MAX);

if(profileState.Stream == 0) {
HashUnlock(&profileState);
return Fail;
Expand Down
5 changes: 5 additions & 0 deletions src/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*F * * * * * * * * * * * * * initialize module * * * * * * * * * * * * * * *
*/

// When a child is forked off, we force profile information to be stored
// in a new file for the child, to avoid corruption.
// This function is for use by the IO package
void InformProfilingThatThisIsAForkedGAP(void);


/****************************************************************************
**
Expand Down