Skip to content

Commit fe6f55d

Browse files
committed
Bug#50057: 'SHOW PROFILE CPU' port for Windows.
Patch contributed by Alex Budovski.
1 parent 1a0f34e commit fe6f55d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

sql/sql_profile.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,26 @@ int make_profile_table_for_show(THD *thd, ST_SCHEMA_TABLE *schema_table)
134134
#define RUSAGE_USEC(tv) ((tv).tv_sec*1000*1000 + (tv).tv_usec)
135135
#define RUSAGE_DIFF_USEC(tv1, tv2) (RUSAGE_USEC((tv1))-RUSAGE_USEC((tv2)))
136136

137+
#ifdef _WIN32
138+
static ULONGLONG FileTimeToQuadWord(FILETIME *ft)
139+
{
140+
// Overlay FILETIME onto a ULONGLONG.
141+
union {
142+
ULONGLONG qwTime;
143+
FILETIME ft;
144+
} u;
145+
146+
u.ft = *ft;
147+
return u.qwTime;
148+
}
149+
150+
151+
// Get time difference between to FILETIME objects in seconds.
152+
static double GetTimeDiffInSeconds(FILETIME *a, FILETIME *b)
153+
{
154+
return ((FileTimeToQuadWord(a) - FileTimeToQuadWord(b)) / 1e7);
155+
}
156+
#endif
137157

138158
PROF_MEASUREMENT::PROF_MEASUREMENT(QUERY_PROFILE *profile_arg, const char
139159
*status_arg)
@@ -224,6 +244,12 @@ void PROF_MEASUREMENT::collect()
224244
time_usecs= (double) my_getsystime() / 10.0; /* 1 sec was 1e7, now is 1e6 */
225245
#ifdef HAVE_GETRUSAGE
226246
getrusage(RUSAGE_SELF, &rusage);
247+
#elif defined(_WIN32)
248+
FILETIME ftDummy;
249+
// NOTE: Get{Process|Thread}Times has a granularity of the clock interval,
250+
// which is typically ~15ms. So intervals shorter than that will not be
251+
// measurable by this function.
252+
GetProcessTimes(GetCurrentProcess(), &ftDummy, &ftDummy, &ftKernel, &ftUser);
227253
#endif
228254
}
229255

@@ -589,6 +615,23 @@ int PROFILING::fill_statistics_info(THD *thd, TABLE_LIST *tables, Item *cond)
589615
(1000.0*1000),
590616
&cpu_stime_decimal);
591617

618+
table->field[4]->store_decimal(&cpu_utime_decimal);
619+
table->field[5]->store_decimal(&cpu_stime_decimal);
620+
table->field[4]->set_notnull();
621+
table->field[5]->set_notnull();
622+
#elif defined(_WIN32)
623+
my_decimal cpu_utime_decimal, cpu_stime_decimal;
624+
625+
double2my_decimal(E_DEC_FATAL_ERROR,
626+
GetTimeDiffInSeconds(&entry->ftUser,
627+
&previous->ftUser),
628+
&cpu_utime_decimal);
629+
double2my_decimal(E_DEC_FATAL_ERROR,
630+
GetTimeDiffInSeconds(&entry->ftKernel,
631+
&previous->ftKernel),
632+
&cpu_stime_decimal);
633+
634+
// Store the result.
592635
table->field[4]->store_decimal(&cpu_utime_decimal);
593636
table->field[5]->store_decimal(&cpu_stime_decimal);
594637
table->field[4]->set_notnull();

sql/sql_profile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class PROF_MEASUREMENT
173173
char *status;
174174
#ifdef HAVE_GETRUSAGE
175175
struct rusage rusage;
176+
#elif defined(_WIN32)
177+
FILETIME ftKernel, ftUser;
176178
#endif
177179

178180
char *function;

0 commit comments

Comments
 (0)