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

29.01.2014 Views

CHAPTER 10. ADDING A NEW KIND OF STATEMENT 88 ../ciltut-lib/src/tut10.c void tut_cache_end(char const *f, int l) { uint64_t final_miss, final_refs, final_time; uint64_t net_miss, net_refs, net_time; double miss_rate; double bandwidth; struct cache_stack *cs = get_CS(); final_miss = perf_get_cache_miss(); final_refs = perf_get_cache_refs(); final_time = tut_get_time(); net_miss = final_miss - cs->s[cs->t - 1].start_miss; net_refs = final_refs - cs->s[cs->t - 1].start_refs; net_time = final_time - cs->s[cs->t - 1].start_time; miss_rate = (double)net_miss/(double)net_refs; bandwidth = (double)(net_miss * 1000000000ULL)/(double)net_time; printf("%s:%d Miss rate was: %f, Bandwidth was %f\n", f, l, miss_rate, bandwidth); fflush(stdout); } cs->t--; return; The program in le test/tut10.c spawns as many threads as there are cores on the machine, and writes into a large array. The loop that writes the array is wrapped in a cache report statement. We can build and run the test as follows: $ ciltutcc --enable-tut10 -o tut10 test/tut10.c $ ./tut10 ... # debug messages from the pthread create wrapper test/tut10.c:26 Miss rate was: 0.133511, Bandwidth was 1176936.786361 test/tut10.c:26 Miss rate was: 0.135422, Bandwidth was 1164002.872631 test/tut10.c:26 Miss rate was: 0.136743, Bandwidth was 1174543.676575 test/tut10.c:26 Miss rate was: 0.126649, Bandwidth was 1073938.382925 On my machine, there are four cores, and the cache line size of the last-level cache is 64-bytes. Thus, the reported miss rate of approximately 1/8 makes sense since we are going sequentially through the array accessing each of the 8-byte uint64 ts only once or twice.

CHAPTER 10. ADDING A NEW KIND OF STATEMENT 89 10.3 Exercises 1. Given the example code in this chapter. It is straightforward to add statements that report on other performance counters, both hardware and software. Look in linux/perf event.h, and add another statement that proles a dierent performance counter. 2. We might also prole values that are specic to a particular application. Notice that, since we know how to translate attribute parameters into C expressions, we could just as well have allowed arguments to be passed to the proling statement we added, e.g. cache report(e) {}. Extend this statement to accept the address of an integer value, whose value is proled instead of a performance counter. 3. In ciltut-lib/src/ciltut libc.c, reading the performance counters is implemented only for Linux. Implement perf get cache miss and perf get cache refs for another OS. 10.4 Further Reading This technique for adding a new statement is exactly the same as the one used by the Shelters [1] extension to C, which adds an atomic {} statement. The statement is replaced by calls into the the Shelters language runtime, which enforces mutual exclusion in multithreaded programs for selected objects touched inside of the statement.

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

10.3 Exercises<br />

1. Given the example code in this chapter. It is straightforward to add statements that report<br />

on other performance counters, both hardware and s<strong>of</strong>tware. Look in linux/perf event.h,<br />

and add another statement that proles a dierent performance counter.<br />

2. We might also prole values that are specic to a particular application. Notice that, since<br />

we know how to translate attribute parameters into C expressions, we could just as well have<br />

allowed arguments to be passed to the proling statement we added, e.g. cache report(e) {}.<br />

Extend this statement to accept the address <strong>of</strong> an integer value, whose value is proled instead<br />

<strong>of</strong> a performance counter.<br />

3. In ciltut-lib/src/ciltut libc.c, reading the performance counters is implemented only<br />

for Linux. Implement perf get cache miss and perf get cache refs for another OS.<br />

10.4 Further Reading<br />

This technique for adding a new statement is exactly the same as the one used by the Shelters [1]<br />

extension to C, which adds an atomic {} statement. The statement is replaced by calls into the the<br />

Shelters language runtime, which enforces mutual exclusion in multithreaded programs for selected<br />

objects touched inside <strong>of</strong> the statement.

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

Saved successfully!

Ooh no, something went wrong!