Skip to content

Commit e553baf

Browse files
committed
Merge branch 'ulan/run-619' into 'master'
RUN-619: Trim IPC buffers of idle sandbox processes This MR implements a memory optimization that trims sandbox IPC buffers when a sandbox process becomes idle. In order to detect idleness, the blocking operations are replaced with their versions that support timeouts. In other words, a blocking operation `op()` changes to ``` result = op_with_timeout(IDLE_TIMEOUT); if result.timed_out() { trim_buffers(); result = op(); } ``` In order to configure timeout for socket operations, we need to allow `setopt` in SELinux policy. See merge request dfinity-lab/public/ic!11902
2 parents 16a1bf3 + c61afdc commit e553baf

File tree

7 files changed

+341
-25
lines changed

7 files changed

+341
-25
lines changed

ic-os/guestos/rootfs/prep/ic-node/ic-node.te

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ create_dirs_pattern(ic_replica_t, tmp_t, tmp_t)
200200

201201
# Replica creates and reads/writes to/from internal unix domain socket,
202202
# as well as the socket connecting it to the sandbox.
203-
allow ic_replica_t self : unix_stream_socket { create read write };
203+
# It also uses setsockopt to configure socket timeouts.
204+
allow ic_replica_t self : unix_stream_socket { create setopt read write };
204205

205206
# Replica uses an internal fifo file
206207
allow ic_replica_t ic_replica_t : fifo_file { read };
@@ -308,7 +309,7 @@ allow ic_canister_sandbox_t ic_canister_sandbox_t : process { getsched };
308309
# communication channel (and such that there is no "accidental" use of any
309310
# differently labeled channel.
310311
allow ic_canister_sandbox_t ic_replica_t : fd use;
311-
allow ic_canister_sandbox_t ic_replica_t : unix_stream_socket { read write };
312+
allow ic_canister_sandbox_t ic_replica_t : unix_stream_socket { setopt read write };
312313

313314
# Allow to access the shared memory area set up by replica. NB this should be
314315
# labelled differently eventually because allowing tmpfs is fairly broad.
@@ -334,7 +335,7 @@ dontaudit ic_canister_sandbox_t ic_orchestrator_t : fd { use };
334335
# This should actually not be allowed, logs should be routed through
335336
# replica.
336337
allow ic_canister_sandbox_t init_t : fd { use };
337-
allow ic_canister_sandbox_t init_t : unix_stream_socket { read write };
338+
allow ic_canister_sandbox_t init_t : unix_stream_socket { setopt read write };
338339

339340
# Deny access to system information as well as own proc file (would
340341
# also allow accessing proc files of *other* sandboxes).

rs/canister_sandbox/backend_lib/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ pub mod sandbox_manager;
44
pub mod sandbox_server;
55

66
use ic_canister_sandbox_common::{
7-
child_process_initialization, controller_client_stub, protocol, rpc, transport,
7+
child_process_initialization, controller_client_stub, protocol, rpc,
8+
transport::{self, SocketReaderConfig},
89
};
910
use ic_config::embedders::Config as EmbeddersConfig;
1011
use ic_logger::new_replica_logger_from_config;
@@ -118,5 +119,6 @@ pub fn run_canister_sandbox(
118119
frame_handler.handle(message);
119120
},
120121
socket,
122+
SocketReaderConfig::for_sandbox(),
121123
);
122124
}

rs/canister_sandbox/common/src/process.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::os::unix::prelude::{CommandExt, RawFd};
44
use std::process::{Child, Command};
55
use std::sync::atomic::{AtomicBool, Ordering};
66

7+
use crate::transport::SocketReaderConfig;
78
use crate::{
89
protocol, protocol::ctlsvc, rpc, sandbox_client_stub::SandboxClientStub,
910
sandbox_service::SandboxService, transport,
@@ -107,6 +108,7 @@ pub fn spawn_canister_sandbox_process_with_factory(
107108
demux.handle(message);
108109
},
109110
socket,
111+
SocketReaderConfig::default(),
110112
);
111113
// If we the connection drops, but it is not terminated from
112114
// our end, that implies that the sandbox process died. At

rs/canister_sandbox/common/src/test_sandbox.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use ic_canister_sandbox_common::protocol::sbxsvc;
21
use ic_canister_sandbox_common::*;
2+
use ic_canister_sandbox_common::{protocol::sbxsvc, transport::SocketReaderConfig};
33
use ic_embedders::{
44
wasm_utils::{Segments, WasmImportsDetails},
55
CompilationResult, SerializedModule, SerializedModuleBytes,
@@ -127,5 +127,6 @@ fn main() {
127127
demux.handle(message);
128128
},
129129
socket,
130+
SocketReaderConfig::for_testing(),
130131
);
131132
}

0 commit comments

Comments
 (0)