|
1 | 1 | use std::collections::HashMap;
|
2 | 2 | use std::ffi::OsStr;
|
3 |
| -use std::fs::{self, File}; |
| 3 | +use std::fs::{self, File, OpenOptions}; |
4 | 4 | use std::io::{BufRead, BufReader, Write};
|
5 | 5 | use std::path::{Path, PathBuf};
|
6 | 6 | use std::process;
|
|
34 | 34 | .map_err(|e| Error::Write(PathBuf::from(file_path.as_ref()), e))
|
35 | 35 | }
|
36 | 36 |
|
| 37 | +fn inherit_from_parent(path: &mut PathBuf, what: &'static str) -> Result<()> { |
| 38 | + // This unwrap() is safe because path_buf is guaranteed to have a parent. |
| 39 | + let mut parent_what = PathBuf::from(path.parent().unwrap()); |
| 40 | + parent_what.push(what); |
| 41 | + |
| 42 | + let f = OpenOptions::new() |
| 43 | + .read(true) |
| 44 | + .open(&parent_what) |
| 45 | + .map_err(|e| Error::FileOpen(parent_what.clone(), e))?; |
| 46 | + |
| 47 | + let mut line = String::new(); |
| 48 | + BufReader::new(f) |
| 49 | + .read_line(&mut line) |
| 50 | + .map_err(|e| Error::ReadLine(parent_what.clone(), e))?; |
| 51 | + |
| 52 | + path.push(what); |
| 53 | + writeln_special(&path, &line)?; |
| 54 | + path.pop(); |
| 55 | + |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
37 | 59 | impl Cgroup {
|
38 | 60 | pub fn new(id: &str, numa_node: u32, exec_file_name: &OsStr) -> Result<Self> {
|
39 | 61 | let f =
|
@@ -84,17 +106,27 @@ impl Cgroup {
|
84 | 106 |
|
85 | 107 | for (controller, mut path_buf) in found_controllers.drain() {
|
86 | 108 | path_buf.push(exec_file_name);
|
| 109 | + |
| 110 | + // TODO: fix this racy thing. |
| 111 | + if controller == "cpuset" && !path_buf.exists() { |
| 112 | + fs::create_dir_all(&path_buf).map_err(|e| Error::CreateDir(path_buf.clone(), e))?; |
| 113 | + |
| 114 | + inherit_from_parent(&mut path_buf, "cpuset.mems")?; |
| 115 | + inherit_from_parent(&mut path_buf, "cpuset.cpus")?; |
| 116 | + } |
| 117 | + |
87 | 118 | path_buf.push(id);
|
88 | 119 |
|
89 |
| - // Create the folders first. The cpuset.cpus and cpuset.mems files really do appear to |
90 |
| - // be inherited AND populated automatically :-s |
91 | 120 | fs::create_dir_all(&path_buf).map_err(|e| Error::CreateDir(path_buf.clone(), e))?;
|
92 | 121 |
|
93 | 122 | if controller == "cpuset" {
|
94 | 123 | // Enforce NUMA node restriction.
|
95 | 124 | path_buf.push("cpuset.mems");
|
96 | 125 | writeln_special(&path_buf, numa_node)?;
|
97 | 126 | path_buf.pop();
|
| 127 | + |
| 128 | + // Inherit cpuset |
| 129 | + inherit_from_parent(&mut path_buf, "cpuset.cpus")?; |
98 | 130 | }
|
99 | 131 |
|
100 | 132 | // And now add "tasks" to get the path of the corresponding tasks file.
|
|
0 commit comments