Compare commits

...

10 Commits

Author SHA1 Message Date
openeuler-ci-bot
28b4fc4e72
!34 [sync] PR-29: riscv64: restore nix-0.16.1 patch
From: @openeuler-sync-bot 
Reviewed-by: @xuxuepeng 
Signed-off-by: @xuxuepeng
2025-03-18 12:31:52 +00:00
laokz
d93a080cb1 riscv64: restore nix-0.16.1 patch
(cherry picked from commit 02747e8137344018f707d5f31d7d5678429c6e39)
2025-03-18 20:15:36 +08:00
openeuler-ci-bot
d0f346a42e
!28 [sync] PR-27: lib-shim-v2: sync some patches
From: @openeuler-sync-bot 
Reviewed-by: @xuxuepeng 
Signed-off-by: @xuxuepeng
2025-03-10 07:01:58 +00:00
kamizjw
1a5e0cc69d lib-shim-v2:sync some patches
(cherry picked from commit b342f7fdfcdeac79309b9bc5a6b136a9ead8e82b)
2025-01-20 19:51:57 +08:00
openeuler-ci-bot
0ff683377a
!24 Resolve loongarch64 patch addition error and add support for loongarch64
From: @dpdwaj 
Reviewed-by: @xuxuepeng 
Signed-off-by: @xuxuepeng
2024-04-23 09:24:15 +00:00
doupengda
f4d73192f8 Resolve loongarch64 patch addition error 2024-04-18 15:02:59 +08:00
xuxuepeng
bfb577fcea !20 Add vsock support
Merge pull request !20 from xuxuepeng/master
2023-09-01 12:24:40 +00:00
laokz
b9c803b8b7 !16 增加riscv构建支持
* add riscv64 support
2023-06-07 04:07:25 +00:00
openeuler-ci-bot
9e8ac112a8
!14 add loongarch64 support for lib-shim-v2
From: @zhangwenlong01 
Reviewed-by: @jingwoo 
Signed-off-by: @jingwoo
2023-05-12 15:53:11 +00:00
Wenlong Zhang
6c598182ea add loongarch64 support for lib-shim-v2
Signed-off-by: Wenlong Zhang <zhangwenlong@loongson.cn>
2023-05-12 17:30:15 +08:00
12 changed files with 2253 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,362 @@
From 442dd4ecbccdec2a84dd3f5c91dc947d560bcc37 Mon Sep 17 00:00:00 2001
From: suoxiaocong <suoxiaocong@kylinos.cn>
Date: Tue, 10 May 2022 16:21:32 +0800
Subject: [PATCH 1/4] simplify Store api, remove unnecessary validation
move container_id into struct Store, makes api more readable
---
src/client/client.rs | 62 +++++++++++++++++---------------------------
src/lib.rs | 22 +++++++---------
2 files changed, 34 insertions(+), 50 deletions(-)
diff --git a/src/client/client.rs b/src/client/client.rs
index 3f231f2..aa1c7c1 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -25,6 +25,7 @@ use ttrpc::client::Client;
#[derive(Clone)]
pub struct Store {
conn: Client,
+ container_id: String,
timeout: i64,
}
@@ -101,6 +102,7 @@ pub fn new_conn(container_id: &String, addr: &String) -> Result<()> {
container_id.clone(),
Store {
conn: Client::new(fd),
+ container_id: container_id.clone(),
timeout: 0,
},
);
@@ -140,19 +142,18 @@ impl ValidateTool {
impl Store {
pub fn create(
&self,
- container_id: &String,
bundle: &String,
terminal: bool,
stdin: &String,
stdout: &String,
stderr: &String,
) -> Result<i32> {
- ValidateTool {}.str_empty(container_id)?.str_empty(bundle)?;
+ ValidateTool {}.str_empty(bundle)?;
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::CreateTaskRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
req.set_bundle(bundle.clone());
req.set_terminal(terminal);
req.set_stdin(stdin.clone());
@@ -166,13 +167,12 @@ impl Store {
Ok(resp.pid as i32)
}
- pub fn start(&self, container_id: &String, exec_id: &String) -> Result<i32> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn start(&self, exec_id: &String) -> Result<i32> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::StartRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
req.set_exec_id(exec_id.clone());
let resp = client
@@ -185,18 +185,14 @@ impl Store {
#[allow(unused)]
pub fn kill(
&self,
- container_id: &String,
- exec_id: &String,
signal: u32,
all: bool,
) -> Result<()> {
- ValidateTool {}.str_empty(container_id)?;
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::KillRequest::new();
- req.set_id(container_id.clone());
- // unused variable: exec_id
+ req.set_id(self.container_id.clone());
req.set_signal(signal);
req.set_all(all);
@@ -207,13 +203,12 @@ impl Store {
Ok(())
}
- pub fn delete(&self, container_id: &String, exec_id: &String) -> Result<DeleteResponse> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn delete(&self, exec_id: &String) -> Result<DeleteResponse> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::DeleteRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
req.set_exec_id(exec_id.clone());
let resp = client
@@ -226,13 +221,12 @@ impl Store {
})
}
- pub fn shutdown(&self, container_id: &String) -> Result<()> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn shutdown(&self) -> Result<()> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::ShutdownRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
client
.shutdown(&req, self.timeout)
@@ -243,7 +237,6 @@ impl Store {
pub fn exec(
&self,
- container_id: &String,
exec_id: &String,
terminal: bool,
stdin: &String,
@@ -252,13 +245,12 @@ impl Store {
spec: &[u8],
) -> Result<()> {
ValidateTool {}
- .str_empty(container_id)?
.str_empty(exec_id)?;
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::ExecProcessRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
req.set_exec_id(exec_id.clone());
req.set_terminal(terminal);
req.set_stdin(stdin.clone());
@@ -281,17 +273,15 @@ impl Store {
pub fn resize_pty(
&self,
- container_id: &String,
exec_id: &String,
height: u32,
width: u32,
) -> Result<()> {
- ValidateTool {}.str_empty(container_id)?;
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::ResizePtyRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
req.set_exec_id(exec_id.clone());
req.set_height(height);
req.set_width(width);
@@ -303,13 +293,12 @@ impl Store {
Ok(())
}
- pub fn pause(&self, container_id: &String) -> Result<()> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn pause(&self) -> Result<()> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::PauseRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
client
.pause(&req, self.timeout)
@@ -318,13 +307,12 @@ impl Store {
Ok(())
}
- pub fn resume(&self, container_id: &String) -> Result<()> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn resume(&self) -> Result<()> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::ResumeRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
client
.resume(&req, self.timeout)
@@ -333,20 +321,19 @@ impl Store {
Ok(())
}
- pub fn state(&self, container_id: &String) -> Result<State> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn state(&self) -> Result<State> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::StateRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
let resp = client
.state(&req, self.timeout)
.map_err(shim_error!(e, "ttrpc call state failed"))?;
Ok(State {
- id: container_id.clone(),
+ id: self.container_id.clone(),
pid: resp.pid,
status: match resp.status {
shim_v2_status::CREATED => Status::CreatedStatus,
@@ -364,11 +351,11 @@ impl Store {
})
}
- pub fn pids(&self, container_id: &String) -> Result<i32> {
+ pub fn pids(&self) -> Result<i32> {
let c = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::PidsRequest::new();
- req.id = container_id.clone();
+ req.id = self.container_id.clone();
let resp = c
.pids(&req, self.timeout)
@@ -378,13 +365,12 @@ impl Store {
Ok(process.pid as i32)
}
- pub fn wait(&self, container_id: &String, exec_id: &String) -> Result<i32> {
- ValidateTool {}.str_empty(container_id)?;
+ pub fn wait(&self, exec_id: &String) -> Result<i32> {
let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
let mut req = protocols::shim::WaitRequest::new();
- req.set_id(container_id.clone());
+ req.set_id(self.container_id.clone());
req.set_exec_id(exec_id.clone());
let resp = client
diff --git a/src/lib.rs b/src/lib.rs
index 7a7d453..b46d800 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -80,7 +80,6 @@ pub extern "C" fn shim_v2_create(
.and_then(|client| {
client
.create(
- &r_container_id,
&r_bundle,
terminal,
&r_stdin,
@@ -110,7 +109,7 @@ pub extern "C" fn shim_v2_start(
get_conn(&r_container_id)
.and_then(|client| {
client
- .start(&r_container_id, &r_exec_id)
+ .start(&r_exec_id)
.map(|process_pid| {
*pid = process_pid;
println!("lib-shim-v2::start::{}:: done.", r_container_id);
@@ -135,7 +134,7 @@ pub extern "C" fn shim_v2_kill(
get_conn(&r_container_id)
.and_then(|client| {
client
- .kill(&r_container_id, &r_exec_id, signal, all)
+ .kill(signal, all)
.map(|_| {
println!("lib-shim-v2::kill::{}:: done.", r_container_id);
0
@@ -163,7 +162,7 @@ pub extern "C" fn shim_v2_delete(
println!("lib-shim-v2::delete::{}:: [{}]", r_container_id, r_exec_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.delete(&r_container_id, &r_exec_id).map(|response| {
+ client.delete(&r_exec_id).map(|response| {
resp.exit_status = response.exit_status;
resp.pid = response.pid;
println!("lib-shim-v2::delete::{}:: done.", r_container_id);
@@ -182,7 +181,7 @@ pub extern "C" fn shim_v2_shutdown(container_id: *const c_char) -> c_int {
println!("lib-shim-v2::shutdown::{}::", r_container_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.shutdown(&r_container_id).map(|_| {
+ client.shutdown().map(|_| {
println!("lib-shim-v2::shutdown::{}:: done.", r_container_id);
0
})
@@ -222,7 +221,6 @@ pub extern "C" fn shim_v2_exec(
.and_then(|client| {
client
.exec(
- &r_container_id,
&r_exec_id,
terminal,
&r_stdin,
@@ -256,7 +254,7 @@ pub extern "C" fn shim_v2_resize_pty(
get_conn(&r_container_id)
.and_then(|client| {
client
- .resize_pty(&r_container_id, &r_exec_id, height, width)
+ .resize_pty(&r_exec_id, height, width)
.map(|_| {
println!("lib-shim-v2::resize_pty::{}:: done.", r_container_id);
0
@@ -277,7 +275,7 @@ pub extern "C" fn shim_v2_pause(container_id: *const c_char) -> c_int {
println!("lib-shim-v2::pause::{}::", r_container_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.pause(&r_container_id).map(|_| {
+ client.pause().map(|_| {
println!("lib-shim-v2::pause::{}:: done.", r_container_id);
0
})
@@ -294,7 +292,7 @@ pub extern "C" fn shim_v2_resume(container_id: *const c_char) -> c_int {
println!("lib-shim-v2::resume::{}::", r_container_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.resume(&r_container_id).map(|_| {
+ client.resume().map(|_| {
println!("lib-shim-v2::resume::{}:: done.", r_container_id);
0
})
@@ -361,7 +359,7 @@ pub extern "C" fn shim_v2_state(container_id: *const c_char, state: &mut State)
println!("lib-shim-v2::state::{}::", r_container_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.state(&r_container_id).map(|container_state| {
+ client.state().map(|container_state| {
state.copy(container_state);
println!("lib-shim-v2::state::{}:: done.", r_container_id);
0
@@ -379,7 +377,7 @@ pub extern "C" fn shim_v2_pids(container_id: *const c_char, pid: &mut c_int) ->
println!("in rutst::shim_v2_pids::{}:: start.", r_container_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.pids(&r_container_id).map(|process_pid| {
+ client.pids().map(|process_pid| {
*pid = process_pid;
println!("in rust::shim_v2_pids::{}:: done", r_container_id);
0
@@ -401,7 +399,7 @@ pub extern "C" fn shim_v2_wait(
println!("lib-shim-v2::wait::{}:: [{}]", r_container_id, r_exec_id);
get_conn(&r_container_id)
.and_then(|client| {
- client.wait(&r_container_id, &r_exec_id).map(|exit_code| {
+ client.wait(&r_exec_id).map(|exit_code| {
*exit_status = exit_code;
println!("lib-shim-v2::wait::{}:: done.", r_container_id);
0
--
2.40.1

View File

@ -0,0 +1,26 @@
From fc7460e84deb77b18c895b7f4b46497a5e3ca740 Mon Sep 17 00:00:00 2001
From: czrz <chengzeruizhi@huawei.com>
Date: Wed, 7 Dec 2022 08:11:14 +0000
Subject: [PATCH 2/4] !20 Check if a path is abstract before connection * Check
if a path is abstract before connection
---
src/client/client.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/client/client.rs b/src/client/client.rs
index 3f231f2..8240b53 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -96,7 +96,7 @@ pub fn new_conn(container_id: &String, addr: &String) -> Result<()> {
};
let path = Path::new(&MAIN_SEPARATOR.to_string()).join(address);
- let fd = connect_to_socket(true, &path.to_string_lossy())?;
+ let fd = connect_to_socket(!addr.starts_with("unix://"), &path.to_string_lossy())?;
TTRPC_CLIENTS.lock().unwrap().insert(
container_id.clone(),
Store {
--
2.40.1

View File

@ -0,0 +1,79 @@
From 7e22a3a81323e59c66abe8a0001d69c363ef31a5 Mon Sep 17 00:00:00 2001
From: xuxuepeng <xuxuepeng1@huawei.com>
Date: Fri, 1 Sep 2023 19:15:53 +0800
Subject: [PATCH 4/4] Add vsock support
Signed-off-by: xuxuepeng <xuxuepeng1@huawei.com>
---
src/client/client.rs | 45 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 39 insertions(+), 6 deletions(-)
diff --git a/src/client/client.rs b/src/client/client.rs
index 3f3e94a..2151e23 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -75,7 +75,35 @@ fn unix_sock(r#abstract: bool, socket_path: &str) -> Result<SockAddr> {
Ok(sockaddr)
}
-fn connect_to_socket(abs: bool, address: &str) -> Result<RawFd> {
+fn virtio_vsock(address: &str) -> Result<SockAddr> {
+ let (cid, port) = {
+ let vec: Vec<String> = address.split(":").map(String::from).collect();
+ if vec.len() != 2 {
+ let err_msg = format!("vsock address {address} is invalid");
+ return Err(other!(err_msg));
+ }
+ let cid = vec[0].parse::<u32>().map_err(other_error!(e, "failed to parse cid: "))?;
+ let port = vec[1].parse::<u32>().map_err(other_error!(e, "failed to parse port: "))?;
+ (cid, port)
+ };
+ let sockaddr = SockAddr::Vsock(VsockAddr::new(cid, port));
+ Ok(sockaddr)
+}
+
+fn connect_to_vsock(address: &str) -> Result<RawFd> {
+ let fd = socket(
+ AddressFamily::Vsock,
+ SockType::Stream,
+ SockFlag::empty(),
+ None,
+ )
+ .map_err(other_error!(e, "failed to create socket fd: "))?;
+ let sockaddr = virtio_vsock(address)?;
+ connect(fd, &sockaddr).map_err(other_error!(e, "failed to connect vsock: "))?;
+ Ok(fd)
+}
+
+fn connect_to_unix_socket(abs: bool, address: &str) -> Result<RawFd> {
let fd = socket(
AddressFamily::Unix,
SockType::Stream,
@@ -90,14 +118,19 @@ fn connect_to_socket(abs: bool, address: &str) -> Result<RawFd> {
}
pub fn new_conn(container_id: &String, addr: &String) -> Result<()> {
- let address = if addr.starts_with("unix://") {
- addr.strip_prefix("unix://").unwrap()
+ let fd = if addr.starts_with("vsock://") {
+ let address = addr.strip_prefix("vsock://").unwrap();
+ connect_to_vsock(address)?
} else {
- addr
+ let address = if addr.starts_with("unix://") {
+ addr.strip_prefix("unix://").unwrap()
+ } else {
+ addr
+ };
+ let path = Path::new(&MAIN_SEPARATOR.to_string()).join(address);
+ connect_to_unix_socket(!addr.starts_with("unix://"), &path.to_string_lossy())?
};
- let path = Path::new(&MAIN_SEPARATOR.to_string()).join(address);
- let fd = connect_to_socket(!addr.starts_with("unix://"), &path.to_string_lossy())?;
TTRPC_CLIENTS.lock().unwrap().insert(
container_id.clone(),
Store {
--
2.40.1

View File

@ -0,0 +1,28 @@
From 0faa4dfeb2158e14fd458a4d3836db6be7ab0214 Mon Sep 17 00:00:00 2001
From: jackey_1024 <jikui2@huawei.com>
Date: Mon, 21 Oct 2024 16:51:02 +0800
Subject: [PATCH] sandbox:sandbox api update
Signed-off-by: jackey_1024 <jikui2@huawei.com>
---
src/client/client.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/client/client.rs b/src/client/client.rs
index 2151e23..fe1b1c2 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -118,8 +118,8 @@ fn connect_to_unix_socket(abs: bool, address: &str) -> Result<RawFd> {
}
pub fn new_conn(container_id: &String, addr: &String) -> Result<()> {
- let fd = if addr.starts_with("vsock://") {
- let address = addr.strip_prefix("vsock://").unwrap();
+ let fd = if addr.starts_with("ttrpc+vsock://") {
+ let address = addr.strip_prefix("ttrpc+vsock://").unwrap();
connect_to_vsock(address)?
} else {
let address = if addr.starts_with("unix://") {
--
2.45.0

View File

@ -0,0 +1,28 @@
From 8b393e10d52876aef1c2c4ac0878eab6e1962f48 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Thu, 19 Dec 2024 15:11:16 +0800
Subject: [PATCH] add adapter for runc sandboxer
Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
src/client/client.rs | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/client/client.rs b/src/client/client.rs
index fe1b1c2..7323197 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -121,6 +121,10 @@ pub fn new_conn(container_id: &String, addr: &String) -> Result<()> {
let fd = if addr.starts_with("ttrpc+vsock://") {
let address = addr.strip_prefix("ttrpc+vsock://").unwrap();
connect_to_vsock(address)?
+ } else if addr.starts_with("ttrpc+unix://") {
+ let address = addr.strip_prefix("ttrpc+unix://").unwrap();
+ let path = Path::new(&MAIN_SEPARATOR.to_string()).join(address);
+ connect_to_unix_socket(!addr.starts_with("ttrpc+unix://"), &path.to_string_lossy())?
} else {
let address = if addr.starts_with("unix://") {
addr.strip_prefix("unix://").unwrap()
--
2.45.0

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,383 @@
From 2de35a3762ae9372cb4834211fdda9324bc215bb Mon Sep 17 00:00:00 2001
From: kamizjw <zhongjiawei1@huawei.com>
Date: Wed, 15 Jan 2025 15:27:30 +0800
Subject: [PATCH] lib-shim-v2:support stats cmd
---
build.rs | 1 +
shim_v2.h | 27 +++
src/client/client.rs | 22 +++
src/lib.rs | 73 +++++++-
src/protocols/mod.rs | 1 +
.../containerd/cgroup/stats/v1/metrics.proto | 158 ++++++++++++++++++
6 files changed, 281 insertions(+), 1 deletion(-)
create mode 100644 src/protocols/protos/github.com/containerd/cgroup/stats/v1/metrics.proto
diff --git a/build.rs b/build.rs
index dda0ccb..f572bc9 100644
--- a/build.rs
+++ b/build.rs
@@ -11,6 +11,7 @@ fn main() {
"src/protocols/protos/google/protobuf/timestamp.proto",
"src/protocols/protos/github.com/containerd/containerd/api/types/mount.proto",
"src/protocols/protos/github.com/containerd/containerd/api/types/task/task.proto",
+ "src/protocols/protos/github.com/containerd/cgroup/stats/v1/metrics.proto",
])
.include("src/protocols/protos")
.rust_protobuf()
diff --git a/shim_v2.h b/shim_v2.h
index 6a719f2..c19a809 100644
--- a/shim_v2.h
+++ b/shim_v2.h
@@ -41,6 +41,32 @@ struct State {
unsigned int exit_status;
};
+struct Stats {
+ uint64_t pids_current;
+ /* CPU usage */
+ uint64_t cpu_use_nanos;
+ uint64_t cpu_system_use;
+ /* BlkIO usage */
+ uint64_t blkio_read;
+ uint64_t blkio_write;
+ /* Memory usage */
+ uint64_t mem_used;
+ uint64_t mem_limit;
+ uint64_t rss_bytes;
+ uint64_t page_faults;
+ uint64_t major_page_faults;
+ /* Kernel Memory usage */
+ uint64_t kmem_used;
+ uint64_t kmem_limit;
+ /* Cache usage */
+ uint64_t cache;
+ uint64_t cache_total;
+ uint64_t inactive_file_total;
+ /* Swap usage*/
+ uint64_t swap_used;
+ uint64_t swap_limit;
+};
+
int shim_v2_new(const char *container_id, const char *addr);
int shim_v2_close(const char *container_id);
@@ -59,6 +85,7 @@ int shim_v2_pause(const char *container_id);
int shim_v2_resume(const char *container_id);
int shim_v2_state(const char *container_id, const struct State *state);
+int shim_v2_stats(const char *container_id, const struct Stats *stats);
int shim_v2_pids(const char *container_id, int *pid);
int shim_v2_wait(const char *container_id, const char *exec_id, int *exit_status);
diff --git a/src/client/client.rs b/src/client/client.rs
index 2151e23..40de955 100644
--- a/src/client/client.rs
+++ b/src/client/client.rs
@@ -11,7 +11,11 @@
// See the Mulan PSL v2 for more details.
use super::error::{Error, Result};
+use crate::protocols::metrics::Metrics;
use crate::protocols;
+use protobuf::{
+ CodedInputStream,Message,
+};
use lazy_static::lazy_static;
use nix::sys::socket::*;
use protocols::task::Status as shim_v2_status;
@@ -384,6 +388,24 @@ impl Store {
})
}
+ pub fn stats(&self) -> Result<Metrics> {
+ let client = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
+
+ let mut req = protocols::shim::StatsRequest::new();
+ req.id= self.container_id.clone();
+ let ctx = context::with_timeout(0);
+
+ let resp = client
+ .stats(ctx, &req)
+ .map_err(shim_error!(e, "ttrpc call stats failed"))?;
+ let mut m = Metrics::new();
+ if let Some(any) = resp.stats.as_ref() {
+ let mut input = CodedInputStream::from_bytes(any.value.as_ref());
+ m.merge_from(&mut input).unwrap();
+ }
+ Ok(m)
+ }
+
pub fn pids(&self) -> Result<i32> {
let c = protocols::shim_ttrpc::TaskClient::new(self.conn.clone());
diff --git a/src/lib.rs b/src/lib.rs
index b46d800..7c77187 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,7 +22,8 @@ use client::client::State as client_state;
use client::client::Status as client_status;
use client::client::{del_conn, get_conn, new_conn};
use std::ffi::{CStr, CString};
-use std::os::raw::{c_char, c_int, c_uint};
+use std::os::raw::{c_char, c_int, c_uint, c_ulonglong};
+use protocols::metrics::Metrics;
fn to_string(x: *const c_char) -> String {
unsafe {
@@ -371,6 +372,76 @@ pub extern "C" fn shim_v2_state(container_id: *const c_char, state: &mut State)
})
}
+#[repr(C)]
+pub struct Stats {
+ pids_current: c_ulonglong,
+ /* CPU usage */
+ cpu_use_nanos: c_ulonglong,
+ cpu_system_use: c_ulonglong,
+ /* BlkIO usage */
+ blkio_read: c_ulonglong,
+ blkio_write: c_ulonglong,
+ /* Memory usage */
+ mem_used: c_ulonglong,
+ mem_limit: c_ulonglong,
+ rss_bytes: c_ulonglong,
+ page_faults: c_ulonglong,
+ major_page_faults: c_ulonglong,
+ /* Kernel Memory usage */
+ kmem_used: c_ulonglong,
+ kmem_limit: c_ulonglong,
+ /* Cache usage */
+ cache: c_ulonglong,
+ cache_total: c_ulonglong,
+ inactive_file_total: c_ulonglong,
+ /* Swap usage*/
+ swap_used: c_ulonglong,
+ swap_limit: c_ulonglong,
+}
+
+impl Stats {
+ fn copy(&mut self, in_obj: Metrics) {
+ self.pids_current = in_obj.pids.current;
+ self.cpu_use_nanos = in_obj.cpu.usage.total;
+ self.cpu_system_use = in_obj.cpu.usage.kernel;
+ self.mem_used = in_obj.memory.usage.usage;
+ self.mem_limit = in_obj.memory.usage.limit;
+ self.rss_bytes = in_obj.memory.rss;
+ self.page_faults = in_obj.memory.pg_fault;
+ self.major_page_faults = in_obj.memory.pg_maj_fault;
+ self.kmem_used = in_obj.memory.kernel.usage;
+ self.kmem_limit = in_obj.memory.kernel.limit;
+ self.inactive_file_total = in_obj.memory.total_inactive_file;
+ self.swap_limit = in_obj.memory.swap.limit;
+ self.swap_used = in_obj.memory.swap.usage;
+ for ele in in_obj.blkio.io_service_bytes_recursive.iter() {
+ if ele.op == "read" {
+ self.blkio_read += ele.value;
+ } else {
+ self.blkio_write += ele.value;
+ }
+ }
+ }
+}
+
+#[no_mangle]
+pub extern "C" fn shim_v2_stats(container_id: *const c_char, stats: &mut Stats) -> c_int {
+ let r_container_id = to_string(container_id);
+ println!("lib-shim-v2::stats::{}::", r_container_id);
+ get_conn(&r_container_id)
+ .and_then(|client| {
+ client.stats().map(|metrics| {
+ println!("lib-shim-v2::metrics data::{:?}", metrics);
+ stats.copy(metrics);
+ 0
+ })
+ })
+ .unwrap_or_else(|e| {
+ println!("lib-shim-v2::stats::{}:: failed, {}.", r_container_id, e);
+ -1
+ })
+}
+
#[no_mangle]
pub extern "C" fn shim_v2_pids(container_id: *const c_char, pid: &mut c_int) -> c_int {
let r_container_id = to_string(container_id);
diff --git a/src/protocols/mod.rs b/src/protocols/mod.rs
index cc5794e..dfe4f42 100644
--- a/src/protocols/mod.rs
+++ b/src/protocols/mod.rs
@@ -13,6 +13,7 @@
pub mod any;
pub mod empty;
pub mod gogo;
+pub mod metrics;
pub mod mount;
pub mod shim;
pub mod shim_ttrpc;
diff --git a/src/protocols/protos/github.com/containerd/cgroup/stats/v1/metrics.proto b/src/protocols/protos/github.com/containerd/cgroup/stats/v1/metrics.proto
new file mode 100644
index 0000000..b3f6cc3
--- /dev/null
+++ b/src/protocols/protos/github.com/containerd/cgroup/stats/v1/metrics.proto
@@ -0,0 +1,158 @@
+syntax = "proto3";
+
+package io.containerd.cgroups.v1;
+
+import "gogoproto/gogo.proto";
+
+message Metrics {
+ repeated HugetlbStat hugetlb = 1;
+ PidsStat pids = 2;
+ CPUStat cpu = 3 [(gogoproto.customname) = "CPU"];
+ MemoryStat memory = 4;
+ BlkIOStat blkio = 5;
+ RdmaStat rdma = 6;
+ repeated NetworkStat network = 7;
+ CgroupStats cgroup_stats = 8;
+ MemoryOomControl memory_oom_control = 9;
+}
+
+message HugetlbStat {
+ uint64 usage = 1;
+ uint64 max = 2;
+ uint64 failcnt = 3;
+ string pagesize = 4;
+}
+
+message PidsStat {
+ uint64 current = 1;
+ uint64 limit = 2;
+}
+
+message CPUStat {
+ CPUUsage usage = 1;
+ Throttle throttling = 2;
+}
+
+message CPUUsage {
+ // values in nanoseconds
+ uint64 total = 1;
+ uint64 kernel = 2;
+ uint64 user = 3;
+ repeated uint64 per_cpu = 4 [(gogoproto.customname) = "PerCPU"];
+
+}
+
+message Throttle {
+ uint64 periods = 1;
+ uint64 throttled_periods = 2;
+ uint64 throttled_time = 3;
+}
+
+message MemoryStat {
+ uint64 cache = 1;
+ uint64 rss = 2 [(gogoproto.customname) = "RSS"];
+ uint64 rss_huge = 3 [(gogoproto.customname) = "RSSHuge"];
+ uint64 mapped_file = 4;
+ uint64 dirty = 5;
+ uint64 writeback = 6;
+ uint64 pg_pg_in = 7;
+ uint64 pg_pg_out = 8;
+ uint64 pg_fault = 9;
+ uint64 pg_maj_fault = 10;
+ uint64 inactive_anon = 11;
+ uint64 active_anon = 12;
+ uint64 inactive_file = 13;
+ uint64 active_file = 14;
+ uint64 unevictable = 15;
+ uint64 hierarchical_memory_limit = 16;
+ uint64 hierarchical_swap_limit = 17;
+ uint64 total_cache = 18;
+ uint64 total_rss = 19 [(gogoproto.customname) = "TotalRSS"];
+ uint64 total_rss_huge = 20 [(gogoproto.customname) = "TotalRSSHuge"];
+ uint64 total_mapped_file = 21;
+ uint64 total_dirty = 22;
+ uint64 total_writeback = 23;
+ uint64 total_pg_pg_in = 24;
+ uint64 total_pg_pg_out = 25;
+ uint64 total_pg_fault = 26;
+ uint64 total_pg_maj_fault = 27;
+ uint64 total_inactive_anon = 28;
+ uint64 total_active_anon = 29;
+ uint64 total_inactive_file = 30;
+ uint64 total_active_file = 31;
+ uint64 total_unevictable = 32;
+ MemoryEntry usage = 33;
+ MemoryEntry swap = 34;
+ MemoryEntry kernel = 35;
+ MemoryEntry kernel_tcp = 36 [(gogoproto.customname) = "KernelTCP"];
+
+}
+
+message MemoryEntry {
+ uint64 limit = 1;
+ uint64 usage = 2;
+ uint64 max = 3;
+ uint64 failcnt = 4;
+}
+
+message MemoryOomControl {
+ uint64 oom_kill_disable = 1;
+ uint64 under_oom = 2;
+ uint64 oom_kill = 3;
+}
+
+message BlkIOStat {
+ repeated BlkIOEntry io_service_bytes_recursive = 1;
+ repeated BlkIOEntry io_serviced_recursive = 2;
+ repeated BlkIOEntry io_queued_recursive = 3;
+ repeated BlkIOEntry io_service_time_recursive = 4;
+ repeated BlkIOEntry io_wait_time_recursive = 5;
+ repeated BlkIOEntry io_merged_recursive = 6;
+ repeated BlkIOEntry io_time_recursive = 7;
+ repeated BlkIOEntry sectors_recursive = 8;
+}
+
+message BlkIOEntry {
+ string op = 1;
+ string device = 2;
+ uint64 major = 3;
+ uint64 minor = 4;
+ uint64 value = 5;
+}
+
+message RdmaStat {
+ repeated RdmaEntry current = 1;
+ repeated RdmaEntry limit = 2;
+}
+
+message RdmaEntry {
+ string device = 1;
+ uint32 hca_handles = 2;
+ uint32 hca_objects = 3;
+}
+
+message NetworkStat {
+ string name = 1;
+ uint64 rx_bytes = 2;
+ uint64 rx_packets = 3;
+ uint64 rx_errors = 4;
+ uint64 rx_dropped = 5;
+ uint64 tx_bytes = 6;
+ uint64 tx_packets = 7;
+ uint64 tx_errors = 8;
+ uint64 tx_dropped = 9;
+}
+
+// CgroupStats exports per-cgroup statistics.
+message CgroupStats {
+ // number of tasks sleeping
+ uint64 nr_sleeping = 1;
+ // number of tasks running
+ uint64 nr_running = 2;
+ // number of tasks in stopped state
+ uint64 nr_stopped = 3;
+ // number of tasks in uninterruptible state
+ uint64 nr_uninterruptible = 4;
+ // number of tasks waiting on IO
+ uint64 nr_io_wait = 5;
+}
--
2.43.0

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -7,19 +7,33 @@
Name: lib-shim-v2
Version: 0.0.1
Release: 5
Release: 11
URL: https://gitee.com/openeuler/lib-shim-v2
Source: %{name}-%{version}.tar.gz
Source1: libc.tar.gz
Summary: lib-shim-v2 is shim v2 ttrpc client which is called by iSulad.
Group: Application/System
License: Mulan PSL v2
ExclusiveArch: x86_64 aarch64
ExclusiveArch: x86_64 aarch64 loongarch64 riscv64
Patch01: 0001-lib-shim-v2-add-support-for-loongarch64.patch
Patch02: 0002-simplify-Store-api-remove-unnecessary-validation.patch
Patch03: 0003-Check-if-a-path-is-abstract-before-connection.patch
Patch04: 0004-Add-vsock-support.patch
Patch05: 0005-sandbox-sandbox-api-update.patch
Patch06: 0006-add-adapter-for-runc-sandboxer.patch
Patch07: 0007-update-dependent-protobuf-and-ttrpc-version.patch
Patch08: 0008-support-stats-cmd.patch
Patch09: 0009-add-riscv-support.patch
BuildRequires: rust
BuildRequires: cargo
BuildRequires: rust-packaging
BuildRequires: gcc
%ifarch riscv64
BuildRequires: protobuf-compiler
%endif
%description
Based on Rust programming language, as a shim v2 ttrpc client, it is called by iSulad.
@ -27,7 +41,7 @@ Based on Rust programming language, as a shim v2 ttrpc client, it is called by i
%package devel
Summary: shim v2 ttrpc client
Group: Libraries
ExclusiveArch: x86_64 aarch64
ExclusiveArch: x86_64 aarch64 loongarch64 riscv64
Requires: %{name} = %{version}-%{release}
%description devel
@ -42,6 +56,10 @@ the %{name}-libs package contains Libraries for shim v2 ttrpc client
sed -i '/\[source.crates-io\]/{n;d}' ./.cargo/config
sed -i '/\[source.local-registry\]/{n;d}' ./.cargo/config
sed -i '/\[source.local-registry\]/a directory = "vendor"' ./.cargo/config
%ifarch loongarch64
rm -rf vendor/libc
tar -xf %{SOURCE1} -C vendor
%endif
%cargo_build -a
%install
@ -59,6 +77,43 @@ install -m 0755 target/release/libshim_v2.so ${RPM_BUILD_ROOT}/%{_libdir}/libshi
%{_includedir}/shim_v2.h
%changelog
* Mon Mar 03 2025 laokz <zhangkai@iscas.ac.cn> - 0.0.1-11
- Type:Fix
- ID:NA
- SUG:NA
- DESC: riscv64: restore nix-0.16.1 patch
* Wed Jan 15 2025 kamizjw <zhongjiawei1@huawei.com> - 0.0.1-10
- Type:NA
- ID:NA
- SUG:NA
- DESC: sync some patches
* Thu Apr 18 2024 Pengda Dou <doupengda@loongson.cn> - 0.0.1-9
- Resolve loongarch64 patch addition error
- add support for loongarch64
* Fri Sep 01 2023 xuxuepeng <xuxuepeng1@huawei.com> - 0.0.1-8
- Type:Enhancement
- ID:NA
- SUG:NA
- DESC: Simplify Store api, remove unnecessary validation
Check if a path is abstract before connection
Add vsock support.
* Mon Jun 05 2023 laokz <zhangkai@iscas.ac.cn> - 0.0.1-7
- Type:Enhancement
- ID:NA
- SUG:NA
- DESC: Add riscv64 support. Patches are from nix v0.17.0 and v0.18.0.
Add protobuf-compiler to avoid patching big binary for prost-build.
* Fri May 12 2023 Wenlong Zhang <zhangwenlong@loongson.cn> - 0.0.1-6
- Type: feature
- ID: NA
- SUG: NA
- DESC: Add loongarch64 architecture
* Fri Dec 09 2022 wujing <wujing50@huawei.com> - 0.0.1-5
- Type:improve
- ID:NA

BIN
libc.tar.gz Normal file

Binary file not shown.