<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.application.impl;

import com.intellij.execution.process.OSProcessUtil;
import com.intellij.ide.PowerSaveMode;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.SystemProperties;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import sun.tools.attach.HotSpotVirtualMachine;

import java.io.File;
import java.io.IOException;

// NOTE: compile with --add-exports jdk.attach/sun.tools.attach=ALL-UNNAMED in module-aware jdk
public class JitSuppressor implements PowerSaveMode.Listener {
  private static final Logger LOG = Logger.getInstance(JitSuppressor.class);
  private static final String SELF_ATTACH_PROP = "jdk.attach.allowAttachSelf";
  private static final String EXCLUDE_ALL_FROM_C2_CLAUSE = "{ match : [\"*.*\"], c2 : { Exclude : true }}";
  // Limit C2 compilation with the given packages only. The package set was computed by test performance analysis:
  // it makes performance tests timing comparable with full C2 and same time IDE does not load CPU heavily with this limitation.
  private static final String[] C2_LIMIT_MASKS = {
    "com/intellij/openapi/application/*.*",
    "com/intellij/openapi/editor/*.*",
    "com/intellij/openapi/project/*.*",
    "com/intellij/openapi/util/text/*.*",
    "com/intellij/openapi/vfs/*.*",
    "com/intellij/openapi/fileTypes/*.*",
    "com/intellij/psi/codeStyle/*.*",
    "com/intellij/psi/util/*.*",
    "com/intellij/util/*.*",
    "java/lang/*.*",
    "java/math/*.*",
    "sun/*.*",
  };

  public JitSuppressor() {
    if (!SystemProperties.getBooleanProperty("enable.jit.suppressor", false)) {
      return;
    }

    // java.specification.version has values "1.8", "1.7" e.t.c. for jdk <= 8 and "9", "10", "11", 12" for others
    if (System.getProperty("java.specification.version").contains(".")) return;

    String javaSpecVendor = System.getProperty("java.vm.specification.vendor");
    if (!"Oracle Corporation".equals(javaSpecVendor)) {
      LOG.warn("JitSuppressor functionality is not supported on non-Oracle vm. This one is " + javaSpecVendor);
      return;
    }

    if (!"true".equals(System.getProperty(SELF_ATTACH_PROP))) {
      String msg = "JitSuppressor wasn't registered. Please ensure the command line contains -D" + SELF_ATTACH_PROP + "=true";
      if (ApplicationInfoImpl.isInStressTest()) {
        LOG.warn(msg + " to get production-like performance");
      }
      else if (!ApplicationManager.getApplication().isUnitTestMode()) {
        LOG.warn(msg);
      }
      return;
    }
    
    LOG.info("JitSuppressor is active");

    ApplicationManager.getApplication().getMessageBus().connect().subscribe(PowerSaveMode.TOPIC, this);

    // call the handler to make initial setup
    powerSaveStateChanged();
  }

  @Override
  public void powerSaveStateChanged() {
    String directives = generateDirectives();

    Runnable runnable = () -> setDirectives(directives);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      runnable.run();
    } else {
      ApplicationManager.getApplication().executeOnPooledThread(runnable);
    }
  }

  private static void setDirectives(String directives) {
    File tmpFile = null;
    try {
      tmpFile = FileUtil.createTempFile("c2_directive", "json");
      FileUtil.writeToFile(tmpFile, directives);

      VirtualMachine vm = VirtualMachine.attach(OSProcessUtil.getApplicationPid());
      HotSpotVirtualMachine hvm = (HotSpotVirtualMachine)vm;
      hvm.executeJCmd("Compiler.directives_clear");
      hvm.executeJCmd("Compiler.directives_add " + tmpFile.getAbsolutePath());
      vm.detach();
    }
    catch (AttachNotSupportedException | IOException e) {
      LOG.error(e);
    }
    finally {
      if (tmpFile != null) {
        FileUtil.delete(tmpFile);<commit_msg>JitSuppressor: don't mention AttachNotSupportedException (IDEA-218413?)<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.application.impl;

import com.intellij.execution.process.OSProcessUtil;
import com.intellij.ide.PowerSaveMode;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.SystemProperties;
import com.sun.tools.attach.VirtualMachine;
import sun.tools.attach.HotSpotVirtualMachine;

import java.io.File;

// NOTE: compile with --add-exports jdk.attach/sun.tools.attach=ALL-UNNAMED in module-aware jdk
public class JitSuppressor implements PowerSaveMode.Listener {
  private static final Logger LOG = Logger.getInstance(JitSuppressor.class);
  private static final String SELF_ATTACH_PROP = "jdk.attach.allowAttachSelf";
  private static final String EXCLUDE_ALL_FROM_C2_CLAUSE = "{ match : [\"*.*\"], c2 : { Exclude : true }}";
  // Limit C2 compilation with the given packages only. The package set was computed by test performance analysis:
  // it makes performance tests timing comparable with full C2 and same time IDE does not load CPU heavily with this limitation.
  private static final String[] C2_LIMIT_MASKS = {
    "com/intellij/openapi/application/*.*",
    "com/intellij/openapi/editor/*.*",
    "com/intellij/openapi/project/*.*",
    "com/intellij/openapi/util/text/*.*",
    "com/intellij/openapi/vfs/*.*",
    "com/intellij/openapi/fileTypes/*.*",
    "com/intellij/psi/codeStyle/*.*",
    "com/intellij/psi/util/*.*",
    "com/intellij/util/*.*",
    "java/lang/*.*",
    "java/math/*.*",
    "sun/*.*",
  };

  public JitSuppressor() {
    if (!SystemProperties.getBooleanProperty("enable.jit.suppressor", false)) {
      return;
    }

    // java.specification.version has values "1.8", "1.7" e.t.c. for jdk <= 8 and "9", "10", "11", 12" for others
    if (System.getProperty("java.specification.version").contains(".")) return;

    String javaSpecVendor = System.getProperty("java.vm.specification.vendor");
    if (!"Oracle Corporation".equals(javaSpecVendor)) {
      LOG.warn("JitSuppressor functionality is not supported on non-Oracle vm. This one is " + javaSpecVendor);
      return;
    }

    if (!"true".equals(System.getProperty(SELF_ATTACH_PROP))) {
      String msg = "JitSuppressor wasn't registered. Please ensure the command line contains -D" + SELF_ATTACH_PROP + "=true";
      if (ApplicationInfoImpl.isInStressTest()) {
        LOG.warn(msg + " to get production-like performance");
      }
      else if (!ApplicationManager.getApplication().isUnitTestMode()) {
        LOG.warn(msg);
      }
      return;
    }
    
    LOG.info("JitSuppressor is active");

    ApplicationManager.getApplication().getMessageBus().connect().subscribe(PowerSaveMode.TOPIC, this);

    // call the handler to make initial setup
    powerSaveStateChanged();
  }

  @Override
  public void powerSaveStateChanged() {
    String directives = generateDirectives();

    Runnable runnable = () -> setDirectives(directives);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      runnable.run();
    } else {
      ApplicationManager.getApplication().executeOnPooledThread(runnable);
    }
  }

  private static void setDirectives(String directives) {
    File tmpFile = null;
    try {
      tmpFile = FileUtil.createTempFile("c2_directive", "json");
      FileUtil.writeToFile(tmpFile, directives);

      VirtualMachine vm = VirtualMachine.attach(OSProcessUtil.getApplicationPid());
      HotSpotVirtualMachine hvm = (HotSpotVirtualMachine)vm;
      hvm.executeJCmd("Compiler.directives_clear");
      hvm.executeJCmd("Compiler.directives_add " + tmpFile.getAbsolutePath());
      vm.detach();
    }
    catch (Exception e) {
      LOG.error(e);
    }
    finally {
      if (tmpFile != null) {
        FileUtil.delete(tmpFile);
{'fraction_non_alphanumeric': 0.12890292795529099, 'fraction_numerical': 0.007532499088810594, 'mean_word_length': 3.7804878048780486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6544851', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2510, 'n_words': 546}