| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * 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: Matthew Khouzam - Initial API and implementation |
| 10 | * Contributors: Simon Marchi - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.ctf.core.event; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | |
| 18 | /** |
| 19 | * Clock description used in CTF traces. |
| 20 | * |
| 21 | * From the TSDL perspective, they describe the clock topology of the system, as well as to detail |
| 22 | * each clock parameter. In absence of clock description, it is assumed that all fields named |
| 23 | * timestamp use the same clock source, which increments once per nanosecond. |
| 24 | * <p> |
| 25 | * Describing a clock and how it is used by streams is threefold: |
| 26 | * <ol> |
| 27 | * <li>the clock and clock topology should be described in a clock description block</li> |
| 28 | * <li>a reference to this clock should be added within an integer type. (timestamp)</li> |
| 29 | * <li>stream declarations can reference the clock they use as a timestamp source</li></ol> |
| 30 | * In for trace compass's perspective, clock attributes are added when the trace is parsed. The ones |
| 31 | * used at this moment are: |
| 32 | * <ul><li>offsets</li><li>names</li><li>frequencies</li></ul> |
| 33 | * |
| 34 | * Most traces only have one clock source. As all events have timestamps offsetted by the same clock. |
| 35 | * It is however possible especially with mixed traces (hardware and software) to have different |
| 36 | * clock sources for a given event. |
| 37 | * <p> |
| 38 | * An individual event should only have one timestamp and therefore only one clock source though. |
| 39 | */ |
| 40 | public class CTFClock { |
| 41 | |
| 42 | private static final long ONE_BILLION_L = 1000000000L; |
| 43 | private static final double ONE_BILLION_D = 1000000000.0; |
| 44 | |
| 45 | private static final String NAME = "name"; //$NON-NLS-1$ |
| 46 | private static final String FREQ = "freq"; //$NON-NLS-1$ |
| 47 | private static final String OFFSET = "offset"; //$NON-NLS-1$ |
| 48 | |
| 49 | private long fClockOffset = 0; |
| 50 | private double fClockScale = 1.0; |
| 51 | private double fClockAntiScale = 1.0; |
| 52 | |
| 53 | /** |
| 54 | * Field properties. |
| 55 | */ |
| 56 | private final Map<String, Object> fProperties = new HashMap<>(); |
| 57 | /** |
| 58 | * Field name. |
| 59 | */ |
| 60 | private String fName; |
| 61 | private boolean fIsScaled = false; |
| 62 | |
| 63 | /** |
| 64 | * Default constructor |
| 65 | */ |
| 66 | public CTFClock() { |
| 67 | // The attributes are added later using addAttribute |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Method addAttribute. |
| 72 | * |
| 73 | * @param key |
| 74 | * String |
| 75 | * @param value |
| 76 | * Object |
| 77 | */ |
| 78 | public void addAttribute(String key, Object value) { |
| 79 | fProperties.put(key, value); |
| 80 | if (key.equals(NAME)) { |
| 81 | fName = (String) value; |
| 82 | } |
| 83 | if (key.equals(FREQ)) { |
| 84 | /* |
| 85 | * Long is converted to a double. the double is then dividing |
| 86 | * another double that double is saved. this is precise as long as |
| 87 | * the long is under 53 bits long. this is ok as long as we don't |
| 88 | * have a system with a frequency of > 1 600 000 000 GHz with |
| 89 | * 200 ppm precision |
| 90 | */ |
| 91 | fIsScaled = !((Long) getProperty(FREQ)).equals(ONE_BILLION_L); |
| 92 | fClockScale = ONE_BILLION_D / ((Long) getProperty(FREQ)).doubleValue(); |
| 93 | fClockAntiScale = 1.0 / fClockScale; |
| 94 | |
| 95 | } |
| 96 | if (key.equals(OFFSET)) { |
| 97 | fClockOffset = (Long) getProperty(OFFSET); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Method getName. |
| 103 | * |
| 104 | * @return String |
| 105 | */ |
| 106 | public String getName() { |
| 107 | return fName; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Method getProperty. |
| 112 | * |
| 113 | * @param key |
| 114 | * String |
| 115 | * @return Object |
| 116 | */ |
| 117 | public Object getProperty(String key) { |
| 118 | return fProperties.get(key); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return the clockOffset |
| 123 | */ |
| 124 | public long getClockOffset() { |
| 125 | return fClockOffset; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @return the clockScale |
| 130 | */ |
| 131 | public double getClockScale() { |
| 132 | return fClockScale; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return the clockAntiScale |
| 137 | */ |
| 138 | public double getClockAntiScale() { |
| 139 | return fClockAntiScale; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return is the clock in ns or cycles? |
| 144 | */ |
| 145 | public boolean isClockScaled() { |
| 146 | return fIsScaled; |
| 147 | } |
| 148 | |
| 149 | } |