#include #include int msrfd = -1; static u64int rdmsr(ulong msr) { u64int val; uchar data[8]; if(pread(msrfd, data, sizeof(data), msr) != sizeof(data)) sysfatal("rdmsr: %r"); val = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24) | ((vlong)data[4] << 32) | ((vlong)data[5] << 40) | ((vlong)data[6] << 48) | ((vlong)data[7] << 56); return val; } static void wrmsr(ulong msr, u64int val) { uchar data[8]; data[0] = val; data[1] = val >> 8; data[2] = val >> 16; data[3] = val >> 24; data[4] = val >> 32; data[5] = val >> 40; data[6] = val >> 48; data[7] = val >> 56; if(pwrite(msrfd, data, sizeof(data), msr) != sizeof(data)) sysfatal("wrmsr: %r"); } static void pin(int cpu) { char ctlbuf[64]; int ctlfd; snprint(ctlbuf, sizeof(ctlbuf), "/proc/%d/ctl", getpid()); ctlfd = open(ctlbuf, OWRITE); if(ctlfd < 0) sysfatal("open %s: %r", ctlbuf); if(fprint(ctlfd, "wired %d\n", cpu) < 0) sysfatal("write %s: %r", ctlbuf); close(ctlfd); } static int pstate_min(void) { return (rdmsr(0xCE) >> 40) & 0xFF; } static int pstate_max(void) { return (rdmsr(0xCE) >> 8) & 0xFF; } static int pstate_status(void) { u64int pstate; // IA32_PERF_STATUS pstate = rdmsr(0x198); return (pstate & 0xFFFF) >> 8; } static void pstate_ctlw(int pstate) { // IA32_PERF_CTL wrmsr(0x199, (pstate & 0xFF) << 8); } static int pstate_ctlr(void) { u64int ctl; ctl = rdmsr(0x199); return (ctl & 0xFFFF) >> 8; } static u64int mperf(void) { return rdmsr(0xE7); } static u64int aperf(void) { return rdmsr(0xE8); } static int load(void) { char buf[8192], *lines[32], *f[10]; int i, fd, ncpu, nf, lv, load; load = 0; fd = open("#c/sysstat", OREAD); if(fd < 0) sysfatal("open sysstat: %r"); if(read(fd, buf, sizeof(buf)) < 0) sysfatal("read sysstat: %r"); fprint(2, "sysstat:\n%s\n", buf); ncpu = gettokens(buf, lines, nelem(lines), "\n"); if(ncpu < 1) sysfatal("no cpus???"); /* sum in idle */ for(i = 0; i < ncpu; i++){ nf =tokenize(lines[i], f, nelem(f)); if(nf != nelem(f)) sysfatal("weird sysstat line: %d fields", nf); lv = atoi(f[8]); load += 100 - lv; } load /= ncpu; return load; } void main(int argc, char *argv[]) { ARGBEGIN{ }ARGEND msrfd = open("#P/msr", ORDWR); if(msrfd < 0) sysfatal("open msr: %r"); int min, max; min = pstate_min(); max = pstate_max(); print("pstate min %d max %d\n", min, max); int status, ctl; status = pstate_status(); ctl = pstate_ctlr(); print("status %d ctl %d\n", status, ctl); print("mperf %llud aperf %llud\n", mperf(), aperf()); u64int pctbusy = aperf()/mperf(); print("ratio = %llud\n", pctbusy); print("load = %d%%\n", load()); exits(nil); }