-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmt.lisp
140 lines (109 loc) · 4.69 KB
/
dmt.lisp
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
;;;; dmt.lisp
;(defpackage #:dmt
; (:use #:cl #:cffi)
(in-package #:dmt)
(defconstant +page-size+ 4096)
;; C bindings
(defctype :pid-t :long)
(defcfun "kill" :int
(pid-t :int) (signal :int))
;; Linux process data structures
(macrolet ((def-proc-struct (name args)
`(defstruct (,name
(:constructor ,(intern (format nil "MAKE-~a" name))
,args))
,@args)))
(def-proc-struct proc-stat
(pid comm state ppid pgrp session tty-nr tpgid
flags minflt cminflt majflt cmajflt utime
stime cutime cstime priority nice num-threads
itrealvalue starttime vsize rss rsslim startcode
endcode startstack kstkesp kstkeip signal blocked
sigignore sigcatch wchan nswap cnswap exit-signal processor
rt-priority policy delayacct-blkio-ticks guest-time cguest-time
start-data end-data start-brk arg-start arg-end env-start env-end
exit-codeq))
(def-proc-struct proc-stat-memory
(size resident shared text data)))
;; The Process class
(defclass process ()
((pid :initarg :pid
:accessor process-pid)
(stat :initarg :stat
:accessor process-stat)
(stat-memory :initarg :stat-memory
:accessor process-stat-memory)
(cmdline :initarg :cmdline)))
(defmethod process-cmdline ((obj process))
(str:trim (format nil "~{~A ~}" (slot-value obj 'cmdline))))
(defun make-process (pid)
"Queries OS for process PID and initializes a process object"
(make-instance 'process
:pid pid
:stat (proc-stat pid)
:stat-memory (proc-stat-memory pid)
:cmdline (proc-cmdline pid)))
(defun proc-pathname (pid &optional file)
(format nil "/proc/~d/~(~a~)" pid (or file "")))
(defun proc-stat-memory (pid)
(let* ((path (proc-pathname pid 'statm))
(stat (uiop:read-file-forms path)))
(destructuring-bind (size resident shared _ text _ data) stat
(make-proc-stat-memory size resident shared text data))))
(defun proc-cmdline (pid)
(let ((path (proc-pathname pid 'cmdline)))
(str:split-omit-nulls "