Disk IO working mechanism
1. Standard way
Physical Disk <--> kernel space <--> user address space
2. Direct I/O
Physical Disk <--> user address space
The database is so dry, from the same system software point of view, the database is on an equal footing with the operating system.
3. Memory Mapping
Physical Disk <--> kernel Space = = = User address space
2 and 3 did not really understand the difference, the internet did not find information.
Performance comparison: Code source thinking in Java
1 PackagePers.jing.io;2 3 ImportJava.io.*;4 ImportJava.nio.IntBuffer;5 ImportJava.nio.channels.FileChannel;6 7 Public classMappedio {8 Public Static Final intNumberofints = 4000000;9 Public Static Final intNumberofubuffints = 2000000;Ten One Private Abstract Static classTester { A PrivateString name; - - PublicTester (String name) { the This. Name =name; - } - - Public voidruntest () { +System.out.print (name + ":")); - Try { + LongStart =system.nanotime (); A test (); at DoubleDuration = System.nanotime ()-start; -System.out.format ("%.2f\n", duration/1.0e9); -}Catch(IOException e) { - Throw NewRuntimeException (e); - } - } in - protected Abstract voidTest ()throwsIOException; to } + - Private StaticTester[] Testers = { the NewTester ("Stream Write") { * @Override $ protected voidTest ()throwsIOException {Panax NotoginsengDataOutputStream dos =NewDataOutputStream ( - NewBufferedoutputstream ( the NewFileOutputStream ("Temp.temp"))); + for(inti = 0; i < numberofints; i++) { A Dos.writeint (i); the } + dos.close (); - } $ }, $ NewTester ("Mapped Write") { - @Override - protected voidTest ()throwsIOException { theFileChannel FC =NewRandomaccessfile ("Temp.temp", "RW") - . Getchannel ();WuyiIntbuffer IB = Fc.map (FileChannel.MapMode.READ_WRITE, 0, Fc.size ()) the . Asintbuffer (); - for(inti = 0; i < numberofints; i++) { Wu ib.put (i); - } About ib.clear (); $ } - }, - NewTester ("Stream Read") { - @Override A protected voidTest ()throwsIOException { +DataInputStream dis =NewDataInputStream ( the NewBufferedinputstream ( - NewFileInputStream ("Temp.temp"))); $ for(inti = 0; i < numberofints; i++) { the dis.readint (); the } the dis.close (); the } - }, in NewTester ("Mapped Read") { the @Override the protected voidTest ()throwsIOException { AboutFileChannel FC =NewRandomaccessfile ("Temp.temp", "RW") the . Getchannel (); theIntbuffer IB = Fc.map (FileChannel.MapMode.READ_WRITE, 0, Fc.size ()) the . Asintbuffer (); + while(Ib.hasremaining ()) { - ib.get (); the }Bayi ib.clear (); the } the }, - NewTester ("Stream rw") { - @Override the protected voidTest ()throwsIOException { theRandomaccessfile RAF =NewRandomaccessfile ("Temp.temp", "RW"); theRaf.writeint (1); the for(inti = 0; i < numberofubuffints; i++) { -Raf.seek (Raf.length ()-4); the Raf.writeint (Raf.readint ()); the } the raf.close ();94 } the }, the NewTester ("Mapped rw") { the @Override98 protected voidTest ()throwsIOException { AboutFileChannel FC =NewRandomaccessfile ("Temp.temp", "RW") - . Getchannel ();101Intbuffer IB = Fc.map (FileChannel.MapMode.READ_WRITE, 0, Fc.size ())102 . Asintbuffer ();103Ib.put (0);104 for(inti = 1; i < numberofubuffints; i++) { theIb.put (Ib.get (i-1));106 }107 fc.close ();108 }109 } the };111 the Public Static voidMain (string[] args) {113 for(Tester tester:testers) { the tester.runtest (); the } the }117}
Java IO (2)