Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
lib: update TTD to use let and const
Browse files Browse the repository at this point in the history
PR-URL: #476
Reviewed-By: Jack Horton <[email protected]>
Reviewed-By: Taylor Woll <[email protected]>
Reviewed-By: Seth Brenith <[email protected]>
Reviewed-By: Jimmy Thomson <[email protected]>
  • Loading branch information
kfarnung committed Mar 7, 2018
1 parent fe578b2 commit c3f8c94
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 61 deletions.
8 changes: 4 additions & 4 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function setupKillAndExit() {
};
}

var ttdSigIntHandler;
let ttdSigIntHandler;
function setupSignalHandlers() {
// Load events module in order to access prototype elements on process like
// process.addListener.
Expand Down Expand Up @@ -237,12 +237,12 @@ function setupSignalHandlers() {
//ENABLE_TTD
if (global.enabledDiagnosticsTrace) {
(function() {
var Signal = process.binding('signal_wrap').Signal;
var wrap = new Signal();
const Signal = process.binding('signal_wrap').Signal;
const wrap = new Signal();

wrap.unref();

var handler = require('trace_mgr').onSigIntHandler;
const handler = require('trace_mgr').onSigIntHandler;
wrap.onsignal = function() {
handler(signalWraps.hasOwnProperty('SIGINT'));
process.emit('SIGINT');
Expand Down
112 changes: 55 additions & 57 deletions lib/trace_mgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

//Disable trace handling inside trace handler to avoid recursion
//Set to true to prevent action till loading is complete -- set to false at end
var reentrantDisable = true;
let reentrantDisable = true;

var process = require('process');

var lazyloadDone = false;
var path;
var fs;
let lazyloadDone = false;
let path;
let fs;

///////////////////////////////
//Trace calls
Expand All @@ -21,7 +19,7 @@ var fs;
*@result 're-entrant' 'disabled', 'no-sample', 'fail', 'success'
*/
function emitTrace(emitKind, optInfo) {
var res = emitTrace_helper(emitKind, optInfo);
const res = emitTrace_helper(emitKind, optInfo);
if (res.flag === 'success') {
//This is an intentional programatic breakpoint
// -- it is only triggerable in --replay-debug mode
Expand All @@ -32,7 +30,7 @@ function emitTrace(emitKind, optInfo) {
exports.emitTrace = emitTrace;

function buildTraceResult(flagv, actionv) {
var realAction = actionv || function() { };
const realAction = actionv || function() { };
return { flag: flagv, action: realAction };
}

Expand Down Expand Up @@ -60,7 +58,7 @@ function emitTrace_helper(emitKind, optInfo) {

//Process a synchronous write action for a unique trace bin
// -- otherwise a async action on a multiple trace bin
var sampleRes = buildTraceResult('no-sample');
let sampleRes = buildTraceResult('no-sample');
if (emitKind === 'emitOnExit' ||
emitKind === 'emitOnException' ||
emitKind === 'emitOnSigInt') {
Expand All @@ -86,13 +84,13 @@ function emitTrace_helper(emitKind, optInfo) {
//Helpers for trace calls
function emitSyncTraceKind(emitKind, optInfo) {
//build up trace name
var traceName = emitKind;
let traceName = emitKind;
if (emitKind === 'emitOnExit') {
traceName += ('_code-' + optInfo);
}

//invoke the trace writer and remote manager if needed
var resolvedPath = createTraceLogTarget(traceName);
const resolvedPath = createTraceLogTarget(traceName);
if (!resolvedPath) {
return buildTraceResult('fail');
}
Expand All @@ -116,19 +114,19 @@ function emitSyncTraceKind(emitKind, optInfo) {

function emitAsyncTraceKind(emitKind) {
//get trace stack and check if we want to emit
var stk = generateFuzzyStack(emitKind);
var entry = checkBinShouldEmit(stk);
const stk = generateFuzzyStack(emitKind);
const entry = checkBinShouldEmit(stk);
if (!entry) {
return buildTraceResult('no-sample');
}

//build up trace name
var traceBucketName = `${emitKind}_${stk.fbin}_bucket-${entry.bucketId}`;
var traceDirName = `trace-${entry.traceCtr}`;
var traceName = path.join(traceBucketName, traceDirName);
const traceBucketName = `${emitKind}_${stk.fbin}_bucket-${entry.bucketId}`;
const traceDirName = `trace-${entry.traceCtr}`;
const traceName = path.join(traceBucketName, traceDirName);

//invoke the trace writer and remote manager if needed
var resolvedPath = createTraceLogTarget(traceName);
const resolvedPath = createTraceLogTarget(traceName);
if (!resolvedPath) {
return buildTraceResult('fail');
}
Expand All @@ -154,17 +152,17 @@ function emitAsyncTraceKind(emitKind) {

//create a directory for emitting the trace (if possible) and return it
function createTraceLogTarget(tracename) {
var traceRootDir = emitOptions.localTraceDirectory ||
const traceRootDir = emitOptions.localTraceDirectory ||
path.dirname(process.mainModule.filename);

// Add the PID to the trace name
tracename = `${tracename}_pid${process.pid}`;

var resolvedTracePath =
const resolvedTracePath =
path.resolve(traceRootDir, '_diagnosticTraces', tracename);

//ensure directory exists and is empty...
var ok = ensureTraceTarget(resolvedTracePath);
const ok = ensureTraceTarget(resolvedTracePath);
if (!ok) {
return undefined;
}
Expand All @@ -179,7 +177,7 @@ function ensureTraceTarget(pth) {
return false;
}

var okdir = createTargetDirectory(pth);
const okdir = createTargetDirectory(pth);
if (!okdir) {
return false;
}
Expand All @@ -189,7 +187,7 @@ function ensureTraceTarget(pth) {

function createTargetDirectory(pth) {
//see if it just exists and, if so, just return true
var accessok = fs.constants.R_OK | fs.constants.W_OK | fs.constants.X_OK;
const accessok = fs.constants.R_OK | fs.constants.W_OK | fs.constants.X_OK;
try {
fs.accessSync(pth, accessok);
if (fs.statSync(pth).isDirectory()) {
Expand All @@ -198,9 +196,9 @@ function createTargetDirectory(pth) {
} catch (ei) { }

//walk up the directory to see where the first valid part of the path is
var prefixPath = pth;
var suffixPaths = [];
var baseFound = false;
let prefixPath = pth;
const suffixPaths = [];
let baseFound = false;
do {
//check for bad prefix
if (prefixPath === path.dirname(prefixPath)) {
Expand Down Expand Up @@ -233,14 +231,14 @@ function createTargetDirectory(pth) {

function deleteTargetDirectoryContents(pth) {
try {
var items = fs.readdirSync(pth);
const items = fs.readdirSync(pth);
for (var i = 0; i < items.length; i++) {
var fpath = path.resolve(pth, items[i]);
var stats = fs.lstatSync(fpath);
const fpath = path.resolve(pth, items[i]);
const stats = fs.lstatSync(fpath);
if (stats.isFile()) {
fs.unlinkSync(fpath);
} else if (stats.isDirectory()) {
var recok = deleteTargetDirectoryContents(fpath);
const recok = deleteTargetDirectoryContents(fpath);
if (!recok) {
return false;
}
Expand All @@ -262,7 +260,7 @@ function deleteTargetDirectoryContents(pth) {
function updateGlobalSampleStats(eventKind) {
currentSampleRate[eventKind] *= emitOptions.backoffFactors[eventKind];

var updateTime = new Date();
const updateTime = new Date();
emitMinTimeValue.emitOnLogWarn = new Date(updateTime);
emitMinTimeValue.emitOnLogError = new Date(updateTime);

Expand All @@ -274,7 +272,7 @@ function updateGlobalSampleStats(eventKind) {
///////////////////////////////
//Trace emit manager code

var emitOptions = {
const emitOptions = {
emitOnExit: 'error', //emit a trace on exit -- off, error, all
emitOnException: true, //emit a trace on uncaught execption
emitOnSigInt: true, //emit a trace on sigint
Expand Down Expand Up @@ -313,30 +311,30 @@ var emitOptions = {
remoteTraceManagerObj: undefined //manager object for remote trace support
};

var callStackEmitInfoMap = new Map();
var bucketCtr = 0;
const callStackEmitInfoMap = new Map();
let bucketCtr = 0;

var emitMinTimeValue = {
const emitMinTimeValue = {
emitOnLogWarn: new Date(0),
emitOnLogError: new Date(0),
emitOnAssert: new Date(0)
};

var currentSampleRate = {};
for (var srp in emitOptions.initialRates) {
const currentSampleRate = {};
for (const srp in emitOptions.initialRates) {
currentSampleRate[srp] = emitOptions.initialRates[srp];
}

/*
*Update emitOptions from the given options object
*/
function setOptions(optionsObj) {
for (var opt in optionsObj) {
for (const opt in optionsObj) {
//TODO: need more error checking on the validity of the option values
emitOptions[opt] = optionsObj[opt];
}

for (var srp in emitOptions.initialRates) {
for (const srp in emitOptions.initialRates) {
currentSampleRate[srp] = emitOptions.initialRates[srp];
}
}
Expand All @@ -363,7 +361,7 @@ function checkGlobalShouldEmit(emitKind, optInfo) {
return false;
}

var sampleInterval = new Date() - emitMinTimeValue[emitKind];
const sampleInterval = new Date() - emitMinTimeValue[emitKind];

//Don't sample too often no matter what (or we can basically live lock)
if (sampleInterval < emitOptions.globalMinInterval) {
Expand All @@ -372,8 +370,8 @@ function checkGlobalShouldEmit(emitKind, optInfo) {

//Relax our global rate if it has been a while
if (sampleInterval >= emitOptions.globalBackoffCancelInterval) {
var currRate = currentSampleRate[emitKind];
var blRate = emitOptions.baselineRates[emitKind];
const currRate = currentSampleRate[emitKind];
const blRate = emitOptions.baselineRates[emitKind];
currentSampleRate[emitKind] = Math.max(currRate, blRate);
}

Expand All @@ -388,15 +386,15 @@ function checkGlobalShouldEmit(emitKind, optInfo) {
*@result if we want to sample return the entry otherwise undefined
*/
function checkBinShouldEmit(fuzzyStack) {
var entry = resolveStackEntry(fuzzyStack);
const entry = resolveStackEntry(fuzzyStack);

//stop sampling after max sampled values -- e.g. we don't need 100 repros
if (entry.traceCtr > emitOptions.binMaxSampled) {
return undefined;
}

//check if we want to sample on this entry -- we don't need every hit on this
var sampleProb = Math.pow(emitOptions.binBackoffFactor, entry.traceCtr);
const sampleProb = Math.pow(emitOptions.binBackoffFactor, entry.traceCtr);
return (Math.random() < sampleProb) ? entry : undefined;
}

Expand All @@ -416,7 +414,7 @@ function resolveStackEntry(fuzzyStack) {
if (!callStackEmitInfoMap.has(fuzzyStack.hash)) {
callStackEmitInfoMap.set(fuzzyStack.hash, []);
}
var stackList = callStackEmitInfoMap.get(fuzzyStack.hash);
const stackList = callStackEmitInfoMap.get(fuzzyStack.hash);

for (var i = 0; i < stackList.length; ++i) {
if (eqFuzzyStacks(fuzzyStack, stackList[i].stack)) {
Expand Down Expand Up @@ -450,7 +448,7 @@ function directIsAbsoluteW32(pth) {
if (len === 0)
return false;

var code = pth.charCodeAt(0);
let code = pth.charCodeAt(0);
if (code === 47/*/*/ || code === 92/*\*/) {
return true;
} else if ((code >= 65/*A*/ && code <= 90/*Z*/) ||
Expand All @@ -470,7 +468,7 @@ function directIsAbsolutePosix(pth) {
return pth.length > 0 && pth.charCodeAt(0) === 47/*/*/;
}

var directIsAbsolute = (process.platform === 'win32') ?
const directIsAbsolute = (process.platform === 'win32') ?
directIsAbsoluteW32 :
directIsAbsolutePosix;

Expand All @@ -479,7 +477,7 @@ var directIsAbsolute = (process.platform === 'win32') ?
*/
function generateFuzzyStack(eventKind) {
//Create an array of the file/lines for user space code in the call stack
var errstk = new Error()
let errstk = new Error()
.stack
.split('\n')
.slice(1)
Expand All @@ -490,14 +488,14 @@ function generateFuzzyStack(eventKind) {
return directIsAbsolute(frame);
});

var lastframe = errstk[errstk.length - 1];
var fname = lastframe.substr(lastframe.lastIndexOf(path.sep) + 1)
const lastframe = errstk[errstk.length - 1];
const fname = lastframe.substr(lastframe.lastIndexOf(path.sep) + 1)
.replace('.js:', '_line-')
.replace(':', '_column-');

//Identify which frames are recursive (appear multiple times in the stack)
var recFrames = new Map();
var hasRecFrames = false;
const recFrames = new Map();
let hasRecFrames = false;
for (var i = 0; i < errstk.length; ++i) {
if (recFrames.has(errstk[i])) {
hasRecFrames = true;
Expand All @@ -509,13 +507,13 @@ function generateFuzzyStack(eventKind) {

if (hasRecFrames) {
//Compress any recursive frames
var cpos = 0;
var fpos = 0;
let cpos = 0;
let fpos = 0;
while (fpos < errstk.length) {
if (recFrames.get(errstk[fpos])) {
var recArray = [];
var spanpos = fpos;
var spanend = errstk.lastIndexOf(errstk[fpos]);
const recArray = [];
let spanpos = fpos;
let spanend = errstk.lastIndexOf(errstk[fpos]);
while (spanpos <= spanend) {
if (recArray.indexOf(errstk[spanpos]) === -1) {
recArray.push(errstk[spanpos]);
Expand All @@ -540,7 +538,7 @@ function generateFuzzyStack(eventKind) {
errstk = errstk.slice(0, cpos);
}

var chash = 5381;
let chash = 5381;
for (i = 0; i < errstk.length; ++i) {
if (Array.isArray(errstk[i])) {
for (var j = 0; j < errstk[i].length; ++j) {
Expand Down

0 comments on commit c3f8c94

Please sign in to comment.