Commit | Line | Data |
---|---|---|
ff71e543 MK |
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 | ||
7ce90559 | 13 | package org.eclipse.tracecompass.btf.core.trace; |
ff71e543 | 14 | |
5dca27ae | 15 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
16 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
17 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
ff71e543 MK |
18 | |
19 | /** | |
20 | * Timstamp format of BTF timescale as per v2.1 of the spec | |
21 | * | |
22 | * @author Matthew Khouzam | |
dbc7991d | 23 | * @since 1.0 |
ff71e543 | 24 | */ |
5dca27ae | 25 | public enum BtfTimestampFormat { |
ff71e543 MK |
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 | ||
5dca27ae | 51 | private BtfTimestampFormat(String name, int scale, double scaleFactor) { |
ff71e543 MK |
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 | */ | |
5dca27ae | 78 | public static BtfTimestampFormat parse(String text) { |
ff71e543 MK |
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$ | |
5dca27ae | 83 | return BtfTimestampFormat.NS; |
ff71e543 | 84 | case "us": //$NON-NLS-1$ |
5dca27ae | 85 | return BtfTimestampFormat.US; |
ff71e543 | 86 | case "ms": //$NON-NLS-1$ |
5dca27ae | 87 | return BtfTimestampFormat.MS; |
ff71e543 | 88 | case "s": //$NON-NLS-1$ |
5dca27ae | 89 | return BtfTimestampFormat.S; |
ff71e543 MK |
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 | */ | |
5dca27ae | 102 | public @NonNull ITmfTimestamp createTimestamp(long timestamp) { |
ff71e543 MK |
103 | return new TmfTimestamp(timestamp, fScale); |
104 | } | |
105 | } |