我正在将linux代码转换为C++,并且正在为替换这个getrusage方法而奋斗。
1. timeval CPUStopwatch::now_timeval() const {
2. rusage ru;
3. getrusage(RUSAGE_SELF, &ru);
4. return ru.ru_utime;
5. }如何将这一行(3)解析为工作的窗口行?我已经实现了rusage和timeval类,所以它们不会成为一个问题。
发布于 2020-06-08 06:42:43
在PostgreSQL源代码中可以找到:
/*
* This code works on:
* solaris_i386
* solaris_sparc
* hpux 9.*
* win32
* which currently is all the supported platforms that don't have a
* native version of getrusage(). So, if configure decides to compile
* this file at all, we just use this version unconditionally.
*/
int
getrusage(int who, struct rusage *rusage)
{
#ifdef WIN32
FILETIME starttime;
FILETIME exittime;
FILETIME kerneltime;
FILETIME usertime;
ULARGE_INTEGER li;
if (who != RUSAGE_SELF)
{
/* Only RUSAGE_SELF is supported in this implementation for now */
errno = EINVAL;
return -1;
}
if (rusage == (struct rusage *) NULL)
{
errno = EFAULT;
return -1;
}
memset(rusage, 0, sizeof(struct rusage));
if (GetProcessTimes(GetCurrentProcess(),
&starttime, &exittime, &kerneltime, &usertime) == 0)
{
_dosmaperr(GetLastError());
return -1;
}
/* Convert FILETIMEs (0.1 us) to struct timeval */
memcpy(&li, &kerneltime, sizeof(FILETIME));
li.QuadPart /= 10L; /* Convert to microseconds */
rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
memcpy(&li, &usertime, sizeof(FILETIME));
li.QuadPart /= 10L; /* Convert to microseconds */
rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
#else /* all but WIN32 */https://stackoverflow.com/questions/62251386
复制相似问题