Skip to content

utils: adds a basic thread/pid api implementation. - #6

Open
dborquez wants to merge 1 commit into
mainfrom
get_vcpus_tids_ch
Open

utils: adds a basic thread/pid api implementation.#6
dborquez wants to merge 1 commit into
mainfrom
get_vcpus_tids_ch

Conversation

@dborquez

Copy link
Copy Markdown
Owner

Draft version of the get_thread_ids that constructs the /proc/$pid/task and returns a tid-vcpu hashmap.

@jodh-intel jodh-intel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @dborquez - a few comments...

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
let c_path = fs::canonicalize(proc_path)?;

// make the task path
let tid_path = fs::canonicalize(Path::new(&c_path).join("task"))?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can drop this second call to canonicalize since at this point, c_path is already canonical so it's safe to append our own path element to it.

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
Err(_) => continue,
};

let tid: u32 = tid_str.parse().expect("Not a valid tid was found");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace the expect() call by ?

Suggested change
let tid: u32 = tid_str.parse().expect("Not a valid tid was found");
let tid: u32 = tid_str.parse()?;

Or,

Suggested change
let tid: u32 = tid_str.parse().expect("Not a valid tid was found");
let tid: u32 = tid_str.parse().map_err(|e| anyhow!("Not valid tid found: {e}"))?;

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
return Err(anyhow!("Status information about the process was not found."));
}

let cpu_id: u32 = vect_proc_pid_stat[CPU_POS_STAT].parse().expect("Not a valid cpu id.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, can you return the error and not call expect() to ensure we don't panic.

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
let tid_path = Path::new("/proc").join(tid_str.clone()).join("stat");

if !tid_path.exists() {
return Err(anyhow!("Tid stat was not found."));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We're using a combination of "tid" and "Tid", so please can you choose one of those (or "TID") and use it consistently everywhere :)

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
}

let cpu_id: u32 = vect_proc_pid_stat[CPU_POS_STAT].parse().expect("Not a valid cpu id.");
tids.insert(tid,cpu_id);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you either run rustfmt manually on the changes, or set up your editor to do this automatically as this doesn't appear to have been formatted (missing space).

@dborquez
dborquez force-pushed the get_vcpus_tids_ch branch 2 times, most recently from 313c1fd to 4558cf7 Compare March 5, 2024 13:38
Comment thread src/tools/pr_8799/src/main.rs Outdated
Comment thread src/tools/pr_8799/src/main.rs Outdated
Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
@dborquez
dborquez force-pushed the get_vcpus_tids_ch branch 4 times, most recently from c3b7b41 to 16d3cf3 Compare March 6, 2024 14:34

@jodh-intel jodh-intel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @dborquez - just a couple of comments...

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
let src = std::fs::canonicalize(proc_path)
.map_err(|e| anyhow!("Invalid /proc/pid path: {proc_path}: {e}"))?;

if !src.metadata().unwrap().is_dir() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't have any unwrap() or expect() calls in the "real" code so can you replace that with ? here?

See: https://github.com/kata-containers/kata-containers/blob/main/docs/code-pr-advice.md#unsafe-code

Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
@dborquez
dborquez force-pushed the get_vcpus_tids_ch branch from 16d3cf3 to 4a06519 Compare March 6, 2024 16:19
Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
Comment thread src/tools/pr_8799/src/virtcontainers.rs Outdated
Err(_) => continue,
};

let comm_path = Path::new("/proc").join(tid_str.clone()).join("comm");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct as we should be looking at /proc/$cloud_hypervisor_pid/task/*/comm only.

@dborquez
dborquez force-pushed the get_vcpus_tids_ch branch from 4a06519 to 678989d Compare March 6, 2024 20:29
Draft version of the get_thread_ids that constructs the
/proc/$pid/task and returns a tid-vcpu hashmap.

Signed-off-by: David Esparza <david.esparza.borquez@intel.com>
@dborquez
dborquez force-pushed the get_vcpus_tids_ch branch from 678989d to ecf269e Compare March 7, 2024 00:26

@jodh-intel jodh-intel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @dborquez. Just one nit.

I think you are now ready to add get_ch_vcpu_tids_by_path(), get_ch_vcpu_tids() and the tests into get_thread_ids() in src/runtime-rs/crates/hypervisor/src/ch/inner_hypervisor.rs.

Before you raise the PR, please test that you can start and stop a container using runtime-rs and CH.


// Create a map of tids-vcpus from /proc/pid/task subdirectories
pub fn get_ch_vcpu_tids_by_path(proc_path: &str) -> Result<HashMap<u32, u32>> {
let mut tids = HashMap::new();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd move this to just above the for loop since if the specified proc_path isn't valid, we've constructed this unecessarily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants