<commit_before>package net.openhft.chronicle.core;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static net.openhft.chronicle.core.Jvm.rethrow;

/**
 * This class is responsible for OS performance tuning diagnostic.
 */
public final class PerformanceTuning {
    static final File TIMESTAMP = new File(OS.TMP, ".tuning." + OS.getUserName());

    private PerformanceTuning() {
    }

    /**
     * Report OS performance tuning issues.
     */
    public static void reportIssues() {

        List<String> issues = issues();
        if (!issues.isEmpty()) {
            StringBuilder msg = new StringBuilder();
            msg.append("OS configuration is non-optimal for high performance:").append("\n");
            issues.forEach(issue -> msg.append(issue).append("\n"));
            msg.append("If you would like assistance, please contact <EMAIL>.");
            Jvm.warn().on(PerformanceTuning.class, msg.toString());
        }
    }

    /**
     * @return list of issues regarding OS performance tuning.
     */
    @NotNull
    public static List<String> issues() {
        if (!OS.isLinux()) {
            return Collections.emptyList();
        }
        if (TIMESTAMP.lastModified() + TimeUnit.MINUTES.toMillis(30) > System.currentTimeMillis())
            return Collections.emptyList();
        try {
            try (FileOutputStream fos = new FileOutputStream(TIMESTAMP, true)) {
            }
        } catch (IOException ioe) {
            Jvm.debug().on(PerformanceTuning.class, ioe);
        }

        List<String> issues = new LinkedList<>();

        issues.add(checkScalingGovernors());

        Set<String> kernelCmdLineParams = kernelCommandLineParameters();
        if (!kernelCmdLineParams.isEmpty()) {
            issues.addAll(asList(
                    checkCStates(kernelCmdLineParams),
                    checkSpectreMitigation(kernelCmdLineParams),
                    checkSoftlockup(kernelCmdLineParams),
                    checkAudit(kernelCmdLineParams),
                    checkPageTableIsolation(kernelCmdLineParams),
                    checkMCE(kernelCmdLineParams)<commit_msg>Append doesn't modify the timestamp when nothing added<commit_after>package net.openhft.chronicle.core;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static net.openhft.chronicle.core.Jvm.rethrow;

/**
 * This class is responsible for OS performance tuning diagnostic.
 */
public final class PerformanceTuning {
    static final File TIMESTAMP = new File(OS.TMP, ".tuning." + OS.getUserName());

    private PerformanceTuning() {
    }

    /**
     * Report OS performance tuning issues.
     */
    public static void reportIssues() {

        List<String> issues = issues();
        if (!issues.isEmpty()) {
            StringBuilder msg = new StringBuilder();
            msg.append("OS configuration is non-optimal for high performance:").append("\n");
            issues.forEach(issue -> msg.append(issue).append("\n"));
            msg.append("If you would like assistance, please contact <EMAIL>.");
            Jvm.warn().on(PerformanceTuning.class, msg.toString());
        }
    }

    /**
     * @return list of issues regarding OS performance tuning.
     */
    @NotNull
    public static List<String> issues() {
        if (!OS.isLinux()) {
            return Collections.emptyList();
        }
        final long lastModified = TIMESTAMP.lastModified();
        final long age = System.currentTimeMillis() - lastModified;
        if (age < TimeUnit.MINUTES.toMillis(30))
            return Collections.emptyList();
        try {
            Jvm.debug().on(PerformanceTuning.class, lastModified == 0 ? "No " + TIMESTAMP+" file" : "Tuning Timestamp " + age / 1e3 + " seconds.");
            try (FileOutputStream fos = new FileOutputStream(TIMESTAMP)) {
            }
        } catch (IOException ioe) {
            Jvm.debug().on(PerformanceTuning.class, ioe);
        }

        List<String> issues = new LinkedList<>();

        issues.add(checkScalingGovernors());

        Set<String> kernelCmdLineParams = kernelCommandLineParameters();
        if (!kernelCmdLineParams.isEmpty()) {
            issues.addAll(asList(
                    checkCStates(kernelCmdLineParams),
                    checkSpectreMitigation(kernelCmdLineParams),
                    checkSoftlockup(kernelCmdLineParams),
                    checkAudit(kernelCmdLineParams),
                    checkPageTableIsolation(kernelCmdLineParams),
                    checkMCE(kernelCmdLineParams)
{'fraction_non_alphanumeric': 0.11237396883593034, 'fraction_numerical': 0.0012832263978001834, 'mean_word_length': 2.696476964769648, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, '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': '5526699', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1439, 'n_words': 324}