Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

609 lines
13 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include <string.h>
  8. #define ONEK 1000
  9. #define FIVEK 5000
  10. #define TENK 10000
  11. #define ONEHUNK 100000
  12. #define ONEMIL 1000000
  13. //
  14. // Define local types.
  15. //
  16. typedef struct _PERFINFO {
  17. DWORD StartTime;
  18. DWORD StopTime;
  19. LPSTR Title;
  20. DWORD Iterations;
  21. } PERFINFO, *PPERFINFO;
  22. VOID
  23. FinishBenchMark (
  24. IN PPERFINFO PerfInfo
  25. )
  26. {
  27. DWORD ContextSwitches;
  28. DWORD Duration;
  29. DWORD Performance;
  30. //
  31. // Print results and announce end of test.
  32. //
  33. PerfInfo->StopTime = GetTickCount();
  34. Duration = PerfInfo->StopTime - PerfInfo->StartTime;
  35. printf(" Test time in milliseconds %d\n", Duration);
  36. printf(" Number of iterations %d\n", PerfInfo->Iterations);
  37. Performance = PerfInfo->Iterations * 1000 / Duration;
  38. printf(" Iterations per second %d\n", Performance);
  39. printf("*** End of Test ***\n\n");
  40. return;
  41. }
  42. VOID
  43. StartBenchMark (
  44. IN PCHAR Title,
  45. IN DWORD Iterations,
  46. IN PPERFINFO PerfInfo
  47. )
  48. {
  49. //
  50. // Announce start of test and the number of iterations.
  51. //
  52. printf("*** Start of test ***\n %s\n", Title);
  53. PerfInfo->Title = Title;
  54. PerfInfo->Iterations = Iterations;
  55. PerfInfo->StartTime = GetTickCount();
  56. return;
  57. }
  58. HANDLE
  59. APIENTRY
  60. FastFindOpenDir(
  61. LPCWSTR lpFileName
  62. )
  63. {
  64. HANDLE hFindFile;
  65. NTSTATUS Status;
  66. OBJECT_ATTRIBUTES Obja;
  67. UNICODE_STRING FileName;
  68. UNICODE_STRING PathName;
  69. IO_STATUS_BLOCK IoStatusBlock;
  70. BOOLEAN TranslationStatus;
  71. RTL_RELATIVE_NAME RelativeName;
  72. PVOID FreeBuffer;
  73. UNICODE_STRING UnicodeInput;
  74. BOOLEAN EndsInDot;
  75. RtlInitUnicodeString(&UnicodeInput,lpFileName);
  76. //
  77. // Bogus code to workaround ~* problem
  78. //
  79. if ( UnicodeInput.Buffer[(UnicodeInput.Length>>1)-1] == (WCHAR)'.' ) {
  80. EndsInDot = TRUE;
  81. }
  82. else {
  83. EndsInDot = FALSE;
  84. }
  85. TranslationStatus = RtlDosPathNameToNtPathName_U(
  86. lpFileName,
  87. &PathName,
  88. &FileName.Buffer,
  89. &RelativeName
  90. );
  91. if ( !TranslationStatus ) {
  92. return NULL;
  93. }
  94. FreeBuffer = PathName.Buffer;
  95. InitializeObjectAttributes(
  96. &Obja,
  97. &PathName,
  98. OBJ_CASE_INSENSITIVE,
  99. NULL,
  100. NULL
  101. );
  102. //
  103. // Open the directory for list access
  104. //
  105. Status = NtOpenFile(
  106. &hFindFile,
  107. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  108. &Obja,
  109. &IoStatusBlock,
  110. FILE_SHARE_READ | FILE_SHARE_WRITE,
  111. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  112. );
  113. if ( !NT_SUCCESS(Status) ) {
  114. return NULL;
  115. }
  116. return hFindFile;
  117. }
  118. BOOL
  119. FastFind(
  120. HANDLE hFindFile,
  121. LPCWSTR lpFileName,
  122. LPWIN32_FIND_DATAW lpFindFileData
  123. )
  124. {
  125. NTSTATUS Status;
  126. OBJECT_ATTRIBUTES Obja;
  127. UNICODE_STRING FileName;
  128. UNICODE_STRING PathName;
  129. IO_STATUS_BLOCK IoStatusBlock;
  130. BOOLEAN TranslationStatus;
  131. RTL_RELATIVE_NAME RelativeName;
  132. PVOID FreeBuffer;
  133. UNICODE_STRING UnicodeInput;
  134. BOOLEAN EndsInDot;
  135. PFILE_BOTH_DIR_INFORMATION DirectoryInfo;
  136. CHAR Buffer[MAX_PATH*2 + sizeof(FILE_BOTH_DIR_INFORMATION)];
  137. RtlInitUnicodeString(&FileName,lpFileName);
  138. DirectoryInfo = (PFILE_BOTH_DIR_INFORMATION)Buffer;
  139. Status = NtQueryDirectoryFile(
  140. hFindFile,
  141. NULL,
  142. NULL,
  143. NULL,
  144. &IoStatusBlock,
  145. DirectoryInfo,
  146. sizeof(Buffer),
  147. FileBothDirectoryInformation,
  148. TRUE,
  149. &FileName,
  150. TRUE
  151. );
  152. if ( !NT_SUCCESS(Status) ) {
  153. return FALSE;
  154. }
  155. //
  156. // Attributes are composed of the attributes returned by NT.
  157. //
  158. lpFindFileData->dwFileAttributes = DirectoryInfo->FileAttributes;
  159. lpFindFileData->ftCreationTime = *(LPFILETIME)&DirectoryInfo->CreationTime;
  160. lpFindFileData->ftLastAccessTime = *(LPFILETIME)&DirectoryInfo->LastAccessTime;
  161. lpFindFileData->ftLastWriteTime = *(LPFILETIME)&DirectoryInfo->LastWriteTime;
  162. lpFindFileData->nFileSizeHigh = DirectoryInfo->EndOfFile.HighPart;
  163. lpFindFileData->nFileSizeLow = DirectoryInfo->EndOfFile.LowPart;
  164. RtlMoveMemory( lpFindFileData->cFileName,
  165. DirectoryInfo->FileName,
  166. DirectoryInfo->FileNameLength );
  167. lpFindFileData->cFileName[DirectoryInfo->FileNameLength >> 1] = UNICODE_NULL;
  168. RtlMoveMemory( lpFindFileData->cAlternateFileName,
  169. DirectoryInfo->ShortName,
  170. DirectoryInfo->ShortNameLength );
  171. lpFindFileData->cAlternateFileName[DirectoryInfo->ShortNameLength >> 1] = UNICODE_NULL;
  172. return TRUE;
  173. }
  174. VOID
  175. FastFindTest(
  176. VOID
  177. )
  178. {
  179. PERFINFO PerfInfo;
  180. int i;
  181. HANDLE hFind;
  182. WIN32_FIND_DATAW FindFileData;
  183. BOOL b;
  184. hFind = FastFindOpenDir(L"d:\\testdir\\client1\\bench");
  185. if ( !hFind ) {
  186. printf("Failed\n");
  187. return;
  188. }
  189. StartBenchMark(
  190. "FastFind Test",
  191. ONEK,
  192. &PerfInfo
  193. );
  194. for ( i=0;i<5*ONEK;i++) {
  195. //
  196. // do 5 calls 3 work, 2 don't
  197. //
  198. b = FastFind(hFind,L"a",&FindFileData);
  199. if ( !b ) {
  200. printf("Test Failure a\n");
  201. ExitProcess(0);
  202. }
  203. b = FastFind(hFind,L"ab",&FindFileData);
  204. if ( b ) {
  205. printf("Test Failure ab\n");
  206. ExitProcess(0);
  207. }
  208. b = FastFind(hFind,L"abc",&FindFileData);
  209. if ( !b ) {
  210. printf("Test Failure abc\n");
  211. ExitProcess(0);
  212. }
  213. b = FastFind(hFind,L"da",&FindFileData);
  214. if ( b ) {
  215. printf("Test Failure da\n");
  216. ExitProcess(0);
  217. }
  218. b = FastFind(hFind,L"dxxa",&FindFileData);
  219. if ( !b ) {
  220. printf("Test Failure dxxa\n");
  221. ExitProcess(0);
  222. }
  223. }
  224. FinishBenchMark(&PerfInfo);
  225. }
  226. VOID
  227. FindFirstTest(
  228. VOID
  229. )
  230. {
  231. PERFINFO PerfInfo;
  232. int i;
  233. HANDLE hFind;
  234. WIN32_FIND_DATAW FindFileData;
  235. BOOL b;
  236. StartBenchMark(
  237. "Stock FindFirst Test",
  238. ONEK,
  239. &PerfInfo
  240. );
  241. for ( i=0;i<5*ONEK;i++) {
  242. //
  243. // do 5 calls 3 work, 2 don't
  244. //
  245. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\a",&FindFileData);
  246. if ( hFind == INVALID_HANDLE_VALUE ) {
  247. printf("Test Failure a\n");
  248. ExitProcess(0);
  249. }
  250. FindClose(hFind);
  251. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\ab",&FindFileData);
  252. if ( hFind != INVALID_HANDLE_VALUE ) {
  253. printf("Test Failure ab\n");
  254. ExitProcess(0);
  255. }
  256. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\abc",&FindFileData);
  257. if ( hFind == INVALID_HANDLE_VALUE ) {
  258. printf("Test Failure abc\n");
  259. ExitProcess(0);
  260. }
  261. FindClose(hFind);
  262. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\da",&FindFileData);
  263. if ( hFind != INVALID_HANDLE_VALUE ) {
  264. printf("Test Failure da\n");
  265. ExitProcess(0);
  266. }
  267. hFind = FindFirstFileW(L"d:\\testdir\\client1\\bench\\dxxa",&FindFileData);
  268. if ( hFind == INVALID_HANDLE_VALUE ) {
  269. printf("Test Failure dxxa\n");
  270. ExitProcess(0);
  271. }
  272. FindClose(hFind);
  273. }
  274. FinishBenchMark(&PerfInfo);
  275. }
  276. VOID
  277. APIENTRY
  278. CreateOpenDirObja(
  279. LPCWSTR lpFileName,
  280. POBJECT_ATTRIBUTES Obja,
  281. PUNICODE_STRING PathName,
  282. LPCWSTR DirName
  283. )
  284. {
  285. UNICODE_STRING FileName;
  286. IO_STATUS_BLOCK IoStatusBlock;
  287. BOOLEAN TranslationStatus;
  288. RTL_RELATIVE_NAME RelativeName;
  289. PVOID FreeBuffer;
  290. HANDLE hDir;
  291. NTSTATUS Status;
  292. if ( ARGUMENT_PRESENT(DirName) ) {
  293. TranslationStatus = RtlDosPathNameToNtPathName_U(
  294. DirName,
  295. PathName,
  296. &FileName.Buffer,
  297. &RelativeName
  298. );
  299. if ( TranslationStatus ) {
  300. //
  301. // Open the directory for list access
  302. //
  303. InitializeObjectAttributes(
  304. Obja,
  305. PathName,
  306. OBJ_CASE_INSENSITIVE,
  307. NULL,
  308. NULL
  309. );
  310. Status = NtOpenFile(
  311. &hDir,
  312. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  313. Obja,
  314. &IoStatusBlock,
  315. FILE_SHARE_READ | FILE_SHARE_WRITE,
  316. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  317. );
  318. if ( !NT_SUCCESS(Status) ) {
  319. printf("Open faild %x\n",Status);
  320. ExitProcess(1);
  321. }
  322. }
  323. }
  324. else {
  325. hDir = NULL;
  326. }
  327. TranslationStatus = RtlDosPathNameToNtPathName_U(
  328. lpFileName,
  329. PathName,
  330. &FileName.Buffer,
  331. &RelativeName
  332. );
  333. if ( !TranslationStatus ) {
  334. return;
  335. }
  336. if ( hDir ) {
  337. PathName->Buffer = PathName->Buffer + 15;
  338. PathName->Length -= 30;
  339. PathName->MaximumLength -= 30;
  340. }
  341. FreeBuffer = PathName->Buffer;
  342. InitializeObjectAttributes(
  343. Obja,
  344. PathName,
  345. OBJ_CASE_INSENSITIVE,
  346. hDir,
  347. NULL
  348. );
  349. }
  350. VOID
  351. APIENTRY
  352. OpenCloseDir(
  353. POBJECT_ATTRIBUTES Obja
  354. )
  355. {
  356. HANDLE hDir;
  357. NTSTATUS Status;
  358. IO_STATUS_BLOCK IoStatusBlock;
  359. //
  360. // Open the directory for list access
  361. //
  362. Status = NtOpenFile(
  363. &hDir,
  364. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  365. Obja,
  366. &IoStatusBlock,
  367. FILE_SHARE_READ | FILE_SHARE_WRITE,
  368. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  369. );
  370. if ( !NT_SUCCESS(Status) ) {
  371. printf("Open faild %x\n",Status);
  372. ExitProcess(1);
  373. }
  374. NtClose(hDir);
  375. }
  376. VOID
  377. OpenDirTest(
  378. VOID
  379. )
  380. {
  381. PERFINFO PerfInfo;
  382. int i;
  383. HANDLE hDir;
  384. OBJECT_ATTRIBUTES Obja;
  385. UNICODE_STRING PathName;
  386. FILE_BASIC_INFORMATION BasicInfo;
  387. #if 0
  388. CreateOpenDirObja(
  389. L"d:\\testdir\\client1\\bench",
  390. &Obja,
  391. &PathName,
  392. NULL
  393. );
  394. StartBenchMark(
  395. "Open Dir NTFS d:\\testdir\\client1\\bench",
  396. FIVEK,
  397. &PerfInfo
  398. );
  399. for ( i=0;i<FIVEK;i++) {
  400. OpenCloseDir(&Obja);
  401. }
  402. FinishBenchMark(&PerfInfo);
  403. StartBenchMark(
  404. "NtQueryAttributes Dir NTFS d:\\testdir\\client1\\bench",
  405. FIVEK,
  406. &PerfInfo
  407. );
  408. for ( i=0;i<FIVEK;i++) {
  409. NtQueryAttributesFile( &Obja, &BasicInfo );
  410. }
  411. FinishBenchMark(&PerfInfo);
  412. CreateOpenDirObja(
  413. L"c:\\testdir\\client1\\bench",
  414. &Obja,
  415. &PathName,
  416. NULL
  417. );
  418. StartBenchMark(
  419. "Open Dir FAT c:\\testdir\\client1\\bench",
  420. FIVEK,
  421. &PerfInfo
  422. );
  423. for ( i=0;i<FIVEK;i++) {
  424. OpenCloseDir(&Obja);
  425. }
  426. FinishBenchMark(&PerfInfo);
  427. #endif
  428. CreateOpenDirObja(
  429. L"d:\\testdir\\client1\\bench",
  430. &Obja,
  431. &PathName,
  432. L"d:\\"
  433. );
  434. StartBenchMark(
  435. "VOL Rel Open Dir NTFS d:\\testdir\\client1\\bench",
  436. FIVEK,
  437. &PerfInfo
  438. );
  439. for ( i=0;i<FIVEK;i++) {
  440. OpenCloseDir(&Obja);
  441. }
  442. FinishBenchMark(&PerfInfo);
  443. CreateOpenDirObja(
  444. L"c:\\testdir\\client1\\bench",
  445. &Obja,
  446. &PathName,
  447. L"c:\\"
  448. );
  449. StartBenchMark(
  450. "Vol Rel Open Dir FAT c:\\testdir\\client1\\bench",
  451. FIVEK,
  452. &PerfInfo
  453. );
  454. for ( i=0;i<FIVEK;i++) {
  455. OpenCloseDir(&Obja);
  456. }
  457. FinishBenchMark(&PerfInfo);
  458. }
  459. DWORD
  460. _cdecl
  461. main(
  462. int argc,
  463. char *argv[],
  464. char *envp[]
  465. )
  466. {
  467. DWORD CryptoKey;
  468. PDWORD p;
  469. printf("sixeof teb %x\n",sizeof(TEB));
  470. CryptoKey = USER_SHARED_DATA->CryptoExponent;
  471. printf("Key %x\n",CryptoKey);
  472. p = &(USER_SHARED_DATA->CryptoExponent);
  473. *p = 1;
  474. //OpenDirTest();
  475. //FastFindTest();
  476. //FindFirstTest();
  477. return 0;
  478. }