-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
windows.lisp
233 lines (207 loc) · 8.36 KB
/
windows.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
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
(in-package #:org.shirakumo.machine-state)
(cffi:define-foreign-library psapi
(:windows "Psapi.dll"))
(cffi:define-foreign-library ntdll
(:windows "Ntdll.dll"))
(cffi:use-foreign-library psapi)
(cffi:use-foreign-library ntdll)
(defmacro windows-call (function &rest args)
`(unless (cffi:foreign-funcall ,function ,@args)
(fail (org.shirakumo.com-on:error-message))))
(defmacro nt-call (function &rest args)
`(let ((value (cffi:foreign-funcall ,function ,@args)))
(unless (= 0 value)
(fail (org.shirakumo.com-on:error-message
(cffi:foreign-funcall "LsaNtStatusToWinError" :uint32 value :ulong))))))
(defun process ()
(cffi:foreign-funcall "GetCurrentProcess" :pointer))
(cffi:defcstruct (io-counters :conc-name io-counters-)
(reads :ullong)
(writes :ullong)
(others :ullong)
(read-bytes :ullong)
(write-bytes :ullong)
(other-bytes :ullong))
(define-implementation process-io-bytes ()
(cffi:with-foreign-object (io-counters '(:struct io-counters))
(windows-call "GetProcessIoCounters"
:pointer (process)
:pointer io-counters
:bool)
(+ (io-counters-read-bytes io-counters)
(io-counters-write-bytes io-counters)
(io-counters-other-bytes io-counters))))
(cffi:defcstruct (memory-counters :conc-name memory-counters-)
(cb :uint32)
(page-fault-count :uint32)
(peak-working-set-size :size)
(working-set-size :size)
(quota-peak-paged-pool-usage :size)
(quota-paged-pool-usage :size)
(quota-peak-non-paged-pool-usage :size)
(quota-non-paged-pool-usage :size)
(pagefile-usage :size)
(peak-page-file-usage :size))
(define-implementation process-room ()
(cffi:with-foreign-objects ((memory-counters '(:struct memory-counters)))
(windows-call "GetProcessMemoryInfo"
:pointer (process)
:pointer memory-counters
:bool)
(memory-counters-working-set-size memory-counters)))
(define-implementation process-time ()
(cffi:with-foreign-objects ((creation-time :uint64)
(exit-time :uint64)
(kernel-time :uint64)
(user-time :uint64))
(windows-call "GetProcessTimes"
:pointer (process)
:pointer creation-time
:pointer exit-time
:pointer kernel-time
:pointer user-time
:bool)
(* (float (cffi:mem-ref user-time :uint64) 0d0)
10d-9)))
(cffi:defcstruct (memory-status :conc-name memory-status-)
(length :uint32)
(memory-load :uint32)
(total-physical :uint64)
(available-physical :uint64)
(total-page-file :uint64)
(available-page-file :uint64)
(total-virtual :uint64)
(available-virtual :uint64)
(available-extended-virtual :uint64))
(define-implementation machine-room ()
(cffi:with-foreign-objects ((memory-status '(:struct memory-status)))
(let ((available (memory-status-available-physical memory-status))
(total (memory-status-total-physical memory-status)))
(values (- total available)
total))))
(cffi:defcstruct (system-info :conc-name system-info-)
(oem-id :uint32)
(page-size :uint32)
(minimum-application-address :pointer)
(maximum-application-address :pointer)
(active-processor-mask :uint64)
(number-of-processors :uint32)
(processor-type :uint32)
(allocation-granularity :uint32)
(processor-level :uint16)
(processor-revision :uint16))
(define-implementation machine-cores ()
(cffi:with-foreign-objects ((system-info '(:struct system-info)))
(cffi:foreign-funcall "GetSystemInfo"
:pointer system-info
:void)
(system-info-number-of-processors system-info)))
(define-implementation machine-uptime ()
(cffi:with-foreign-objects ((time :long-long)
(freq :long-long))
(windows-call "QueryUnbiasedInterruptTime" :pointer time :bool)
(values (round (cffi:mem-ref time :long-long) 10000000))))
(defmacro with-thread-handle ((handle thread &optional (default 0)) &body body)
`(if (or (eql ,thread T)
(eql ,thread (bt:current-thread)))
(let ((,handle (cffi:foreign-funcall "GetCurrentThread" :pointer)))
,@body)
,default))
(define-implementation thread-time (thread)
(with-thread-handle (handle thread)
(cffi:with-foreign-objects ((creation-time :uint64)
(exit-time :uint64)
(kernel-time :uint64)
(user-time :uint64))
(windows-call "GetThreadTimes"
:pointer handle
:pointer creation-time
:pointer exit-time
:pointer kernel-time
:pointer user-time
:bool)
(* (float (cffi:mem-ref user-time :uint64) 0d0)
10d-9))))
(cffi:defcstruct (thread-info :conc-name thread-info-)
(exit-status :uint32)
(base-address :pointer)
(process :pointer)
(thread :pointer)
(affinity-mask :uint64)
(priority :long)
(base-priority :long))
(define-implementation thread-core-mask (thread)
(with-thread-handle (handle thread (1- (ash 1 (machine-cores))))
(cffi:with-foreign-objects ((info '(:struct thread-info)))
(cffi:foreign-funcall "NtQueryInformationThread"
:pointer handle
:int #x04
:pointer info
:ulong (cffi:foreign-type-size '(:struct thread-info))
:uint32)
(thread-info-affinity-mask info))))
(define-implementation (setf thread-core-mask) (mask thread)
(with-thread-handle (handle thread (1- (ash 1 (machine-cores))))
(if (= 0 (cffi:foreign-funcall "SetThreadAffinityMask"
:pointer handle
:uint64 mask
:uint64))
(fail (org.shirakumo.com-on:error-message))
mask)))
(define-implementation process-priority ()
(let ((value (cffi:foreign-funcall "GetPriorityClass" :pointer (process))))
(case (cffi:foreign-funcall "GetPriorityClass" :pointer (process))
(#x00000000 (fail (org.shirakumo.com-on:error-message)))
(#x00000040 :idle)
(#x00004000 :low)
(#x00000020 :normal)
(#x00000080 :high)
(#x00000100 :realtime)
(T :normal))))
(define-implementation (setf process-priority) (priority)
(windows-call "SetPriorityClass"
:pointer (process)
:uint16 (ecase priority
(:idle #x00000040)
(:low #x00004000)
(:normal #x00000020)
(:high #x00000080)
(:realtime #x00000100))
:bool)
priority)
(define-implementation thread-priority (thread)
(with-thread-handle (handle thread :normal)
(let ((value (cffi:foreign-funcall "GetThreadPriority" :pointer handle :uint)))
(when (= value 2147483647)
(fail (org.shirakumo.com-on:error-message)))
(cond ((< value -8) :idle)
((< value 0) :low)
((= value 0) :normal)
((< value +8) :high)
(T :realtime)))))
(define-implementation (setf thread-priority) (thread priority)
(with-thread-handle (handle thread :normal)
(windows-call "SetThreadPriority"
:pointer handle
:int (ecase priority
(:idle -15)
(:low -1)
(:normal 0)
(:high 2)
(:realtime 15))
:bool)
priority))
(define-implementation storage-room (path)
(cffi:with-foreign-objects ((available-to-caller :int64)
(total :int64)
(available :int64))
(org.shirakumo.com-on:with-wstring (str (pathname-utils:native-namestring
(pathname-utils:to-directory path)))
(windows-call "GetDiskFreeSpaceExW"
:pointer str
:pointer available-to-caller
:pointer total
:pointer available
:bool))
(values (cffi:mem-ref available :int64)
(cffi:mem-ref total :int64))))