source: drsdaq/VME/struck/sis1100/V2.02/test/shortblock.c@ 23

Last change on this file since 23 was 22, checked in by ogrimm, 16 years ago
First commit of drsdaq program
File size: 6.1 KB
Line 
1#define _GNU_SOURCE
2#define _LARGEFILE_SOURCE
3#define _LARGEFILE64_SOURCE
4#define _FILE_OFFSET_BITS 64
5#include <stdio.h>
6#include <errno.h>
7#include <string.h>
8#include <sys/types.h>
9#include <time.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <fcntl.h>
13#include <sys/ioctl.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
16#include <arpa/inet.h>
17#include <netdb.h>
18
19#include "dev/pci/sis1100_var.h"
20
21#define IVAL 1
22
23#define VMESTART 0x84000000
24
25int sis1100, interval;
26off_t devstart;
27size_t maxsize;
28
29static void
30printusage(int argc, char* argv[])
31{
32 printf("usage: %s [-i interval][-s maxsize] sis1100_path\n",
33 argv[0]);
34}
35
36static int
37getoptions(int argc, char* argv[])
38{
39 extern char *optarg;
40 extern int optind;
41 extern int opterr;
42 extern int optopt;
43 int errflag, c;
44 char* sis1100_path=0;
45 const char* args="i:s:";
46
47 optarg=0; errflag=0;
48
49 while (!errflag && ((c=getopt(argc, argv, args))!=-1)) {
50 switch (c) {
51 case 'i': interval=atoi(optarg); break;
52 case 's': maxsize=atoi(optarg); break;
53 default: errflag=1;
54 }
55 }
56
57 if (errflag || optind!=argc-1) {
58 printusage(argc, argv);
59 return -1;
60 }
61
62 sis1100_path=argv[optind];
63 if ((sis1100=open(sis1100_path, O_RDWR, 0))<0) {
64 printf("open \"%s\": %s\n", sis1100_path, strerror(errno));
65 return -1;
66 }
67
68 return 0;
69}
70
71static int do_write(int p, int start, int size, int* data)
72{
73 off_t pos;
74 int res;
75
76 pos=lseek(p, sizeof(int)*start+devstart, SEEK_SET);
77 if (pos==(off_t)-1) {
78 perror("do_write::lseek");
79 return 1;
80 }
81 res=write(p, data, size*sizeof(int));
82 if (res!=size*sizeof(int)) {
83 u_int32_t error;
84 ioctl(p, SIS1100_LAST_ERROR, &error);
85 if (res<0) {
86 printf("write: %s; error=0x%x\n", strerror(errno), error);
87 } else {
88 printf("write: res=%d; error=0x%x\n", res, error);
89 }
90 return 1;
91 }
92 return 0;
93}
94
95static int do_read(int p, int start, int size, int* data)
96{
97 off_t pos;
98 int res;
99
100 pos=lseek(p, sizeof(int)*start+devstart, SEEK_SET);
101 if (pos==(off_t)-1) {
102 perror("do_read::lseek");
103 return 1;
104 }
105 res=read(p, data, size*sizeof(int));
106 if (res!=size*sizeof(int)) {
107 u_int32_t error;
108 ioctl(p, SIS1100_LAST_ERROR, &error);
109 if (res<0) {
110 printf("read: %s; error=0x%x\n", strerror(errno), error);
111 } else {
112 printf("read: res=%d; error=0x%x\n", res, error);
113 }
114 return 1;
115 }
116 return 0;
117}
118
119static void
120set_break(int p, int size)
121{
122 struct vmespace space;
123 int res;
124 space.am=0xb;
125 space.datasize=4;
126 space.swap=1;
127 space.mapit=0;
128 space.mindmalen=size;
129 res=ioctl(p, SETVMESPACE, &space);
130 if (res<0)
131 printf("ioctl(SETVMESPACE): %s\n", strerror(errno));
132}
133
134static int
135do_check(int p, int num, int* ibuf, int* obuf)
136{
137 int val0, stopsec, i, loops;
138 struct timeval start, stop;
139
140 printf("%d", num); fflush(stdout);
141
142 val0=random();
143 for (i=0; i<num; i++) obuf[i]=i+val0;
144
145 if (do_write(p, 0, num, obuf)) {
146 printf("\ni_write failed\n");
147 return -1;
148 }
149 if (do_read(p, 0, num, ibuf)) {
150 printf("\ni_read failed\n");
151 printf("p=%d, num=%d, ibuf=%p\n", p, num, ibuf);
152 return -1;
153 }
154
155 if (bcmp(obuf, ibuf, num*sizeof(int)!=0)) {
156 printf("\nmismatch at num=%d\n", num);
157 return -1;
158 }
159
160 loops=0;
161 gettimeofday(&start, 0);
162 stopsec=start.tv_sec+interval;
163 do {
164 if (do_write(p, 0, num, obuf)) {
165 printf("\nwrite failed\n");
166 return -1;
167 }
168 gettimeofday(&stop, 0);
169 loops++;
170 } while (stop.tv_sec<stopsec);
171 printf(" %d", loops); fflush(stdout);
172
173 loops=0;
174 gettimeofday(&start, 0);
175 stopsec=start.tv_sec+interval;
176 do {
177 if (do_read(p, 0, num, ibuf)) {
178 printf("\nread failed\n");
179 return -1;
180 }
181 gettimeofday(&stop, 0);
182 loops++;
183 } while (stop.tv_sec<stopsec);
184 printf(" %d\n", loops); fflush(stdout);
185
186 return 0;
187}
188
189int main(int argc, char* argv[])
190{
191 int *ibuf=0, *obuf=0;
192 u_int32_t max=0;
193 int size;
194 int devtype;
195
196 sis1100=-1;
197 interval=IVAL;
198 maxsize=0;
199
200 if (getoptions(argc, argv)<0) goto fehler;
201
202 if (ioctl(sis1100, SIS1100_DEVTYPE, &devtype)<0) {
203 printf("ioctl(SIS1100_DEVTYPE): %s\n", strerror(errno));
204 goto fehler;
205 }
206 switch (devtype) {
207 case 0: printf("using VME Device\n"); break;
208 case 1: printf("using RAM Device\n"); break;
209 case 2: printf("cannot use SHARC Device\n"); goto fehler;
210 default:
211 printf("cannot use unknown device %d\n", devtype);
212 goto fehler;
213 }
214
215 switch (devtype) {
216 case 0:
217 max=0x04000000;
218 devstart=VMESTART;
219 break;
220 case 1:
221/*
222 max=lseek(sis1100, 0, SEEK_END);
223 if (max==(off_t)-1) {
224 perror("lseek(0, SEEK_END)");
225 goto fehler;
226 }
227*/
228 {
229 if (ioctl(sis1100, SIS1100_MAPSIZE, &max)) {
230 perror("ioctl(MAPSIZE)");
231 return 1;
232 }
233 devstart=0;
234 }
235 break;
236 }
237 max/=sizeof(int);
238 printf("usable size is 0x%08x (%d MWords)\n", max, max/(1<<20));
239 if (maxsize) max=maxsize;
240 printf("using %d Words\n", max);
241
242 ibuf=obuf=0;
243 ibuf=calloc(max, sizeof(int));
244 if (!ibuf) {
245 printf("calloc %d bytes for ibuf: %s\n", max*sizeof(int), strerror(errno));
246 goto fehler;
247 }
248 obuf=calloc(max, sizeof(int));
249 if (!obuf) {
250 printf("calloc %d bytes for obuf: %s\n", max*sizeof(int), strerror(errno));
251 goto fehler;
252 }
253 printf("ibuf=%p, obuf=%p\n", ibuf, obuf);
254
255 if (do_write(sis1100, 0, max, obuf)) {
256 printf("initial write failed\n");
257 goto fehler;
258 }
259
260 set_break(sis1100, 1);
261 for (;;) {
262 for (size=1; size<=max; size++) {
263 if (do_check(sis1100, size, ibuf, obuf)) {
264 printf("\nfailed\n");
265 goto fehler;
266 }
267 }
268 }
269
270fehler:
271 if (obuf) free(obuf);
272 if (ibuf) free(ibuf);
273 if (sis1100) close(sis1100);
274 return 0;
275}
Note: See TracBrowser for help on using the repository browser.