|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Ericsson |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License 2.0 which |
| 6 | + * accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.tracecompass.examples.core.data.provider; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Map.Entry; |
| 19 | + |
| 20 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 21 | +import org.eclipse.jdt.annotation.NonNull; |
| 22 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 23 | +import org.eclipse.jdt.annotation.Nullable; |
| 24 | +import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 25 | +import org.eclipse.tracecompass.tmf.core.event.ITmfLostEvent; |
| 26 | +import org.eclipse.tracecompass.tmf.core.model.CommonStatusMessage; |
| 27 | +import org.eclipse.tracecompass.tmf.core.model.tree.ITmfTreeDataProvider; |
| 28 | +import org.eclipse.tracecompass.tmf.core.model.tree.TmfTreeDataModel; |
| 29 | +import org.eclipse.tracecompass.tmf.core.model.tree.TmfTreeModel; |
| 30 | +import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; |
| 31 | +import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; |
| 32 | +import org.eclipse.tracecompass.tmf.core.response.ITmfResponse; |
| 33 | +import org.eclipse.tracecompass.tmf.core.response.ITmfResponse.Status; |
| 34 | +import org.eclipse.tracecompass.tmf.core.response.TmfModelResponse; |
| 35 | +import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
| 36 | +import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 37 | + |
| 38 | +/** |
| 39 | + * Simple events statistics data provider |
| 40 | + * |
| 41 | + * @author Bernd Hufmann |
| 42 | + */ |
| 43 | +@SuppressWarnings("null") |
| 44 | +@NonNullByDefault |
| 45 | +public class ExampleEventsStatisticsDataProvider implements ITmfTreeDataProvider<TmfTreeDataModel> { |
| 46 | + private static long fCount = 0; |
| 47 | + |
| 48 | + private @Nullable ITmfTrace fTrace; |
| 49 | + private @Nullable StatsPerTypeRequest fRequest; |
| 50 | + private @Nullable List<TmfTreeDataModel> fCachedResult = null; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor |
| 54 | + * @param trace |
| 55 | + * the trace (not experiment) |
| 56 | + */ |
| 57 | + public ExampleEventsStatisticsDataProvider(ITmfTrace trace) { |
| 58 | + fTrace = trace; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public @NonNull String getId() { |
| 63 | + return "org.eclipse.tracecompass.examples.nomodulestats"; //$NON-NLS-1$ |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public @NonNull TmfModelResponse<TmfTreeModel<TmfTreeDataModel>> fetchTree(Map<String, Object> fetchParameters, @Nullable IProgressMonitor monitor) { |
| 68 | + |
| 69 | + ITmfTrace trace = fTrace; |
| 70 | + if (trace == null) { |
| 71 | + return new TmfModelResponse<>(null, ITmfResponse.Status.FAILED, CommonStatusMessage.ANALYSIS_INITIALIZATION_FAILED); |
| 72 | + } |
| 73 | + |
| 74 | + StatsPerTypeRequest request = fRequest; |
| 75 | + if (request == null) { |
| 76 | + // Start new request |
| 77 | + request = new StatsPerTypeRequest(trace, TmfTimeRange.ETERNITY); |
| 78 | + trace.sendRequest(request); |
| 79 | + TmfTreeModel<TmfTreeDataModel> model = new TmfTreeModel<>(List.of("Name", "Value"), Collections.emptyList()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 80 | + fRequest = request; |
| 81 | + return new TmfModelResponse<>(model, Status.RUNNING, CommonStatusMessage.RUNNING); |
| 82 | + } |
| 83 | + |
| 84 | + if (request.isCancelled()) { |
| 85 | + TmfTreeModel<TmfTreeDataModel> model = new TmfTreeModel<>(List.of("Name", "Value"), Collections.emptyList()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 86 | + return new TmfModelResponse<>(model, Status.CANCELLED, CommonStatusMessage.TASK_CANCELLED); |
| 87 | + } |
| 88 | + |
| 89 | + if (!request.isCompleted()) { |
| 90 | + TmfTreeModel<TmfTreeDataModel> model = new TmfTreeModel<>(List.of("Name", "Value"), Collections.emptyList()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 91 | + return new TmfModelResponse<>(model, Status.RUNNING, CommonStatusMessage.RUNNING); |
| 92 | + } |
| 93 | + |
| 94 | + List<TmfTreeDataModel> values = fCachedResult; |
| 95 | + if (values == null) { |
| 96 | + long traceId = fCount++; |
| 97 | + values = new ArrayList<>(); |
| 98 | + long total = 0; |
| 99 | + for (Entry<String, Long> entry : request.getResults().entrySet()) { |
| 100 | + values.add(new TmfTreeDataModel(fCount++, traceId, List.of(entry.getKey(), String.valueOf(entry.getValue())))); |
| 101 | + total += entry.getValue(); |
| 102 | + } |
| 103 | + TmfTreeDataModel traceEntry = new TmfTreeDataModel(traceId, -1, List.of(trace.getName(), String.valueOf(total))); |
| 104 | + values.add(0, traceEntry); |
| 105 | + fCachedResult = values; |
| 106 | + } |
| 107 | + TmfTreeModel<TmfTreeDataModel> model = new TmfTreeModel<>(List.of("Name", "Value"), values); //$NON-NLS-1$ //$NON-NLS-2$ |
| 108 | + return new TmfModelResponse<>(model, Status.COMPLETED, CommonStatusMessage.COMPLETED); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + private class StatsPerTypeRequest extends TmfEventRequest { |
| 113 | + |
| 114 | + /* Map in which the results are saved */ |
| 115 | + private final Map<@NonNull String, @NonNull Long> stats; |
| 116 | + |
| 117 | + public StatsPerTypeRequest(ITmfTrace trace, TmfTimeRange range) { |
| 118 | + super(trace.getEventType(), range, 0, ITmfEventRequest.ALL_DATA, |
| 119 | + ITmfEventRequest.ExecutionType.BACKGROUND); |
| 120 | + this.stats = new HashMap<>(); |
| 121 | + } |
| 122 | + |
| 123 | + public Map<@NonNull String, @NonNull Long> getResults() { |
| 124 | + return stats; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void handleData(final ITmfEvent event) { |
| 129 | + super.handleData(event); |
| 130 | + if (event.getTrace() == fTrace) { |
| 131 | + String eventType = event.getName(); |
| 132 | + /* |
| 133 | + * Special handling for lost events: instead of counting just |
| 134 | + * one, we will count how many actual events it represents. |
| 135 | + */ |
| 136 | + if (event instanceof ITmfLostEvent) { |
| 137 | + ITmfLostEvent le = (ITmfLostEvent) event; |
| 138 | + incrementStats(eventType, le.getNbLostEvents()); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + /* For standard event types, just increment by one */ |
| 143 | + incrementStats(eventType, 1L); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private void incrementStats(@NonNull String key, long count) { |
| 148 | + stats.merge(key, count, Long::sum); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void dispose() { |
| 154 | + fRequest = null; |
| 155 | + fCachedResult = null; |
| 156 | + } |
| 157 | +} |
0 commit comments