Skip to content

Commit d89027a

Browse files
committed
renice self for background actions
1 parent 68efccc commit d89027a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ruma-headers = {path = "../ruma-headers"}
3131
flate2 = { version = "*" }
3232
libalpm-rs = "*"
3333
memory-stats = { version = "*", optional = true }
34+
libc = "*"
3435

3536
[[bin]]
3637
name = "deltaclient"

client/src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,51 @@ fn main() {
133133
blacklist,
134134
no_fuz,
135135
only_delta,
136-
} => full_upgrade(global, server, pacman_sync, blacklist, no_fuz, only_delta),
136+
} => {
137+
renice();
138+
full_upgrade(global, server, pacman_sync, blacklist, no_fuz, only_delta)
139+
}
137140
Commands::Download {
138141
server,
139142
delta_cache,
140143
no_fuz,
141144
only_delta,
142145
} => {
146+
renice();
143147
std::fs::create_dir_all(&delta_cache).unwrap();
144148
mkruntime()
145149
.block_on(do_upgrade(global, server, vec![], delta_cache, !no_fuz, only_delta))
146150
.unwrap();
147151
}
148152
Commands::Sync { server } => {
153+
renice();
149154
mkruntime().block_on(sync(global, server)).unwrap();
150155
}
151156
Commands::Stats { number: count } => util::calc_stats(count.unwrap_or(5)).unwrap(),
152157
}
153158
}
154159

160+
fn renice() {
161+
//SAFETY: this purely calls some libc functions, and does so correctly.
162+
// No memory stuff is done.
163+
unsafe {
164+
// getpid always succeeds
165+
let pid = libc::getpid() as u32;
166+
// Not checking for error, only possible errors are misuse.
167+
let curprio = libc::getpriority(libc::PRIO_PROCESS, pid);
168+
trace!("current prio/nice is {curprio}");
169+
if curprio > -10 {
170+
let e = libc::setpriority(libc::PRIO_PROCESS, pid, -10);
171+
if e == -1 {
172+
let err = std::io::Error::last_os_error();
173+
debug!("could not set/lower priority: {}", err);
174+
} else {
175+
trace!("set prio/nice to -10");
176+
}
177+
}
178+
}
179+
}
180+
155181
async fn sync(global: GlobalState, server: Url) -> anyhow::Result<()> {
156182
let server = server.join("archdb/")?;
157183
//TODO sync databases configured in /etc/pacman.conf

0 commit comments

Comments
 (0)