29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10. ADDING A NEW KIND OF STATEMENT 83<br />

The cacheReportAdder visitor nds statements satisfying isCacheReportStmt, and brackets the<br />

statements nested within them inside <strong>of</strong> the calls to our runtime, which are given by makeCacheReportStmts.<br />

class cacheReportAdder = object(self)<br />

inherit nopCilVisitor<br />

method vstmt (s : stmt) =<br />

let action s =<br />

match isCacheReportStmt s with<br />

| Some b → begin<br />

let bs, es = makeCacheReportStmts (get stmtLoc s.skind) in<br />

let nb = mkBlock [bs; mkStmt(Block b); es] in<br />

s.skind ← Block nb;<br />

s<br />

end<br />

| None → s<br />

in<br />

ChangeDoChildrenPost(s, action)<br />

end<br />

The function tut10 is the entry point to this module.<br />

invokes the cacheReportAdder visitor on the le f.<br />

It Initializes the runtime functions, and<br />

let tut10 (f : file) : unit =<br />

initCacheFunctions f;<br />

let vis = new cacheReportAdder in<br />

visitCilFile vis f<br />

Now we describe all <strong>of</strong> the hard work that takes place inside <strong>of</strong> the C functions tut cache begin and<br />

tut cache end. In addition we must explain the data structures used to maintain per-thread state,<br />

and the method for reading the hardware performance counters for cache misses and references.<br />

10.2 tut10.c<br />

This C source le contains the implementation <strong>of</strong> the runtime for the cache report statement. It<br />

must accomplish three things. First, each thread must use Linux's perf event open system call to<br />

access the hardware performance counters for cache misses and references. Secondly, each thread<br />

must maintain a stack that mirrors the nesting structure <strong>of</strong> the cache report statement. Finally,<br />

we must use the dynamic linker to override the C Library function pthread create to ensure that<br />

new threads are set up to access the performance counters and to maintain the stack.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!