<commit_before>/*
 *
 *  *  Copyright 2010-2018 OrientDB LTD (http://orientdb.com)
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *       http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *  *
 *  * For more information: http://orientdb.com
 *
 */
package com.orientechnologies.common.jna;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.util.OMemory;
import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Platform;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ONative {
  private static volatile OCLibrary C_LIBRARY;
  private static final    String    DEFAULT_MEMORY_CGROUP_PATH = "/sys/fs/memory";

  private static volatile ONative instance = null;
  private static final    Lock    initLock = new ReentrantLock();

  public static ONative instance() {
    if (instance != null)
      return instance;

    initLock.lock();
    try {
      if (instance != null)
        return instance;

      if (Platform.isLinux()) {
        C_LIBRARY = Native.loadLibrary("c", OCLibrary.class);
      } else {
        C_LIBRARY = null;
      }

      instance = new ONative();
    } finally {
      initLock.unlock();
    }

    return instance;
  }

  /**
   * Prevent initialization outside singleton
   */
  private ONative() {
  }

  /**
   * Detects limit of limit of open files.
   *
   * @param recommended recommended value of limit of open files.
   * @param defLimit    default value for limit of open files.
   *
   * @return limit of open files, available for the system.
   */
  public int getOpenFilesLimit(boolean verbose, int recommended, int defLimit) {
    if (Platform.isLinux()) {
      final OCLibrary.Rlimit rlimit = new OCLibrary.Rlimit();
      final int result = C_LIBRARY.getrlimit(OCLibrary.RLIMIT_NOFILE, rlimit);

      if (result == 0 && rlimit.rlim_cur > 0) {
        if (verbose) {
          OLogManager.instance().infoNoDb(this, "Detected limit of amount of simultaneously open files is %d, "
              + " limit of open files for disk cache will be set to ", rlimit.rlim_cur, rlimit.rlim_cur / 2 - 512);
        }

        if (rlimit.rlim_cur < recommended) {
          OLogManager.instance()
              .warnNoDb(this, "Value of limit of simultaneously open files is too small, recommended value is %d",
                  recommended);
        }

        return (int) rlimit.rlim_cur / 2 - 512;
      } else {
        if (verbose) {
          OLogManager.instance().infoNoDb(this, "Can not detect value of limit of open files.");<commit_msg>File limit message is fixed.<commit_after>/*
 *
 *  *  Copyright 2010-2018 OrientDB LTD (http://orientdb.com)
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *       http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *  *
 *  * For more information: http://orientdb.com
 *
 */
package com.orientechnologies.common.jna;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.util.OMemory;
import com.sun.jna.LastErrorException;
import com.sun.jna.Native;
import com.sun.jna.Platform;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ONative {
  private static volatile OCLibrary C_LIBRARY;
  private static final    String    DEFAULT_MEMORY_CGROUP_PATH = "/sys/fs/memory";

  private static volatile ONative instance = null;
  private static final    Lock    initLock = new ReentrantLock();

  public static ONative instance() {
    if (instance != null)
      return instance;

    initLock.lock();
    try {
      if (instance != null)
        return instance;

      if (Platform.isLinux()) {
        C_LIBRARY = Native.loadLibrary("c", OCLibrary.class);
      } else {
        C_LIBRARY = null;
      }

      instance = new ONative();
    } finally {
      initLock.unlock();
    }

    return instance;
  }

  /**
   * Prevent initialization outside singleton
   */
  private ONative() {
  }

  /**
   * Detects limit of limit of open files.
   *
   * @param recommended recommended value of limit of open files.
   * @param defLimit    default value for limit of open files.
   *
   * @return limit of open files, available for the system.
   */
  public int getOpenFilesLimit(boolean verbose, int recommended, int defLimit) {
    if (Platform.isLinux()) {
      final OCLibrary.Rlimit rlimit = new OCLibrary.Rlimit();
      final int result = C_LIBRARY.getrlimit(OCLibrary.RLIMIT_NOFILE, rlimit);

      if (result == 0 && rlimit.rlim_cur > 0) {
        if (verbose) {
          OLogManager.instance().infoNoDb(this, "Detected limit of amount of simultaneously open files is %d, "
              + " limit of open files for disk cache will be set to %d", rlimit.rlim_cur, rlimit.rlim_cur / 2 - 512);
        }

        if (rlimit.rlim_cur < recommended) {
          OLogManager.instance()
              .warnNoDb(this, "Value of limit of simultaneously open files is too small, recommended value is %d",
                  recommended);
        }

        return (int) rlimit.rlim_cur / 2 - 512;
      } else {
        if (verbose) {
          OLogManager.instance().infoNoDb(this, "Can not detect value of limit of open files.");
{'fraction_non_alphanumeric': 0.09554052188319757, 'fraction_numerical': 0.006074830871185973, 'mean_word_length': 3.0650953984287317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2142495', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2072, 'n_words': 642}