mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Fix rusage memory reporting on NT
This issue was caused by c23b6ecc3
a few days ago, where we started
polling that information for the first time.
This commit is contained in:
parent
27bc49034c
commit
1ff1854107
4 changed files with 111 additions and 44 deletions
|
@ -41,13 +41,14 @@ textwindows int sys_getrusage_nt(int who, struct rusage *usage) {
|
|||
if ((who == RUSAGE_SELF ? GetProcessTimes : GetThreadTimes)(
|
||||
(who == RUSAGE_SELF ? GetCurrentProcess : GetCurrentThread)(),
|
||||
&CreationFileTime, &ExitFileTime, &KernelFileTime, &UserFileTime)) {
|
||||
/* xxx: shouldn't clobber memory on failure below */
|
||||
usage->ru_utime = WindowsDurationToTimeVal(ReadFileTime(UserFileTime));
|
||||
usage->ru_stime = WindowsDurationToTimeVal(ReadFileTime(KernelFileTime));
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
if (GetProcessMemoryInfo(GetCurrentProcess(), &memcount, sizeof(memcount))) {
|
||||
usage->ru_maxrss = memcount.PeakWorkingSetSize;
|
||||
usage->ru_maxrss = memcount.PeakWorkingSetSize / 1024;
|
||||
usage->ru_majflt = memcount.PageFaultCount;
|
||||
} else {
|
||||
return __winerr();
|
||||
|
|
|
@ -108,7 +108,7 @@ textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
|||
if (opt_out_rusage) {
|
||||
bzero(opt_out_rusage, sizeof(*opt_out_rusage));
|
||||
if (GetProcessMemoryInfo(handles[i], &memcount, sizeof(memcount))) {
|
||||
opt_out_rusage->ru_maxrss = memcount.PeakWorkingSetSize;
|
||||
opt_out_rusage->ru_maxrss = memcount.PeakWorkingSetSize / 1024;
|
||||
opt_out_rusage->ru_majflt = memcount.PageFaultCount;
|
||||
} else {
|
||||
STRACE("%s failed %u", "GetProcessMemoryInfo", GetLastError());
|
||||
|
|
|
@ -23,26 +23,35 @@
|
|||
#include "libc/runtime/clktck.h"
|
||||
#include "libc/stdio/append.internal.h"
|
||||
|
||||
static void AppendInt(char **b, int64_t x) {
|
||||
char buf[27], *e;
|
||||
e = FormatInt64Thousands(buf, x);
|
||||
appendd(b, buf, e - buf);
|
||||
struct State {
|
||||
char **b;
|
||||
const char *nl;
|
||||
char ibuf[27];
|
||||
};
|
||||
|
||||
static void AppendNl(struct State *s) {
|
||||
appends(s->b, s->nl);
|
||||
}
|
||||
|
||||
static void AppendMetric(char **b, const char *s1, int64_t x, const char *s2,
|
||||
const char *nl) {
|
||||
appends(b, s1);
|
||||
AppendInt(b, x);
|
||||
appends(b, s2);
|
||||
appends(b, nl);
|
||||
static void AppendInt(struct State *s, int64_t x) {
|
||||
char *e = FormatInt64Thousands(s->ibuf, x);
|
||||
appendd(s->b, s->ibuf, e - s->ibuf);
|
||||
}
|
||||
|
||||
static void AppendUnit(char **b, int64_t x, const char *s) {
|
||||
AppendInt(b, x);
|
||||
appendw(b, ' ');
|
||||
appends(b, s);
|
||||
static void AppendMetric(struct State *s, const char *s1, int64_t x,
|
||||
const char *s2) {
|
||||
appends(s->b, s1);
|
||||
AppendInt(s, x);
|
||||
appends(s->b, s2);
|
||||
AppendNl(s);
|
||||
}
|
||||
|
||||
static void AppendUnit(struct State *s, int64_t x, const char *t) {
|
||||
AppendInt(s, x);
|
||||
appendw(s->b, ' ');
|
||||
appends(s->b, t);
|
||||
if (x == 1) {
|
||||
appendw(b, 's');
|
||||
appendw(s->b, 's');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,74 +59,78 @@ static void AppendUnit(char **b, int64_t x, const char *s) {
|
|||
* Generates process resource usage report.
|
||||
*/
|
||||
void AppendResourceReport(char **b, struct rusage *ru, const char *nl) {
|
||||
char ibuf[27];
|
||||
struct State s;
|
||||
long utime, stime;
|
||||
long double ticks;
|
||||
struct State *st = &s;
|
||||
s.b = b;
|
||||
s.nl = nl;
|
||||
asm("" : "+r"(st));
|
||||
if (ru->ru_maxrss) {
|
||||
AppendMetric(b, "ballooned to ", ru->ru_maxrss, "kb in size", nl);
|
||||
AppendMetric(st, "ballooned to ", ru->ru_maxrss, "kb in size");
|
||||
}
|
||||
if ((utime = ru->ru_utime.tv_sec * 1000000 + ru->ru_utime.tv_usec) |
|
||||
(stime = ru->ru_stime.tv_sec * 1000000 + ru->ru_stime.tv_usec)) {
|
||||
appends(b, "needed ");
|
||||
AppendInt(b, utime + stime);
|
||||
AppendInt(st, utime + stime);
|
||||
appends(b, "us cpu (");
|
||||
AppendInt(b, (long double)stime / (utime + stime) * 100);
|
||||
AppendInt(st, (long double)stime / (utime + stime) * 100);
|
||||
appends(b, "% kernel)");
|
||||
appends(b, nl);
|
||||
AppendNl(st);
|
||||
ticks = ceill((long double)(utime + stime) / (1000000.L / CLK_TCK));
|
||||
if (ru->ru_idrss) {
|
||||
AppendMetric(b, "needed ", lroundl(ru->ru_idrss / ticks),
|
||||
" memory on average", nl);
|
||||
AppendMetric(st, "needed ", lroundl(ru->ru_idrss / ticks),
|
||||
" memory on average");
|
||||
}
|
||||
if (ru->ru_isrss) {
|
||||
AppendMetric(b, "needed ", lroundl(ru->ru_isrss / ticks),
|
||||
" stack on average", nl);
|
||||
AppendMetric(st, "needed ", lroundl(ru->ru_isrss / ticks),
|
||||
" stack on average");
|
||||
}
|
||||
if (ru->ru_ixrss) {
|
||||
AppendMetric(b, "needed ", lroundl(ru->ru_ixrss / ticks),
|
||||
" shared on average", nl);
|
||||
AppendMetric(st, "needed ", lroundl(ru->ru_ixrss / ticks),
|
||||
" shared on average");
|
||||
}
|
||||
}
|
||||
if (ru->ru_minflt || ru->ru_majflt) {
|
||||
appends(b, "caused ");
|
||||
AppendInt(b, ru->ru_minflt + ru->ru_majflt);
|
||||
AppendInt(st, ru->ru_minflt + ru->ru_majflt);
|
||||
appends(b, " page faults (");
|
||||
AppendInt(
|
||||
b, (long double)ru->ru_minflt / (ru->ru_minflt + ru->ru_majflt) * 100);
|
||||
st, (long double)ru->ru_minflt / (ru->ru_minflt + ru->ru_majflt) * 100);
|
||||
appends(b, "% memcpy)");
|
||||
appends(b, nl);
|
||||
AppendNl(st);
|
||||
}
|
||||
if (ru->ru_nvcsw + ru->ru_nivcsw > 1) {
|
||||
AppendInt(b, ru->ru_nvcsw + ru->ru_nivcsw);
|
||||
AppendInt(st, ru->ru_nvcsw + ru->ru_nivcsw);
|
||||
appends(b, " context switch (");
|
||||
AppendInt(b,
|
||||
AppendInt(st,
|
||||
(long double)ru->ru_nvcsw / (ru->ru_nvcsw + ru->ru_nivcsw) * 100);
|
||||
appends(b, "% consensual)");
|
||||
appends(b, nl);
|
||||
AppendNl(st);
|
||||
}
|
||||
if (ru->ru_msgrcv || ru->ru_msgsnd) {
|
||||
appends(b, "received ");
|
||||
AppendUnit(b, ru->ru_msgrcv, "message");
|
||||
AppendUnit(st, ru->ru_msgrcv, "message");
|
||||
appends(b, " and sent ");
|
||||
AppendInt(b, ru->ru_msgsnd);
|
||||
appends(b, nl);
|
||||
AppendInt(st, ru->ru_msgsnd);
|
||||
AppendNl(st);
|
||||
}
|
||||
if (ru->ru_inblock || ru->ru_oublock) {
|
||||
appends(b, "performed ");
|
||||
AppendUnit(b, ru->ru_inblock, "read");
|
||||
AppendUnit(st, ru->ru_inblock, "read");
|
||||
appends(b, " and ");
|
||||
AppendInt(b, ru->ru_oublock);
|
||||
AppendInt(st, ru->ru_oublock);
|
||||
appends(b, " write i/o operations");
|
||||
appends(b, nl);
|
||||
AppendNl(st);
|
||||
}
|
||||
if (ru->ru_nsignals) {
|
||||
appends(b, "received ");
|
||||
AppendUnit(b, ru->ru_nsignals, "signal");
|
||||
appends(b, nl);
|
||||
AppendUnit(st, ru->ru_nsignals, "signal");
|
||||
AppendNl(st);
|
||||
}
|
||||
if (ru->ru_nswap) {
|
||||
appends(b, "got swapped ");
|
||||
AppendUnit(b, ru->ru_nswap, "time");
|
||||
appends(b, nl);
|
||||
AppendUnit(st, ru->ru_nswap, "time");
|
||||
AppendNl(st);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue