aboutsummaryrefslogtreecommitdiff
path: root/caliper/src/main/java/com/google/caliper/platform/Platform.java
blob: c5392ce08d68fab348785ebf3e16320739ba3911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.caliper.platform;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.io.File;
import java.util.Collection;
import java.util.Map;

/**
 * An abstraction of the platform within which caliper (both the scheduler and the actual workers)
 * will run.
 */
public abstract class Platform {

  private final Platform.Type platformType;

  public Platform(Type platformType) {
    this.platformType = checkNotNull(platformType);
  }

  /**
   * Get the executable for the virtual machine for this platform.
   *
   * @param vmHome the home directory of the virtual machine, allows testing across multiple vms on
   *     the same platform in one go.
   */
  public abstract File vmExecutable(File vmHome);

  /**
   * Additional virtual machine arguments common to all instruments that are passed to a worker.
   */
  public abstract ImmutableSet<String> commonInstrumentVmArgs();

  /**
   * The name of the platform type.
   */
  public String name() {
    return platformType.name;
  }

  /**
   * Additional arguments that should be passed to a worker.
   */
  public abstract ImmutableSet<String> workerProcessArgs();

  /**
   * The class path that should be used to run a worker..
   */
  protected abstract String workerClassPath();

  /**
   * Construct the set of arguments that specify the classpath which
   * is passed to the worker.
   *
   * <p>By default this is just the {@code -cp $workerClassPath}.</p>
   */
  public ImmutableList<String> workerClassPathArgs() {
    return ImmutableList.of("-cp", workerClassPath());
  }

  /**
   * Checks to see whether the specific class is supported on this platform.
   *
   * <p>This checks to see whether {@link SupportedPlatform} specifies a {@link Type} that
   * matches this platform.
   *
   * @param clazz the class to check.
   * @return true if it is supported, false otherwise.
   */
  public boolean supports(Class<?> clazz) {
    SupportedPlatform annotation = clazz.getAnnotation(SupportedPlatform.class);
    if (annotation == null) {
      // Support must be explicitly declared.
      return false;
    }

    Platform.Type[] types = annotation.value();
    for (Type type : types) {
      if (type.equals(platformType)) {
        return true;
      }
    }

    return false;
  }

  /**
   * Get the input arguments for the current running JVM.
   */
  public abstract Collection<String> inputArguments();

  /**
   * Selects the names of properties that will be used to populate the
   * {@link com.google.caliper.model.VmSpec} for a specific run.
   */
  public abstract Predicate<String> vmPropertiesToRetain();

  /**
   * Checks that the vm options are appropriate for this platform, throws an exception if they are
   * not.
   */
  public abstract void checkVmProperties(Map<String, String> options);

  /**
   * Get the default vm home directory.
   */
  public File defaultVmHomeDir() {
    // Currently both supported platforms use java.home property to refer to the 'home' directory
    // of the vm, in the case of Android it is the directory containing the dalvikvm executable.
    return new File(System.getProperty("java.home"));
  }

  /**
   * Get the home directory of a custom virtual machine.
   * @param vmGroupMap the configuration properties for all VMs, may contain default properties that
   *     apply to all VMs.
   * @param vmConfigName the name of the VM within the configuration, used to access VM specific
   *     properties from the {@code vmGroupMap}.
   * @throws VirtualMachineException if there was a problem with the VM, either the configuration
   *     or the file system.
   */
  public abstract File customVmHomeDir(Map<String, String> vmGroupMap, String vmConfigName)
          throws VirtualMachineException;

  /**
   * The type of platforms supported.
   */
  public enum Type {
    DALVIK("Dalvik"),
    JVM("Java");

    private final String name;

    Type(String name) {
      this.name = name;
    }
  }
}