Skip to content

Commit a300f00

Browse files
author
Datadog Syncup Service
committed
Merge branch 'upstream-master'
2 parents fd8adb2 + 6b4fe75 commit a300f00

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8314120
27+
* @summary Sanity test for FileDescriptor.sync
28+
* @library /test/lib
29+
* @run main Sync
30+
*/
31+
32+
import java.io.File;
33+
import java.io.FileDescriptor;
34+
import java.io.FileOutputStream;
35+
import java.io.IOException;
36+
import java.io.SyncFailedException;
37+
38+
public class Sync {
39+
40+
static final String TEST_DIR = System.getProperty("test.dir", ".");
41+
static final int TRIES = 10_000;
42+
43+
public static void testWith(File file) throws Exception {
44+
try (FileOutputStream fos = new FileOutputStream(file)) {
45+
FileDescriptor fd = fos.getFD();
46+
for (int t = 0; t < TRIES; t++) {
47+
fd.sync();
48+
}
49+
} catch (SyncFailedException sfe) {
50+
// Can happen on some filesystems, print it in the log
51+
System.out.println("Sync failed (acceptable)");
52+
sfe.printStackTrace();
53+
}
54+
}
55+
56+
public static void main(String[] args) throws Exception {
57+
// Run on platform threads
58+
System.out.println("With platform threads");
59+
run();
60+
61+
System.out.println("Complete");
62+
}
63+
64+
private static class AutoDelete implements AutoCloseable {
65+
private final File file;
66+
67+
public AutoDelete(File file) {
68+
this.file = file;
69+
}
70+
71+
public File file() {
72+
return file;
73+
}
74+
75+
@Override
76+
public void close() throws Exception {
77+
file.delete();
78+
}
79+
}
80+
81+
public static void run() throws Exception {
82+
try (var w = new AutoDelete(new File(TEST_DIR, "FileDescriptorSync1"))) {
83+
testWith(w.file());
84+
}
85+
86+
try (var w = new AutoDelete(File.createTempFile("FileDescriptorSync2", "tmp"))) {
87+
testWith(w.file());
88+
}
89+
}
90+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.io;
24+
25+
import org.openjdk.jmh.annotations.*;
26+
27+
import java.io.File;
28+
import java.io.FileDescriptor;
29+
import java.io.FileOutputStream;
30+
import java.io.IOException;
31+
import java.io.SyncFailedException;
32+
import java.util.concurrent.TimeUnit;
33+
34+
/**
35+
* Tests the cost of FileDescriptor.sync
36+
*/
37+
@BenchmarkMode(Mode.AverageTime)
38+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
39+
@State(Scope.Thread)
40+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
41+
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
42+
@Fork(3)
43+
public class FileDescriptorSync {
44+
45+
private FileOutputStream fos;
46+
private FileDescriptor fd;
47+
48+
@Setup
49+
public void setup() throws IOException {
50+
File tmp = File.createTempFile("FileDescriptorSync", "bin");
51+
fos = new FileOutputStream(tmp);
52+
fd = fos.getFD();
53+
}
54+
55+
@TearDown
56+
public void tearDown() throws IOException {
57+
fos.close();
58+
}
59+
60+
@Benchmark
61+
public void sync() {
62+
try {
63+
fd.sync();
64+
} catch (SyncFailedException e) {
65+
// The test assumes the temp filesystem accepts syncs.
66+
// Avoid failing if it does not, measure the exceptional path then.
67+
}
68+
}
69+
70+
}

0 commit comments

Comments
 (0)