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 import java.io.File;
24 import jdk.test.lib.ProcessTools;
25 import jdk.test.lib.OutputAnalyzer;
26 import java.util.ArrayList;
27
28 /*
29 * @test
30 * @bug 6515172
31 * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux
32 * @requires os.family == "linux"
33 * @library /testlibrary
34 * @build jdk.test.lib.*
35 * @run driver AvailableProcessors
36 */
37 public class AvailableProcessors {
38
39 static final String SUCCESS_STRING = "Found expected processors: ";
40
41 public static void main(String[] args) throws Exception {
42 if (args.length > 0)
43 checkProcessors(Integer.parseInt(args[0]));
44 else {
45 // run ourselves under different cpu configurations
46 // using the taskset command
47 String taskset;
48 final String taskset1 = "/bin/taskset";
49 final String taskset2 = "/usr/bin/taskset";
50 if (new File(taskset1).exists())
76 System.out.println("Testing for " + i + " processors ...");
77 int max = i - 1;
78 ArrayList<String> cmdline = new ArrayList<>(master.command());
79 // prepend taskset command
80 cmdline.add(0, "0-" + max);
81 cmdline.add(0, "-c");
82 cmdline.add(0, taskset);
83 // append expected processor count
84 cmdline.add(String.valueOf(i));
85 ProcessBuilder pb = new ProcessBuilder(cmdline);
86 System.out.println("Final command line: " +
87 ProcessTools.getCommandLine(pb));
88 OutputAnalyzer output = ProcessTools.executeProcess(pb);
89 output.shouldContain(SUCCESS_STRING);
90 }
91 }
92 }
93
94 static void checkProcessors(int expected) {
95 int available = Runtime.getRuntime().availableProcessors();
96 if (available != expected)
97 throw new Error("Expected " + expected + " processors, but found "
98 + available);
99 else
100 System.out.println(SUCCESS_STRING + available);
101 }
102 }
|
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 import java.io.File;
24 import jdk.test.lib.ProcessTools;
25 import jdk.test.lib.OutputAnalyzer;
26 import java.util.ArrayList;
27
28 /*
29 * @test
30 * @bug 6515172 8148766
31 * @summary Check that availableProcessors reports the correct value when running in a cpuset on linux
32 * @requires os.family == "linux"
33 * @library /testlibrary
34 * @build jdk.test.lib.*
35 * @run driver AvailableProcessors
36 */
37 public class AvailableProcessors {
38
39 static final String SUCCESS_STRING = "Found expected processors: ";
40
41 public static void main(String[] args) throws Exception {
42 if (args.length > 0)
43 checkProcessors(Integer.parseInt(args[0]));
44 else {
45 // run ourselves under different cpu configurations
46 // using the taskset command
47 String taskset;
48 final String taskset1 = "/bin/taskset";
49 final String taskset2 = "/usr/bin/taskset";
50 if (new File(taskset1).exists())
76 System.out.println("Testing for " + i + " processors ...");
77 int max = i - 1;
78 ArrayList<String> cmdline = new ArrayList<>(master.command());
79 // prepend taskset command
80 cmdline.add(0, "0-" + max);
81 cmdline.add(0, "-c");
82 cmdline.add(0, taskset);
83 // append expected processor count
84 cmdline.add(String.valueOf(i));
85 ProcessBuilder pb = new ProcessBuilder(cmdline);
86 System.out.println("Final command line: " +
87 ProcessTools.getCommandLine(pb));
88 OutputAnalyzer output = ProcessTools.executeProcess(pb);
89 output.shouldContain(SUCCESS_STRING);
90 }
91 }
92 }
93
94 static void checkProcessors(int expected) {
95 int available = Runtime.getRuntime().availableProcessors();
96 // available can dynamically drop below expected due to aggressive power management
97 // but we should never have more than expected, else taskset is broken
98 if (available > expected)
99 throw new Error("Expected " + expected + " processors, but found "
100 + available);
101 else
102 System.out.println(SUCCESS_STRING + available);
103 }
104 }
|