OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_14/usb_fpga_2_14/trunk

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [examples/] [flashbench/] [FlashBench.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   flashbench -- Flash memory benchmark
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   Licensed under the Apache License, Version 2.0 (the "License");
7
   you may not use this file except in compliance with the License.
8
   You may obtain a copy of the License at
9
 
10
       http://www.apache.org/licenses/LICENSE-2.0
11
 
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
%*/
18
 
19
import java.io.*;
20
import java.util.*;
21
import java.nio.*;
22
 
23
import org.usb4java.*;
24
 
25
import ztex.*;
26
 
27
// *****************************************************************************
28
// ******* ParameterException **************************************************
29
// *****************************************************************************
30
// Exception the prints a help message
31
class ParameterException extends Exception {
32
    public final static String helpMsg = new String (
33
                "Parameters:\n"+
34
                "    -d <number>   Device Number (default: 0)\n" +
35
                "    -s <number>   Number of sectors to be tested (64 KByte sector default: 16; 512 Bytes sector default: 8192)\n" +
36
                "    -w            Perform write tests (may destroy data)\n" +
37
                "    -1            Test primary flash (default if no secondary Flash available\n" +
38
                "    -2            Test secondary Flash  (default if available)\n" +
39
                "    -p            Print bus info\n" +
40
                "    -h            This help" );
41
 
42
    public ParameterException (String msg) {
43
        super( msg + "\n" + helpMsg );
44
    }
45
}
46
 
47
// *****************************************************************************
48
// ******* Test0 ***************************************************************
49
// *****************************************************************************
50
class FlashBench extends Ztex1v1 {
51
 
52
// ******* FlashBench **********************************************************
53
// constructor
54
    public FlashBench ( ZtexDevice1 pDev ) throws UsbException {
55
        super ( pDev );
56
    }
57
 
58
// ******* testRW **************************************************************
59
// measures read + write performance
60
    public int testRW (boolean secondary, int num ) throws UsbException, InvalidFirmwareException, CapabilityException {
61
        int flashSectorSize = secondary ? flash2SectorSize() : flashSectorSize();
62
        int secNum = Math.max(1, 2048 / flashSectorSize);
63
        byte[] buf1 = new byte[flashSectorSize * secNum];
64
        byte[] buf2 = new byte[flashSectorSize * secNum];
65
 
66
        long t0 = new Date().getTime();
67
 
68
        for ( int i=0; i<num; i+=secNum ) {
69
            int l = Math.min(num-i,secNum);
70
            for (int k=0; k<flashSectorSize*l; k++) {
71
                buf1[k] = (byte) (int) Math.floor(256.0*Math.random());
72
            }
73
 
74
            System.out.print("Sector " + (i+l) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
75
            if ( secondary ) {
76
                flash2WriteSector(i,l,buf1);
77
                flash2ReadSector(i,l,buf2);
78
            } else {
79
                flashWriteSector(i,l,buf1);
80
                flashReadSector(i,l,buf2);
81
            }
82
            int diffs=flashSectorSize*l;
83
            for (int k=0; k<flashSectorSize*l; k++)
84
                if ( buf1[k] == buf2[k] )
85
                    diffs -= 1;
86
            if ( diffs!=0 ) {
87
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
88
            }
89
        }
90
 
91
        return (int) Math.round(num*flashSectorSize*1.0/(new Date().getTime() - t0));
92
    }
93
 
94
// ******* testW **************************************************************
95
// measures write performance
96
    public int testW (boolean secondary,  int num, byte[] backup ) throws UsbException, InvalidFirmwareException, CapabilityException {
97
        int flashSectorSize = secondary ? flash2SectorSize() : flashSectorSize();
98
        int secNum = Math.max(1, 2048 / flashSectorSize );
99
        byte[] buf = new byte[flashSectorSize * secNum];
100
        long t0 = new Date().getTime();
101
        for ( int i=0; i<num; i+=secNum ) {
102
            int j = Math.min(num-i,secNum);
103
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
104
            System.arraycopy(backup,flashSectorSize*i, buf,0, flashSectorSize*j);
105
            if ( secondary ) flash2WriteSector(i,j,buf);
106
            else flashWriteSector(i,j,buf);
107
        }
108
        return (int) Math.round(num*flashSectorSize*1.0/(new Date().getTime() - t0));
109
    }
110
 
111
// ******* testR **************************************************************
112
// measures read performance
113
    public int testR (boolean secondary,  int num, boolean verify, byte[] backup ) throws UsbException, InvalidFirmwareException, CapabilityException {
114
        int flashSectorSize = secondary ? flash2SectorSize() : flashSectorSize();
115
        int secNum = Math.max(1, 2048 / flashSectorSize );
116
        byte[] buf = new byte[flashSectorSize * secNum];
117
        int errors = 0;
118
        long t0 = new Date().getTime();
119
        for ( int i=0; i<num; i+=secNum ) {
120
            int j = Math.min(num-i,secNum);
121
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
122
            if ( secondary ) flash2ReadSector(i,j,buf);
123
            else flashReadSector(i,j,buf);
124
            if ( backup != null ) {
125
                if ( verify ) {
126
                    int diffs = flashSectorSize*j;
127
                    int l = flashSectorSize * i;
128
                    for (int k=0; k<flashSectorSize*j; k++)
129
                        if ( buf[k] == backup[flashSectorSize*i+k] ) diffs-=1;
130
                    if ( diffs!=0 ) System.out.println("Error occured at sector " + i +": " + diffs + " differences");
131
                }
132
                else {
133
                    System.arraycopy(buf,0, backup,flashSectorSize*i, flashSectorSize*j);
134
                }
135
            }
136
        }
137
        return (int) Math.round(num*flashSectorSize*1.0/(new Date().getTime() - t0));
138
    }
139
 
140
// ******* main ****************************************************************
141
    public static void main (String args[]) {
142
 
143
        int devNum = 0;
144
        int sectors = 0;
145
        boolean flash1 = false, have1 = false;
146
        boolean flash2 = false, have2 = false;
147
        boolean writeTests = false;
148
 
149
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
150
            Runtime.getRuntime().addShutdownHook(new Thread() {
151
                public void run() {
152
                    Scanner s=new Scanner(System.in);
153
                    System.out.println("Press <enter> to continue ...");
154
                    s.nextLine();
155
                }
156
            });
157
        }
158
 
159
        try {
160
// Scan the USB. This also creates and initializes a new USB context.
161
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
162
 
163
// scan the command line arguments
164
            for (int i=0; i<args.length; i++ ) {
165
                if ( args[i].equals("-d") ) {
166
                    i++;
167
                    try {
168
                        if (i>=args.length) throw new Exception();
169
                        devNum = Integer.parseInt( args[i] );
170
                    }
171
                    catch (Exception e) {
172
                        throw new ParameterException("Device number expected after -d");
173
                    }
174
                }
175
                if ( args[i].equals("-s") ) {
176
                    i++;
177
                    try {
178
                        if (i>=args.length) throw new Exception();
179
                        sectors = Integer.parseInt( args[i] );
180
                    }
181
                    catch (Exception e) {
182
                        throw new ParameterException("Number of sectors expected after -s");
183
                    }
184
                }
185
                else if ( args[i].equals("-1") ) {
186
                    flash1 = true;
187
                }
188
                else if ( args[i].equals("-2") ) {
189
                    flash2 = true;
190
                }
191
                else if ( args[i].equals("-w") ) {
192
                    writeTests = true;
193
                }
194
                else if ( args[i].equals("-p") ) {
195
                    bus.printBus(System.out);
196
                    System.exit(0);
197
                }
198
                else if ( args[i].equals("-h") ) {
199
                    System.err.println(ParameterException.helpMsg);
200
                    System.exit(0);
201
                }
202
                else throw new ParameterException("Invalid Parameter: "+args[i]);
203
            }
204
 
205
 
206
// create the main class            
207
            if ( bus.numberOfDevices() <= 0) {
208
                System.err.println("No devices found");
209
                System.exit(0);
210
            }
211
            FlashBench ztex = new FlashBench ( bus.device(devNum) );
212
            bus.unref();
213
 
214
// print some information
215
            System.out.println("Capabilities: " + ztex.capabilityInfo(", "));
216
 
217
            if ( ztex.InterfaceCapabilities(CAPABILITY_FLASH) ) {
218
                System.out.println("Primary Flash enabled: " + ztex.flashEnabled());
219
                System.out.println("Primary Flash sector size: " + ztex.toHumanStr(ztex.flashSectorSize())+" Bytes");
220
                System.out.println("Primary Flash size: " + ztex.toHumanStr(ztex.flashSize())+" Bytes");
221
            }
222
 
223
            if ( ztex.InterfaceCapabilities(CAPABILITY_FLASH2) ) {
224
                System.out.println("Secondary Flash enabled: " + ztex.flash2Enabled());
225
                System.out.println("Secondary Flash sector size: " + ztex.toHumanStr(ztex.flash2SectorSize())+" Bytes");
226
                System.out.println("Secondary Flash size: " + ztex.toHumanStr(ztex.flash2Size())+" Bytes");
227
                if ( !flash1 ) flash2 = true;
228
            }
229
            else if ( !flash2 ) flash1 = true;
230
 
231
// primary flash test
232
            if ( flash1 && ztex.InterfaceCapabilities(CAPABILITY_FLASH) && ztex.flashEnabled() ) {
233
                System.out.println("Testing Primary Flash ...");
234
                int s = sectors > 1 ? sectors : ztex.flashSectorSize() > 1024 ? 16 : 8192;
235
                if (s > ztex.flashSectors() ) s = ztex.flashSectors();
236
                byte[] backup = writeTests ? new byte[s*ztex.flashSectorSize()] : null;
237
                System.out.println("Read Performance: " + ztex.testR(false, s, false, backup) + " kb/s     ");
238
                if ( writeTests ) {
239
                    System.out.println("Read + Write Performance: " + ztex.testRW(false, s) + " kb/s      ");
240
                    System.out.println("Write Performance: " + ztex.testW(false, s, backup) + " kb/s      ");
241
                    System.out.println("Read Performance: " + ztex.testR(false, s, true, backup) + " kb/s     ");
242
                }
243
            }
244
 
245
// secondary flash test
246
            if ( flash2 && ztex.InterfaceCapabilities(CAPABILITY_FLASH2) && ztex.flash2Enabled() ) {
247
                System.out.println("Testing Secondary Flash ...");
248
                int s = sectors > 1 ? sectors : ztex.flash2SectorSize() > 1024 ? 16 : 8192;
249
                if (s > ztex.flash2Sectors() ) s = ztex.flash2Sectors();
250
                byte[] backup = writeTests ? new byte[s*ztex.flash2SectorSize()] : null;
251
                System.out.println("Read Performance: " + ztex.testR(true, s, false, backup) + " kb/s     ");
252
                if ( writeTests ) {
253
                    System.out.println("Read + Write Performance: " + ztex.testRW(true, s) + " kb/s      ");
254
                    System.out.println("Write Performance: " + ztex.testW(true, s, backup) + " kb/s      ");
255
                    System.out.println("Read Performance: " + ztex.testR(true, s, true, backup) + " kb/s     ");
256
                }
257
            }
258
 
259
// release resources
260
            ztex.dispose();
261
 
262
        }
263
        catch (Exception e) {
264
            System.out.println("Error: "+e.getLocalizedMessage() );
265
        }
266
   }
267
 
268
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.