00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
#include <nt.h>
00023
#include <ntrtl.h>
00024
#include <nturtl.h>
00025
#include <string.h>
00026
00027
#include "ntnapdef.h"
00028
#include "ntnap.h"
00029
00030
00031
00032
00033
00034
00035
00036 BOOLEAN
NapInitialized =
FALSE;
00037
00038
00039
00040
00041
00042 LARGE_INTEGER
NapFrequency;
00043
00044
00045
00046
00047
00048 PNAPCONTROL NapControl =
NULL;
00049
00050
00051
00052
00053
00054
00055
00056
00057 PNAPDATA
NapProfilingData =
NULL;
00058
00059
00060
VOID
00061 NapDllInit (
00062 VOID
00063 )
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 {
00086 ULONG i;
00087 LARGE_INTEGER CurrentCounter;
00088
00089
if (!
NapInitialized) {
00090
00091
00092
00093
00094
00095
NapInitialized =
TRUE;
00096
00097
00098
00099
00100
00101
00102
NtQueryPerformanceCounter(&CurrentCounter,
00103 &
NapFrequency);
00104
00105
00106
00107
00108
00109
if (
NT_SUCCESS(
NapCreateDataSection(&
NapControl))) {
00110
00111
NapProfilingData = (PNAPDATA)(
NapControl + 1);
00112
00113
if (!
NapControl->
Initialized) {
00114
00115
00116
00117
00118
00119
00120
NapControl->
Initialized =
TRUE;
00121
00122
NapControl->
Paused = 0;
00123
00124
00125
00126
00127
00128
NapProfilingData[0].NapLock = 0
L;
00129
NapProfilingData[0].Calls = 0
L;
00130
NapProfilingData[0].TimingErrors = 0
L;
00131
NapProfilingData[0].TotalTime.HighPart = 0
L;
00132
NapProfilingData[0].TotalTime.LowPart = 0
L;
00133
NapProfilingData[0].FirstTime.HighPart = 0
L;
00134
NapProfilingData[0].FirstTime.LowPart = 0
L;
00135
NapProfilingData[0].MaxTime.HighPart = 0
L;
00136
NapProfilingData[0].MaxTime.LowPart = 0
L;
00137
NapProfilingData[0].MinTime.HighPart =
MAX_LONG;
00138
NapProfilingData[0].MinTime.LowPart =
MAX_ULONG;
00139
00140
00141
00142
00143
00144
NapClearData();
00145
00146
00147
00148
00149
00150
00151
00152
for (i=0; i<
NUM_ITERATIONS; i++) {
00153
NapCalibrate();
00154 }
00155 }
00156 }
00157 }
00158 }
00159
00160
00161
00162
00163
NTSTATUS
00164 NapCreateDataSection(
00165
PNAPCONTROL *NapSectionPointer
00166 )
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 {
00188
NTSTATUS Status;
00189 HANDLE NapSectionHandle;
00190 HANDLE MyProcess;
00191 STRING NapSectionName;
00192 OBJECT_ATTRIBUTES
ObjectAttributes;
00193 LARGE_INTEGER AllocationSize;
00194 ULONG ViewSize;
00195
PNAPCONTROL SectionPointer;
00196
00197
00198
00199
00200
00201 NapSectionName.Length = 15;
00202 NapSectionName.MaximumLength = 15;
00203 NapSectionName.Buffer =
"\\NapDataSection";
00204
00205
ObjectAttributes.Length =
sizeof(OBJECT_ATTRIBUTES);
00206
ObjectAttributes.ObjectName = &NapSectionName;
00207
ObjectAttributes.RootDirectory =
NULL;
00208
ObjectAttributes.SecurityDescriptor =
NULL;
00209
ObjectAttributes.SecurityQualityOfService =
NULL;
00210
ObjectAttributes.Attributes = OBJ_OPENIF |
00211 OBJ_CASE_INSENSITIVE;
00212
00213
00214 AllocationSize.HighPart = 0;
00215 AllocationSize.LowPart = (NAP_API_COUNT + 1) *
sizeof(NAPDATA);
00216
00217
Status =
NtCreateSection(&NapSectionHandle,
00218 SECTION_MAP_READ |
00219 SECTION_MAP_WRITE,
00220 &
ObjectAttributes,
00221 &AllocationSize,
00222 PAGE_READWRITE,
00223 SEC_COMMIT,
00224
NULL);
00225
00226
if (
NT_SUCCESS(
Status)) {
00227
00228 MyProcess = NtCurrentProcess();
00229
00230 ViewSize = AllocationSize.LowPart;
00231
00232 SectionPointer =
NULL;
00233
00234
Status =
NtMapViewOfSection(NapSectionHandle,
00235 MyProcess,
00236 (PVOID *)&SectionPointer,
00237 0,
00238 AllocationSize.LowPart,
00239
NULL,
00240 &ViewSize,
00241 ViewUnmap,
00242 MEM_TOP_DOWN,
00243 PAGE_READWRITE);
00244
00245 *NapSectionPointer = SectionPointer;
00246 }
00247
00248
return(
Status);
00249 }
00250
00251
00252
00253
00254
VOID
00255 NapRecordInfo (
00256 IN ULONG ServiceNumber,
00257 IN LARGE_INTEGER Counters[]
00258 )
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 {
00289 LARGE_INTEGER ElapsedTime;
00290 LARGE_INTEGER Difference;
00291
00292
00293
00294
00295
00296
00297
if (
NapControl !=
NULL) {
00298
00299
00300
00301
00302
if (
NapControl->
Paused == 0) {
00303
00304
00305
00306
00307
00308
00309 ServiceNumber++;
00310
00311
00312
00313
00314
00315
00316 NapAcquireSpinLock(&(
NapProfilingData[ServiceNumber].NapLock));
00317
00318
00319
00320
00321
00322
NapProfilingData[ServiceNumber].Calls++;
00323
00324
00325
00326
00327
00328 ElapsedTime =
00329 RtlLargeIntegerSubtract(Counters[0],Counters[1]);
00330
00331
00332
00333
00334
00335
NapProfilingData[ServiceNumber].TotalTime =
00336 RtlLargeIntegerAdd(
NapProfilingData[ServiceNumber].TotalTime,
00337 ElapsedTime);
00338
00339
if (
NapProfilingData[ServiceNumber].Calls == 1) {
00340
NapProfilingData[ServiceNumber].FirstTime = ElapsedTime;
00341 }
00342
else {
00343 Difference =
00344 RtlLargeIntegerSubtract(
00345
NapProfilingData[ServiceNumber].MaxTime,
00346 ElapsedTime);
00347
if (Difference.HighPart < 0) {
00348
00349
00350
00351
00352
00353
NapProfilingData[ServiceNumber].MaxTime = ElapsedTime;
00354
00355 }
00356 Difference =
00357 RtlLargeIntegerSubtract(
00358 ElapsedTime,
00359
NapProfilingData[ServiceNumber].MinTime);
00360
if (Difference.HighPart < 0) {
00361
00362
00363
00364
00365
00366
NapProfilingData[ServiceNumber].MinTime = ElapsedTime;
00367 }
00368 }
00369
00370
NapReleaseSpinLock(&(
NapProfilingData[ServiceNumber].NapLock));
00371 }
00372 }
00373 }
00374
00375
00376
00377
NTSTATUS
00378 NapClearData (
00379 VOID
00380 )
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 {
00401 ULONG ServiceNumber;
00402
00403
00404
00405
00406
00407 NapAcquireSpinLock(&(
NapProfilingData[1].NapLock));
00408
00409
NapProfilingData[1].Calls = 0
L;
00410
NapProfilingData[1].TimingErrors = 0
L;
00411
NapProfilingData[1].TotalTime.HighPart = 0
L;
00412
NapProfilingData[1].TotalTime.LowPart = 0
L;
00413
NapProfilingData[1].FirstTime.HighPart = 0
L;
00414
NapProfilingData[1].FirstTime.LowPart = 0
L;
00415
NapProfilingData[1].MaxTime.HighPart = 0
L;
00416
NapProfilingData[1].MaxTime.LowPart = 0
L;
00417
NapProfilingData[1].MinTime.HighPart =
MAX_LONG;
00418
NapProfilingData[1].MinTime.LowPart =
MAX_ULONG;
00419
00420
00421
00422
00423
00424
00425
for (ServiceNumber = 2; ServiceNumber <= NAP_API_COUNT; ServiceNumber++) {
00426
00427 NapAcquireSpinLock(&(
NapProfilingData[ServiceNumber].NapLock));
00428
00429
NapProfilingData[ServiceNumber] =
NapProfilingData[1];
00430
00431
NapReleaseSpinLock(&(
NapProfilingData[ServiceNumber].NapLock));
00432 }
00433
00434
NapReleaseSpinLock(&(
NapProfilingData[1].NapLock));
00435
00436
return(STATUS_SUCCESS);
00437 }
00438
00439
00440
00441
NTSTATUS
00442 NapRetrieveData (
00443 OUT NAPDATA *NapApiData,
00444 OUT PCHAR **NapApiNames,
00445 OUT PLARGE_INTEGER *NapCounterFrequency
00446 )
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 {
00478 ULONG ServiceNumber;
00479
00480 *NapApiNames =
NapNames;
00481
00482 *NapCounterFrequency = &
NapFrequency;
00483
00484
for (ServiceNumber = 0; ServiceNumber <= NAP_API_COUNT; ServiceNumber++) {
00485
00486 NapAcquireSpinLock(&(
NapProfilingData[ServiceNumber].NapLock));
00487
00488 NapApiData[ServiceNumber] =
NapProfilingData[ServiceNumber];
00489
00490
NapReleaseSpinLock(&(
NapProfilingData[ServiceNumber].NapLock));
00491
NapReleaseSpinLock(&(NapApiData[ServiceNumber].NapLock));
00492 }
00493
00494
return(STATUS_SUCCESS);
00495 }
00496
00497
00498
00499
NTSTATUS
00500 NapGetApiCount (
00501 OUT PULONG NapApiCount
00502 )
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 {
00524
00525 *NapApiCount = NAP_API_COUNT;
00526
00527
return(STATUS_SUCCESS);
00528
00529 }
00530
00531
00532
00533
00534
NTSTATUS
00535 NapPause (
00536 VOID
00537 )
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557 {
00558
NapControl->
Paused++;
00559
00560
return(STATUS_SUCCESS);
00561 }
00562
00563
00564
NTSTATUS
00565 NapResume (
00566 VOID
00567 )
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587 {
00588
NapControl->
Paused--;
00589
00590
return(STATUS_SUCCESS);
00591 }