1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info:  http://www.jrobin.org
6  * Project Lead:  Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003-2005, by Sasa Markovic.
9  *
10  * Developers:    Sasa Markovic (saxon@jrobin.org)
11  *
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package net.bull.javamelody.internal.model;
27
28 import java.io.IOException;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Method;
31 import java.nio.ByteBuffer;
32 import java.nio.MappedByteBuffer;
33 import java.nio.channels.FileChannel;
34 import java.util.Timer;
35 import java.util.TimerTask;
36
37 import org.jrobin.core.RrdFileBackend;
38
39 import sun.nio.ch.DirectBuffer; // NOPMD
40
41 /**
42  * JRobin backend which is used to store RRD data to ordinary disk files
43  * by using fast java.nio.* package. This is the default backend engine since JRobin 1.4.0.
44  */

45 public class RrdNioBackend extends RrdFileBackend {
46     private static final Object THE_UNSAFE = getTheUnsafe();
47     private static final Method JAVA9_INVOKE_CLEANER = getJava9InvokeCleaner();
48     private static Timer fileSyncTimer;
49
50     private MappedByteBuffer byteBuffer;
51     private final TimerTask syncTask = new TimerTask() {
52         @Override
53         public void run() {
54             sync();
55         }
56     };
57
58     /**
59      * Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.
60      *
61      * @param path       Path to a file
62      * @param readOnly   True, if file should be open in a read-only mode. False otherwise
63      * @param syncPeriod See {@link RrdNioBackendFactory#setSyncPeriod(int)} for explanation
64      * @throws IOException Thrown in case of I/O error
65      */

66     protected RrdNioBackend(String path, boolean readOnly, int syncPeriod) throws IOException {
67         super(path, readOnly);
68         try {
69             mapFile();
70             if (!readOnly) {
71                 fileSyncTimer.schedule(syncTask, syncPeriod * 1000L, syncPeriod * 1000L);
72             }
73         } catch (final IOException ioe) {
74             super.close(); // NOPMD
75             throw ioe;
76         } catch (final IllegalStateException e) {
77             // issue #592 (IllegalStateException: Timer already cancelled)
78             unmapFile();
79             super.close(); // NOPMD
80             throw e;
81         }
82     }
83
84     /**
85      * @return The timer to synchronize files.
86      */

87     public static Timer getFileSyncTimer() {
88         return fileSyncTimer;
89     }
90
91     /**
92      * Sets the timer.
93      * @param timer timer to synchronize files.
94      */

95     public static void setFileSyncTimer(Timer timer) {
96         fileSyncTimer = timer;
97     }
98
99     private void mapFile() throws IOException {
100         final long length = getLength();
101         if (length > 0) {
102             final FileChannel.MapMode mapMode =
103                     // (issue 328) readOnly ? FileChannel.MapMode.READ_ONLY :
104                     FileChannel.MapMode.READ_WRITE;
105             byteBuffer = file.getChannel().map(mapMode, 0, length);
106         }
107     }
108
109     private void unmapFile() {
110         if (byteBuffer != null) {
111             if (JAVA9_INVOKE_CLEANER == null || THE_UNSAFE == null) {
112                 if (byteBuffer instanceof DirectBuffer) {
113                     // for Java 8 and before
114                     ((DirectBuffer) byteBuffer).cleaner().clean();
115                 }
116             } else {
117                 // for Java 9 and later:
118                 // sun.nio.ch.DirectBuffer methods are not accessible,
119                 // so the new sun.misc.Unsafe.theUnsafe.invokeCleaner(ByteBuffer) is used.
120                 // See https://bugs.openjdk.java.net/browse/JDK-8171377
121                 try {
122                     JAVA9_INVOKE_CLEANER.invoke(THE_UNSAFE, byteBuffer);
123                 } catch (final Exception e) {
124                     throw new IllegalStateException(e);
125                 }
126             }
127             byteBuffer = null;
128         }
129     }
130
131     private static Object getTheUnsafe() {
132         try {
133             final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
134             final Field theUnsafeField = unsafeClass.getDeclaredField("theUnsafe");
135             theUnsafeField.setAccessible(true);
136             return theUnsafeField.get(null);
137         } catch (final Exception e) {
138             return null;
139         }
140     }
141
142     private static Method getJava9InvokeCleaner() {
143         try {
144             final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
145             return unsafeClass.getMethod("invokeCleaner", ByteBuffer.class);
146         } catch (final Exception e) {
147             return null;
148         }
149     }
150
151     /**
152      * Sets length of the underlying RRD file. This method is called only once, immediately
153      * after a new RRD file gets created.
154      *
155      * @param newLength Length of the RRD file
156      * @throws IOException Thrown in case of I/O error.
157      */

158     @Override
159     protected synchronized void setLength(long newLength) throws IOException {
160         unmapFile();
161         super.setLength(newLength);
162         mapFile();
163     }
164
165     /**
166      * Writes bytes to the underlying RRD file on the disk
167      *
168      * @param offset Starting file offset
169      * @param b      Bytes to be written.
170      */

171     @Override
172     protected synchronized void write(long offset, byte[] b) throws IOException {
173         if (byteBuffer != null) {
174             byteBuffer.position((int) offset);
175             byteBuffer.put(b);
176         } else {
177             throw new IOException("Write failed, file " + getPath() + " not mapped for I/O");
178         }
179     }
180
181     /**
182      * Reads a number of bytes from the RRD file on the disk
183      *
184      * @param offset Starting file offset
185      * @param b      Buffer which receives bytes read from the file.
186      */

187     @Override
188     protected synchronized void read(long offset, byte[] b) throws IOException {
189         if (byteBuffer != null) {
190             byteBuffer.position((int) offset);
191             byteBuffer.get(b);
192         } else {
193             throw new IOException("Read failed, file " + getPath() + " not mapped for I/O");
194         }
195     }
196
197     /**
198      * Closes the underlying RRD file.
199      *
200      * @throws IOException Thrown in case of I/O error
201      */

202     @Override
203     public synchronized void close() throws IOException {
204         // cancel synchronization
205         try {
206             if (syncTask != null) {
207                 syncTask.cancel();
208             }
209             sync();
210             unmapFile();
211         } finally {
212             super.close();
213         }
214     }
215
216     /**
217      * This method forces all data cached in memory but not yet stored in the file,
218      * to be stored in it.
219      */

220     protected synchronized void sync() {
221         if (byteBuffer != null) {
222             byteBuffer.force();
223         }
224     }
225 }
226