< prev index next >

test/jdk/com/sun/management/OperatingSystemMXBean/TestTotalSwap.java

Print this page
rev 51542 : 8210039: move OSInfo to top level testlibrary
Reviewed-by: duke


  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     4858522
  27  * @summary Basic unit test of OperatingSystemMXBean.getTotalSwapSpaceSize()
  28  * @author  Steve Bohne
  29  * @author  Jaroslav Bachorik
  30  *
  31  * @library /lib/testlibrary

  32  *
  33  * @build TestTotalSwap jdk.testlibrary.*
  34  * @run main TestTotalSwap
  35  */
  36 
  37 /*
  38  * This test tests the actual swap size on linux and solaris.
  39  * The correct value should be checked manually:
  40  * Solaris:
  41  *   1. In a shell, enter the command: "swap -l"
  42  *   2. The value (reported in blocks) is in the "blocks" column.
  43  * Linux:
  44  *   1. In a shell, enter the command: "cat /proc/meminfo"
  45  *   2. The value (reported in bytes) is in "Swap" entry, "total" column.
  46  * Windows NT/XP/2000:
  47  *   1. Run Start->Accessories->System Tools->System Information.
  48  *   2. The value (reported in Kbytes) is in the "Page File Space" entry
  49  * Windows 98/ME:
  50  *   Unknown.
  51  *
  52  * Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace]
  53  */
  54 
  55 import com.sun.management.OperatingSystemMXBean;
  56 import java.lang.management.*;
  57 
  58 import jdk.testlibrary.OSInfo;
  59 import jdk.testlibrary.ProcessTools;
  60 import jdk.testlibrary.OutputAnalyzer;
  61 
  62 public class TestTotalSwap {
  63 
  64     private static final OperatingSystemMXBean mbean =
  65         (com.sun.management.OperatingSystemMXBean)
  66         ManagementFactory.getOperatingSystemMXBean();
  67 
  68     // Careful with these values.
  69     // Min size for pass dynamically determined below.
  70     // zero if no swap space is configured.
  71     private static long       min_size_for_pass = 0;
  72     private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;
  73 
  74     public static void main(String args[]) throws Throwable {
  75         // yocto might ignore the request to report swap size in bytes
  76         boolean swapInKB = mbean.getVersion().contains("yocto");
  77 
  78         long expected_swap_size = getSwapSizeFromOs();


  93                     throw new RuntimeException("Expected total swap size      : " +
  94                                                expected_swap_size +
  95                                                " but getTotalSwapSpaceSize returned: " +
  96                                                size);
  97                 }
  98             }
  99         }
 100 
 101         // sanity check
 102         if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {
 103             throw new RuntimeException("Total swap space size " +
 104                                        "illegal value: " + size + " bytes " +
 105                                        "(MIN = " + min_size_for_pass + "; " +
 106                                        "MAX = " + MAX_SIZE_FOR_PASS + ")");
 107         }
 108 
 109         System.out.println("Test passed.");
 110     }
 111 
 112     private static long getSwapSizeFromOs() throws Throwable {
 113         OSInfo.OSType os = OSInfo.getOSType();
 114 
 115         switch (os) {
 116             // total       used       free     shared    buffers     cached
 117             // Mem:    16533540864 13638467584 2895073280  534040576 1630248960 6236909568
 118             // -/+ buffers/cache: 5771309056 10762231808
 119             // Swap:   15999168512          0 15999168512
 120 
 121             case LINUX: {
 122                 String swapSizeStr = ProcessTools.executeCommand("free", "-b")
 123                                         .firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);
 124                 return Long.parseLong(swapSizeStr);
 125             }
 126             case SOLARIS: {
 127                 // swapfile             dev   swaplo blocks   free
 128                 // /dev/dsk/c0t0d0s1   136,1      16 1638608 1600528
 129                 OutputAnalyzer out= ProcessTools.executeCommand(
 130                     "/usr/sbin/swap",
 131                     "-l"
 132                 );
 133 
 134                 long swapSize = 0;
 135 
 136                 for (String line : out.asLines()) {
 137                     if (line.contains("swapfile")) continue;
 138 
 139                     String[] vals = line.split("\\s+");
 140                     if (vals.length == 5) {
 141                         swapSize += Long.parseLong(vals[3]) * 512; // size is reported in 512b blocks
 142                     }
 143                 }
 144 
 145                 return swapSize;
 146             }
 147             case MACOSX: {
 148                 // total = 8192.00M used = 7471.11M free = 720.89M (encrypted)
 149                 String swapSizeStr = ProcessTools.executeCommand(
 150                     "/usr/sbin/sysctl",
 151                     "-n",
 152                     "vm.swapusage"
 153                 ).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);
 154                 if (swapSizeStr.toLowerCase().endsWith("m")) {
 155                     swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);
 156                     return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB
 157                 }
 158                 return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);
 159             }
 160             default: {
 161                 System.err.println("Unsupported operating system: " + os);
 162             }
 163         }
 164 
 165         return -1;
 166     }
 167 }


  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     4858522
  27  * @summary Basic unit test of OperatingSystemMXBean.getTotalSwapSpaceSize()
  28  * @author  Steve Bohne
  29  * @author  Jaroslav Bachorik
  30  *
  31  * @library /lib/testlibrary
  32  * @library /test/lib
  33  *
  34  * @build TestTotalSwap jdk.testlibrary.*
  35  * @run main TestTotalSwap
  36  */
  37 
  38 /*
  39  * This test tests the actual swap size on linux and solaris.
  40  * The correct value should be checked manually:
  41  * Solaris:
  42  *   1. In a shell, enter the command: "swap -l"
  43  *   2. The value (reported in blocks) is in the "blocks" column.
  44  * Linux:
  45  *   1. In a shell, enter the command: "cat /proc/meminfo"
  46  *   2. The value (reported in bytes) is in "Swap" entry, "total" column.
  47  * Windows NT/XP/2000:
  48  *   1. Run Start->Accessories->System Tools->System Information.
  49  *   2. The value (reported in Kbytes) is in the "Page File Space" entry
  50  * Windows 98/ME:
  51  *   Unknown.
  52  *
  53  * Usage: GetTotalSwapSpaceSize <expected swap size | "sanity-only"> [trace]
  54  */
  55 
  56 import com.sun.management.OperatingSystemMXBean;
  57 import java.lang.management.*;
  58 
  59 import jdk.test.lib.Platform;
  60 import jdk.testlibrary.ProcessTools;
  61 import jdk.testlibrary.OutputAnalyzer;
  62 
  63 public class TestTotalSwap {
  64 
  65     private static final OperatingSystemMXBean mbean =
  66         (com.sun.management.OperatingSystemMXBean)
  67         ManagementFactory.getOperatingSystemMXBean();
  68 
  69     // Careful with these values.
  70     // Min size for pass dynamically determined below.
  71     // zero if no swap space is configured.
  72     private static long       min_size_for_pass = 0;
  73     private static final long MAX_SIZE_FOR_PASS = Long.MAX_VALUE;
  74 
  75     public static void main(String args[]) throws Throwable {
  76         // yocto might ignore the request to report swap size in bytes
  77         boolean swapInKB = mbean.getVersion().contains("yocto");
  78 
  79         long expected_swap_size = getSwapSizeFromOs();


  94                     throw new RuntimeException("Expected total swap size      : " +
  95                                                expected_swap_size +
  96                                                " but getTotalSwapSpaceSize returned: " +
  97                                                size);
  98                 }
  99             }
 100         }
 101 
 102         // sanity check
 103         if (size < min_size_for_pass || size > MAX_SIZE_FOR_PASS) {
 104             throw new RuntimeException("Total swap space size " +
 105                                        "illegal value: " + size + " bytes " +
 106                                        "(MIN = " + min_size_for_pass + "; " +
 107                                        "MAX = " + MAX_SIZE_FOR_PASS + ")");
 108         }
 109 
 110         System.out.println("Test passed.");
 111     }
 112 
 113     private static long getSwapSizeFromOs() throws Throwable {
 114         if (Platform.isLinux()) {


 115             // total       used       free     shared    buffers     cached
 116             // Mem:    16533540864 13638467584 2895073280  534040576 1630248960 6236909568
 117             // -/+ buffers/cache: 5771309056 10762231808
 118             // Swap:   15999168512          0 15999168512


 119             String swapSizeStr = ProcessTools.executeCommand("free", "-b")
 120                                              .firstMatch("Swap:\\s+([0-9]+)\\s+.*", 1);
 121             return Long.parseLong(swapSizeStr);
 122         } else if (Platform.isSolaris()) {

 123             // swapfile             dev   swaplo blocks   free
 124             // /dev/dsk/c0t0d0s1   136,1      16 1638608 1600528
 125             OutputAnalyzer out= ProcessTools.executeCommand(
 126                     "/usr/sbin/swap",
 127                     "-l"
 128             );
 129 
 130             long swapSize = 0;
 131 
 132             for (String line : out.asLines()) {
 133                 if (line.contains("swapfile")) continue;
 134 
 135                 String[] vals = line.split("\\s+");
 136                 if (vals.length == 5) {
 137                     swapSize += Long.parseLong(vals[3]) * 512; // size is reported in 512b blocks
 138                 }
 139             }
 140 
 141             return swapSize;
 142         } else if (Platform.isOSX()) {

 143             // total = 8192.00M used = 7471.11M free = 720.89M (encrypted)
 144             String swapSizeStr = ProcessTools.executeCommand(
 145                     "/usr/sbin/sysctl",
 146                     "-n",
 147                     "vm.swapusage"
 148             ).firstMatch("total\\s+=\\s+([0-9]+(\\.[0-9]+)?[Mm]?).*", 1);
 149             if (swapSizeStr.toLowerCase().endsWith("m")) {
 150                 swapSizeStr = swapSizeStr.substring(0, swapSizeStr.length() - 1);
 151                 return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024); // size in MB
 152             }
 153             return (long)(Double.parseDouble(swapSizeStr) * 1024 * 1024);
 154         } else {
 155             System.err.println("Unsupported operating system: " + Platform.getOsName());


 156         }
 157 
 158         return -1;
 159     }
 160 }
< prev index next >