Example usage for org.apache.lucene.util Constants LINUX

List of usage examples for org.apache.lucene.util Constants LINUX

Introduction

In this page you can find the example usage for org.apache.lucene.util Constants LINUX.

Prototype

boolean LINUX

To view the source code for org.apache.lucene.util Constants LINUX.

Click Source Link

Document

True iff running on Linux.

Usage

From source file:com.b2international.index.lucene.Directories.java

License:Apache License

/**
 * Just like {@link #openFile(File)}, but allows you to also specify a custom {@link LockFactory}.
 *//*from w  ww  . j a va  2  s .  c  om*/
public static FSDirectory openFile(final Path path, final LockFactory lockFactory) throws IOException {
    if ((Constants.WINDOWS || Constants.SUN_OS || Constants.LINUX || Constants.MAC_OS_X)
            && Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {

        return new MMapDirectory(path, lockFactory);
    } else if (Constants.WINDOWS) {
        return new SimpleFSDirectory(path, lockFactory);
    } else {
        return new NIOFSDirectory(path, lockFactory);
    }
}

From source file:io.crate.monitor.SysInfo.java

License:Apache License

/**
 * Retrieve system uptime in milliseconds
 * https://en.wikipedia.org/wiki/Uptime// www  .ja v a 2  s. c  om
 */
static long getSystemUptime() {
    long uptime = -1L;
    if (Constants.WINDOWS) {
        List<String> lines = SysInfo.sysCall(new String[] { "net", "stats", "srv" }, "");
        for (String line : lines) {
            if (line.startsWith("Statistics since")) {
                SimpleDateFormat format = new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm:ss a",
                        Locale.ROOT);
                try {
                    Date bootTime = format.parse(line);
                    return System.currentTimeMillis() - bootTime.getTime();
                } catch (ParseException e) {
                    LOGGER.debug("Failed to parse uptime: {}", e.getMessage());
                }
            }
        }
    } else if (Constants.LINUX) {
        File procUptime = new File("/proc/uptime");
        if (procUptime.exists()) {
            try {
                List<String> lines = Files.readAllLines(procUptime.toPath());
                if (!lines.isEmpty()) {
                    String[] parts = lines.get(0).split(" ");
                    if (parts.length == 2) {
                        Double uptimeMillis = Float.parseFloat(parts[1]) * 1000.0;
                        return uptimeMillis.longValue();
                    }
                }
            } catch (IOException e) {
                LOGGER.debug("Failed to read '{}': {}", procUptime.getAbsolutePath(), e.getMessage());
            }
        }
    } else if (Constants.MAC_OS_X) {
        Pattern pattern = Pattern.compile("kern.boottime: \\{ sec = (\\d+), usec = (\\d+) \\} .*");
        List<String> lines = SysInfo.sysCall(new String[] { "sysctl", "kern.boottime" }, "");
        for (String line : lines) {
            Matcher matcher = pattern.matcher(line);
            if (matcher.matches()) {
                return Long.parseLong(matcher.group(1)) * 1000L;
            }
        }
    }
    return uptime;
}

From source file:org.apache.cassandra.utils.CLibrary.java

License:Apache License

public static String rlimitToString(long value) {
    assert Constants.LINUX || Constants.MAC_OS_X;
    if (value == CLibrary.RLIM_INFINITY) {
        return "unlimited";
    } else {//from www .j a  va  2s . c  om
        // TODO, on java 8 use Long.toUnsignedString, since thats what it is.
        return Long.toUnsignedString(value);
    }
}

From source file:org.elasticsearch.bootstrap.BootstrapCheck.java

License:Apache License

static List<Check> checks(final Settings settings) {
    final List<Check> checks = new ArrayList<>();
    checks.add(new HeapSizeCheck());
    final FileDescriptorCheck fileDescriptorCheck = Constants.MAC_OS_X ? new OsXFileDescriptorCheck()
            : new FileDescriptorCheck();
    checks.add(fileDescriptorCheck);//from  www  .  j a v  a2 s  .  c  om
    checks.add(new MlockallCheck(BootstrapSettings.MEMORY_LOCK_SETTING.get(settings)));
    if (Constants.LINUX) {
        checks.add(new MaxNumberOfThreadsCheck());
    }
    if (Constants.LINUX || Constants.MAC_OS_X) {
        checks.add(new MaxSizeVirtualMemoryCheck());
    }
    if (Constants.LINUX) {
        checks.add(new MaxMapCountCheck());
    }
    checks.add(new ClientJvmCheck());
    checks.add(new OnErrorCheck());
    checks.add(new OnOutOfMemoryErrorCheck());
    return Collections.unmodifiableList(checks);
}

From source file:org.elasticsearch.bootstrap.BootstrapChecks.java

License:Apache License

static List<BootstrapCheck> checks(final Settings settings) {
    final List<BootstrapCheck> checks = new ArrayList<>();
    checks.add(new HeapSizeCheck());
    final FileDescriptorCheck fileDescriptorCheck = Constants.MAC_OS_X ? new OsXFileDescriptorCheck()
            : new FileDescriptorCheck();
    checks.add(fileDescriptorCheck);/* www  .  j a  va2  s  . c  om*/
    checks.add(new MlockallCheck(BootstrapSettings.MEMORY_LOCK_SETTING.get(settings)));
    if (Constants.LINUX) {
        checks.add(new MaxNumberOfThreadsCheck());
    }
    if (Constants.LINUX || Constants.MAC_OS_X) {
        checks.add(new MaxSizeVirtualMemoryCheck());
    }
    if (Constants.LINUX) {
        checks.add(new MaxMapCountCheck());
    }
    checks.add(new ClientJvmCheck());
    checks.add(new UseSerialGCCheck());
    checks.add(new SystemCallFilterCheck(BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(settings)));
    checks.add(new OnErrorCheck());
    checks.add(new OnOutOfMemoryErrorCheck());
    checks.add(new G1GCCheck());
    return Collections.unmodifiableList(checks);
}

From source file:org.elasticsearch.bootstrap.EvilJNANativesTests.java

License:Apache License

public void testSetMaximumNumberOfThreads() throws IOException {
    if (Constants.LINUX) {
        final List<String> lines = Files.readAllLines(PathUtils.get("/proc/self/limits"));
        if (!lines.isEmpty()) {
            for (String line : lines) {
                if (line != null && line.startsWith("Max processes")) {
                    final String[] fields = line.split("\\s+");
                    final long limit = "unlimited".equals(fields[2]) ? JNACLibrary.RLIM_INFINITY
                            : Long.parseLong(fields[2]);
                    assertThat(JNANatives.MAX_NUMBER_OF_THREADS, equalTo(limit));
                    return;
                }/*  w w w.  ja va2s.  c  o  m*/
            }
        }
        fail("should have read max processes from /proc/self/limits");
    } else {
        assertThat(JNANatives.MAX_NUMBER_OF_THREADS, equalTo(-1L));
    }
}

From source file:org.elasticsearch.bootstrap.EvilJNANativesTests.java

License:Apache License

public void testSetMaxSizeVirtualMemory() throws IOException {
    if (Constants.LINUX) {
        final List<String> lines = Files.readAllLines(PathUtils.get("/proc/self/limits"));
        if (!lines.isEmpty()) {
            for (String line : lines) {
                if (line != null && line.startsWith("Max address space")) {
                    final String[] fields = line.split("\\s+");
                    final String limit = fields[3];
                    assertEquals(JNANatives.rlimitToString(JNANatives.MAX_SIZE_VIRTUAL_MEMORY), limit);
                    return;
                }//from w  ww  . jav  a 2 s .  c o  m
            }
        }
        fail("should have read max size virtual memory from /proc/self/limits");
    } else if (Constants.MAC_OS_X) {
        assertThat(JNANatives.MAX_SIZE_VIRTUAL_MEMORY,
                anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L)));
    } else {
        assertThat(JNANatives.MAX_SIZE_VIRTUAL_MEMORY, equalTo(Long.MIN_VALUE));
    }
}

From source file:org.elasticsearch.bootstrap.JNANatives.java

License:Apache License

static void tryMlockall() {
    int errno = Integer.MIN_VALUE;
    String errMsg = null;//from   w w  w  .  j  a  v a  2s .c  om
    boolean rlimitSuccess = false;
    long softLimit = 0;
    long hardLimit = 0;

    try {
        int result = JNACLibrary.mlockall(JNACLibrary.MCL_CURRENT);
        if (result == 0) {
            LOCAL_MLOCKALL = true;
            return;
        }

        errno = Native.getLastError();
        errMsg = JNACLibrary.strerror(errno);
        if (Constants.LINUX || Constants.MAC_OS_X) {
            // we only know RLIMIT_MEMLOCK for these two at the moment. 
            JNACLibrary.Rlimit rlimit = new JNACLibrary.Rlimit();
            if (JNACLibrary.getrlimit(JNACLibrary.RLIMIT_MEMLOCK, rlimit) == 0) {
                rlimitSuccess = true;
                softLimit = rlimit.rlim_cur.longValue();
                hardLimit = rlimit.rlim_max.longValue();
            } else {
                logger.warn(
                        "Unable to retrieve resource limits: " + JNACLibrary.strerror(Native.getLastError()));
            }
        }
    } catch (UnsatisfiedLinkError e) {
        // this will have already been logged by CLibrary, no need to repeat it
        return;
    }

    // mlockall failed for some reason
    logger.warn("Unable to lock JVM Memory: error=" + errno + ",reason=" + errMsg);
    logger.warn("This can result in part of the JVM being swapped out.");
    if (errno == JNACLibrary.ENOMEM) {
        if (rlimitSuccess) {
            logger.warn("Increase RLIMIT_MEMLOCK, soft limit: " + rlimitToString(softLimit) + ", hard limit: "
                    + rlimitToString(hardLimit));
            if (Constants.LINUX) {
                // give specific instructions for the linux case to make it easy
                String user = System.getProperty("user.name");
                logger.warn("These can be adjusted by modifying /etc/security/limits.conf, for example: \n"
                        + "\t# allow user '" + user + "' mlockall\n" + "\t" + user + " soft memlock unlimited\n"
                        + "\t" + user + " hard memlock unlimited");
                logger.warn(
                        "If you are logged in interactively, you will have to re-login for the new limits to take effect.");
            }
        } else {
            logger.warn("Increase RLIMIT_MEMLOCK (ulimit).");
        }
    }
}

From source file:org.elasticsearch.bootstrap.JNANatives.java

License:Apache License

static String rlimitToString(long value) {
    assert Constants.LINUX || Constants.MAC_OS_X;
    if (value == JNACLibrary.RLIM_INFINITY) {
        return "unlimited";
    } else {/*from  w  w  w . ja va2  s. c om*/
        // TODO, on java 8 use Long.toUnsignedString, since thats what it is.
        return Long.toUnsignedString(value);
    }
}

From source file:org.elasticsearch.bootstrap.MaxMapCountCheckTests.java

License:Apache License

public void testGetMaxMapCountOnLinux() {
    if (Constants.LINUX) {
        final BootstrapCheck.MaxMapCountCheck check = new BootstrapCheck.MaxMapCountCheck();
        assertThat(check.getMaxMapCount(), greaterThan(0L));
    }//from  ww  w .  j  a va  2  s  .  c  o m
}