Skip to content

Commit

Permalink
feat&test: open_msr and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingfish404 committed May 18, 2023
1 parent 3e935c8 commit 21a3238
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
17 changes: 17 additions & 0 deletions libpmu/pmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ uint64_t pmu_event_to_hexcode(PMU_EVENT *event)
return hexcode;
}

inline int pmu_open_msr(int core)
{
core %= 512;
char msr_path[32];
sprintf(msr_path, "/dev/cpu/%d/msr", core);
return open(msr_path, O_RDWR);
}

inline void pmu_set_event(int core, int *msr_fd, uint64_t hexcode, size_t pmu_id)
{
core %= 512;
Expand All @@ -64,6 +72,15 @@ inline void pmu_set_event(int core, int *msr_fd, uint64_t hexcode, size_t pmu_id
lseek(*msr_fd, 0x38F, SEEK_SET);
}

inline void pmu_set_msr_event(int msr_fd, uint64_t hexcode, size_t pmu_id)
{
/* DISABLE ALL COUNTERS */
write_to_IA32_PERF_GLOBAL_CTRL(msr_fd, 0ull);

write_to_IA32_PERFEVTSELi(msr_fd, pmu_id, hexcode);
lseek(msr_fd, 0x38F, SEEK_SET);
}

inline void pmu_set_pmc(int msr_fd, size_t pmu_id, uint64_t val)
{
write_to_IA32_PMCi(msr_fd, pmu_id, val);
Expand Down
4 changes: 4 additions & 0 deletions libpmu/pmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ typedef struct PMU_EVENT_STRUCT

uint64_t pmu_event_to_hexcode(PMU_EVENT *event);

int pmu_open_msr(int core);

void pmu_set_event(int core, int *msr_fd, uint64_t hexcode, size_t pmu_id);

void pmu_set_msr_event(int msr_fd, uint64_t hexcode, size_t pmu_id);

void pmu_set_pmc(int msr_fd, size_t pmu_id, uint64_t val);

void pmu_record_start(int msr_fd);
Expand Down
8 changes: 7 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ clean:
rm -rf *.out

test:
make clean && make && taskset -c 0 sudo ./basic.c.out
make clean

$(CC) $(LIBC) basic.c -o basic.c.out $(CFLAGS)
$(CC) $(LIBC) all.c -o all.c.out $(CFLAGS)

taskset -c 0 sudo ./basic.c.out
taskset -c 0 sudo ./all.c.out
48 changes: 48 additions & 0 deletions tests/all.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#include "cacheutils.h"
#include "./../libpmu/pmu.h"

int main(int argc, char const *argv[])
{
PMU_EVENT pe0 = {
.event_code = 0xD1,
.umask = 0x08,
.user_mode = 1,
.os = 0,
.edge_detect = 0,
.pc = 0,
.int_enable = 0,
.enable_couters = 1,
.invert = 0,
.counter_mask = 0,
}; // MEM_LOAD_RETIRED.L1_MISS

uint64_t hexcode = pmu_event_to_hexcode(&pe0);

int msr_fd = pmu_open_msr(0), core = 0, pmu_id = 0;

pmu_set_event(core, &msr_fd, hexcode, pmu_id);

pmu_set_msr_event(msr_fd, hexcode, pmu_id);

pmu_set_pmc(msr_fd, pmu_id, 0);

pmu_record_start(msr_fd);

uint64_t pmu_val_msr = pmu_get_MSR_pmc(msr_fd, pmu_id);

uint64_t pmu_val = pmu_get_rdpmc(pmu_id);

uint64_t msrs_num = pmu_get_MSRs_num();

uint64_t pmc_bit_width = pmu_get_PMC_bit_width();

pmu_record_stop(msr_fd);

printf("pmu-utils all test function passed\n");

return 0;
}
6 changes: 5 additions & 1 deletion tests/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char const *argv[])
uint64_t pmc_bit_width = pmu_get_PMC_bit_width();
printf("PMC_bit_width: \t%lu\n", pmc_bit_width);

int msr_fd, core = 0, pmu_id = 0;
int core = 0, pmu_id = 0;
PMU_EVENT pe0 = {
.event_code = 0xD1,
.umask = 0x08,
Expand All @@ -31,7 +31,11 @@ int main(int argc, char const *argv[])
uint64_t pe0_code = pmu_event_to_hexcode(&pe0);
printf("Event Hexcode: \t0x%lx\n", pe0_code);

int msr_fd = pmu_open_msr(core);

pmu_set_event(core, &msr_fd, pe0_code, pmu_id);
pmu_set_msr_event(msr_fd, pe0_code, pmu_id);

pmu_set_pmc(msr_fd, pmu_id, 0);
pmu_record_start(msr_fd);
uint64_t start_pmu_value = pmu_get_MSR_pmc(msr_fd, pmu_id);
Expand Down

0 comments on commit 21a3238

Please sign in to comment.