Skip to content

Commit efc0c27

Browse files
committed
feat: add procfs process tracking
1 parent 018a806 commit efc0c27

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

lading/src/observer/linux/procfs.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ impl Sampler {
8383
// A tally of the total RSS and PSS consumed by the parent process and
8484
// its children.
8585
let mut aggr = memory::smaps_rollup::Aggregator::default();
86+
let mut processes_found: i32 = 0;
87+
let mut pids_skipped: FxHashSet<i32> = FxHashSet::default();
8688

8789
// Every sample run we collect all the child processes rooted at the
8890
// parent. As noted by the procfs documentation is this done by
@@ -115,13 +117,17 @@ impl Sampler {
115117
pids.insert(pid);
116118
}
117119
}
118-
}
120+
}
119121
}
120122
}
121123

124+
processes_found += 1;
122125
let pid = process.pid();
123-
if let Err(e) = self.handle_process(process, &mut aggr, include_smaps).await {
124-
warn!("Encountered uncaught error when handling `/proc/{pid}/`: {e}");
126+
match self.handle_process(process, &mut aggr, include_smaps).await {
127+
Ok(false) => {
128+
pids_skipped.insert(pid);
129+
}
130+
true => {}
125131
}
126132
}
127133

@@ -130,10 +136,22 @@ impl Sampler {
130136

131137
gauge!("total_rss_bytes").set(aggr.rss as f64);
132138
gauge!("total_pss_bytes").set(aggr.pss as f64);
139+
gauge!("processes_found").set(processes_found as f64);
140+
141+
// If we skipped any processes, log a warning.
142+
if !pids_skipped.is_empty() {
143+
warn!(
144+
"Skipped {} processes: {:?}",
145+
pids_skipped.len(),
146+
pids_skipped
147+
);
148+
}
133149

134150
Ok(())
135151
}
136152

153+
/// Handle a process. Returns true if the process was handled successfully,
154+
/// false if it was skipped for any reason.
137155
#[allow(
138156
clippy::similar_names,
139157
clippy::too_many_lines,
@@ -146,7 +164,7 @@ impl Sampler {
146164
process: Process,
147165
aggr: &mut memory::smaps_rollup::Aggregator,
148166
include_smaps: bool,
149-
) -> Result<(), Error> {
167+
) -> bool {
150168
let pid = process.pid();
151169

152170
// `/proc/{pid}/status`
@@ -156,12 +174,12 @@ impl Sampler {
156174
warn!("Couldn't read status: {:?}", e);
157175
// The pid may have exited since we scanned it or we may not
158176
// have sufficient permission.
159-
return Ok(());
177+
return false;
160178
}
161179
};
162180
if status.tgid != pid {
163181
// This is a thread, not a process and we do not wish to scan it.
164-
return Ok(());
182+
return false;
165183
}
166184

167185
// If we haven't seen this process before, initialize its ProcessInfo.
@@ -174,7 +192,7 @@ impl Sampler {
174192
warn!("Couldn't read exe for pid {}: {:?}", pid, e);
175193
// The pid may have exited since we scanned it or we may not
176194
// have sufficient permission.
177-
return Ok(());
195+
return false;
178196
}
179197
};
180198
let comm = match proc_comm(pid).await {
@@ -183,7 +201,7 @@ impl Sampler {
183201
warn!("Couldn't read comm for pid {}: {:?}", pid, e);
184202
// The pid may have exited since we scanned it or we may not
185203
// have sufficient permission.
186-
return Ok(());
204+
return false;
187205
}
188206
};
189207
let cmdline = match proc_cmdline(pid).await {
@@ -192,7 +210,7 @@ impl Sampler {
192210
warn!("Couldn't read cmdline for pid {}: {:?}", pid, e);
193211
// The pid may have exited since we scanned it or we may not
194212
// have sufficient permission.
195-
return Ok(());
213+
return false;
196214
}
197215
};
198216
let pid_s = format!("{pid}");
@@ -238,7 +256,7 @@ impl Sampler {
238256
// which will happen if we don't have permissions or, more
239257
// likely, the process has exited.
240258
warn!("Couldn't process `/proc/{pid}/stat`: {e}");
241-
return Ok(());
259+
return false;
242260
}
243261

244262
if include_smaps {
@@ -317,10 +335,10 @@ impl Sampler {
317335
// which will happen if we don't have permissions or, more
318336
// likely, the process has exited.
319337
warn!("Couldn't process `/proc/{pid}/smaps_rollup`: {err}");
320-
return Ok(());
338+
return false;
321339
}
322340

323-
Ok(())
341+
return true;
324342
}
325343
}
326344

0 commit comments

Comments
 (0)