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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use std::io::Read;
use std::path::{Path, PathBuf};

use super::{FileWrapper, Io, Schedstat, Stat, Status};
use crate::{ProcError, ProcResult};
use procfs_core::FromRead;
use rustix::fd::{BorrowedFd, OwnedFd};

/// A task (aka Thread) inside of a [`Process`](crate::process::Process)
///
/// Created by [`Process::tasks`](crate::process::Process::tasks), tasks in
/// general are similar to Processes and should have mostly the same fields.
#[derive(Debug)]
pub struct Task {
    fd: OwnedFd,
    /// The ID of the process that this task belongs to
    pub pid: i32,
    /// The task ID
    pub tid: i32,
    /// Task root: `/proc/<pid>/task/<tid>`
    pub(crate) root: PathBuf,
}

impl Task {
    /// Create a new `Task` inside of the process
    ///
    /// This API is designed to be ergonomic from inside of [`TasksIter`](super::TasksIter)
    pub(crate) fn from_process_at<P: AsRef<Path>, Q: AsRef<Path>>(
        base: P,
        dirfd: BorrowedFd,
        path: Q,
        pid: i32,
        tid: i32,
    ) -> ProcResult<Task> {
        use rustix::fs::{Mode, OFlags};

        let p = path.as_ref();
        let root = base.as_ref().join(p);
        let fd = wrap_io_error!(
            root,
            rustix::fs::openat(
                dirfd,
                p,
                OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC,
                Mode::empty()
            )
        )?;

        Ok(Task { fd, pid, tid, root })
    }

    /// Thread info from `/proc/<pid>/task/<tid>/stat`
    ///
    /// Many of the returned fields will be the same as the parent process, but some fields like `utime` and `stime` will be per-task
    pub fn stat(&self) -> ProcResult<Stat> {
        self.read("stat")
    }

    /// Thread info from `/proc/<pid>/task/<tid>/status`
    ///
    /// Many of the returned fields will be the same as the parent process
    pub fn status(&self) -> ProcResult<Status> {
        self.read("status")
    }

    /// Thread IO info from `/proc/<pid>/task/<tid>/io`
    ///
    /// This data will be unique per task.
    pub fn io(&self) -> ProcResult<Io> {
        self.read("io")
    }

    /// Thread scheduler info from `/proc/<pid>/task/<tid>/schedstat`
    ///
    /// This data will be unique per task.
    pub fn schedstat(&self) -> ProcResult<Schedstat> {
        self.read("schedstat")
    }

    /// Thread children from `/proc/<pid>/task/<tid>/children`
    ///
    /// WARNING:
    /// This interface is not reliable unless all the child processes are stoppped or frozen.
    /// If a child task exits while the file is being read, non-exiting children may be omitted.
    /// See the procfs(5) man page for more information.
    ///
    /// This data will be unique per task.
    pub fn children(&self) -> ProcResult<Vec<u32>> {
        let mut buf = String::new();
        let mut file = FileWrapper::open_at(&self.root, &self.fd, "children")?;
        file.read_to_string(&mut buf)?;
        buf.split_whitespace()
            .map(|child| {
                child
                    .parse()
                    .map_err(|_| ProcError::Other("Failed to parse task's child PIDs".to_string()))
            })
            .collect()
    }

    /// Deliberately generate an IO error
    #[cfg(test)]
    pub(crate) fn generate_error(&self) -> ProcResult<()> {
        let _ = FileWrapper::open_at(&self.root, &self.fd, "does_not_exist")?;
        Ok(())
    }

    /// Parse a file relative to the task proc structure.
    pub fn read<T: FromRead>(&self, path: &str) -> ProcResult<T> {
        FromRead::from_read(FileWrapper::open_at(&self.root, &self.fd, path)?)
    }
}

#[cfg(test)]
mod tests {
    use crate::process::Io;
    use crate::ProcError;
    use rustix;
    use std::process;
    use std::sync::{Arc, Barrier};

    #[test]
    #[cfg(not(tarpaulin))] // this test is unstable under tarpaulin, and i'm yet sure why
                           // When this test runs in CI, run it single-threaded
    fn test_task_runsinglethread() {
        use std::io::Read;

        let me = crate::process::Process::myself().unwrap();

        let (work_barrier, w_a, w_b) = {
            let b = Arc::new(Barrier::new(3));
            (b.clone(), b.clone(), b)
        };
        let (done_barrier, d_a, d_b) = {
            let b = Arc::new(Barrier::new(3));
            (b.clone(), b.clone(), b)
        };

        let bytes_to_read = 2_000_000;

        // create a new task to do some work
        let join_a = std::thread::Builder::new()
            .name("one".to_owned())
            .spawn(move || {
                let mut vec = Vec::new();
                let zero = std::fs::File::open("/dev/zero").unwrap();

                zero.take(bytes_to_read).read_to_end(&mut vec).unwrap();
                assert_eq!(vec.len(), bytes_to_read as usize);

                // spin for about 52 ticks (utime accounting isn't perfectly accurate)
                let dur = std::time::Duration::from_millis(52 * (1000 / crate::ticks_per_second()) as u64);
                let start = std::time::Instant::now();
                while start.elapsed() <= dur {
                    // spin
                }

                w_a.wait();
                d_a.wait()
            })
            .unwrap();

        // create a new task that does nothing
        let join_b = std::thread::Builder::new()
            .name("two".to_owned())
            .spawn(move || {
                w_b.wait();
                d_b.wait();
            })
            .unwrap();

        work_barrier.wait();

        let mut found_one = false;
        let mut found_two = false;
        let mut summed_io = Io {
            rchar: 0,
            wchar: 0,
            syscr: 0,
            syscw: 0,
            read_bytes: 0,
            write_bytes: 0,
            cancelled_write_bytes: 0,
        };
        for task in me.tasks().unwrap() {
            let task = task.unwrap();
            let stat = task.stat().unwrap();
            let status = task.status().unwrap();
            let io = task.io().unwrap();

            summed_io.rchar += io.rchar;
            summed_io.wchar += io.wchar;
            summed_io.syscr += io.syscr;
            summed_io.syscw += io.syscw;
            summed_io.read_bytes += io.read_bytes;
            summed_io.write_bytes += io.write_bytes;
            summed_io.cancelled_write_bytes += io.cancelled_write_bytes;

            if stat.comm == "one" && status.name == "one" {
                found_one = true;
                assert!(io.rchar >= bytes_to_read);
                assert!(stat.utime >= 50, "utime({}) too small", stat.utime);
            }
            if stat.comm == "two" && status.name == "two" {
                found_two = true;
                // The process might read miscellaneous things from procfs or
                // things like /sys/devices/system/cpu/online; allow some small
                // reads.
                assert!(io.rchar < bytes_to_read);
                assert_eq!(io.wchar, 0);
                assert_eq!(stat.utime, 0);
            }
        }

        let proc_io = me.io().unwrap();
        // these should be mostly the same (though creating the IO struct in the above line will cause some IO to occur)
        println!("{:?}", summed_io);
        println!("{:?}", proc_io);

        // signal the threads to exit
        done_barrier.wait();
        join_a.join().unwrap();
        join_b.join().unwrap();

        assert!(found_one);
        assert!(found_two);
    }

    #[test]
    fn test_task_children() {
        // Use tail -f /dev/null to create two infinite processes
        let mut command = process::Command::new("tail");
        command.arg("-f").arg("/dev/null");
        let (mut child1, mut child2) = (command.spawn().unwrap(), command.spawn().unwrap());

        let tid = rustix::thread::gettid();

        let children = crate::process::Process::myself()
            .unwrap()
            .task_from_tid(tid.as_raw_nonzero().get() as i32)
            .unwrap()
            .children()
            .unwrap();

        assert!(children.contains(&child1.id()));
        assert!(children.contains(&child2.id()));

        child1.kill().unwrap();
        child2.kill().unwrap();
    }

    #[test]
    fn test_error_msg() {
        let myself = crate::process::Process::myself().unwrap();
        // let mytask = myself.task_main_thread().unwrap();
        for task in myself.tasks().unwrap() {
            let task = task.unwrap();
            let err = task.generate_error().unwrap_err();
            // make sure the contained path in the error is correct
            if let ProcError::NotFound(Some(p)) = err {
                assert!(
                    p.display()
                        .to_string()
                        .ends_with(&format!("/task/{}/does_not_exist", task.tid)),
                    "NotFound path({:?}) doesn't look right",
                    p
                );
            } else {
                panic!("Unexpected error from task.generate_error()");
            }
        }
    }
}