<commit_before>//
// Sysinfo
//
// Copyright (c) 2015 <NAME>
//

use sys::component::{self, Component};
use sys::disk;
use sys::network;
use sys::process::*;
use sys::processor::*;
use sys::Disk;
use sys::NetworkData;
use Pid;
use {DiskExt, ProcessExt, RefreshKind, SystemExt};

use libc::{self, gid_t, sysconf, uid_t, _SC_CLK_TCK, _SC_PAGESIZE};
use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::error::Error;
use std::fs::{self, read_link, File};
use std::io::{self, BufRead, BufReader, Read};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use utils::realpath;

use rayon::prelude::*;

// This whole thing is to prevent having too much files open at once. It could be problematic
// for processes using a lot of files and using sysinfo at the same time.
pub(crate) static mut REMAINING_FILES: once_cell::sync::Lazy<Arc<Mutex<usize>>> =
    once_cell::sync::Lazy::new(|| unsafe {
        let mut limits = libc::rlimit {
            rlim_cur: 0,
            rlim_max: 0,
        };
        if libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) != 0 {
            // Most linux system now defaults to 1024.
            return Arc::new(Mutex::new(1024 - 1024 / 10));
        }
        // We save the value in case the update fails.
        let current = limits.rlim_cur;

        // The set the soft limit to the hard one.
        limits.rlim_cur = limits.rlim_max;
        // In this part, we leave minimum 10% of the available file descriptors to the process
        // using sysinfo.
        Arc::new(Mutex::new(if libc::setrlimit(libc::RLIMIT_NOFILE, &limits) == 0 {
            limits.rlim_cur - limits.rlim_cur / 10
        } else {
            current - current / 10
        } as _))
    });

macro_rules! to_str {
    ($e:expr) => {
        unsafe { ::std::str::from_utf8_unchecked($e) }
    };
}

/// Structs containing system's information.
#[derive(Debug)]
pub struct System {
    process_list: Process,
    mem_total: u64,
    mem_free: u64,
    swap_total: u64,
    swap_free: u64,
    processors: Vec<Processor>,<commit_msg>Handle Android separately for REMANING_FILES initialization<commit_after>//
// Sysinfo
//
// Copyright (c) 2015 <NAME>
//

use sys::component::{self, Component};
use sys::disk;
use sys::network;
use sys::process::*;
use sys::processor::*;
use sys::Disk;
use sys::NetworkData;
use Pid;
use {DiskExt, ProcessExt, RefreshKind, SystemExt};

use libc::{self, gid_t, sysconf, uid_t, _SC_CLK_TCK, _SC_PAGESIZE};
use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::error::Error;
use std::fs::{self, read_link, File};
use std::io::{self, BufRead, BufReader, Read};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

use utils::realpath;

use rayon::prelude::*;

// This whole thing is to prevent having too much files open at once. It could be problematic
// for processes using a lot of files and using sysinfo at the same time.
pub(crate) static mut REMAINING_FILES: once_cell::sync::Lazy<Arc<Mutex<usize>>> =
    once_cell::sync::Lazy::new(|| {
        #[cfg(target_os = "android")]
        {
            // The constant "RLIMIT_NOFILE" doesn't exist on Android so we have to return a value.
            // The default value seems to be 1024 so let's return 90% of it...
            Arc::new(Mutex::new(1024 - 1024 / 10))
        }
        #[cfg(not(target_os = "android"))]
        unsafe {
            let mut limits = libc::rlimit {
                rlim_cur: 0,
                rlim_max: 0,
            };
            if libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) != 0 {
                // Most linux system now defaults to 1024.
                return Arc::new(Mutex::new(1024 - 1024 / 10));
            }
            // We save the value in case the update fails.
            let current = limits.rlim_cur;

            // The set the soft limit to the hard one.
            limits.rlim_cur = limits.rlim_max;
            // In this part, we leave minimum 10% of the available file descriptors to the process
            // using sysinfo.
            Arc::new(Mutex::new(if libc::setrlimit(libc::RLIMIT_NOFILE, &limits) == 0 {
                limits.rlim_cur - limits.rlim_cur / 10
            } else {
                current - current / 10
            } as _))
        }
    });

macro_rules! to_str {
    ($e:expr) => {
        unsafe { ::std::str::from_utf8_unchecked($e) }
    };
}

/// Structs containing system's information.
#[derive(Debug)]
pub struct System {
    process_list: Process,
    mem_total: u64,
    mem_free: u64,
    swap_total: u64,
    swap_free: u64,
    processors: Vec<Processor>,
{'fraction_non_alphanumeric': 0.13700084961767206, 'fraction_numerical': 0.019116397621070518, 'mean_word_length': 2.725474683544304, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3175956', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1520, 'n_words': 444}