~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to chirp/src/chirp_benchmark.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "chirp_reli.h"
 
9
 
 
10
#include "full_io.h"
 
11
#include "auth_all.h"
 
12
 
 
13
#include <stdio.h>
 
14
#include <unistd.h>
 
15
#include <sys/stat.h>
 
16
#include <stdlib.h>
 
17
#include <fcntl.h>
 
18
#include <sys/time.h>
 
19
#include <string.h>
 
20
#include <math.h>
 
21
#include <stdarg.h>
 
22
#include <errno.h>
 
23
#ifndef CCTOOLS_OPSYS_CYGWIN
 
24
#include <sys/syscall.h>
 
25
#endif
 
26
 
 
27
char *host;
 
28
time_t stoptime;
 
29
 
 
30
double measure[10000];
 
31
double total;
 
32
double average;
 
33
double variance;
 
34
double stddev;
 
35
int loops,cycles;
 
36
int do_chirp;
 
37
int measure_bandwidth = 0;
 
38
 
 
39
#ifdef CCTOOLS_OPSYS_DARWIN
 
40
#define do_sync 0
 
41
#else
 
42
#define do_sync (getenv("CHIRP_SYNC") ? O_SYNC : 0)
 
43
#endif
 
44
 
 
45
long do_open( const char *file, int flags, int mode )
 
46
{
 
47
        if(do_chirp) {
 
48
                return (long) chirp_reli_open(host,file,flags,mode,stoptime);
 
49
        } else {
 
50
                return open(file,flags,mode);
 
51
        }
 
52
}
 
53
 
 
54
int do_close( long fd )
 
55
{
 
56
        if(do_chirp) {
 
57
                return chirp_reli_close((struct chirp_file*)fd,stoptime);
 
58
        } else {
 
59
                return close(fd);
 
60
        }
 
61
}
 
62
 
 
63
int do_fsync( long fd )
 
64
{
 
65
        if(do_chirp) {
 
66
                return 0;
 
67
        } else {
 
68
                return fsync(fd);
 
69
        }
 
70
}
 
71
 
 
72
int do_pread( long fd, char *buffer, int length, int offset )
 
73
{
 
74
        if(do_chirp) {
 
75
                return chirp_reli_pread((struct chirp_file*)fd,buffer,length,offset,stoptime);
 
76
        } else {
 
77
                return full_pread(fd,buffer,length,offset);
 
78
        }
 
79
}
 
80
 
 
81
int do_pwrite( long fd, const char *buffer, int length, int offset )
 
82
{
 
83
        if(do_chirp) {
 
84
                return chirp_reli_pwrite((struct chirp_file*)fd,buffer,length,offset,stoptime);
 
85
        } else {
 
86
                return full_pwrite(fd,buffer,length,offset);
 
87
        }
 
88
}
 
89
 
 
90
int do_stat( const char *file, struct stat *buf )
 
91
{
 
92
        if(do_chirp) {
 
93
                struct chirp_stat lbuf;
 
94
                return chirp_reli_stat(host,file,&lbuf,stoptime);
 
95
        } else {
 
96
                return stat(file,buf);
 
97
        }
 
98
}
 
99
 
 
100
int do_bandwidth( const char *file, int bytes, int blocksize, int do_write )
 
101
{
 
102
        int offset = 0;
 
103
        long fd;
 
104
        char *buffer = malloc(blocksize);
 
105
        int i;
 
106
 
 
107
        for(i=0;i<blocksize;i++) buffer[i] = (char)i;
 
108
 
 
109
        fd = do_open(file,(do_write ? O_WRONLY : O_RDONLY)|O_CREAT|do_sync,0777);
 
110
        if(fd<0 || fd==0) {
 
111
                printf("couldn't open %s: %s",file,strerror(errno));
 
112
                return 0;
 
113
        }
 
114
 
 
115
        while(bytes>0) {
 
116
                if(do_write) {
 
117
                        do_pwrite(fd,buffer,blocksize,offset);
 
118
                } else {
 
119
                        do_pread(fd,buffer,blocksize,offset);
 
120
                }
 
121
                offset += blocksize;
 
122
                bytes -= blocksize;
 
123
        }
 
124
 
 
125
        do_close(fd);
 
126
        free(buffer);
 
127
        return 1;
 
128
}
 
129
 
 
130
void print_total()
 
131
{
 
132
        int j;
 
133
        total = 0;
 
134
        variance = 0;
 
135
        for(j=0;j<cycles;j++) total += measure[j];
 
136
        average = total/cycles;
 
137
        for(j=0;j<cycles;j++) variance += (measure[j]-average)*(measure[j]-average);
 
138
        stddev = sqrt(variance/(cycles-1));
 
139
 
 
140
        printf("%9.4f +/- %9.4f ",average,stddev);
 
141
 
 
142
        if(measure_bandwidth) {
 
143
                printf(" MB/s\n");
 
144
        } else {
 
145
                printf(" usec\n");
 
146
        }
 
147
}
 
148
 
 
149
#define RUN_LOOP( name, test ) \
 
150
        printf("%s\t",name);\
 
151
        for(j=0;j<cycles;j++) {\
 
152
                gettimeofday( &start, 0 );\
 
153
                for( i=0; i<loops; i++ ) {\
 
154
                        test;\
 
155
                }\
 
156
                gettimeofday( &stop, 0 );\
 
157
                runtime = (stop.tv_sec-start.tv_sec)*1000000 + (stop.tv_usec-start.tv_usec);\
 
158
                if(measure_bandwidth) {\
 
159
                        measure[j] = (filesize*loops/(double)runtime);\
 
160
                } else {\
 
161
                        measure[j] = ((double)(runtime))/loops;\
 
162
                }\
 
163
        }\
 
164
        print_total();
 
165
 
 
166
 
 
167
int main( int argc, char *argv[] )
 
168
{
 
169
        long fd;
 
170
        int i,j,k;
 
171
        int bwloops;
 
172
        char *fname;
 
173
        char data[8192];
 
174
        int runtime;
 
175
        struct stat buf;
 
176
        struct timeval start, stop;
 
177
        stoptime = time(0)+3600;
 
178
        int filesize = 16*1024*1024;
 
179
 
 
180
        if(argc!=6) {
 
181
                printf("use: %s <host> <file> <loops> <cycles> <bwloops>\n",argv[0]);
 
182
                return -1;
 
183
        }
 
184
 
 
185
        auth_register_all();
 
186
 
 
187
        host = argv[1];
 
188
        fname = argv[2];
 
189
        loops = atoi(argv[3]);
 
190
        cycles = atoi(argv[4]);
 
191
        bwloops = atoi(argv[5]);
 
192
 
 
193
        if(!strcmp(host,"unix")) {
 
194
                do_chirp = 0;
 
195
        } else {
 
196
                do_chirp = 1;
 
197
        }
 
198
 
 
199
#ifdef SYS_getpid
 
200
        RUN_LOOP("getpid",syscall(SYS_getpid));
 
201
#else
 
202
        RUN_LOOP("getpid",getpid());
 
203
#endif
 
204
 
 
205
        fd = do_open(fname,O_WRONLY|O_CREAT|do_sync,0777);
 
206
        if(fd<0 || fd==0) {
 
207
                perror(fname);
 
208
                return -1;
 
209
        }
 
210
 
 
211
        RUN_LOOP("write1",do_pwrite(fd,data,1,0));
 
212
        RUN_LOOP("write8",do_pwrite(fd,data,8192,0));
 
213
 
 
214
        do_close(fd);
 
215
 
 
216
        fd = do_open(fname,O_RDONLY|do_sync,0777);
 
217
        if(fd<0) {
 
218
                perror(fname);
 
219
                return -1;
 
220
        }
 
221
 
 
222
        RUN_LOOP("read1",do_pread(fd,data,1,0));
 
223
        RUN_LOOP("read8",do_pread(fd,data,8192,0));
 
224
 
 
225
        do_close(fd);
 
226
 
 
227
        RUN_LOOP("stat",do_stat(fname,&buf));
 
228
        RUN_LOOP("open",fd = do_open(fname,O_RDONLY|do_sync,0777); do_close(fd); );
 
229
 
 
230
        if(bwloops==0) return 0;
 
231
 
 
232
        loops = bwloops;
 
233
        measure_bandwidth = 1;
 
234
 
 
235
        for(k=filesize;k>=(4*1024);k=k/2) {
 
236
                printf("%4d ",k/1024);
 
237
                RUN_LOOP("write",do_bandwidth(fname,filesize,k,1));
 
238
                sync();
 
239
                printf("%4d ",k/1024);
 
240
                RUN_LOOP("read",do_bandwidth(fname,filesize,k,0));
 
241
                sync();
 
242
        }
 
243
 
 
244
        return 0;
 
245
}