Skip to content

Commit

Permalink
Improve detection of whether setitimer() et al are available.
Browse files Browse the repository at this point in the history
In particular, fixes the Cygwin build.  Issue mmp#127.
  • Loading branch information
mmp committed Apr 22, 2017
1 parent d7ede46 commit 957018c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/core/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@
#define PBRT_FORCEINLINE __attribute__((always_inline)) inline
#endif

// Is setitimer() available (and working)?
#if !defined(PBRT_IS_WINDOWS) && !defined(__CYGWIN__)
#define PBRT_HAVE_ITIMER
#endif

#endif // PBRT_CORE_PORT_H
24 changes: 12 additions & 12 deletions src/core/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
#include <type_traits>
#include "parallel.h"
#include "stringprint.h"
#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
#include <sys/time.h>
#endif // !PBRT_IS_WINDOWS
#endif // PBRT_HAVE_ITIMER

namespace pbrt {

Expand All @@ -71,9 +71,9 @@ static std::array<ProfileSample, profileHashSize> profileSamples;

static std::chrono::system_clock::time_point profileStartTime;

#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
static void ReportProfileSample(int, siginfo_t *, void *);
#endif // !PBRT_IS_WINDOWS
#endif // PBRT_HAVE_ITIMER

// Statistics Definitions
void ReportThreadStats() {
Expand Down Expand Up @@ -217,7 +217,7 @@ void InitProfiler() {

profileStartTime = std::chrono::system_clock::now();
// Set timer to periodically interrupt the system for profiling
#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = ReportProfileSample;
Expand All @@ -243,7 +243,7 @@ void SuspendProfiler() { ++profilerSuspendCount; }
void ResumeProfiler() { CHECK_GE(--profilerSuspendCount, 0); }

void ProfilerWorkerThreadInit() {
#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
// The per-thread initialization in the worker threads has to happen
// *before* the profiling signal handler is installed.
CHECK(!profilerRunning || profilerSuspendCount > 0);
Expand All @@ -254,7 +254,7 @@ void ProfilerWorkerThreadInit() {
// happen now, rather than in the signal handler, where this isn't
// allowed.
ProfilerState = ProfToBits(Prof::SceneConstruction);
#endif // !PBRT_IS_WINDOWS
#endif // PBRT_HAVE_ITIMER
}

void ClearProfiler() {
Expand All @@ -266,19 +266,19 @@ void ClearProfiler() {

void CleanupProfiler() {
CHECK(profilerRunning);
#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
static struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value = timer.it_interval;

CHECK_EQ(setitimer(ITIMER_PROF, &timer, NULL), 0)
<< "Timer could not be disabled: " << strerror(errno);
#endif // !PBRT_IS_WINDOWS
#endif // PBRT_HAVE_ITIMER
profilerRunning = false;
}

#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
static void ReportProfileSample(int, siginfo_t *, void *) {
if (profilerSuspendCount > 0) return;
if (ProfilerState == 0) return; // A ProgressReporter thread, most likely.
Expand All @@ -296,7 +296,7 @@ static void ReportProfileSample(int, siginfo_t *, void *) {
profileSamples[h].profilerState = ProfilerState;
++profileSamples[h].count;
}
#endif // !PBRT_IS_WINDOWS
#endif // PBRT_HAVE_ITIMER

static std::string timeString(float pct, std::chrono::system_clock::time_point now) {
pct /= 100.; // remap passed value to to [0,1]
Expand All @@ -316,7 +316,7 @@ static std::string timeString(float pct, std::chrono::system_clock::time_point n
}

void ReportProfilerResults(FILE *dest) {
#ifndef PBRT_IS_WINDOWS
#ifdef PBRT_HAVE_ITIMER
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();

PBRT_CONSTEXPR int NumProfCategories = (int)Prof::NumProfCategories;
Expand Down

0 comments on commit 957018c

Please sign in to comment.