#include "config.h"
#include "SuperSampler.h"
#include "Options.h"
#include <wtf/DataLog.h>
#include <wtf/Lock.h>
#include <wtf/Threading.h>
namespace JSC {
volatile uint32_t g_superSamplerCount;
volatile bool g_superSamplerEnabled;
static Lock lock;
static double in;
static double out;
void initializeSuperSampler()
{
if (!Options::useSuperSampler())
return;
Thread::create(
"JSC Super Sampler",
[] () {
const int sleepQuantum = 3;
const int printingPeriod = 3000;
for (;;) {
for (int ms = 0; ms < printingPeriod; ms += sleepQuantum) {
if (g_superSamplerEnabled) {
LockHolder locker(lock);
if (g_superSamplerCount)
in++;
else
out++;
}
sleep(Seconds::fromMilliseconds(sleepQuantum));
}
printSuperSamplerState();
if (static_cast<int32_t>(g_superSamplerCount) < 0)
dataLog("WARNING: Super sampler undercount detected!\n");
}
});
}
void resetSuperSamplerState()
{
LockHolder locker(lock);
in = 0;
out = 0;
}
void printSuperSamplerState()
{
if (!Options::useSuperSampler())
return;
LockHolder locker(lock);
double percentage = 100.0 * in / (in + out);
if (percentage != percentage)
percentage = 0.0;
dataLog("Percent time behind super sampler flag: ", percentage, "\n");
}
void enableSuperSampler()
{
LockHolder locker(lock);
g_superSamplerEnabled = true;
}
void disableSuperSampler()
{
LockHolder locker(lock);
g_superSamplerEnabled = false;
}
}