forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
164 lines (146 loc) · 5.27 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#[cfg(not(target_family = "windows"))]
use clap::{crate_description, crate_name, value_t_or_exit, App, Arg};
use log::*;
#[cfg(target_os = "linux")]
#[allow(dead_code)]
fn tune_poh_service_priority(uid: u32) {
fn find_pid<P: AsRef<std::path::Path>, F>(
name: &str,
path: P,
uid: u32,
processor: F,
) -> Option<u64>
where
F: Fn(&std::fs::DirEntry) -> Option<u64>,
{
for entry in std::fs::read_dir(path).expect("Failed to read /proc folder") {
use std::os::unix::fs::MetadataExt;
if let Ok(dir) = entry {
if let Ok(meta) = std::fs::metadata(dir.path()) {
if uid == meta.uid() {
let mut path = dir.path();
path.push("comm");
if let Ok(comm) = std::fs::read_to_string(path.as_path()) {
if comm.starts_with(name) {
if let Some(pid) = processor(&dir) {
return Some(pid);
}
}
}
}
}
}
}
None
}
use std::{process::Command, str::from_utf8};
if let Some(pid) = find_pid("solana-validato", "/proc", uid, |dir| {
let mut path = dir.path();
path.push("task");
find_pid("solana-poh-serv", path, uid, |dir1| {
if let Ok(pid) = dir1.file_name().into_string() {
pid.parse::<u64>().ok()
} else {
None
}
})
}) {
info!("PoH thread PID is {}", pid);
let pid = format!("{}", pid);
let output = Command::new("chrt")
.args(&["-r", "-p", "99", pid.as_str()])
.output()
.expect("Expected to set priority of thread");
if output.status.success() {
info!("Done setting thread priority");
} else {
error!("chrt stderr: {}", from_utf8(&output.stderr).unwrap_or("?"));
}
} else {
error!("Could not find pid for PoH thread");
}
}
#[cfg(target_os = "linux")]
fn tune_kernel_udp_buffers_and_vmmap() {
use sysctl::{CtlValue::String, Sysctl};
fn sysctl_write(name: &str, value: &str) {
if let Ok(ctl) = sysctl::Ctl::new(name) {
info!("Old {} value {:?}", name, ctl.value());
let ctl_value = String(value.to_string());
match ctl.set_value(String(value.to_string())) {
Ok(v) if v == ctl_value => info!("Updated {} to {:?}", name, ctl_value),
Ok(v) => info!(
"Update returned success but {} was set to {:?}, instead of {:?}",
name, v, ctl_value
),
Err(e) => error!("Failed to set {} to {:?}. Err {:?}", name, ctl_value, e),
}
} else {
error!("Failed to find sysctl {}", name);
}
}
// Reference: https://medium.com/@CameronSparr/increase-os-udp-buffers-to-improve-performance-51d167bb1360
sysctl_write("net.core.rmem_max", "134217728");
sysctl_write("net.core.rmem_default", "134217728");
sysctl_write("net.core.wmem_max", "134217728");
sysctl_write("net.core.wmem_default", "134217728");
// increase mmap counts for many append_vecs
sysctl_write("vm.max_map_count", "1000000");
}
#[cfg(unix)]
fn main() {
solana_logger::setup();
let matches = App::new(crate_name!())
.about(crate_description!())
.version(solana_version::version!())
.arg(
Arg::with_name("user")
.long("user")
.value_name("user name")
.takes_value(true)
.required(true)
.help("Username of the peer process"),
)
.get_matches();
let user = value_t_or_exit!(matches, "user", String);
#[cfg(target_os = "linux")]
tune_kernel_udp_buffers_and_vmmap();
info!("Tune will service requests only from user {}", user);
unsafe { libc::umask(0o077) };
if let Err(e) = std::fs::remove_file(solana_sys_tuner::SOLANA_SYS_TUNER_PATH) {
if e.kind() != std::io::ErrorKind::NotFound {
panic!("Failed to remove stale socket file: {:?}", e)
}
}
let listener = unix_socket::UnixListener::bind(solana_sys_tuner::SOLANA_SYS_TUNER_PATH)
.expect("Failed to bind to the socket file");
let peer_uid;
// set socket permission
if let Some(user) = users::get_user_by_name(&user) {
peer_uid = user.uid();
info!("UID for solana is {}", peer_uid);
nix::unistd::chown(
solana_sys_tuner::SOLANA_SYS_TUNER_PATH,
Some(nix::unistd::Uid::from_raw(peer_uid)),
None,
)
.expect("Expected to change UID of the socket file");
} else {
panic!("Could not find UID for {:?} user", user);
}
info!("Waiting for tuning requests");
for stream in listener.incoming() {
if stream.is_ok() {
info!("Tuning the system now");
{
#![cfg(target_os = "linux")]
// tune_poh_service_priority(peer_uid);
}
}
}
info!("exiting");
}
#[cfg(not(unix))]
fn main() {
error!("Unsupported platform");
}