Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2015 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.caliper.platform;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.common.base.Predicate;
     22 import com.google.common.collect.ImmutableSet;
     23 
     24 import java.io.File;
     25 import java.util.Collection;
     26 import java.util.Map;
     27 
     28 /**
     29  * An abstraction of the platform within which caliper (both the scheduler and the actual workers)
     30  * will run.
     31  */
     32 public abstract class Platform {
     33 
     34   private final Platform.Type platformType;
     35 
     36   public Platform(Type platformType) {
     37     this.platformType = checkNotNull(platformType);
     38   }
     39 
     40   /**
     41    * Get the executable for the virtual machine for this platform.
     42    *
     43    * @param vmHome the home directory of the virtual machine, allows testing across multiple vms on
     44    *     the same platform in one go.
     45    */
     46   public abstract File vmExecutable(File vmHome);
     47 
     48   /**
     49    * Additional virtual machine arguments common to all instruments that are passed to a worker.
     50    */
     51   public abstract ImmutableSet<String> commonInstrumentVmArgs();
     52 
     53   /**
     54    * The name of the platform type.
     55    */
     56   public String name() {
     57     return platformType.name;
     58   }
     59 
     60   /**
     61    * Additional arguments that should be passed to a worker.
     62    */
     63   public abstract ImmutableSet<String> workerProcessArgs();
     64 
     65   /**
     66    * The class path that should be used to run a worker..
     67    */
     68   public abstract String workerClassPath();
     69 
     70   /**
     71    * Checks to see whether the specific class is supported on this platform.
     72    *
     73    * <p>This checks to see whether {@link SupportedPlatform} specifies a {@link Type} that
     74    * matches this platform.
     75    *
     76    * @param clazz the class to check.
     77    * @return true if it is supported, false otherwise.
     78    */
     79   public boolean supports(Class<?> clazz) {
     80     SupportedPlatform annotation = clazz.getAnnotation(SupportedPlatform.class);
     81     if (annotation == null) {
     82       // Support must be explicitly declared.
     83       return false;
     84     }
     85 
     86     Platform.Type[] types = annotation.value();
     87     for (Type type : types) {
     88       if (type.equals(platformType)) {
     89         return true;
     90       }
     91     }
     92 
     93     return false;
     94   }
     95 
     96   /**
     97    * Get the input arguments for the current running JVM.
     98    */
     99   public abstract Collection<String> inputArguments();
    100 
    101   /**
    102    * Selects the names of properties that will be used to populate the
    103    * {@link com.google.caliper.model.VmSpec} for a specific run.
    104    */
    105   public abstract Predicate<String> vmPropertiesToRetain();
    106 
    107   /**
    108    * Checks that the vm options are appropriate for this platform, throws an exception if they are
    109    * not.
    110    */
    111   public abstract void checkVmProperties(Map<String, String> options);
    112 
    113   /**
    114    * Get the default vm home directory.
    115    */
    116   public File defaultVmHomeDir() {
    117     // Currently both supported platforms use java.home property to refer to the 'home' directory
    118     // of the vm, in the case of Android it is the directory containing the dalvikvm executable.
    119     return new File(System.getProperty("java.home"));
    120   }
    121 
    122   /**
    123    * Get the home directory of a custom virtual machine.
    124    * @param vmGroupMap the configuration properties for all VMs, may contain default properties that
    125    *     apply to all VMs.
    126    * @param vmConfigName the name of the VM within the configuration, used to access VM specific
    127    *     properties from the {@code vmGroupMap}.
    128    * @throws VirtualMachineException if there was a problem with the VM, either the configuration
    129    *     or the file system.
    130    */
    131   public abstract File customVmHomeDir(Map<String, String> vmGroupMap, String vmConfigName)
    132           throws VirtualMachineException;
    133 
    134   /**
    135    * The type of platforms supported.
    136    */
    137   public enum Type {
    138     DALVIK("Dalvik"),
    139     JVM("Java");
    140 
    141     private final String name;
    142 
    143     Type(String name) {
    144       this.name = name;
    145     }
    146   }
    147 }
    148