Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / pounder21 / src / memxfer5b / memxfer5b.c
blob6092368f65f434fc0bd75cd1055a5fa2fc2ee6a2
1 /* Memory streaming benchmark */
3 /*
4 * Copyright (C) 2003-2006 IBM
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #define __int64 long long
23 #include <sys/time.h>
24 #define SLASHC '/'
25 #define SLASHSTR "/"
26 #include <sys/types.h>
27 #include <string.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
33 #define equal !strcmp
35 size_t atoik(char *);
36 void *Malloc(size_t sz);
37 void tstart(void);
38 void tend(void);
39 double tval(void);
41 char *methods[] = {
42 "\"memcpy\"",
43 "\"char *\"",
44 "\"short *\"",
45 "\"int *\"",
46 "\"long *\"",
47 "\"__int64 *\"",
48 "\"double *\"",
50 int nmethods = sizeof(methods)/sizeof(methods[0]);
52 int fflag = 0; // if 0, then just Malloc once; else malloc/free each time
53 int wflag = 0; // if 1, call SetProcessWorkingSetSize() (WINDOWS ONLY)
54 int sflag = 0; // if 1, only print averages.
55 int pflag = 0;
56 int csvflag = 0; // Print Comma separated list for spreadsheet input.
57 char *progname;
59 double tottim = 0.0;
61 int main(int ac, char *av[])
63 size_t size;
64 int i;
65 unsigned ui;
66 size_t j;
67 unsigned cnt;
68 int method = 0;
69 char *p1, *p2;
70 char *p,*q;
71 short *sp, *sq;
72 int *ip, *iq;
73 long *lp, *lq;
74 __int64 *llp, *llq;
75 double *dp, *dq;
76 double t;
78 progname = av[0];
79 if(strrchr(progname,SLASHC))
80 progname = strrchr(progname,SLASHC) + 1;
82 while(ac > 1) {
83 if(equal(av[1], "-f")) {
84 ac--;
85 fflag = 1;
86 av++;
88 else if(equal(av[1], "-w")) {
89 ac--;
90 wflag = 1;
91 av++;
93 else if(equal(av[1], "-s")) {
94 ac--;
95 sflag = 1;
96 av++;
98 else if(equal(av[1], "-p")) {
99 ac--;
100 pflag = 1;
101 av++;
103 else if(equal(av[1], "-csv")) {
104 ac--;
105 csvflag++;
106 av++;
108 else
109 break;
111 if(ac < 3) {
112 (void)printf("Usage: %s [-f] [-w] [-s] [-p] size cnt [method]\n",progname);
113 (void)printf("\t-f flag says to malloc and free of the \"cnt\" times.\n");
114 (void)printf("\t-w = set process min and max working set size to \"size\"\n");
115 (void)printf("\t-s = silent; only print averages\n");
116 (void)printf("\t-p = prep; \"freshen\" cache before; -w disables\n");
117 (void)printf("\t-csv = print output in CSV format\n");
119 (void)printf("\tmethods:\n");
120 for(i = 0; i < nmethods; i++)
121 printf("\t%2d:\t%s\n",i,methods[i]);
122 return 0;
125 size = atoik(av[1]);
128 // Round size up to 4*sizeof(double) bytes.
130 if(size != ((size/ (4*sizeof(double)) ) * (4*sizeof(double)) )) {
131 size += (4*sizeof(double));
132 size /= (4*sizeof(double));
133 size *= (4*sizeof(double));
135 cnt = (unsigned)atoik(av[2]);
137 if(fflag == 0) {
138 p1 = (char *)Malloc(size);
139 p2 = (char *)Malloc(size);
140 if(pflag)
141 memcpy(p1,p2,size);
144 printf("%s ",progname);
145 if(fflag) printf("-f ");
146 if(wflag) printf("-w ");
147 if(sflag) printf("-s ");
148 if(pflag) printf("-p ");
149 if(csvflag) printf("-csv ");
150 printf("%u %u ", size, cnt);
151 if(csvflag) {
152 printf("Linux");
154 printf("\n");
156 if(ac == 3) {
157 ac = 4;
158 av[3] = "0";
161 for(; ac > 3; ac--, av++) {
162 if(isdigit(*av[3])) method = *av[3] - '0';
163 if(method < 0 || method >= nmethods)
164 method = 0;
165 if(sflag)
166 tstart();
167 for(ui = 0; ui < cnt; ui++) {
168 if(!sflag) {
169 (void)printf("%s %d %d %-18.18s\t",
170 progname, size, cnt, methods[method]);
171 tstart();
173 if(fflag == 1) {
174 p1 = (char *)Malloc(size);
175 p2 = (char *)Malloc(size);
177 switch(method) {
178 case 0:
179 (void)memcpy(p1, p2, size);
180 break;
181 case 1:
182 p = p1;
183 q = p2;
184 for(j = 0; j < size; j++)
185 *p++ = *q++;
186 break;
187 case 2:
188 sp = (short *)p1;
189 sq = (short *)p2;
190 for(j = 0; j < size; j += sizeof(short))
191 *sp++ = *sq++;
192 break;
193 case 3:
194 ip = (int *)p1;
195 iq = (int *)p2;
196 for(j = 0; j < size; j += sizeof(int))
197 *ip++ = *iq++;
198 break;
199 case 4:
200 lp = (long *)p1;
201 lq = (long *)p2;
202 for(j = 0; j < size; j += sizeof(long))
203 *lp++ = *lq++;
204 break;
205 case 5:
206 llp = (__int64 *)p1;
207 llq = (__int64 *)p2;
208 for(j = 0; j < size; j += sizeof(__int64))
209 *llp++ = *llq++;
210 break;
211 case 6:
212 dp = (double *)p1;
213 dq = (double *)p2;
214 for(j = 0; j < size; j += 4*sizeof(double)) {
215 *dp++ = *dq++;
216 *dp++ = *dq++;
217 *dp++ = *dq++;
218 *dp++ = *dq++;
220 break;
223 if(fflag == 1) {
224 free(p1);
225 free(p2);
227 if(!sflag) {
228 tend();
229 t = tval();
230 tottim += t;
231 if(t == 0.0)
232 t = .0001;
233 printf(" %8.6f seconds %8.3f MB/s\n",
235 (double)size/t/1000000.);
238 if(sflag) {
239 tend();
240 tottim = tval();
242 if(csvflag) {
243 printf("%s,%u,%u,%8.3f,%8.3f\n",
244 methods[method],size,size*cnt,tottim,(double)size/(tottim/cnt)/1000000.);
246 else {
247 (void)printf("\tAVG: %d %-18.18s\t", size, methods[method]);
248 (void)printf(" %8.3f MB/s\n", (double)size/(tottim/cnt)/1000000.);
250 tottim = 0.0;
252 return 0;
256 size_t atoik(char *s)
258 size_t ret = 0;
259 size_t base;
261 if(*s == '0') {
262 base = 8;
263 if(*++s == 'x' || *s == 'X') {
264 base = 16;
265 s++;
268 else
269 base = 10;
271 for(; isxdigit(*s); s++) {
272 if(base == 16)
273 if(isalpha(*s))
274 ret = base*ret + (toupper(*s) - 'A');
275 else
276 ret = base*ret + (*s - '0');
277 else if(isdigit(*s))
278 ret = base*ret + (*s - '0');
279 else
280 break;
282 for(; isalpha(*s); s++) {
283 switch(toupper(*s)) {
284 case 'K': ret *= 1024; break;
285 case 'M': ret *= 1024*1024; break;
286 default:
287 return ret;
290 return ret;
292 void *Malloc(size_t sz)
294 char *p;
296 p = (char *)malloc(sz);
297 if(p == NULL) {
298 (void)printf("malloc(%d) failed\n",sz);
299 exit(1);
301 return (void *)p;
304 static struct timeval _tstart, _tend;
306 void tstart(void)
308 gettimeofday(&_tstart, NULL);
310 void tend(void)
312 gettimeofday(&_tend, NULL);
315 double tval()
317 double t1, t2;
319 t1 = (double)_tstart.tv_sec + (double)_tstart.tv_usec/(1000*1000);
320 t2 = (double)_tend.tv_sec + (double)_tend.tv_usec/(1000*1000);
321 return t2-t1;