|
| 1 | +// Copyright 2019 ETH Zurich and University of Bologna. |
| 2 | +// Copyright and related rights are licensed under the Solderpad Hardware |
| 3 | +// License, Version 0.51 (the "License"); you may not use this file except in |
| 4 | +// compliance with the License. You may obtain a copy of the License at |
| 5 | +// http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law |
| 6 | +// or agreed to in writing, software, hardware and materials distributed under |
| 7 | +// this License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | +// specific language governing permissions and limitations under the License. |
| 10 | + |
| 11 | +extern int printf(const char *format, ...); |
| 12 | + |
| 13 | +#define read_csr(reg) ({ unsigned long __tmp; \ |
| 14 | + asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \ |
| 15 | + __tmp; }) |
| 16 | + |
| 17 | +char buffer[1024 * 1024]; |
| 18 | + |
| 19 | +void sweep(int stride) |
| 20 | +{ |
| 21 | + long instret_start, cycle_start; |
| 22 | + int max_j = 4 * 1024; |
| 23 | + int working_set = max_j * stride; |
| 24 | + |
| 25 | + for(int i = 0; i < 10; i++) |
| 26 | + { |
| 27 | + if(i == 1) |
| 28 | + { |
| 29 | + instret_start = read_csr(instret); |
| 30 | + cycle_start = read_csr(cycle); |
| 31 | + } |
| 32 | + |
| 33 | + for(int j = 0; j < max_j; j += 4) |
| 34 | + { |
| 35 | + buffer[(j + 0) * stride] = 0; |
| 36 | + buffer[(j + 1) * stride] = 0; |
| 37 | + buffer[(j + 2) * stride] = 0; |
| 38 | + buffer[(j + 3) * stride] = 0; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + long instrets = read_csr(instret) - instret_start; |
| 43 | + long cycles = read_csr(cycle) - cycle_start; |
| 44 | + |
| 45 | + printf("working_set = %2dKB, %ld instructions, %ld cycles, CPI = %f\n", |
| 46 | + working_set / 1024, instrets, cycles, (float) cycles / instrets); |
| 47 | +} |
| 48 | + |
| 49 | +int main() |
| 50 | +{ |
| 51 | + sweep(0); |
| 52 | + sweep(1); |
| 53 | + sweep(2); |
| 54 | + sweep(4); |
| 55 | + sweep(8); |
| 56 | + sweep(16); |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
0 commit comments