1 | /* $Id: ntsetfreq.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Set the NT timer frequency.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/nt/nt.h>
|
---|
32 |
|
---|
33 | #include <iprt/initterm.h>
|
---|
34 | #include <iprt/getopt.h>
|
---|
35 | #include <iprt/message.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | int main(int argc, char **argv)
|
---|
43 | {
|
---|
44 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
45 | if (RT_FAILURE(rc))
|
---|
46 | return RTMsgInitFailure(rc);
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Parse arguments.
|
---|
50 | */
|
---|
51 | bool fVerbose = true;
|
---|
52 | uint32_t u32NewRes = 0;
|
---|
53 | uint32_t cSecsSleep = UINT32_MAX;
|
---|
54 |
|
---|
55 | static const RTGETOPTDEF s_aOptions[] =
|
---|
56 | {
|
---|
57 | { "--resolution", 'r', RTGETOPT_REQ_UINT32 },
|
---|
58 | { "--sleep", 's', RTGETOPT_REQ_UINT32 },
|
---|
59 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
60 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
61 | };
|
---|
62 |
|
---|
63 | RTGETOPTUNION ValueUnion;
|
---|
64 | RTGETOPTSTATE GetState;
|
---|
65 | RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
|
---|
66 | while ((rc = RTGetOpt(&GetState, &ValueUnion)))
|
---|
67 | {
|
---|
68 | switch (rc)
|
---|
69 | {
|
---|
70 | case 'r':
|
---|
71 | u32NewRes = ValueUnion.u32;
|
---|
72 | if (u32NewRes > 16*10000 /* 16 ms */ || u32NewRes < 1000 /* 100 microsec */)
|
---|
73 | return RTMsgErrorExit(RTEXITCODE_SYNTAX,
|
---|
74 | "syntax error: the new timer resolution (%RU32) is out of range\n",
|
---|
75 | u32NewRes);
|
---|
76 | break;
|
---|
77 |
|
---|
78 | case 's':
|
---|
79 | cSecsSleep = ValueUnion.u32;
|
---|
80 | break;
|
---|
81 |
|
---|
82 | case 'q':
|
---|
83 | fVerbose = false;
|
---|
84 | break;
|
---|
85 |
|
---|
86 | case 'v':
|
---|
87 | fVerbose = true;
|
---|
88 | break;
|
---|
89 |
|
---|
90 | case 'h':
|
---|
91 | RTPrintf("Usage: ntsetfreq [-q|--quiet] [-v|--verbose] [-r|--resolution <100ns>] [-s|--sleep <1s>]\n");
|
---|
92 | return RTEXITCODE_SUCCESS;
|
---|
93 |
|
---|
94 | default:
|
---|
95 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Query the current resolution.
|
---|
102 | */
|
---|
103 | ULONG Cur = UINT32_MAX;
|
---|
104 | ULONG Min = UINT32_MAX;
|
---|
105 | ULONG Max = UINT32_MAX;
|
---|
106 | NTSTATUS rcNt = STATUS_SUCCESS;
|
---|
107 | if (fVerbose || !u32NewRes)
|
---|
108 | {
|
---|
109 | rcNt = NtQueryTimerResolution(&Min, &Max, &Cur);
|
---|
110 | if (NT_SUCCESS(rcNt))
|
---|
111 | RTMsgInfo("cur: %u (%u.%02u Hz) min: %u (%u.%02u Hz) max: %u (%u.%02u Hz)\n",
|
---|
112 | Cur, 10000000 / Cur, (10000000 / (Cur * 100)) % 100,
|
---|
113 | Min, 10000000 / Min, (10000000 / (Min * 100)) % 100,
|
---|
114 | Max, 10000000 / Max, (10000000 / (Max * 100)) % 100);
|
---|
115 | else
|
---|
116 | RTMsgError("NTQueryTimerResolution failed with status %#x\n", rcNt);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (u32NewRes)
|
---|
120 | {
|
---|
121 | rcNt = NtSetTimerResolution(u32NewRes, TRUE, &Cur);
|
---|
122 | if (!NT_SUCCESS(rcNt))
|
---|
123 | RTMsgError("NTSetTimerResolution(%RU32,,) failed with status %#x\n", u32NewRes, rcNt);
|
---|
124 | else if (fVerbose)
|
---|
125 | {
|
---|
126 | Cur = Min = Max = UINT32_MAX;
|
---|
127 | rcNt = NtQueryTimerResolution(&Min, &Max, &Cur);
|
---|
128 | if (NT_SUCCESS(rcNt))
|
---|
129 | RTMsgInfo("new: %u (%u.%02u Hz) requested %RU32 (%u.%02u Hz)\n",
|
---|
130 | Cur, 10000000 / Cur, (10000000 / (Cur * 100)) % 100,
|
---|
131 | u32NewRes, 10000000 / u32NewRes, (10000000 / (u32NewRes * 100)) % 100);
|
---|
132 | else
|
---|
133 | RTMsgError("NTSetTimerResolution succeeded but the NTQueryTimerResolution call failed with status %#x (ignored)\n",
|
---|
134 | rcNt);
|
---|
135 | rcNt = STATUS_SUCCESS;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (u32NewRes && NT_SUCCESS(rcNt))
|
---|
140 | {
|
---|
141 | if (cSecsSleep == UINT32_MAX)
|
---|
142 | for (;;)
|
---|
143 | RTThreadSleep(RT_INDEFINITE_WAIT);
|
---|
144 | else
|
---|
145 | while (cSecsSleep-- > 0)
|
---|
146 | RTThreadSleep(1000);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return NT_SUCCESS(rcNt) ? 0 : 1;
|
---|
150 | }
|
---|
151 |
|
---|