Show / Hide Table of Contents

How to read kernel time and user time

Sometimes its a very tricky situation when you need to know time spent in kernel and user process. This kind of flexibility required when user want to do benchmarking of a software or debugging slow process or it could be any other reason to know the time spent.

Code Snippet

Here is small sample code snippet to print the time and this code is tested using Visual Studio 2012

void PrintKernelUserTime() { 
    FILETIME CreationTime = { 0 }; 
    FILETIME ExitTime     = { 0 }; 
    FILETIME KernelTime   = { 0 }; 
    FILETIME UserTime     = { 0 }; 

    // Get Process times. 
    GetProcessTimes( GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &UserTime ); 

    // Format time to readable form. 
    SYSTEMTIME SystemTime = { 0 }; 
    FileTimeToSystemTime( &KernelTime, & SystemTime ); 

    // Kernel Time in HH:MM:SS:mmm. 
    char csKernelTime[64] = ""; 
    sprintf(csKernelTime, "Kernel Time\t%02d:%02d:%02d:%04d", SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds ); 

    // Format user time to readable form. 
    FileTimeToSystemTime( &UserTime, & SystemTime ); 

    // Kernel Time in HH:MM:SS:mmm. 
    char csUserTime[64] = ""; 
    sprintf(csUserTime, "User Time\t%02d:%02d:%02d:%04d", SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds ); 

    // Kernel Time in HH:MM:SS:mmm. 
    char csProcessCreationTime[64] = ""; 
    FileTimeToSystemTime( &CreationTime, & SystemTime ); 
    sprintf(csProcessCreationTime, "Creation Time\t%02d:%02d:%02d:%04d", SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds ); 

    // Kernel Time in HH:MM:SS:mmm. 
    char csExitTime[64] = ""; 
    FileTimeToSystemTime( &ExitTime, & SystemTime ); 
    sprintf(csExitTime, "Exit Time\t%02d:%02d:%02d:%04d", SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds ); 

    myst EndTime; 
    memset(&EndTime,0,sizeof(EndTime)); 
    char csEndTime[64] = ""; 
    GetSystemTime(&EndTime); 
    sprintf(csEndTime, "End Time\t%02d:%02d:%02d:%04d", EndTime.wHour, EndTime.wMinute, EndTime.wSecond,    EndTime.wMilliseconds); 
    myst t = EndTime - StartTime; 
    sprintf(csElapsed, "Elapsed Time\t%02d:%02d:%02d:%04d", t.wHour, t.wMinute, t.wSecond, t.wMilliseconds); 


    printf("\n%s", csKernelTime); 
    printf("\n%s", csUserTime); 
    printf("\n%s", csProcessCreationTime); 
    printf("\n%s", csCurrentTime); 
    printf("\n%s\n", csEndTime); 
    printf("\n%s\n", csElapsed); 
}

Question OR Feedback?

Note

If you want to ask anything related this post, please tweet your question or feedback @JangidUK I will try to respond as soon as possible.

instagram linkedin youtube twitter facebook