<commit_before>package org.rabix.executor.service.impl;

import java.lang.management.ManagementFactory;

import org.rabix.bindings.BindingException;
import org.rabix.bindings.Bindings;
import org.rabix.bindings.BindingsFactory;
import org.rabix.bindings.model.Job;
import org.rabix.bindings.model.requirement.ResourceRequirement;
import org.rabix.executor.service.JobFitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JobFitterImpl implements JobFitter {

  static int count = 0;
  
  private static final long MEGABYTE = 1024L * 1024L;

  private static final Logger logger = LoggerFactory.getLogger(JobFitterImpl.class);
  
  private Long availableCores;
  private Long availableMemory;

  @SuppressWarnings("restriction")
  public JobFitterImpl() {
    com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    long totalPhysicalMemorySize = bytesToMeg(operatingSystemMXBean.getTotalPhysicalMemorySize());
    long jvmMaxMemory = bytesToMeg(Runtime.getRuntime().maxMemory());
    
    availableMemory = totalPhysicalMemorySize - jvmMaxMemory;
    availableCores = (long) operatingSystemMXBean.getAvailableProcessors();
  }
  
  public static long bytesToMeg(long bytes) {
    return bytes / MEGABYTE ;
   }
  
  @Override
  public synchronized boolean tryToFit(Job job) throws BindingException {
    Bindings bindings = BindingsFactory.create(job);
    ResourceRequirement resourceRequirement = bindings.getResourceRequirement(job);

    Long cpu = resourceRequirement.getCpuMin();
    if (cpu != null && cpu > availableCores) {
      return false;
    }

    Long memory = resourceRequirement.getMemMinMB();
    if (memory != null && memory > availableMemory) {
      return false;
    }
    availableCores -= cpu != null ? cpu : 0;
    availableMemory -= memory != null ? memory : 0;
    logger.info("Job {} fits. Starting execution...", job.getId());
    return true;
  }

  @Override
  public synchronized void free(Job job) throws BindingException {
    Bindings bindings = BindingsFactory.create(job);
    ResourceRequirement resourceRequirement = bindings.getResourceRequirement(job);<commit_msg>skip the evaluation if the tool is self executable (no containers)<commit_after>package org.rabix.executor.service.impl;

import java.lang.management.ManagementFactory;

import org.rabix.bindings.BindingException;
import org.rabix.bindings.Bindings;
import org.rabix.bindings.BindingsFactory;
import org.rabix.bindings.model.Job;
import org.rabix.bindings.model.requirement.ResourceRequirement;
import org.rabix.executor.service.JobFitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JobFitterImpl implements JobFitter {

  private static final long MEGABYTE = 1024L * 1024L;

  private static final Logger logger = LoggerFactory.getLogger(JobFitterImpl.class);
  
  private Long availableCores;
  private Long availableMemory;

  @SuppressWarnings("restriction")
  public JobFitterImpl() {
    com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    long totalPhysicalMemorySize = bytesToMeg(operatingSystemMXBean.getTotalPhysicalMemorySize());
    long jvmMaxMemory = bytesToMeg(Runtime.getRuntime().maxMemory());
    
    availableMemory = totalPhysicalMemorySize - jvmMaxMemory;
    availableCores = (long) operatingSystemMXBean.getAvailableProcessors();
  }
  
  public static long bytesToMeg(long bytes) {
    return bytes / MEGABYTE ;
   }
  
  @Override
  public synchronized boolean tryToFit(Job job) throws BindingException {
    Bindings bindings = BindingsFactory.create(job);
    if (bindings.canExecute(job)) {
      return true;
    }
    ResourceRequirement resourceRequirement = bindings.getResourceRequirement(job);

    Long cpu = resourceRequirement.getCpuMin();
    if (cpu != null && cpu > availableCores) {
      return false;
    }

    Long memory = resourceRequirement.getMemMinMB();
    if (memory != null && memory > availableMemory) {
      return false;
    }
    availableCores -= cpu != null ? cpu : 0;
    availableMemory -= memory != null ? memory : 0;
    logger.info("Job {} fits. Starting execution...", job.getId());
    return true;
  }

  @Override
  public synchronized void free(Job job) throws BindingException {
    Bindings bindings = BindingsFactory.create(job);
    ResourceRequirement resourceRequirement = bindings.getResourceRequirement(job);
{'fraction_non_alphanumeric': 0.09176728869374313, 'fraction_numerical': 0.005488474204171241, 'mean_word_length': 4.610837438423645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1893912', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1290, 'n_words': 289}