I can answer at least the question on the process times.
Operation systems provide functions to get the times a process has spend. But these functions are thread specific. So you must get the time for each thread. This is usually done when a thread will terminate.
With Windows, use
GetThreadTimes()
passing the handle of the thread.
With Linux call
clock_gettime()
with
CLOCK_THREAD_CPUTIME_ID
from within the thread. To get the total thread run time, store the start time when the process is started to use it later for the calculation.
Here an example for Windows:
void PrintThreadTimes(HANDLE hThread, LPCSTR lpszName)
{
FILETIME ftCreate, ftExit, ftKernel, ftUser;
SYSTEM_INFO si;
::GetSystemInfo(&si);
if (::GetThreadTimes(hThread, &ftCreate, &ftExit, &ftKernel, &ftUser))
{
unsigned long long nCreate, nUser, nKernel, nExit;
SYSTEMTIME st1, st2;
DWORD dwExitCode;
if (STILL_ACTIVE == ::GetExitCodeThread(hThread, &dwExitCode))
::GetSystemTimeAsFileTime(&ftExit);
nUser = (ftUser.dwHighDateTime << 32ULL) + ftUser.dwLowDateTime;
nKernel = (ftKernel.dwHighDateTime << 32ULL) + ftKernel.dwLowDateTime;
nExit = (ftExit.dwHighDateTime << 32ULL) + ftExit.dwLowDateTime;
nCreate = (ftCreate.dwHighDateTime << 32ULL) + ftCreate.dwLowDateTime;
double fCpuUsage = 100.0 * (nKernel + nUser) / (nExit - nCreate);
double fAvCpuUsage = fCpuUsage / si.dwNumberOfProcessors;
::FileTimeToSystemTime(&ftKernel, &st1);
::FileTimeToSystemTime(&ftUser, &st2);
printf("%s thread times: kernel %02u:%02u:%02u.%03u, user %02u:%02u:%02u.%03u, CPU usage %.1f %%\n",
lpszName,
st1.wHour, st1.wMinute, st1.wSecond, st1.wMilliseconds,
st2.wHour, st2.wMinute, st2.wSecond, st2.wMilliseconds,
fAvCpuUsage);
}
}