summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/testprot/tpctl/cpuperf.c
blob: ebd92cb1151085aaa5c5929e315d79bec81eed2e (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
//  Include files
//
// #include <ntos.h>

#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <ntexapi.h>

#include <windows.h>

extern   VOID printf(UCHAR *,...);
// extern   LARGE_INTEGER KeQueryPerformanceCounter(PLARGE_INTEGER);

#define  MAX_CPUS 64                   // supports maximum of 64 cpus...

// #include "tpdefs.h"
// #include "media.h"
// #include "tpprocs.h"
// #include "string.h"

PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION pStartData;
PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION pEndData;
ULONG                                     NumCpus;
ULONG                                     ProcessorBufSize;
PULONG                                    pKernelPercent;
DWORD                                     StartTestTime;

// --------------------------------------------------
//
// Function:  TpPerfInitCpuUsage
//
// Arguments: none
//
// Returns:   none
//
// Descript:  This function allocates and initializes all the structures
//            necessary for finding the %cpu usage during performance tests
//
// --------------------------------------------------


VOID
CpuUsageInit(VOID)
{
   if (!NumCpus)                       // if NumCpus is zero, need to do first pass initializations
   {                                   // (allocate all buffers, set NumCpus)

      SYSTEM_BASIC_INFORMATION                  BasicInfo;

      //
      // First get the number of processors...
      //

      NtQuerySystemInformation(SystemBasicInformation,
                               &BasicInfo,
                               sizeof(SYSTEM_BASIC_INFORMATION),
                               NULL);

      NumCpus = BasicInfo.NumberOfProcessors;
      if ( (NumCpus < 1) || (NumCpus > MAX_CPUS) )
      {
         printf("CpuUsageInit:  Illegal number of cpus\n");
         goto init_abort;
      }

      //
      // get the memory for the processor instance data
      //

      ProcessorBufSize = NumCpus * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);

      if ( (pStartData = GlobalAlloc(GMEM_FIXED, ProcessorBufSize)) == NULL)
      {
         printf("CpuUsageInit: unable to allocate pStartData buffer\n");
         goto init_abort;
      }

      if ( (pEndData = GlobalAlloc(GMEM_FIXED, ProcessorBufSize)) == NULL)
      {
         printf("CpuUsageInit: unable to allocate pEndData buffer\n");
         goto init_abort;
      }

      if ( (pKernelPercent = GlobalAlloc(GMEM_FIXED , (NumCpus + 1) * sizeof(ULONG))) == NULL)
      {
         printf("CpuUsageInit: unable to allocate pKernelPercent buffer\n");
init_abort:
         if (pStartData)
         {
            GlobalFree(pStartData);
//            pStartData = NULL;
         }
         if (pEndData)
         {
            GlobalFree(pEndData);
//            pEndData = NULL
         }
         if (pKernelPercent)
         {
            GlobalFree(pKernelPercent);
//            pKernelPercent = NULL;
         }
         NumCpus = 0;
         return;
      }
   }

   NtQuerySystemInformation(SystemProcessorPerformanceInformation,
                            pStartData,
                            ProcessorBufSize,
                            NULL);
   StartTestTime = GetTickCount();

}


// ------------------------------------------------
//
// Function:   TpPerfGetCpuUsage
//
// Arguments:  oldptr -- cpu processor performance data from time 0
//             ProcessorTime -- place to put processor times
//             KernelTime    -- place to put kernel times
//
// Returns:    number of processors--0 if error
//
// Descript:   This function reads the performance counters
//             at the end of the test, stored them in an appropriate
//             location, and then cleans up the structures and exits
//
// -------------------------------------------------


ULONG
CpuUsageGetData(PULONG  *ppKernPC,
                ULONG   TestTime)
{
   SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *pOldProcessorInformation;
   SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *pNewProcessorInformation;
   ULONG          CurProc;
   LARGE_INTEGER  TotalProcessorTime;
   LARGE_INTEGER  TotalKernelTime;
   LARGE_INTEGER  TempUser;
   LARGE_INTEGER  TempKern;
   LARGE_INTEGER  TempIdle;
   LARGE_INTEGER  trash;
   LARGE_INTEGER  WholeTestTime;
   DWORD          EndTestTime;
   ULONG          flag;

   if (!NumCpus)
   {
      printf("CpuUsageGetData:  called before initialization\n");
      return 0;
   }


   NtQuerySystemInformation(SystemProcessorPerformanceInformation,
                            pEndData,
                            ProcessorBufSize,
                            NULL);
   //
   // find the total time in milliseconds
   //
   EndTestTime = GetTickCount();
//   if (EndTestTime > StartTestTime)
//   {
       WholeTestTime.LowPart = EndTestTime - StartTestTime;
//   }
//   else
//   {
//
//   }
//   WholeTestTime.LowPart  = GetTickCount() - StartTestTime;
   WholeTestTime.HighPart = 0;
//   printf("Kludge factor = %d/%d\n", TestTime, WholeTestTime.LowPart);

   TotalProcessorTime.HighPart = 0;
   TotalProcessorTime.LowPart  = 0;
   TotalKernelTime.HighPart    = 0;
   TotalKernelTime.LowPart     = 0;

   //
   // Total time = UserTime + KernelTime
   // KernelTime = IdleTime + Priviledged time
   // We need total time and priviledged time
   //

   pOldProcessorInformation = pStartData;
   pNewProcessorInformation = pEndData;

   for ( CurProc = 0; CurProc < NumCpus; CurProc++ )
   {
// DEBUG
//         printf("\nCpuUsageGetData:  processor %d\n", CurProc);
//         printf("Initial Idletime = %08x%08x\n",   pOldProcessorInformation->IdleTime.HighPart,
//                                                   pOldProcessorInformation->IdleTime.LowPart);
//         printf("Initial Usertime = %08x%08x\n",   pOldProcessorInformation->UserTime.HighPart,
//                                                   pOldProcessorInformation->UserTime.LowPart);
//         printf("Initial Kerntime = %08x%08x\n\n", pOldProcessorInformation->KernelTime.HighPart,
//                                                   pOldProcessorInformation->KernelTime.LowPart);
//
//         printf("Final   Idletime = %08x%08x\n",   pNewProcessorInformation->IdleTime.HighPart,
//                                                   pNewProcessorInformation->IdleTime.LowPart);
//         printf("Final   Usertime = %08x%08x\n",   pNewProcessorInformation->UserTime.HighPart,
//                                                   pNewProcessorInformation->UserTime.LowPart);
//         printf("Final   Kerntime = %08x%08x\n\n", pNewProcessorInformation->KernelTime.HighPart,
//                                                   pNewProcessorInformation->KernelTime.LowPart);
// END DEBUG

      // first, find all the deltas...

      TempUser = RtlLargeIntegerSubtract(pNewProcessorInformation->UserTime,
                                         pOldProcessorInformation->UserTime);
      TempKern = RtlLargeIntegerSubtract(pNewProcessorInformation->KernelTime,
                                         pOldProcessorInformation->KernelTime);
      TempIdle = RtlLargeIntegerSubtract(pNewProcessorInformation->IdleTime,
                                         pOldProcessorInformation->IdleTime);
      // check for wrapping
//      if (pOldProcessor->UserTime.HighPart > pNewProcessorInformation->UserTime.HighPart)
//      {
//
//      }

//      printf("Delta IdleTime = %08x%08x\n", TempIdle.HighPart, TempIdle.LowPart);
//      printf("Delta UserTime = %08x%08x\n", TempUser.HighPart, TempUser.LowPart);
//      printf("Delta KernTime = %08x%08x\n", TempKern.HighPart, TempKern.LowPart);

      // now find the total processor time = UserTime + KernelTime

      TempUser = RtlLargeIntegerAdd(TempUser, TempKern);

//      printf("Total ProcTime = %08x%08x\n", TempUser.HighPart, TempUser.LowPart);

      // adjust by kludge factor -- TestTime/WholeTestTime

      TempUser = RtlExtendedIntegerMultiply(TempUser, TestTime);
      TempUser = RtlLargeIntegerDivide(TempUser, WholeTestTime, &trash);

      if ((TempUser.HighPart == 0) && (TempUser.LowPart < 10))    // sanity check
      {
          flag = 0;
          printf("Kludge factor = %d/%d\n", TestTime, WholeTestTime.LowPart);
          printf("Adjusted ProcTime = %08x%08x\n", TempUser.HighPart, TempUser.LowPart);
      }
      else
      {
          flag = 1;
      }

      TotalProcessorTime = RtlLargeIntegerAdd(TotalProcessorTime, TempUser);

      // now find the true kernel time = KernelTime - IdleTime

      TempKern = RtlLargeIntegerSubtract(TempKern, TempIdle);

//      printf("True KernTime = %08x%08x\n", TempKern.HighPart, TempKern.LowPart);

      if (TempKern.HighPart < 0)
      {
         TempKern.HighPart = 0;
         TempKern.LowPart  = 0;
      }
      TotalKernelTime = RtlLargeIntegerAdd(TotalKernelTime, TempKern);

      //
      // finally, calc the percent kernel is of total
      //

      if (flag)
      {
         TempKern = RtlExtendedIntegerMultiply(TempKern, 1000);
         TempKern = RtlLargeIntegerDivide(TempKern, TempUser, &trash);
      }
      else
      {
         TempKern.LowPart = 0;
      }
      pKernelPercent[CurProc+1] = TempKern.LowPart;

      // move to info for next processor

      pNewProcessorInformation++;
      pOldProcessorInformation++;
   }

   //
   // last of all,  calc the percent kernel is of total
   //
   if ((TotalProcessorTime.HighPart == 0) && (TotalProcessorTime.LowPart < 10))
   {
      TempKern.LowPart = 0;
   }
   else
   {
      TempKern = RtlExtendedIntegerMultiply(TotalKernelTime, 1000);
      TempKern = RtlLargeIntegerDivide(TempKern, TotalProcessorTime, &trash);
   }
   pKernelPercent[0]    = TempKern.LowPart;

   *ppKernPC = pKernelPercent;

   return NumCpus;
}