-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
feat: persist network thread profile #5628
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bb4d4f
feat: write network thread profile
twoeths b172d05
fix: persist .cpuprofile file
twoeths ff1e500
fix: disconnect inspector session
twoeths bb89ac1
feat: support dirpath and refactor
twoeths 83c289d
chore: fix lint in api
twoeths 3b9337a
feat: specify duration for the api
twoeths 8c3f8df
chore: fix check-types in beacon-node
twoeths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import worker from "node:worker_threads"; | ||
import fs from "node:fs"; | ||
import {createFromProtobuf} from "@libp2p/peer-id-factory"; | ||
import {expose} from "@chainsafe/threads/worker"; | ||
import {chainConfigFromJson, createBeaconConfig} from "@lodestar/config"; | ||
|
@@ -10,6 +11,7 @@ import {Clock} from "../../util/clock.js"; | |
import {wireEventsOnWorkerThread} from "../../util/workerEvents.js"; | ||
import {NetworkEventBus, NetworkEventData, networkEventDirection} from "../events.js"; | ||
import {peerIdToString} from "../../util/peerId.js"; | ||
import {profileNodeJS} from "../../util/profile.js"; | ||
import {getNetworkCoreWorkerMetrics} from "./metrics.js"; | ||
import {NetworkWorkerApi, NetworkWorkerData} from "./types.js"; | ||
import {NetworkCore} from "./networkCore.js"; | ||
|
@@ -147,6 +149,12 @@ const libp2pWorkerApi: NetworkWorkerApi = { | |
dumpGossipPeerScoreStats: () => core.dumpGossipPeerScoreStats(), | ||
dumpDiscv5KadValues: () => core.dumpDiscv5KadValues(), | ||
dumpMeshPeers: () => core.dumpMeshPeers(), | ||
writeProfile: async (dirpath = ".") => { | ||
const profile = await profileNodeJS(); | ||
const filePath = `${dirpath}/network_thread_${new Date().toISOString()}.cpuprofile`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use path.join for concatenation, or path.resolve if you need an absolute path |
||
fs.writeFileSync(filePath, profile); | ||
return filePath; | ||
}, | ||
}; | ||
|
||
expose(libp2pWorkerApi as WorkerModule<keyof NetworkWorkerApi>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {sleep} from "@lodestar/utils"; | ||
|
||
const DEFAULT_PROFILE_DURATION = 10 * 60 * 1000; | ||
|
||
/** | ||
* Take 10m profile of the current thread without promise tracking. | ||
*/ | ||
export async function profileNodeJS(): Promise<string> { | ||
const inspector = await import("node:inspector"); | ||
|
||
// due to some typing issues, not able to use promisify here | ||
return new Promise<string>((resolve, reject) => { | ||
// Start the inspector and connect to it | ||
const session = new inspector.Session(); | ||
session.connect(); | ||
|
||
session.post("Profiler.enable", () => { | ||
session.post("Profiler.start", async () => { | ||
await sleep(DEFAULT_PROFILE_DURATION); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a optional parameter of the route defaulting to 1 epoch |
||
session.post("Profiler.stop", (err, {profile}) => { | ||
if (!err) { | ||
resolve(JSON.stringify(profile)); | ||
} else { | ||
reject(err); | ||
} | ||
|
||
// Detach from the inspector and close the session | ||
session.post("Profiler.disable"); | ||
session.disconnect(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every route uses different styles:
someroute
some-route
some_route
<- let's use this one here to align with beacon APIs https://github.com/ethereum/beacon-APIs/blob/master/beacon-node-oapi.yamlI can PR for the rest after merging this PR