-
Notifications
You must be signed in to change notification settings - Fork 47
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
Profile code #22
Open
gdanezis
wants to merge
3
commits into
main
Choose a base branch
from
profile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Profile code #22
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[build] | ||
rustflags = [ | ||
"-C", | ||
"force-frame-pointers=y", | ||
] |
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,2 +1,5 @@ | ||
[workspace] | ||
members = ["store", "crypto", "network", "mempool", "consensus", "node"] | ||
members = ["store", "crypto", "network", "mempool", "consensus", "node", "profile"] | ||
|
||
[profile.release] | ||
debug = true |
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,179 @@ | ||
import re | ||
import sys | ||
|
||
log_regexp = "^\[.*T(.*):(.*):(.*)\.(.*)Z DEBUG .*\] <APROF> (.*)$" | ||
log_lines = re.findall(log_regexp, open(sys.argv[1]).read(), re.M) | ||
|
||
print(f"Log lines {len(log_lines)}") | ||
|
||
task_names = {} | ||
task_parents = {} | ||
task_wake = {} | ||
task_states = {} | ||
|
||
|
||
class TaskState: | ||
|
||
def __init__(self, no, time): | ||
self.no = no | ||
self.tot_time = 0 | ||
self.blocked_time = 0 | ||
self.pending_time = 0 | ||
self.running_time = 0 | ||
|
||
# inner | ||
self.prev_time = time | ||
self.can_work = True | ||
self.running = False | ||
self.never_wake = True | ||
self.calls = 0 | ||
|
||
def node_label(self): | ||
name = self.name.split(":")[0] | ||
if self.never_wake or self.calls == 0 or not self.to_print(): | ||
return name | ||
|
||
per_call = (self.running_time + self.pending_time) / self.calls | ||
return f"{name} | {self.running_time}ms / {self.calls} | ({int(per_call*1000):}us P:{self.pending_time / (self.running_time + self.pending_time):.2%})" | ||
|
||
def to_print(self): | ||
return (self.running_time + self.pending_time > 10) or self.never_wake | ||
|
||
def summary(self): | ||
return f"R:{self.running_time:6} P:{self.pending_time:6} B:{self.blocked_time:6} {self.name} " | ||
|
||
def name(self, name): | ||
self.name = name | ||
|
||
def resume(self, time): | ||
assert not self.running | ||
self.calls += 1 | ||
period = time - self.prev_time | ||
if self.can_work: | ||
self.pending_time += period | ||
else: | ||
self.blocked_time += period | ||
self.tot_time += period | ||
|
||
# reset | ||
self.can_work = False | ||
self.running = True | ||
self.prev_time = time | ||
|
||
def pause(self, time): | ||
assert self.running | ||
period = time - self.prev_time | ||
self.running_time += period | ||
self.tot_time += period | ||
|
||
# reset | ||
self.running = False | ||
self.prev_time = time | ||
|
||
def signal(self, time): | ||
self.never_wake = False | ||
if self.can_work: | ||
return | ||
self.can_work = True | ||
if not self.running: | ||
period = time - self.prev_time | ||
self.blocked_time += period | ||
self.tot_time += period | ||
|
||
# reset | ||
self.prev_time = time | ||
|
||
|
||
for (H,M,S,Mill,line) in log_lines: | ||
# print((H,M,S,Mill,line)) | ||
time_ms = int(H)*60*60*1000 + int(M)*60*1000 + int(S)*1000 + int(Mill) | ||
|
||
# Task creation | ||
if line[:4] == "Task": | ||
# Define a task | ||
match_obj = re.match("Task (.*) from (.*) defined (.*)", line) | ||
task_no = match_obj.group(1) | ||
parent_no = match_obj.group(2) | ||
if parent_no == "None": | ||
parent_no = None | ||
else: | ||
# Strip the Some(*) | ||
parent_no = parent_no[5:-1] | ||
|
||
source = match_obj.group(3) | ||
|
||
task_names[task_no] = f"{source}-{task_no}" | ||
task_parents[task_no] = parent_no | ||
|
||
if task_no not in task_states: | ||
task_states[task_no] = TaskState(task_no, time_ms) | ||
task_states[task_no].name(task_names[task_no]) | ||
|
||
# Wake relations | ||
if line[:4] == "Wake": | ||
match_obj = re.match("Wake: (.*) -> (.*)", line) | ||
source = match_obj.group(1) | ||
if source == "None": | ||
source = None | ||
else: | ||
source = source[5:-1] | ||
target = match_obj.group(2) | ||
|
||
pair = (source, target) | ||
task_states[target].signal(time_ms) | ||
if pair not in task_wake: | ||
task_wake[pair] = 1 | ||
else: | ||
task_wake[pair] += 1 | ||
|
||
if line[:4] == "Paus": | ||
task_no = line[len("Pause task: "):] | ||
task_states[task_no].pause(time_ms) | ||
|
||
if line[:4] == "Resu": | ||
task_no = line[len("Resume task: "):] | ||
if task_no not in task_states: | ||
task_states[task_no] = TaskState(task_no, time_ms) | ||
task_states[task_no].resume(time_ms) | ||
|
||
wake_number = sum(task_wake.values()) | ||
|
||
show = {} | ||
|
||
# Make a graph of task parent relations | ||
parent_graph = open('parentgraph.dot', 'w') | ||
print("digraph regexp {", file=parent_graph) | ||
print('graph [ rankdir = "LR" ];', file=parent_graph) | ||
|
||
for task_no in task_names: | ||
if task_states[task_no].to_print(): | ||
print(f'{task_no} [label="{task_states[task_no].node_label()}", shape = "record"];', file=parent_graph) | ||
show[task_no] = True | ||
else: | ||
show[task_no] = False | ||
|
||
for task_no in task_parents: | ||
if task_parents[task_no] is None: | ||
continue | ||
if task_states[task_no].to_print(): | ||
if not show[task_parents[task_no]]: | ||
print(f'{task_parents[task_no]} [label="{task_states[task_parents[task_no]].node_label()}", shape = "record"];', file=parent_graph) | ||
show[task_parents[task_no]] = True | ||
|
||
print(f'{task_parents[task_no]} -> {task_no};', file=parent_graph) | ||
|
||
print(f'edge [weight=1000 style=dashed color=dimgrey]', file=parent_graph) | ||
|
||
for (source_no, target_no) in task_wake: | ||
pc = task_wake[(source_no, target_no)] / wake_number | ||
|
||
if source_no is None: | ||
source_no = "Env" | ||
|
||
if (source_no == "Env" or task_states[source_no].to_print()) and task_states[target_no].to_print(): | ||
print(f'{source_no} -> {target_no} [label="{pc:.2%}"];', file=parent_graph) | ||
|
||
print("}", file=parent_graph) | ||
|
||
for task_no in task_states: | ||
print(task_states[task_no].summary()) |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,10 @@ use network::{NetReceiver, NetSender}; | |||||
use store::Store; | ||||||
use tokio::sync::mpsc::{channel, Sender}; | ||||||
|
||||||
use profile::pspawn; | ||||||
use profile::*; | ||||||
|
||||||
|
||||||
#[cfg(test)] | ||||||
#[path = "tests/consensus_tests.rs"] | ||||||
pub mod consensus_tests; | ||||||
|
@@ -36,12 +40,12 @@ impl Consensus { | |||||
x | ||||||
})?; | ||||||
let network_receiver = NetReceiver::new(address, tx_core.clone()); | ||||||
tokio::spawn(async move { | ||||||
pspawn!("Net-Receiver", { | ||||||
network_receiver.run().await; | ||||||
}); | ||||||
|
||||||
let mut network_sender = NetSender::new(rx_network); | ||||||
tokio::spawn(async move { | ||||||
pspawn!("Net-Sender", { | ||||||
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.
Suggested change
|
||||||
network_sender.run().await; | ||||||
}); | ||||||
|
||||||
|
@@ -76,7 +80,7 @@ impl Consensus { | |||||
/* network_channel */ tx_network, | ||||||
commit_channel, | ||||||
); | ||||||
tokio::spawn(async move { | ||||||
pspawn!("Consensus-Core", { | ||||||
core.run().await; | ||||||
}); | ||||||
|
||||||
|
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 | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,9 @@ use std::collections::HashMap; | |||||
use store::Store; | ||||||
use tokio::sync::mpsc::{channel, Receiver, Sender}; | ||||||
|
||||||
use profile::pspawn; | ||||||
use profile::*; | ||||||
|
||||||
type DriverMessage = (Vec<u8>, Block, Receiver<()>); | ||||||
|
||||||
pub struct MempoolDriver<Mempool> { | ||||||
|
@@ -22,7 +25,7 @@ impl<Mempool: 'static + NodeMempool> MempoolDriver<Mempool> { | |||||
pub fn new(mempool: Mempool, core_channel: Sender<CoreMessage>, store: Store) -> Self { | ||||||
let (tx_inner, mut rx_inner): (_, Receiver<DriverMessage>) = channel(1000); | ||||||
let mut waiting = FuturesUnordered::new(); | ||||||
tokio::spawn(async move { | ||||||
pspawn!("Mempool", { | ||||||
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.
Suggested change
|
||||||
loop { | ||||||
tokio::select! { | ||||||
Some((wait_on, block, handler)) = rx_inner.recv().fuse() => { | ||||||
|
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,9 @@ use tokio::net::{TcpListener, TcpStream}; | |||||
use tokio::sync::mpsc::Sender; | ||||||
use tokio_util::codec::{Framed, LengthDelimitedCodec}; | ||||||
|
||||||
use profile::pspawn; | ||||||
use profile::*; | ||||||
|
||||||
pub struct Front { | ||||||
address: SocketAddr, | ||||||
deliver: Sender<Transaction>, | ||||||
|
@@ -38,7 +41,7 @@ impl Front { | |||||
} | ||||||
|
||||||
async fn spawn_worker(socket: TcpStream, peer: SocketAddr, deliver: Sender<Transaction>) { | ||||||
tokio::spawn(async move { | ||||||
pspawn!("Front-Worker", { | ||||||
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.
Suggested change
|
||||||
let mut transport = Framed::new(socket, LengthDelimitedCodec::new()); | ||||||
while let Some(frame) = transport.next().await { | ||||||
match frame { | ||||||
|
Oops, something went wrong.
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.