-
Notifications
You must be signed in to change notification settings - Fork 2
/
GetVmlatency.ps1
277 lines (229 loc) · 7.72 KB
/
GetVmlatency.ps1
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<#
.SYNOPSIS
Build vmlatency driver and measure VM-Entry and VM-Exit turnaroound time.
.DESCRIPTION
This scripts builds vmlatency driver and measures VM-Entry and VM-Exit
turnaroound time in cycles.
.PARAMETER Command
Supported commands:
"build" Build vmlatency kernel driver.
"measure" Default. Build vmatency kernel driver and measure VM-Entry and
VM-Exit turnaround time.
"set-testsigning" Switch current host to test mode and create self-signed
certificate.
"clean-testsigning" Revert changes made by "set-testsigning" command.
#>
Param (
[ValidateSet("build", "measure", "set-testsigning",
"clean-testsigning")][String]$Command = "measure",
[Switch]$UseICC=$False
)
$VmlatencyRoot = Split-Path -Parent -Path $Script:MyInvocation.MyCommand.Definition
$WDKRoot = "C:\WinDDK\7600.16385.1"
$CertificateSubject = "VmlatencyTestCertificate"
Function Build([String]$Component) {
Echo "Building Vmlatency ($Component)"
$Cmds = "/C"
$Cmds += " $WDKRoot\bin\setenv.bat $WDKRoot fre x64 WIN7"
If ($Script:ICCPath) {
$Cmds += " & set SUBSTITUTE_AMD64_CC=`"$Script:ICCPath`""
# Disable Inter-Procedural Optimization (IPO). Windows linker does not
# support link time optimizable objects produced with this optimization
$Cmds += " & set USER_C_FLAGS=/Qipo-"
}
$Cmds += " & cd /d $VmlatencyRoot\$Component"
$Cmds += " & build /cfFgwb"
$P = (Start-Process cmd -ArgumentList $Cmds -Wait -NoNewWindow -PassThru)
If (-Not $P.ExitCode -Eq 0) {
Echo "*** Failed to build Vmlatency ($Component)"
Exit 1
}
}
Function Setup-EventLog([String]$Driver) {
$Xreg = 'HKLM:SYSTEM\CurrentControlSet\Services\EventLog\System\vmlatency'
$Param = @{
'Path' = $Xreg
'ErrorAction' = 'SilentlyContinue'
}
If (-Not (Get-Item @Param)) {
$Param = @{
'Path' = 'HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\System'
'name' = 'vmlatency'
}
New-Item @Param > $null
}
$Param = @{
'Path' = $Xreg
'Name' = 'EventMessageFile'
'PropertyType' = 'String'
'Value' = $Driver
'ErrorAction' = 'SilentlyContinue'
}
New-ItemProperty @Param > $null
$Param = @{
'Path' = $Xreg
'Name' = 'TypesSupported'
'PropertyType' = 'Dword'
'Value' = 0x00000007
'ErrorAction' = 'SilentlyContinue'
}
New-ItemProperty @Param > $null
}
Function Load-Vmlatency() {
$Driver = "$VmlatencyRoot\build-win7-fre\amd64\vmlatency.sys"
Setup-EventLog $Driver
# It's impossible to create kernel service using New-Service cmdlet
$ScArgs = "create vmlatency binPath=$Driver type=kernel"
$P = (Start-Process sc.exe -ArgumentList $ScArgs -Wait -NoNewWindow -PassThru)
If (-Not $P.ExitCode -Eq 0) {
Echo "*** Failed to create vmatency service"
Exit 1
}
$Vmlatency = Get-Service vmlatency
try {
$Vmlatency.Start()
} catch {
Echo "*** Failed to start vmlatency service"
Echo "*** Look at system logs for more information"
Unload-Vmlatency
Exit 1
}
}
Function Unload-Vmlatency() {
$Vmlatency = Get-Service vmlatency -ErrorAction SilentlyContinue
If (-Not $Vmlatency) { Return }
If ($Vmlatency.Status -eq 'Running') { $Vmlatency.Stop() }
# Remove-Service cmdlet is available starting with PowerShell 6.0
$ScArgs = "delete vmlatency"
$P = (Start-Process sc.exe -ArgumentList $ScArgs -Wait -NoNewWindow -PassThru)
If (-Not $P.ExitCode -Eq 0) {
Echo "*** Failed to delete vmlatency service"
Exit 1
}
$Param = @{
'Path' = 'HKLM:SYSTEM\CurrentControlSet\Services\EventLog\System\vmlatency'
'ErrorAction' = 'SilentlyContinue'
}
Remove-Item @Param > $null
}
Function SetupBuildEnvironment() {
If (-Not (Test-Path $WDKRoot)) {
Echo "*** Windows Driver Kit Version 7 is not found"
Exit 1
}
Echo "Using WDK='$WDKRoot'"
If ($UseICC) {
ForEach ($ICC in Get-Item Env:ICPP_COMPILER*) {
If ($LatestICC.name -lt $ICC.name) { $LatestICC = $ICC }
}
If (-Not $LatestICC) {
Echo "ICC not found"
Exit 1
}
$Script:ICCPath = $LatestICC.value + "\bin\intel64\icl.exe"
Echo "Using ICC='$Script:ICCPath'"
}
}
Function Sign([String]$File) {
$SignTool = "$WDKRoot\bin\amd64\SignTool.exe"
$Args = " sign /t http://timestamp.digicert.com"
$Args += " /n $CertificateSubject"
$Args += " $File"
Echo "Signing $File"
$P = (Start-Process $SignTool -ArgumentList $Args -NoNewWindow -PassThru)
$P.WaitForExit()
If ($P.ExitCode -Eq 1) {
Echo "*** Failed to sign $File"
Exit 1
}
}
Function SetupSelfSigning() {
Foreach ($c in (Get-ChildItem -Path "Cert:\CurrentUser\My")) {
If ($c.Subject -Eq "CN=$CertificateSubject") {
$t = $c.Thumbprint;
Break
}
}
If ($t) {
Echo "Self-signed certificate for vmlatency $t already exists."
} Else {
$Param = @{
'CertStoreLocation' = "Cert:\CurrentUser\My"
'Subject' = "CN=$CertificateSubject"
'Type' = "CodeSigningCert"
}
$c = New-SelfSignedCertificate @Param
If (-Not $c) {
Echo "Failed to create self-signed certificate."
Exit 1
}
Echo ("Self-signed certificate " + $c.Thumbprint + " created.")
}
SetTestsigning "ON"
}
Function CleanupSelfSigning() {
SetTestsigning "OFF"
Foreach ($c in (Get-ChildItem -Path "Cert:\" -Recurse)) {
If ($c.Subject -Eq "CN=VmlatencyTestCertificate") { Rm $c.PsPath }
}
}
Function SetTestsigning($Value) {
$CheckTestSigningCmd = "bcdedit.exe /v | Select-String testsigning"
$CheckTestSigningCmd += " | Select-String Yes"
$SetTestSigningCmd = "bcdedit.exe /set testsigning $Value"
$ExitUnchanged = 104
$ExitReboot = 1077
$Change = "{ $SetTestSigningCmd; Exit $ExitReboot }"
$AlreadySet = "{ Exit $ExitUnchanged }"
If ($Value -Eq "ON") {
$Cmd = "If ($CheckTestSigningCmd) $AlreadySet Else $Change"
} ElseIf ($Value -Eq "OFF") {
$Cmd = "If ($CheckTestSigningCmd) $Change Else $AlreadySet"
} Else {
Echo "Unsupported testsigning value $Value."
Exit 1
}
$Param = @{
'Verb' = 'runAs'
'ArgumentList' = "$Cmd"
'WindowStyle' = 'Hidden'
}
$p = (Start-Process powershell @Param -PassThru)
$p.WaitForExit()
If (-Not $p){
Echo "Failed to elevate"
Exit 0
}
If ($p.ExitCode -Eq $ExitUnchanged) {
Echo "Testsigning mode is already $Value."
} ElseIf ($p.ExitCode -Eq $ExitReboot) {
Echo "Testsigning mode changed to $Value."
Echo "Reboot is required for the changes to take effect."
} Else {
Echo ("Failed to set testsigning to $Value. ExitCode: " + $p.ExitCode)
Exit 1
}
}
Function Dump-Results([System.DateTime]$StartTime) {
$CPUName = @(Get-WmiObject win32_processor)[0].name
$L = (Get-EventLog -LogName System -Message *vmlatency]* -After $StartTime)
[Array]::Reverse($L)
$Data = $L.Message.Replace("[vmlatency] ", "").Replace("`n", "")
Set-Content -Path ($CPUName + ".txt") -Value $Data
}
Switch ($Command) {
"set-testsigning" { SetupSelfSigning }
"clean-testsigning" { CleanupSelfSigning }
Default {
SetupBuildEnvironment
Build vmm
Build win
If ($Command -Eq "measure") {
Sign "$VmlatencyRoot\build-win7-fre\amd64\vmlatency.sys"
$StartTime = Get-Date
Load-Vmlatency
Dump-Results $StartTime
Unload-Vmlatency
}
}
}