forked from gvanem/wsock-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
252 lines (207 loc) · 6.8 KB
/
cpu.c
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
/*
* cpu.c - Part of Wsock-Trace.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define IN_CPU_C
#include "common.h"
#include "init.h"
#include "cpu.h"
#define ADD_VALUE(opt,dll,func) { opt, NULL, dll, #func, (void**)&p_##func }
static struct LoadTable dyn_funcs2 [] = {
ADD_VALUE (1, "kernel32.dll", QueryThreadCycleTime),
ADD_VALUE (1, "kernel32.dll", GetSystemTimePreciseAsFileTime),
ADD_VALUE (1, "ntdll.dll", NtQueryInformationThread),
ADD_VALUE (1, "ntdll.dll", NtQuerySystemInformation)
};
static int num_cpus = -1;
static void init_cpu (void)
{
SYSTEM_INFO sys_info;
if (num_cpus >= 0)
return;
memset (&sys_info, 0, sizeof(sys_info));
GetSystemInfo (&sys_info);
num_cpus = sys_info.dwNumberOfProcessors;
load_dynamic_table (dyn_funcs2, DIM(dyn_funcs2));
}
/**
* Return FILETIME in seconds as a double.
*/
static double filetime_sec (const FILETIME *filetime)
{
const LARGE_INTEGER *ft = (const LARGE_INTEGER*) filetime;
long double rc = (long double) ft->QuadPart;
return (double) (rc/1E7); /* from 100 nano-sec periods to sec */
}
/**
* Print some times (and CPU cycle counts) for a thread.
* I.e. the WinPcap receiver thread.
*/
void print_thread_times (HANDLE thread)
{
FILETIME ctime, etime, ktime, utime;
double life_span;
init_cpu();
if (thread == NULL)
{
TRACE (2, " GetThreadTimes(NULL) called!.\n");
return;
}
memset (&etime, '\0', sizeof(etime)); /* exit-time */
if (!GetThreadTimes(thread, &ctime, &etime, &ktime, &utime))
{
DWORD err = GetLastError();
TRACE (2, " GetThreadTimes (%" ADDR_FMT ") %s.\n", ADDR_CAST(thread), win_strerror(err));
return;
}
/* The thread has not yet exited.
*/
if (CompareFileTime(&etime,&ctime) < 0)
{
if (p_GetSystemTimePreciseAsFileTime)
(*p_GetSystemTimePreciseAsFileTime) (&etime);
else GetSystemTimeAsFileTime (&etime);
}
/* If this is negative, the thread lived in kernel all the time.
*/
life_span = filetime_sec (&etime) - filetime_sec (&ctime);
if (life_span < 0.0)
life_span = -life_span;
trace_printf (" kernel-time: %.6fs, user-time: %.6fs, life-span: %.6fs",
filetime_sec(&ktime), filetime_sec(&utime), life_span);
if (p_QueryThreadCycleTime)
{
ULONG64 cycle_time;
if (!(*p_QueryThreadCycleTime) (thread, &cycle_time))
trace_printf (", cycle-time: <failed>");
else trace_printf (", cycle-time: %s clocks", qword_str(cycle_time));
}
if (p_NtQueryInformationThread)
{
LARGE_INTEGER perf_count;
NTSTATUS rc = (*p_NtQueryInformationThread) (
thread, ThreadPerformanceCount, &perf_count,
sizeof(perf_count), NULL);
if (rc != STATUS_SUCCESS)
trace_printf (", perf-count: <fail %ld>", (long)rc);
else trace_printf (", perf-count: %s", qword_str(perf_count.QuadPart));
}
trace_putc ('\n');
}
/**
* Print some times for a process.
*/
void print_process_times (void)
{
HANDLE proc = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, GetCurrentProcessId());
FILETIME cr_time, exit_time, krnl_time, usr_time;
if (!proc)
return;
if (GetProcessTimes(proc, &cr_time, &exit_time, &krnl_time, &usr_time))
{
const struct tm *tm;
char time_str [50];
time_t ct = FILETIME_to_time_t (&cr_time);
uint64 fract_t = FILETIME_to_unix_epoch (&cr_time) % U64_SUFFIX(1000000);
tzset();
tm = localtime (&ct);
if (tm)
strftime (time_str, sizeof(time_str), "%Y%m%d/%H:%M:%S", tm);
else strcpy (time_str, "??");
/* 'exit_time' is not printed since the process has not exited yet.
* Therefore it is zero.
*/
trace_printf ("\ncreation-time: %s.%06" U64_FMT ", kernel-time: %.6fs, user-time: %.6fs\n",
time_str, fract_t, filetime_sec(&krnl_time), filetime_sec(&usr_time));
}
CloseHandle (proc);
}
/*
* Taken from NetPerf's netcpu_ntperf.c:
* http://www.netperf.org/netperf
*
* System CPU time information class.
* Used to get CPU time information.
*
* SDK/inc/ntexapi.h:
* Function x8: SystemProcessorPerformanceInformation
* DataStructure: SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
*/
#define SystemProcessorPerformanceInformation 0x08
typedef struct {
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER DpcTime;
LARGE_INTEGER InterruptTime;
LONG InterruptCount;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
#define MAX_CPUS 256
/**
* Print some performance timers.
*
* \todo:
* Store the counters before and after to get the delta-times.
*/
void print_perf_times (void)
{
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION info [MAX_CPUS];
DWORD i, ret_len, ret_num_CPUs;
NTSTATUS rc;
init_cpu();
if (!p_NtQuerySystemInformation || num_cpus == 0)
{
TRACE (2, " p_NtQuerySystemInformation = NULL!\n");
return;
}
/* Get the current CPUTIME information.
*/
rc = (*p_NtQuerySystemInformation) (SystemProcessorPerformanceInformation,
&info, sizeof(info), &ret_len);
if (rc != 0)
{
DWORD err = GetLastError();
TRACE (2, " NtQuerySystemInformation() %s.\n", win_strerror(err));
return;
}
/* Validate that NtQuery returned a reasonable amount of data
*/
if ((ret_len % sizeof(info[0])) != 0)
{
TRACE (1, "NtQuery didn't return expected amount of data\n"
"Expected a multiple of %u, returned %lu.\n",
SIZEOF(info[0]), (u_long)ret_len);
return;
}
ret_num_CPUs = ret_len / sizeof(info[0]);
if (ret_num_CPUs != num_cpus)
{
TRACE (1, "NtQuery didn't return expected amount of data\n"
"Expected data for %i CPUs, returned %lu.\n",
num_cpus, (u_long)ret_num_CPUs);
return;
}
/* Print total all of the CPUs:
* KernelTime needs to be fixed-up; it includes both idle & true kernel time.
*/
for (i = 0; i < ret_num_CPUs; i++)
{
ULONG64 x;
trace_printf ("CPU %lu:%s", (u_long)i, (i == 0) ? "\t\t\t CPU clocks\n" : "\n");
x = info[i].KernelTime.QuadPart - info[i].IdleTime.QuadPart;
trace_printf (" KernelTime: %18s\n", qword_str(x));
x = info[i].IdleTime.QuadPart;
trace_printf (" IdleTime: %18s\n", qword_str(x));
x = info[i].UserTime.QuadPart;
trace_printf (" UserTime: %18s\n", qword_str(x));
x = info[i].DpcTime.QuadPart;
trace_printf (" DpcTime: %18s\n", qword_str(x));
x = info[i].InterruptTime.QuadPart;
trace_printf (" InterruptTime: %18s\n", qword_str(x));
trace_printf (" InterruptCount: %18s\n", dword_str(info[i].InterruptCount));
}
}