Skip to content

Commit

Permalink
SQUASH: generate process tree using processinfo index
Browse files Browse the repository at this point in the history
Also, remove some unnecessary fields from process infos
  • Loading branch information
isaacs committed Apr 6, 2019
1 parent 2ffe591 commit a8ecf7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bin/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ config.isChildProcess = true
config._processInfo = {
pid: process.pid,
ppid: process.ppid,
parent: process.env.NYC_PROCESS_ID,
parent: process.env.NYC_PROCESS_ID || null,
root: process.env.NYC_ROOT_ID
}
if (process.env.NYC_PROCESSINFO_EXTERNAL_ID) {
Expand Down
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,7 @@ NYC.prototype.writeProcessIndex = function () {
// build the actual index!
const index = infos.reduce((index, info) => {
index.processes[info.uuid] = {}
if (info.parent) {
index.processes[info.uuid].parent = info.parent
}
index.processes[info.uuid].parent = info.parent
if (info.externalId) {
if (index.externalIds[info.externalId]) {
throw new Error(`External ID ${info.externalId} used by multiple processes`)
Expand Down Expand Up @@ -526,19 +524,25 @@ NYC.prototype._checkCoverage = function (summary, thresholds, file) {
}

NYC.prototype._loadProcessInfos = function () {
var _this = this
var files = fs.readdirSync(this.processInfoDirectory())

return files.filter(f => f !== 'index.json').map(function (f) {
return fs.readdirSync(this.processInfoDirectory()).map(f => {
let data
try {
return new ProcessInfo(JSON.parse(fs.readFileSync(
path.resolve(_this.processInfoDirectory(), f),
data = JSON.parse(fs.readFileSync(
path.resolve(this.processInfoDirectory(), f),
'utf-8'
)))
))
} catch (e) { // handle corrupt JSON output.
return {}
return null
}
})
if (f !== 'index.json') {
data.nodes = []
data = new ProcessInfo(data)
}
return { file: path.basename(f, '.json'), data: data }
}).filter(Boolean).reduce((infos, info) => {
infos[info.file] = info.data
return infos
}, {})
}

NYC.prototype.eachReport = function (filenames, iterator, baseDirectory) {
Expand Down
33 changes: 11 additions & 22 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,19 @@ Object.defineProperty(ProcessInfo.prototype, 'label', {
})

ProcessInfo.buildProcessTree = function (infos) {
var treeRoot = new ProcessInfo({ _label: 'nyc', nodes: [] })
var nodes = { }

infos = infos.sort(function (a, b) {
return a.time - b.time
})

infos.forEach(function (p) {
p.nodes = []
nodes[p.root + ':' + p.pid] = p
})

infos.forEach(function (p) {
if (!p.ppid) {
return
const treeRoot = new ProcessInfo({ _label: 'nyc', nodes: [] })
const index = infos.index
for (const id in index.processes) {
const node = infos[id]
if (!node) {
throw new Error(`Invalid entry in processinfo index: ${id}`)
}

var parent = nodes[p.root + ':' + p.ppid]
if (!parent) {
parent = treeRoot
const idx = index.processes[id]
node.nodes = idx.children.map(id => infos[id])
if (!node.parent) {
treeRoot.nodes.push(node)
}

parent.nodes.push(p)
})
}

return treeRoot
}
Expand Down

0 comments on commit a8ecf7f

Please sign in to comment.