LCA2011 version
[tridge/junkcode.git] / cache.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4
5 #ifdef SOLARIS2
6 #include <sys/rusage.h>
7 #endif
8
9 #include <sys/resource.h>
10 #include <unistd.h>
11
12 /*Blocksize is 1M words (on intels, this is 1Mb)
13 */
14
15 #define MAXBSIZE (1024*1024)
16 #define LOOPTIME 2.0
17
18 typedef unsigned int ui;
19
20 double readtime(void);
21 double read_test(ui);
22 void build_buffer(int, int);
23
24 ui buf[MAXBSIZE * sizeof(ui *)];
25 FILE *record;
26
27 double readtime()
28         {
29         struct rusage r;
30         double rv;
31
32         getrusage(RUSAGE_SELF,&r);
33
34 #ifdef SOLARIS2 || _POSIX_C_SOURCE
35         rv = (double)r.ru_utime.tv_sec + (double)(r.ru_utime.tv_nsec/1e9);
36 #else
37         rv = (double)r.ru_utime.tv_sec + (double)(r.ru_utime.tv_usec/1e6);
38 #endif
39         return(rv);
40         }
41
42 main()
43         {
44         double t,wps;
45         int str;
46         ui count,buflen;
47         int i,pcount=0;
48         char strbuf[10];
49
50         /* Open our log file */
51         for(i=1; i<32; ++i)
52            {
53            sprintf(strbuf,"cache.%d",i);
54            if (access(strbuf,0)) break;
55            }
56         record=fopen(strbuf,"w");
57         if (record==NULL) printf("Could not open record file %s. No record kept\n",strbuf);
58         else printf("\t\tA copy of this run will be kept in %s\n\n",strbuf);
59
60         printf("Memory tester v1.0\n");
61         printf("Author: Anthony.Wesley@anu.edu.au, January 1996\n\n");
62         printf("On this machine, one word = %d bytes\n",sizeof(ui *));
63         printf("Reported access times are in nS per word.\n\n");
64         if (record!=NULL)
65            {
66            fprintf(record,"On this machine, one word = %d bytes\n",sizeof(ui *));
67            fprintf(record,"Reported access times are in nS per word.\n\n");
68            }
69
70         /* Start with a block of 256 words, each word is of type (ui *) */
71         build_buffer(256,1);
72         count=8;
73         do
74            {
75            count *= 2;
76            t = read_test(count);
77            }
78         while(t < 2);
79
80         fflush(stdout);
81
82         print_header();
83         count = (ui)((double)count * LOOPTIME / t);
84         for(buflen=256; buflen<=MAXBSIZE; buflen*=2)
85            {
86            if (buflen<256*1024) sprintf(strbuf,"%uk",buflen/256);
87            else sprintf(strbuf,"%uM",buflen/(256*1024));
88
89            printf("%-5.5s|",strbuf); fflush(stdout);
90            if (record!=NULL) fprintf(record,"%-5.5s|",strbuf);
91
92            for(str=1; str<buflen; str*=2)
93                 {
94                 build_buffer(buflen,str);
95
96                 while(1)
97                    {
98                    t = read_test(count);
99                    if (t<1.0) count*=3; else break;
100                    }
101         
102                 wps = (double)(count) / t;
103                 wps = 1.0e9 / wps;
104                 if (wps>=1000)
105                    {
106                    printf("1k+ ");
107                    if (record!=NULL) fprintf(record,"1k+ ");
108                    }
109                 else 
110                    {
111                    printf("%-3d ", (int)wps);
112                    if (record!=NULL) fprintf(record,"%-3d ", (int)wps);
113                    }
114
115                 fflush(stdout); if (record!=NULL) fflush(record);
116
117                 count = (ui)((double)count * LOOPTIME / t);
118                 }
119            putchar('\n');
120            if (record!=NULL) putc('\n',record);
121            }
122
123         if (record!=NULL) fclose(record);
124         }
125
126 double read_test(ui count)
127         {
128         double t1;
129         register ui *o,i=0;
130
131         o=(ui *)buf[0];
132         t1=readtime(); while(i<count)
133                 {
134                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
135                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
136                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
137                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
138                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
139                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
140                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
141                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
142                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
143                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
144                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
145                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
146                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
147                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
148                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
149                 o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o); o=(ui *)(*o);
150                 i+=64;
151                 }
152         return(readtime()-t1);
153         }
154
155 void
156 build_buffer(int size, int stridelen)
157         {
158         ui i;
159
160         for(i=0; i<(size-stridelen); i+=stridelen)
161                 buf[i]=(ui)&buf[i+stridelen];
162         buf[i] = (ui)&buf[0];
163         }
164
165 print_header()
166         {
167         char strbuf[32];
168         unsigned v,val,i;
169         char c=0;
170
171         printf(
172 "Block|--------------------------Memory Stride (words)------------------------\n");
173 printf("size |");
174
175         if (record!=NULL)
176            {
177            fprintf(record,
178            "Block|--------------------------Memory Stride (words)------------------------\n");
179            fprintf(record,"size |");
180            }
181
182         for(val=1; val<=(1024*128); val*=2)
183            {
184            v=val;
185            if (val>=(1024*1024)) { c='M'; v = val / (1024*1024); }
186            else if (val>=1024) { c='k'; v = val / 1024; }
187
188            sprintf(strbuf,"%d%c",v,c);
189            if (v>64 && c) {strbuf[2]=c; strbuf[3]=0;}
190            printf("%-3s ",strbuf);
191            if (record!=NULL) fprintf(record,"%-3s ",strbuf);
192            }
193         putchar('\n'); if (record!=NULL) putc('\n',record);
194         printf("     |\n");
195         if (record!=NULL) fprintf(record,"     |\n");
196         }