forked from microsoft/diskspd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmdRequestCreator.cpp
195 lines (169 loc) · 6.13 KB
/
CmdRequestCreator.cpp
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
/*
DISKSPD
Copyright(c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// CmdRequestCreator.cpp : Defines the entry point for the console application.
//
#include "CmdRequestCreator.h"
#include <windows.h>
#include <stdlib.h>
#include <assert.h>
#include "common.h"
#include "errors.h"
#include "CmdLineParser.h"
#include "XmlProfileParser.h"
#include "IORequestGenerator.h"
#include "ResultParser.h"
#include "XmlResultParser.h"
/*****************************************************************************/
// global variables
static HANDLE g_hAbortEvent = NULL; // handle to the 'abort' event
// it allows stopping I/O Request Generator in the middle of its work
// the results of its work will be passed to the Results Parser
static HANDLE g_hEventStarted = NULL; // event signalled to notify that the actual (measured) test is to be started
static HANDLE g_hEventFinished = NULL; // event signalled to notify that the actual test has finished
/*****************************************************************************/
// wrapper for printf. printf cannot be used directly, because IORequestGenerator.dll
// may be consumed by gui app which doesn't have stdout
void WINAPI PrintOut(const char *format, va_list args)
{
vprintf(format, args);
}
/*****************************************************************************/
// wrapper for fprintf. fprintf cannot be used directly, because IORequestGenerator.dll
// may be consumed by gui app which doesn't have stdout
void WINAPI PrintError(const char *format, va_list args)
{
vfprintf(stderr, format, args);
}
/*****************************************************************************/
BOOL WINAPI ctrlCRoutine(DWORD dwCtrlType)
{
if( CTRL_C_EVENT == dwCtrlType )
{
printf("\n*** Interrupted by Ctrl-C. Stopping I/O Request Generator. ***\n");
if( !SetEvent(g_hAbortEvent) )
{
fprintf(stderr, "Warning: Setting abort event failed (error code: %u)\n", GetLastError());
}
SetConsoleCtrlHandler(ctrlCRoutine, FALSE);
//indicate that the signal has been handled
return TRUE;
}
else
{
return FALSE;
}
}
/*****************************************************************************/
void TestStarted()
{
if( (NULL != g_hEventStarted) && !SetEvent(g_hEventStarted) )
{
fprintf(stderr, "Warning: Setting test start notification event failed (error code: %u)\n", GetLastError());
}
}
/*****************************************************************************/
void TestFinished()
{
if( (NULL != g_hEventFinished) && !SetEvent(g_hEventFinished) )
{
fprintf(stderr, "Warning: Setting test finish notification event failed (error code: %u)\n", GetLastError());
}
}
/*****************************************************************************/
int __cdecl main(int argc, const char* argv[])
{
//
// parse cmd line parameters
//
struct Synchronization synch; //sychronization structure
synch.ulStructSize = sizeof(synch);
synch.hStopEvent = NULL;
synch.hStartEvent = NULL;
CmdLineParser cmdLineParser;
Profile profile;
if (!cmdLineParser.ParseCmdLine(argc, argv, &profile, &synch, &g_SystemInformation))
{
return ERROR_PARSE_CMD_LINE;
}
synch.pfnCallbackTestStarted = TestStarted;
synch.pfnCallbackTestFinished = TestFinished;
//
// create abort event if stop event is not explicitly provided by the user (otherwise use the stop event)
//
if (NULL == synch.hStopEvent)
{
synch.hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if( NULL == synch.hStopEvent )
{
fprintf(stderr, "Unable to create an abort event for CTRL+C\n");
//FUTURE EXTENSION: change error code
return 1;
}
}
g_hAbortEvent = synch.hStopEvent; // set abort event to either stop event provided by user or the just created event
//
// capture ctrl+c
//
if( !SetConsoleCtrlHandler(ctrlCRoutine, TRUE) )
{
fprintf(stderr, "Unable to set CTRL+C routine\n");
//FUTURE EXTENSION: change error code
return 1;
}
//
// call IO request generator
//
ResultParser resultParser;
XmlResultParser xmlResultParser;
IResultParser *pResultParser = nullptr;
if (profile.GetResultsFormat() == ResultsFormat::Xml)
{
pResultParser = &xmlResultParser;
}
else
{
pResultParser = &resultParser;
}
IORequestGenerator ioGenerator;
if (!ioGenerator.GenerateRequests(profile, *pResultParser, (PRINTF)PrintOut, (PRINTF)PrintError, (PRINTF)PrintOut, &synch))
{
fprintf(stderr, "Error generating I/O requests\n");
return 1;
}
if( NULL != synch.hStartEvent )
{
CloseHandle(synch.hStartEvent);
}
if( NULL != synch.hStopEvent )
{
CloseHandle(synch.hStopEvent);
}
if( g_hEventStarted )
{
CloseHandle(g_hEventStarted);
}
if( NULL != g_hEventFinished )
{
CloseHandle(g_hEventFinished);
}
return 0;
}