| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2012 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 | * Patrick Tasse - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.views.timechart; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Iterator; |
| 17 | |
| 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 19 | import org.eclipse.linuxtools.tmf.ui.views.colors.ColorSettingsManager; |
| 20 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent; |
| 21 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; |
| 22 | |
| 23 | /** |
| 24 | * Event in the time chart view |
| 25 | * |
| 26 | * @version 1.0 |
| 27 | * @author Patrick Tasse |
| 28 | */ |
| 29 | public class TimeChartEvent implements ITimeEvent { |
| 30 | |
| 31 | private static final byte TIMESTAMP_SCALE = -9; |
| 32 | |
| 33 | private final TimeChartAnalysisEntry fParentEntry; |
| 34 | private long fTime; |
| 35 | private long fDuration; |
| 36 | private long fFirstRank; |
| 37 | private long fLastRank; |
| 38 | private final RankRangeList fRankRangeList; |
| 39 | private long fNbEvents; |
| 40 | private int fColorSettingPriority; |
| 41 | private boolean fIsBookmark; |
| 42 | private boolean fIsVisible; |
| 43 | private boolean fIsSearchMatch; |
| 44 | private TimeChartAnalysisEntry fItemizedEntry; |
| 45 | private boolean fItemizing; |
| 46 | |
| 47 | /** |
| 48 | * Standard constructor |
| 49 | * |
| 50 | * @param parentEntry |
| 51 | * The parent entry |
| 52 | * @param event |
| 53 | * The event from which this time chart event originates |
| 54 | * @param rank |
| 55 | * The rank of the event in the trace |
| 56 | * @param decorationProvider |
| 57 | * The decoration provider to use |
| 58 | */ |
| 59 | public TimeChartEvent(TimeChartAnalysisEntry parentEntry, ITmfEvent event, |
| 60 | long rank, TimeChartDecorationProvider decorationProvider) { |
| 61 | fParentEntry = parentEntry; |
| 62 | fTime = event.getTimestamp().normalize(0, TIMESTAMP_SCALE).getValue(); |
| 63 | fDuration = 0; |
| 64 | fFirstRank = fLastRank = rank; |
| 65 | fRankRangeList = new RankRangeList(rank); |
| 66 | fNbEvents = 1; |
| 67 | fColorSettingPriority = ColorSettingsManager.getColorSettingPriority(event); |
| 68 | fIsBookmark = decorationProvider.isBookmark(rank); |
| 69 | fIsVisible = decorationProvider.isVisible(event); |
| 70 | fIsSearchMatch = decorationProvider.isSearchMatch(event); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public ITimeGraphEntry getEntry() { |
| 75 | return fParentEntry; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public long getTime() { |
| 80 | return fTime; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public long getDuration() { |
| 85 | return fDuration; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Retrieve the rank of the trace event which started this time event. |
| 90 | * |
| 91 | * @return The rank of the beginning |
| 92 | */ |
| 93 | public long getFirstRank() { |
| 94 | return fFirstRank; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Retrieve the rank of the trace event which *finished* this time event. |
| 99 | * |
| 100 | * @return The rank of the end |
| 101 | */ |
| 102 | public long getLastRank() { |
| 103 | return fLastRank; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the list of rank ranges corresponding to this time event. |
| 108 | * |
| 109 | * @return The rank range list |
| 110 | */ |
| 111 | public RankRangeList getRankRangeList() { |
| 112 | return fRankRangeList; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Merge another time event with this one. |
| 117 | * |
| 118 | * @param event |
| 119 | * The other event |
| 120 | */ |
| 121 | public void merge(TimeChartEvent event) { |
| 122 | mergeDecorations(event); |
| 123 | if (fTime == event.getTime() && fDuration == event.getDuration()) { |
| 124 | return; |
| 125 | } |
| 126 | long endTime = Math.max(fTime + fDuration, event.getTime() + event.getDuration()); |
| 127 | fTime = Math.min(fTime, event.getTime()); |
| 128 | fDuration = endTime - fTime; |
| 129 | fFirstRank = Math.min(fFirstRank, event.fFirstRank); |
| 130 | fLastRank = Math.max(fLastRank, event.fLastRank); |
| 131 | fNbEvents += event.fNbEvents; |
| 132 | fItemizedEntry = null; |
| 133 | synchronized (fRankRangeList) { |
| 134 | fRankRangeList.merge(event.getRankRangeList()); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Merge the decorations of another time event with the decorations of this |
| 140 | * one. |
| 141 | * |
| 142 | * @param event |
| 143 | * The other event |
| 144 | */ |
| 145 | public void mergeDecorations(TimeChartEvent event) { |
| 146 | fColorSettingPriority = Math.min(fColorSettingPriority, event.getColorSettingPriority()); |
| 147 | fIsBookmark |= event.fIsBookmark; |
| 148 | fIsVisible |= event.fIsVisible; |
| 149 | fIsSearchMatch |= event.fIsSearchMatch; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the number of time events that have been merged with this one (starts |
| 154 | * counting at 1 if no merge happened). |
| 155 | * |
| 156 | * @return The current number of events in the bath |
| 157 | */ |
| 158 | public long getNbEvents() { |
| 159 | return fNbEvents; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Retrieve the color setting priority. |
| 164 | * |
| 165 | * @return The priority |
| 166 | */ |
| 167 | public int getColorSettingPriority() { |
| 168 | return fColorSettingPriority; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Set the color setting priority. |
| 173 | * |
| 174 | * @param priority |
| 175 | * The priority to set |
| 176 | */ |
| 177 | public void setColorSettingPriority(int priority) { |
| 178 | fColorSettingPriority = priority; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Check if this time event is bookmarked |
| 183 | * |
| 184 | * @return Y/N |
| 185 | */ |
| 186 | public boolean isBookmarked() { |
| 187 | return fIsBookmark; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Set this time event to be bookmarked or not. |
| 192 | * |
| 193 | * @param isBookmarked |
| 194 | * Should time time event become a bookmark, or not |
| 195 | */ |
| 196 | public void setIsBookmarked(boolean isBookmarked) { |
| 197 | fIsBookmark = isBookmarked; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Check if this time is currently visible or not. |
| 202 | * |
| 203 | * @return If the event is visible |
| 204 | */ |
| 205 | public boolean isVisible() { |
| 206 | return fIsVisible; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Set this time event to visible (or to non-visible). |
| 211 | * |
| 212 | * @param isVisible The new status |
| 213 | */ |
| 214 | public void setIsVisible(boolean isVisible) { |
| 215 | fIsVisible = isVisible; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Check if the time event matches the current search. |
| 220 | * |
| 221 | * @return If it matches, Y/N |
| 222 | */ |
| 223 | public boolean isSearchMatch() { |
| 224 | return fIsSearchMatch; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Mark this event as matching (or non-matching) the current search. |
| 229 | * |
| 230 | * @param isSearchMatch |
| 231 | * The new matching status |
| 232 | */ |
| 233 | public void setIsSearchMatch(boolean isSearchMatch) { |
| 234 | fIsSearchMatch = isSearchMatch; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Set this event's itemized entry. |
| 239 | * |
| 240 | * @param timeAnalysisEntry |
| 241 | * The entry to set |
| 242 | */ |
| 243 | public void setItemizedEntry(TimeChartAnalysisEntry timeAnalysisEntry) { |
| 244 | fItemizedEntry = timeAnalysisEntry; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Retrieve this event's itemized entry. |
| 249 | * |
| 250 | * @return The itemized entry that was previously set |
| 251 | */ |
| 252 | public TimeChartAnalysisEntry getItemizedEntry() { |
| 253 | return fItemizedEntry; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @return Has this time event been set to itemizing? |
| 258 | */ |
| 259 | public boolean isItemizing() { |
| 260 | return fItemizing; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Set this event's itemizing flag to true or false. |
| 265 | * |
| 266 | * @param itemizing |
| 267 | * The new value |
| 268 | */ |
| 269 | public void setItemizing(boolean itemizing) { |
| 270 | fItemizing = itemizing; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Inner class to define a range in terms of ranks in the trace. |
| 275 | * |
| 276 | * @version 1.0 |
| 277 | * @author Patrick Tasse |
| 278 | */ |
| 279 | public class RankRange { |
| 280 | private long firstRank; |
| 281 | private long lastRank; |
| 282 | |
| 283 | /** |
| 284 | * Standard constructor |
| 285 | * |
| 286 | * @param firstRank |
| 287 | * The first (earliest) rank of the range |
| 288 | * @param lastRank |
| 289 | * The last (latest) rank of the range |
| 290 | */ |
| 291 | public RankRange(long firstRank, long lastRank) { |
| 292 | this.firstRank = firstRank; |
| 293 | this.lastRank = lastRank; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Retrieve the start rank of this range. |
| 298 | * |
| 299 | * @return The first rank |
| 300 | */ |
| 301 | public long getFirstRank() { |
| 302 | return firstRank; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Retrieve the end rank of this range |
| 307 | * |
| 308 | * @return The end rank |
| 309 | */ |
| 310 | public long getLastRank() { |
| 311 | return lastRank; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Calculate the minimal distance between two RankRange's |
| 316 | * |
| 317 | * @param range |
| 318 | * The other range |
| 319 | * @return The distance, in "number of events" between the two ranges |
| 320 | */ |
| 321 | public long distanceFrom(RankRange range) { |
| 322 | if (range.lastRank < fFirstRank) { |
| 323 | return fFirstRank - range.lastRank; |
| 324 | } else if (range.firstRank > fLastRank) { |
| 325 | return range.firstRank - fLastRank; |
| 326 | } else { |
| 327 | return 0; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public String toString() { |
| 333 | return "["+firstRank+","+lastRank+"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private class RankRangeList extends ArrayList<RankRange> { |
| 338 | |
| 339 | private static final long serialVersionUID = 6060485531208535986L; |
| 340 | |
| 341 | public RankRangeList(long rank) { |
| 342 | super(1); |
| 343 | add(new RankRange(rank, rank)); |
| 344 | } |
| 345 | |
| 346 | public void merge(RankRangeList rankRangeList) { |
| 347 | long threshold = fParentEntry.getTrace().getCacheSize(); |
| 348 | for (RankRange newRange : rankRangeList) { |
| 349 | boolean merged = false; |
| 350 | for (RankRange oldRange : fRankRangeList) { |
| 351 | if (newRange.distanceFrom(oldRange) <= threshold) { |
| 352 | oldRange.firstRank = Math.min(oldRange.firstRank, newRange.firstRank); |
| 353 | oldRange.lastRank = Math.max(oldRange.lastRank, newRange.lastRank); |
| 354 | merged = true; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | if (!merged) { |
| 359 | add(newRange); |
| 360 | } |
| 361 | } |
| 362 | Iterator<RankRange> iterator = fRankRangeList.iterator(); |
| 363 | RankRange previous = null; |
| 364 | while (iterator.hasNext()) { |
| 365 | RankRange range = iterator.next(); |
| 366 | if (previous != null && range.distanceFrom(previous) <= threshold) { |
| 367 | previous.firstRank = Math.min(previous.firstRank, range.firstRank); |
| 368 | previous.lastRank = Math.max(previous.lastRank, range.lastRank); |
| 369 | iterator.remove(); |
| 370 | } |
| 371 | previous = range; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |