| 1 | #include <sys/types.h>
|
|---|
| 2 | #include <fcntl.h>
|
|---|
| 3 | #include <sys/mman.h>
|
|---|
| 4 | #include <sys/syssgi.h>
|
|---|
| 5 | #include <sys/immu.h>
|
|---|
| 6 | #include <errno.h>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 |
|
|---|
| 9 | /* The following works on SGI Power Challenge systems */
|
|---|
| 10 |
|
|---|
| 11 | typedef unsigned long iotimer_t;
|
|---|
| 12 |
|
|---|
| 13 | unsigned int cycleval;
|
|---|
| 14 | volatile iotimer_t *iotimer_addr, base_counter;
|
|---|
| 15 | double resolution;
|
|---|
| 16 |
|
|---|
| 17 | /* address_t is an integer type big enough to hold an address */
|
|---|
| 18 | typedef unsigned long address_t;
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | void timer_init()
|
|---|
| 23 | {
|
|---|
| 24 |
|
|---|
| 25 | int fd;
|
|---|
| 26 | char *virt_addr;
|
|---|
| 27 | address_t phys_addr, page_offset, pagemask, pagebase_addr;
|
|---|
| 28 |
|
|---|
| 29 | pagemask = getpagesize() - 1;
|
|---|
| 30 | errno = 0;
|
|---|
| 31 | phys_addr = syssgi(SGI_QUERY_CYCLECNTR, &cycleval);
|
|---|
| 32 | if (errno != 0) {
|
|---|
| 33 | perror("SGI_QUERY_CYCLECNTR");
|
|---|
| 34 | exit(1);
|
|---|
| 35 | }
|
|---|
| 36 | /* rel_addr = page offset of physical address */
|
|---|
| 37 | page_offset = phys_addr & pagemask;
|
|---|
| 38 | pagebase_addr = phys_addr - page_offset;
|
|---|
| 39 | fd = open("/dev/mmem", O_RDONLY);
|
|---|
| 40 |
|
|---|
| 41 | virt_addr = mmap(0, pagemask, PROT_READ, MAP_PRIVATE, fd, pagebase_addr);
|
|---|
| 42 | virt_addr = virt_addr + page_offset;
|
|---|
| 43 | iotimer_addr = (iotimer_t *)virt_addr;
|
|---|
| 44 | /* cycleval in picoseconds to this gives resolution in seconds */
|
|---|
| 45 | resolution = 1.0e-12*cycleval;
|
|---|
| 46 | base_counter = *iotimer_addr;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | void wtime_(double *time)
|
|---|
| 50 | {
|
|---|
| 51 | static int initialized = 0;
|
|---|
| 52 | volatile iotimer_t counter_value;
|
|---|
| 53 | if (!initialized) {
|
|---|
| 54 | timer_init();
|
|---|
| 55 | initialized = 1;
|
|---|
| 56 | }
|
|---|
| 57 | counter_value = *iotimer_addr - base_counter;
|
|---|
| 58 | *time = (double)counter_value * resolution;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | void wtime(double *time)
|
|---|
| 63 | {
|
|---|
| 64 | static int initialized = 0;
|
|---|
| 65 | volatile iotimer_t counter_value;
|
|---|
| 66 | if (!initialized) {
|
|---|
| 67 | timer_init();
|
|---|
| 68 | initialized = 1;
|
|---|
| 69 | }
|
|---|
| 70 | counter_value = *iotimer_addr - base_counter;
|
|---|
| 71 | *time = (double)counter_value * resolution;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|