| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Matthew Khouzam - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.btf.core.trace; |
| 14 | |
| 15 | import org.eclipse.jdt.annotation.NonNull; |
| 16 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
| 17 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
| 18 | |
| 19 | /** |
| 20 | * Timstamp format of BTF timescale as per v2.1 of the spec |
| 21 | * |
| 22 | * @author Matthew Khouzam |
| 23 | * @since 1.0 |
| 24 | */ |
| 25 | public enum BtfTimestampFormat { |
| 26 | /** |
| 27 | * Picoseconds |
| 28 | */ |
| 29 | PS("ps", -12, 0.001), //$NON-NLS-1$ |
| 30 | /** |
| 31 | * Nanoseconds |
| 32 | */ |
| 33 | NS("ns", -9, 1.0), //$NON-NLS-1$ |
| 34 | /** |
| 35 | * Microseconds |
| 36 | */ |
| 37 | US("us", -6, 1000.0), //$NON-NLS-1$ |
| 38 | /** |
| 39 | * Milliseconds |
| 40 | */ |
| 41 | MS("ms", -3, 1000000.0), //$NON-NLS-1$ |
| 42 | /** |
| 43 | * Seconds |
| 44 | */ |
| 45 | S("s", 0, 1000000000.0); //$NON-NLS-1$ |
| 46 | |
| 47 | private final String fName; |
| 48 | private final int fScale; |
| 49 | private final double fScaleFactor; |
| 50 | |
| 51 | private BtfTimestampFormat(String name, int scale, double scaleFactor) { |
| 52 | fName = name; |
| 53 | fScale = scale; |
| 54 | fScaleFactor = scaleFactor; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the scaling factor |
| 59 | * |
| 60 | * @return the scaling factor |
| 61 | */ |
| 62 | public double getScaleFactor() { |
| 63 | return fScaleFactor; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public String toString() { |
| 68 | return fName; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Parse a string to get a scale |
| 73 | * |
| 74 | * @param text |
| 75 | * the timestamp in text "ns", "ms" ... |
| 76 | * @return a BtfTimestampFormat object |
| 77 | */ |
| 78 | public static BtfTimestampFormat parse(String text) { |
| 79 | switch (text.toLowerCase()) { |
| 80 | case "ps": //$NON-NLS-1$ |
| 81 | throw new IllegalArgumentException("ps not yet supported"); //$NON-NLS-1$ |
| 82 | case "ns": //$NON-NLS-1$ |
| 83 | return BtfTimestampFormat.NS; |
| 84 | case "us": //$NON-NLS-1$ |
| 85 | return BtfTimestampFormat.US; |
| 86 | case "ms": //$NON-NLS-1$ |
| 87 | return BtfTimestampFormat.MS; |
| 88 | case "s": //$NON-NLS-1$ |
| 89 | return BtfTimestampFormat.S; |
| 90 | default: |
| 91 | throw new IllegalArgumentException(text + " not a valid argument, use ps, ns, us, ms, s"); //$NON-NLS-1$ |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create an ITmfTimestamp with a proper scale |
| 97 | * |
| 98 | * @param timestamp |
| 99 | * timestamp without scale |
| 100 | * @return TmfTimestamp with proper scale |
| 101 | */ |
| 102 | public @NonNull ITmfTimestamp createTimestamp(long timestamp) { |
| 103 | return new TmfTimestamp(timestamp, fScale); |
| 104 | } |
| 105 | } |