Skip to content

Commit a2a87f3

Browse files
Aaron LamoreauxXyene
authored andcommitted
Used procfs to give traced threads more helpful names
Signed-off-by: Aaron Lamoreaux <alamoreaux@janestreet.com>
1 parent d072a30 commit a2a87f3

5 files changed

Lines changed: 88 additions & 18 deletions

File tree

src/process_info.ml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
open! Core
2+
open! Import
3+
4+
module Entry = struct
5+
module Cmdline = struct
6+
type t = string list
7+
end
8+
end
9+
10+
let state = Hashtbl.create (module Pid)
11+
12+
let read_proc_info pid =
13+
let line = In_channel.read_lines [%string "/proc/%{pid#Pid}/cmdline"] |> List.hd in
14+
match line with
15+
| None -> ()
16+
| Some args ->
17+
let cmdline =
18+
String.split ~on:(Char.of_int_exn 0) args |> List.filter ~f:(Fn.non String.is_empty)
19+
in
20+
Hashtbl.set state ~key:pid ~data:cmdline
21+
;;
22+
23+
let read_all_proc_info () =
24+
Sys_unix.readdir "/proc"
25+
|> Array.iter ~f:(fun filename ->
26+
try Pid.of_string filename |> read_proc_info with
27+
| _ -> ())
28+
;;
29+
30+
let cmdline_of_pid pid = Hashtbl.find state pid

src/process_info.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
open! Core
2+
open! Import
3+
4+
module Entry : sig
5+
module Cmdline : sig
6+
type t = string list
7+
end
8+
end
9+
10+
val read_proc_info : Pid.t -> unit
11+
val read_all_proc_info : unit -> unit
12+
val cmdline_of_pid : Pid.t -> Entry.Cmdline.t option

src/trace.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
227227
end
228228

229229
let attach (opts : Record_opts.t) ~elf ~debug_print_perf_commands ~subcommand pid =
230+
Process_info.read_all_proc_info ();
230231
let%bind.Deferred.Or_error snap_loc =
231232
match elf with
232233
| None -> return (Ok None)

src/trace_writer.ml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,24 @@ let create_thread t event =
464464
| None -> Mapped_time.start_of_trace
465465
| Some time -> map_time t time
466466
in
467-
let trace_pid =
468-
allocate_pid
469-
t
470-
~name:[%string "%{opt_pid_to_string thread.pid}/%{opt_pid_to_string thread.tid}"]
467+
let pid = opt_pid_to_string thread.pid in
468+
let tid = opt_pid_to_string thread.tid in
469+
let default_name =
470+
if String.(pid = tid)
471+
then [%string "[(p|t)id=%{pid}]"]
472+
else [%string "[pid=%{pid}] [tid=%{tid}]"]
473+
in
474+
let name =
475+
match thread.pid with
476+
| None -> default_name
477+
| Some pid ->
478+
(match Process_info.cmdline_of_pid pid with
479+
| None -> default_name
480+
| Some cmdline ->
481+
let concat_cmdline = String.concat ~sep:" " cmdline in
482+
[%string "%{concat_cmdline} %{default_name}"])
471483
in
484+
let trace_pid = allocate_pid t ~name in
472485
let thread = allocate_thread t ~pid:trace_pid ~name:"main" in
473486
{ Thread_info.thread
474487
; callstack = Callstack.create ~create_time:effective_time

test/test.ml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ let%expect_test "random perfs" =
175175
{|
176176
(Interned_string (index 1) (value process))
177177
(Tick_initialization (ticks_per_second 1000000000))
178-
(Interned_string (index 102) (value 1234/456))
178+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
179179
(Process_name_change (name 102) (pid 1))
180180
(Interned_string (index 103) (value main))
181181
(Thread_name_change (name 103) (pid 1) (tid 2))
182182
(Interned_thread (index 1)
183-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
183+
(value
184+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
185+
(thread_name (main)))))
184186
(Interned_string (index 104) (value address))
185187
(Interned_string (index 105) (value S))
186188
(Interned_string (index 106) (value symbol))
@@ -229,12 +231,14 @@ let%expect_test "random perfs" =
229231
{|
230232
(Interned_string (index 1) (value process))
231233
(Tick_initialization (ticks_per_second 1000000000))
232-
(Interned_string (index 102) (value 1234/456))
234+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
233235
(Process_name_change (name 102) (pid 1))
234236
(Interned_string (index 103) (value main))
235237
(Thread_name_change (name 103) (pid 1) (tid 2))
236238
(Interned_thread (index 1)
237-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
239+
(value
240+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
241+
(thread_name (main)))))
238242
(Interned_string (index 104) (value address))
239243
(Interned_string (index 105) (value "\025J\015\018\023\018"))
240244
(Interned_string (index 106) (value symbol))
@@ -255,12 +259,14 @@ let%expect_test "random perfs" =
255259
{|
256260
(Interned_string (index 1) (value process))
257261
(Tick_initialization (ticks_per_second 1000000000))
258-
(Interned_string (index 102) (value 1234/456))
262+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
259263
(Process_name_change (name 102) (pid 1))
260264
(Interned_string (index 103) (value main))
261265
(Thread_name_change (name 103) (pid 1) (tid 2))
262266
(Interned_thread (index 1)
263-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
267+
(value
268+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
269+
(thread_name (main)))))
264270
(Interned_string (index 104) (value "\025J\015\018\023\018"))
265271
(Interned_string (index 105) (value ""))
266272
(Event
@@ -301,12 +307,14 @@ let%expect_test "random perfs" =
301307
{|
302308
(Interned_string (index 1) (value process))
303309
(Tick_initialization (ticks_per_second 1000000000))
304-
(Interned_string (index 102) (value 1234/456))
310+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
305311
(Process_name_change (name 102) (pid 1))
306312
(Interned_string (index 103) (value main))
307313
(Thread_name_change (name 103) (pid 1) (tid 2))
308314
(Interned_thread (index 1)
309-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
315+
(value
316+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
317+
(thread_name (main)))))
310318
(Interned_string (index 104) (value address))
311319
(Interned_string (index 105) (value S))
312320
(Interned_string (index 106) (value symbol))
@@ -385,12 +393,14 @@ let%expect_test "with initial returns" =
385393
{|
386394
(Interned_string (index 1) (value process))
387395
(Tick_initialization (ticks_per_second 1000000000))
388-
(Interned_string (index 102) (value 1234/456))
396+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
389397
(Process_name_change (name 102) (pid 1))
390398
(Interned_string (index 103) (value main))
391399
(Thread_name_change (name 103) (pid 1) (tid 2))
392400
(Interned_thread (index 1)
393-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
401+
(value
402+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
403+
(thread_name (main)))))
394404
(Interned_string (index 104) (value "\025J\015\018\023\018"))
395405
(Interned_string (index 105) (value ""))
396406
(Event
@@ -520,12 +530,14 @@ let%expect_test "time batch spreading" =
520530
{|
521531
(Interned_string (index 1) (value process))
522532
(Tick_initialization (ticks_per_second 1000000000))
523-
(Interned_string (index 102) (value 1234/456))
533+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
524534
(Process_name_change (name 102) (pid 1))
525535
(Interned_string (index 103) (value main))
526536
(Thread_name_change (name 103) (pid 1) (tid 2))
527537
(Interned_thread (index 1)
528-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
538+
(value
539+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
540+
(thread_name (main)))))
529541
(Interned_string (index 104) (value address))
530542
(Interned_string (index 105) (value sub))
531543
(Interned_string (index 106) (value symbol))
@@ -610,12 +622,14 @@ let%expect_test "enqueing events at start" =
610622
{|
611623
(Interned_string (index 1) (value process))
612624
(Tick_initialization (ticks_per_second 1000000000))
613-
(Interned_string (index 102) (value 1234/456))
625+
(Interned_string (index 102) (value "[pid=1234] [tid=456]"))
614626
(Process_name_change (name 102) (pid 1))
615627
(Interned_string (index 103) (value main))
616628
(Thread_name_change (name 103) (pid 1) (tid 2))
617629
(Interned_thread (index 1)
618-
(value ((pid 1) (tid 2) (process_name (1234/456)) (thread_name (main)))))
630+
(value
631+
((pid 1) (tid 2) (process_name ("[pid=1234] [tid=456]"))
632+
(thread_name (main)))))
619633
(Interned_string (index 104) (value fn3))
620634
(Interned_string (index 105) (value ""))
621635
(Event

0 commit comments

Comments
 (0)