Some ulib code that controls the sampler

上级 00019b41
......@@ -59,4 +59,20 @@ void fprintf(int, const char*, ...);
void snprintf(char *buf, unsigned int n, const char *fmt, ...);
void die(const char* errstr, ...) __attribute__((noreturn));
#define assert(c) if (!(c)) { fprintf(2, "%s:%d: ", __FILE__, __LINE__); die("assertion failure"); }
// perf.cc
// Default selector for AMD 10h:
// [35 - 32] event mask [11 - 8]
// [31 - 24] counter mask
// [22] counter enable
// [17] operating system mode
// [16] user mode
// [15 - 8] unit mask
// [7 - 0] event mask [7 - 0]
#define PERF_SELECTOR \
(0UL<<32 | 1<<24 | 1<<22 | 1<<20 | 1<<17 | 1<<16 | 0x00<<8 | 0x76)
// Default period
#define PERF_PERIOD 100000
void perf_stop(void);
void perf_start(u64 selector, u64 period);
END_DECLS
......@@ -2,7 +2,7 @@ $(O)/lib/%.o: CFLAGS:=$(CFLAGS)
$(O)/lib/%.o: CXXFLAGS:=$(CXXFLAGS)
ULIB = ulib.o usys.o printf.o umalloc.o uthread.o fmt.o stream.o ipc.o \
threads.o crt.o wq.o
threads.o crt.o wq.o perf.o
ULIB := $(addprefix $(O)/lib/, $(ULIB))
.PRECIOUS: $(O)/lib/%.o
......
#include "types.h"
#include "user.h"
#include "fcntl.h"
#include "sampler.h"
static int perf_fd = -1;
static void
conf(int fd, sampop_t op, u64 selector = 0, u64 period = 0)
{
struct sampconf c = {
op: op,
selector: selector,
period: period,
};
if (write(fd, &c, sizeof(c)) != sizeof(c))
die("perf: write failed");
}
void
perf_stop(void)
{
assert(perf_fd >= 0);
conf(perf_fd, SAMP_DISABLE);
}
void
perf_start(u64 selector, u64 period)
{
if (perf_fd == -1) {
perf_fd = open("/dev/sampler", O_WRONLY);
if (perf_fd < 0)
die("perf: open failed");
}
conf(perf_fd, SAMP_ENABLE, selector, period);
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论